Unprojecting with Fragment Shader

Started by
3 comments, last by ThomasM 18 years, 11 months ago
Hi! I want to unproject the pixels rendered with the use of a fragment shader to reconstruct the points in 3D and return them by the fragment color (r = x, g = y, b = z). Thus reconstructing the world coordinates from the screen coordinates with the use of the fragment shader. I thought it would work with the following (GLSL Shader language used):

vec4 P2d = gl_FragCoord;
P2d.w = 1 / P2d.w;
gl_FragColor = vec4(gl_ModelViewMatrixInverse * P2d);

But that doesn't work correct at all... The computed values are totally weird. Any idea?
Advertisement
check correct syntax

--vertex shader --
glColor = gl_Vertex.xyz;

-- fragment_shader --
gl_FragColor = glColor

u might wanna use gl_FragColor = glColor * (inv scenescale) eg 0.01 or something else its only really gonna work with vertices between 0->1

I've read the GLSL specification and it says .xyz can be equally used like .rgb.

My values are not only between 0 and 1 because I use the framebuffer_object extension with a renderbuffer of type GL_RGB32F_ARB.

So that should be no problem...
Quote:I've read the GLSL specification and it says .xyz can be equally used like .rgb.

yeah i mostly just use xyzw for everything even colors/texturecoords

Quote:My values are not only between 0 and 1 because I use the framebuffer_object extension with a renderbuffer of type GL_RGB32F_ARB.
So that should be no problem...

that doesnt help, i was talking about
glColor = gl_Vertex.xyz; (which btw doesnt have to be color it could be some other varying eg varying vec3 ws_vert = gl_Vertex.xyz;
say u have a pixel at (100,50,10) now if u draw the color (100,50,10) its gonna be white no matter what u do.
if u first scale the values by a hundrenth (100,50,10)*0.01 then u get (1,0.5,0.1) some orange color
Okay, I think I now understand you, but I don't want to display the returned color (which in fact is the coordinate in 3D / world coordinate). I want to use it for a vertex buffer object to draw objects with these world coordinates.

So it doesn't matter that the values aren't between 0 and 1, quite the contrary. The values should be the right world coordinates, which normally are NOT between 0 and 1.
Anyway, they are not correct either...

This topic is closed to new replies.

Advertisement