Hello, I have recently tried reading back some computations of vector data on the GPU, but I can’t seem to get the right numbers. So as a test, I started played around with just reading back vertex data and still could not get it right.
I figured the best way to readback a non-normalised vector, is to save it as a normalized vector with its length. In case the length is greater than 1, I pre-scale that value to the range [0,1]. So my test fragment shader is:
// transformed vertex
varying vec3 g_vertex;
// specify an scale to use eg 10
uniform float scale;
void main()
{
vec3 v;
// we let the 4th coord be the vector scale
float w = length(g_vertex) / scale;
v = normalize(g_vertex);
v = 0.5 * v + 0.5;
gl_FragColor = vec4(v, w);
}
It’s ok when the scale is close to one, but for every point further from (0,0,0) I start to get concentric circle effect based on the scale I choose. For example scale = 6, then there will be some bands where vertices will round to. I’ve tried in different spaces object/eye and get the same result, I might be going about it the wrong way. It’s something about that 4th component not working right…
I’ve made 3 snaps of scale = 1, and 3 snaps of scale = 6 images
You mean you readback data in GL_RGBA8 ?? Of course you will loose a lot of precision, with only 8 bits of precision and 8 bits of scale.
You need to use floating point renderbuffers instead.
Unless you specifically allocated a floating-point buffer, this doesn’t change the fact that you only have 8 bits of precision for each component. You need to use an FBO and allocate a floating-point buffer. This will also mean you don’t need to normalize the vector, since floating-point images can store arbitrary numbers.
Thanks for the info. Not having used FBO’s before, I think they are a much more elegant than seeing flashes of each rendering pass, something I should have done long ago
and get a very nice result for vertex reconstruction.
So going back to non-normalized vectors, it seems like this is the best internal format for FBO texture to obtain maximum possible accuracy. ARB_texture_float
Sized Base R G B A L I
Internal Format Internal Format bits bits bits bits bits bits
--------------------------- --------------- ---- ---- ---- ---- ---- ----
RGBA32F_ARB RGBA f32 f32 f32 f32