get objct spce pos in fgmt shdr wihout v.s. output

Hi all,
I m trying to get the objt spce pos in the fragment shdr but without using the outputs from v.s. This is how i am doing it at the moment,

vec3 NDC = vec3(gl_FragCoord.xy/viewportSize, gl_FragCoord.z);
vec3 CSP = NDC * 2 -1;   
mat4 M   = mview_Inv*proj_Inv;
vec4 OSP = M*vec4(CSP,1);
OSP.w = 1.0/OSP.w; =*OSP.w;

This gives me the following output if i output OSP as color.

I should get a blend of colors not discrete colors as I am getting. Is my method correct or am i missing anythng?

You do have a blend of color, near the middle.
If color goes outside the [0;1] range, it is clamped, and stay at 0 or 1.

Try changing the last line to this to reduce the range of OSP. You should see a larger gradient : = (*OSP.w)/20+0.5;

Hi ZBuffer and thanks for the quick reply.
OK adding what you said gives me the blend for the cube model however if i use a larger model I get the same banding again. This means that in order to output correct positions, i need to adjust the scalar to scale the position to 0-1 range so that it may not be clamped

So can I assume that the shader code is correct. Is there any other way I can verify that my shader code is correct.


It look correct indeed.
However debugging with colors has to be done with care : for best results make sure the min and max values are within [0;1]
Pass the largest size of the object to the shader, instead of hardcoded 20, to have “autoadjust”.

Thanks ZBuffer for the quick reply,