   opengl depth buffer to D3D clip space
  Hi, everyone.I'd like to know more about the following.Let's say I glReadPixels in GL_DEPTH_COMPONENT format with GL_UNSIGNED_SHORT type.I then calculate a float beetween 0 and 1 using glReadPixels man componentconversion table.Now, is the coordinate I get a good coordinate in D3D clip space (in shader)(more precisely float4(x, y, depth, 1) in (unnormalized)CS) ?Need I GL unproject and then D3D project (which seems overkill to me) ?(This may be a wrong topic for this forum but I didn't see elsewhere to post)Thanks for helping[Edited by - yazgoo on May 13, 2009 8:24:02 AM]
  Personally, I would avoid using glReadPixels.Why are you interested in clip space? clip space is just projection * modelview * vertex.You can do that calculation in your shader.You can even do a gluProject in your shader.   ;Quote:Personally, I would avoid using glReadPixels.I think I didn't explain well enough :I use glReadPixel to recover the ZBuffer but then i use my depth image in a D3D program, more precisely in a shader.A graphic is more appropriate :Opengl side :   [ OPENGL scene ]----glReadPixels---->[ depth buffer Image]D3D side :   [depth buffer image]--pixelValue-->[ vertex shader ]-->float4(pixelValue,x,y,1)Quote:Why are you interested in clip space? clip space is just projection * modelview * vertex.Correct me if I'm wrong, but clip Zbuffer as given by glReadPixel is in OpenGL clip space. Vertex shader projection is in (D3D) clip space. Then I was thinking I could avoid any projection/unprojection directly giving coordinates in clip space.So my questions are, more precisely :- Are OGL(as returned by glReadBuffer and scaled as I explained)/D3D clipping space coordinates the same  ?- If it isn't is there a simple transformation which would avoid gluUnproject and then mul by WorldViewProj ?[Edited by - yazgoo on May 13, 2009 10:57:23 AM];Quote:Original post by yazgooCorrect me if I'm wrong, but clip Zbuffer as given by glReadPixel is in OpenGL clip space. Vertex shader projection is in (D3D) clip space. Then I was thinking I could avoid any projection/unprojection directly giving coordinates in clip space.So my questions are, more precisely :- Are OGL(as returned by glReadBuffer and scaled as I explained)/D3D clipping space coordinates the same  ?- If it isn't is there a simple transformation which would avoid gluUnproject and then mul by WorldViewProj ?No, glReadPixels gives the depth values in normalized device coordinates (NDC), which is 0.0 to 1.0. Actually, I'm not sure if this is called NDC but whatever.For D3D, the depth buffer contents is the same : 0.0 to 1.0   ;  Thank's V-man, but it does not seem to work.Maybe I'm not declaring clip space coordinates right, this is the way I do this in my shader :out.position.z = depth; // between 0 and 1out.position.x = xCoordinate; // between -1 and 1out.position.y = yCoordinate; // between -1 and 1out.position.w = 1;to calculate the depth I use something like the following(with a depth texture filled with two bytes zbuffer data) :depth = (255 * zbuffer.r * 256 + 255 * zbuffer.g) / (pow(2,16)-1);[Edited by - yazgoo on May 18, 2009 10:51:14 AM];  Turns out I've done a little maths to find if D3D and OpenGl clip spacesare homogeneous. Here should be the transformation needed (on the basis I have openGL clip space coordinates) :multiply by w; OpenglProjectionInverse; DirectXProjectionThose matrices are :Pogl: matrix( [n/r,0,0,0],  [0,n/t,0,0],  [0,0,-(n+f)/(f-n),-2*f*n/(f-n)],  [0,0,-1,0]);Pd3d : matrix([n/r,0,0,0],  [0,n/t,0,0],  [0,0,f/(f-n),1],  [0,0,-f*n/(f-n),0]);and Pd3d * rightHandToLeftHand * Pogl^(-1) is :matrix( [-1,0,0,0], [0,1,0,0], [0,0,-(f-n)/(2*f*n),-(-n-f)/(2*f*n)-f/(f-n)], [0,0,0,(f*n)/(f-n)])[edit]I've corrected the matrix and added a left to right hand matrix which is Identity with (0,0) == -1[/edit][Edited by - yazgoo on May 19, 2009 10:45:02 AM]