Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[SOLVED] Depth Texture Lookup Problem

Started by Aptos Jun 9, 2010 at 7:30 PM 2 replies 1.5k views
Original Post
Aptos
Aptos
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[color]);   ////// RENDERING ///////   updateView(); // set the viewing transform   updateLight(); // set the light sources   updateGeom(); // Draw objects   // Tear down   shader->detach();   glutSwapBuffers(); // Swap the double buffer.   glutPostRedisplay();


vertex shader:
    varying vec3 normal;    varying vec4 pos;    void main()    {        //Grab the texture coord for depth map lookup        gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;           //Same old, same old        normal = gl_NormalMatrix * gl_Normal;        pos = gl_ModelViewMatrix * gl_Vertex;	gl_Position = ftransform();     } 


fragment shader:
    varying vec3 normal;    varying vec4 pos;    uniform sampler2D depthTex;    float DepthToLinearZ(float dVal)    {        float near = 1.0;        float far = 100.0;        return (-2.0*far*near) / ((dVal*(far-near))-(far+near)); //Inverse of perspective z transform    }    void main()    {        // DEPTH STUFF        vec2 coord = gl_TexCoord[0].xy;        float depth = DepthToLinearZ( texture2D(depthTex, coord).r );        depth = depth / 100.0; //depth over far plane so I get [0,1]        gl_FragColor = vec4(depth, depth, depth, 1.0);    }


[Edited by - Aptos on June 9, 2010 7:59:56 PM]
Aptos
Aptos
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.
karwosts
karwosts
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.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Aptos
Aptos
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 [color] as my "depthTex" uniform.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.