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;
// 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
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.