Jump to content
  • Advertisement
Sign in to follow this  
MindWipe

Problems with rendering the depth buffer

This topic is 3805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm finally getting into shaders. Got a simple vertex and pixel shader running. But now I wanted to do some shadow mapping, so I began with getting the depth buffer from the lights point of view rendered. And I get the view rendered to a texture, it works great and I got the texture shown no problem. But when I then wanted to show the depth values in that texture I'm not getting anything to work. Either nothing is shown or a solid color is shown. And I've checked numerous sources and my code looks alot alike. So hope you guys can help me out

// Here I create the shadow map texture

GLuint createShadowMapTexture( int w, int h ) {
	GLuint shadowmapTxt;
	glGenTextures(1, &shadowmapTxt);
    glBindTexture(GL_TEXTURE_2D, shadowmapTxt);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, w, h, 0,
        GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	return shadowmapTxt;
}

/*

Here the code where I supposedly render the shadow map. I got the scene rendered correctly from the lights point of view as mentioned. It's when dealing with the depthbuffer I'm running into trouble
*/

// Draw shadow map
	glCullFace(GL_FRONT);
	useLightView();
	glPushMatrix();
	gluLookAt( LightPosition[ 0 ], LightPosition[ 1 ], LightPosition[ 2 ], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );
	glDisable(GL_DEPTH_TEST);
	drawScene();
	glEnable(GL_DEPTH_TEST);
	glBindTexture( GL_TEXTURE_2D, shadowMap );
	glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapWidth, shadowMapHeight);
	glPopMatrix();
	glCullFace(GL_BACK);
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


// And here I'm trying to draw it out on the screen. But in this case it just turns up as one solid red color matching the diffuse color.

void drawShadowMap( GLint texId ) {
	glEnable(GL_TEXTURE_2D);	
	glBindTexture( GL_TEXTURE_2D, texId );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE );
	glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE );

	glBegin( GL_QUADS );
		glColor3f( 1.0f, 0.0f, 0.0f );
		glNormal3f( 0.0f, 0.0f, 1.0f);					// Normal Pointing Towards Viewer
		glTexCoord2f(0.0f, 0.0f); glVertex3f( -4.0f, -4.0f,  5.0f);	// Point 1 (Front)
		glTexCoord2f(1.0f, 0.0f); glVertex3f( -4.0f, 4.0f,  5.0f);	// Point 2 (Front)
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 4.0f,  4.0f,  5.0f);	// Point 3 (Front)
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 4.0f,  -4.0f,  5.0f);	// Point 4 (Front)
	glEnd();
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);

}


Also, many use GL_DEPTH_COMPONENT16. Is the Z-Buffer usually 16bit? And if someone is super nice, could that someone explain glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE ); glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE ); why I'm doing that when trying to draw the buffer? Cheers!! /MindWipe

Share this post


Link to post
Share on other sites
Advertisement
The z buffer is usually 16 bit or 24 bit and where the 8 bit is not used or is for the stencil. Use glGetIntegerv(GL_DEPTH_BITS, ...) to know what your frame buffer is.

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE );

means that the texels are sampled and just passed.
If you put GL_COMPARE_R_TO_TEXTURE, then it will compare r to texture and if r is less, 0 is returned which means it is shadowed or 1 is returned.

glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE );
This says how to interpret the texels. Read the spec because I forget.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
Try not to use GL_CLAMP. Use GL_CLAMP_TO_EDGE instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!