Sign in to follow this  
MARS_999

Odd shadow problem

Recommended Posts

I am trying out somthing new in my engine for shadows and this code works fine.
void StoreLightMatrix(void)
{
	memset(modelViewMatrix, 0, sizeof(float) * 16);
	memset(modelProjectionMatrix, 0, sizeof(float) * 16);

	sunDir[0] = camera.pos.x - lightPosition[0];
	sunDir[1] = camera.pos.y - lightPosition[1];
	sunDir[2] = camera.pos.z - lightPosition[2];
	
    glPushMatrix();
		glPushMatrix();
			glLoadIdentity();
			gluLookAt(camera.pos.x, camera.pos.y, camera.pos.z, 
					  camera.pos.x + sunDir[0], camera.pos.y + sunDir[1], camera.pos.z + sunDir[2], 
					  0.0, 1.0, 0.0);
			glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix);
		glPopMatrix();
		glLoadIdentity();
		glOrtho(-FOV_SHADOWMAP, FOV_SHADOWMAP, -FOV_SHADOWMAP, FOV_SHADOWMAP, -FOV_SHADOWMAP, FOV_SHADOWMAP);
		glGetFloatv(GL_MODELVIEW_MATRIX, modelProjectionMatrix);
	glPopMatrix();
}

But with this my depth buffer has an image in it but will not render the shadow for some reason and this is the only change I make is between these two functions?
void StoreLightMatrix(void)
{
	memset(modelViewMatrix, 0, sizeof(float) * 16);
	memset(modelProjectionMatrix, 0, sizeof(float) * 16);

	sunDir[0] = camera.pos.x - lightPosition[0];
	sunDir[1] = camera.pos.y - lightPosition[1];
	sunDir[2] = camera.pos.z - lightPosition[2];

	double lightToSceneDistance = sqrt(lightPosition[0] * lightPosition[0] + lightPosition[1] * lightPosition[1] + lightPosition[2] * lightPosition[2]);
    double nearPlane = lightToSceneDistance - 350.0;
    if(nearPlane < 5.0)
		nearPlane = 5.0;

    double fieldOfView = 17000.0 / lightToSceneDistance;
	
    glPushMatrix();
		glPushMatrix();
			glLoadIdentity();
			gluLookAt(camera.pos.x, camera.pos.y, camera.pos.z, 
					  camera.pos.x + sunDir[0], camera.pos.y + sunDir[1], camera.pos.z + sunDir[2], 
					  0.0, 1.0, 0.0);
			glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix);
		glPopMatrix();
		glLoadIdentity();
		gluPerspective(fieldOfView, 1.0, nearPlane, nearPlane + 50.0);
		glGetFloatv(GL_MODELVIEW_MATRIX, modelProjectionMatrix);
	glPopMatrix();
}

Share this post


Link to post
Share on other sites
Where are you getting 350.0 and 17000.0?

Your 350.0 would matter only if lightToSceneDistance<=350.0. gluPerspective chunks if nearPlane <= 0. Also, 17000.0 is a huge number, and would require an equally huge number to counter act it. Again, if fieldOfView > 180.0, gluPerspective pukes.

But I can't see any other potential problems in the code. Everything's pretty much done the exact same way as your working code. :)

EDIT: Sorry, didn't notice you set nearPlane to at least 5.0. Might be a problem for objects very close to your light source, but I doubt it would cause anything but visual artifacts.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this