Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shadow Mapping Problem

Started by StockwellLadSteve Feb 28, 2007 at 2:54 PM 2 replies 850+ views
Original Post
StockwellLadSteve
StockwellLadSteve
Dear All, I'm having massive problems with a shadow map. Wondering if any experts out there may be able to help me in the right direction. I have a simple scene which can be seen here. Scene Then I have my depth buffer seen here Depth Map However, when i follow the code (incidentally from OpenGL Superbible) I get the weirdest effect and not at all what I want weird Notice the now completely black ball, and the weird banding on the lamppost. Can anyone give me any tips? The code is listed below

//in init method
        GLuint shadowTextureID;

	glGenTextures(1, &shadowTextureID);
	glBindTexture(GL_TEXTURE_2D, shadowTextureID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB, 0.1f);
	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);

	GetShadowMap();


//Get ShadowMap method listed here
void GetShadowMap(void)
{
	GLfloat lightToSceneDistance, nearPlane, fieldOfView;
	GLfloat lightModelview[16], lightProjection[16];

	lightToSceneDistance = sqrt(scene.light.x * scene.light.x + scene.light.y * scene.light.y + scene.light.z * scene.light.z);
	nearPlane = 10.0f;

	fieldOfView = 17000.0f;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(fieldOfView, 1.0f, nearPlane, nearPlane + 300.0f);
	glGetFloatv(GL_PROJECTION_MATRIX, lightProjection);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(scene.light.x, scene.light.y, scene.light.z, scene.light.x, 0.0f, scene.light.z, 0.0f, 0.0f, 1.0f);
	glGetFloatv(GL_MODELVIEW_MATRIX, lightModelview);

	glViewport(0, 0, 512, 512);

	glClear(GL_DEPTH_BUFFER_BIT);

	glShadeModel(GL_FLAT);
	glDisable(GL_LIGHTING);
	glDisable(GL_COLOR_MATERIAL);
	glDisable(GL_NORMALIZE);
	glColorMask(0,0,0,0);

	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.1f, 4.0f);

	scene.DrawScene();

	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 512, 512, 0);

	glShadeModel(GL_SMOOTH);
	glEnable(GL_LIGHTING);
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_NORMALIZE);
	glColorMask(1,1,1,1);
	glDisable(GL_POLYGON_OFFSET_FILL);

	glMatrixMode(GL_TEXTURE);
	glLoadIdentity();
	glTranslatef(0.5f, 0.5f, 0.5f);
	glScalef(0.5f, 0.5f, 0.5f);
	glMultMatrixf(lightProjection);
	glMultMatrixf(lightModelview);
}

//And lastly the code in the render method

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);				
	glLoadIdentity();												
	glTranslatef(0.0f, 0.0f, -1.0f);

	
	glDisable(GL_LIGHTING);
	glRasterPos2f(cnt1, cnt2);
	font.glPrint("FPS: %d", (int)fps);
	glEnable(GL_LIGHTING);

	glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMultMatrixf(&camera.projMatrix[0][0]);
	
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMultMatrixf(&camera.viewMatrix[0][0]);	

	
    scene.cgOff = true;
	
    glViewport(0,0, WIDTH, HEIGHT);
    GLfloat lightPos[] = {scene.light.x, scene.light.y, scene.light.z};

		GLfloat sPlane[4] = {1.0f, 0.0f, 0.0f, 0.0f};
		GLfloat tPlane[4] = {0.0f, 1.0f, 0.0f, 0.0f};
		GLfloat rPlane[4] = {0.0f, 0.0f, 1.0f, 0.0f};
		GLfloat qPlane[4] = {0.0f, 0.0f, 0.0f, 1.0f};
	
		GLfloat ambientLight[] = {0.2f, 0.2f,0.2f, 1.0f};
		GLfloat diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};

		glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
		glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);

		glEnable(GL_TEXTURE_2D);
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

		

		glTexGenfv(GL_S, GL_EYE_PLANE, sPlane);
		glTexGenfv(GL_T, GL_EYE_PLANE, tPlane);
		glTexGenfv(GL_R, GL_EYE_PLANE, rPlane);
		glTexGenfv(GL_Q, GL_EYE_PLANE, qPlane);

		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glEnable(GL_TEXTURE_GEN_R);
		glEnable(GL_TEXTURE_GEN_Q);

		scene.DrawScene();

		glDisable(GL_ALPHA_TEST);
		glDisable(GL_TEXTURE_2D);
		glDisable(GL_TEXTURE_GEN_S);
		glDisable(GL_TEXTURE_GEN_T);
		glDisable(GL_TEXTURE_GEN_R);
		glDisable(GL_TEXTURE_GEN_Q);


Thanks so much for your help guys.
jrmcv
jrmcv
i dont know much openGL so dont know how much i can help you but the banding around the light is a common problem with shadow mapping. because of floating point inaccuracy, you need to use a bias when comparing the distance to the light source.
Im not sure exactly how your doing the shadow mapping in this example because i dont use openGl. Are you using and shaders to create the shadow map and render it?

Your prob best trying to find another example or a tutorial on the internet somewhere. I think theres plenty of shadow map examples / tutorials in the resources section of gamedev.

sorry i couldnt be more use.. maybe someone else can help more.

EDIT: P.s - try posting this again on the opengl forum.
ET4000
ET4000
To get around the bias issue, you could alternatively cast shadows from the back faces. Simply change your CullMode to clockwise or counterclockwise, depending on what it's currently set to.
StockwellLadSteve
StockwellLadSteve
Hi Guys,

Thanks for the replies.

However, I'm still left wanting to know why the sphere, which has actually grey, has become black and why if its just a case of the bias, I should still expect to see 'some' shadowing on the ground. There is none however.

The shadow map is not being rendered through shaders - its being done with OpenGL function calls. The code is taken from the OpenGL superbible.

Topic Locked

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

Sign in to reply to this topic.