Original Post
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++
vertex shader:
fragment shader:
[Edited by - Aptos on June 9, 2010 7:59:56 PM]
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]