Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Odd shadow problem

This topic is 4288 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

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
Advertisement
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
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!