   [SOLVED] Depth Texture Lookup Problem
  Hey guys, I'm having a bit of an issue getting the correct texture coordinate while rendering.Basically, I've got a 2 pass engine right now, the first pass renders to a framebuffer object that captures all the color, normal, and depth information into 3 textures. Then in the second pass I draw the scene normally, but with my shaders attached. In the fragment shader I want to lookup depth values from my texture, and use that for SSAO, but firstly just to visualize the depth map.My problem is that when rendering to the screen regularly and using my fragment shader, how can I find out what coordinates to look up in the depth texture?Any help would be greatly appreciated!Code below:C++   /////////////////////////////////////////////////////////////////////////////   //// PASS 1 (write to textures)   /////////////////////////////////////////////////////////////////////////////   //Setup   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   glEnable(GL_DEPTH_TEST); ////// RENDERING ///////   updateView(); // set the viewing transform   updateLight(); // set the light sources   updateGeom(); // Draw objects   /////////////////////////////////////////////////////////////////////////////   //// PASS 2 (write to screen)   /////////////////////////////////////////////////////////////////////////////   //Setup   glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   glEnable(GL_DEPTH_TEST);   //attach shader and set it up   shader->attach();   GLint loc = glGetUniformLocation(shader->shaderProg, "depthTex");   glUniform1i(loc,0);   glActiveTexture(GL_TEXTURE0);   glBindTexture(GL_TEXTURE_2D, textures);<br><br>   ////// RENDERING ///////<br>   updateView(); // set the viewing transform<br>   updateLight(); // set the light sources<br>   updateGeom(); // Draw objects<br><br>   // Tear down<br>   shader-&gt;detach();<br>   glutSwapBuffers(); // Swap the double buffer.<br>   glutPostRedisplay();<br></pre><br><br><b>vertex shader:</b><br><pre><br>    varying vec3 normal;<br>    varying vec4 pos;<br><br>    void main()<br>    {<br>   //Grab the texture coord for depth map lookup<br>   gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;<br>   <br>   //Same old, same old<br>   normal = gl_NormalMatrix * gl_Normal;<br>   pos = gl_ModelViewMatrix * gl_Vertex;<br>gl_Position = ftransform();<br>} <br></pre><br><br><b>fragment shader:</b><br><pre><br>    varying vec3 normal;<br>    varying vec4 pos;<br>    uniform sampler2D depthTex;<br><br>    float DepthToLinearZ(float dVal)<br>    {<br>   float near = 1.0;<br>   float far = 100.0;<br>   return (-2.0*far*near) / ((dVal*(far-near))-(far+near)); //Inverse of perspective z transform<br>    }<br><br>    void main()<br>    {<br>   // DEPTH STUFF<br>   vec2 coord = gl_TexCoord[0].xy;<br>   float depth = DepthToLinearZ( texture2D(depthTex, coord).r );<br>   depth = depth / 100.0; //depth over far plane so I get [0,1]<br>   gl_FragColor = vec4(depth, depth, depth, 1.0);<br>    }<br></pre><br><br><!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - Aptos on June 9, 2010 7:59:56 PM]<!–EDIT–></span><!–/EDIT–>
  I fixed the issue.My coordinates couldn't be looked up with gl_MultiTexCoord0 anymore since I wasn't drawing a full screen quad. I just used gl_FragCoord.xy in my fragment shader and normalized it based on screen width and height. That served excellently as a lookup into my depth texture.   ;  If I understand your question right, you just have to treat your depth texture like any regular textured quad you want to draw.Say you originally draw your screen to a texture:----------------|    ||    ||    ||    |----------------Now for textures the s,t range from 0-1. So when you draw your second pass, you just draw a fullscreen quad with the edge cases for the texture coordinates. (0,1)(1,1)o--------------o|    ||    ||    ||    |o--------------o(0,0)(1,0)Then in your fragment shader the gpu will interpolate this just like any other texcoord, giving you the point you want to lookup:----------------|    ||    (0.8,0.4) ||o   ||    |----------------There's really very little you need to calculate to get this to work.   ;  Thanks for the post! I just didn't want to draw a full screen quad, I wanted to render the scene as I would normally, but still get correct texture lookup coordinates.EDIT: Also, there's a typo in my C++ code listed above: I meant to pass textures[depth] not  as my "depthTex" uniform.   