Sign in to follow this  

Shadow Mapping without GLSL

This topic is 2953 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 want to shadow my scene, but it is not working. I don't know where my problem is. If there's someone who can help me I'd be very thankful! So this is my confusing code
[/if (RenderSingleton::Instance()._GenerateShadowMap!=true)
	{
//first render pass
		const Vector3f _LightPosition(501.0f, 501.0f, 510.0f );
		Vector3d _LightLookAt(0.0, 0.0, 0.0 );
		
		glClear(GL_DEPTH);
		

	    //Shading states
	    glShadeModel(GL_SMOOTH);
	    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	    //Depth states
	    glClearDepth(1.0f);
	    //zeichnet alles was gleich oder niedriger ist
	    glDepthFunc(GL_LEQUAL);
		//nur tiefentest wird aktiviert

		glEnable(GL_DEPTH_TEST);

	    glEnable(GL_CULL_FACE);

		const Vector4f Color(0.2f,0.2f,0.2f,0.2f);
		//Lichtquelle
		glLightfv(GL_LIGHT1,GL_POSITION,_LightPosition);
		glLightfv(GL_LIGHT1, GL_AMBIENT, Color);
	    glLightfv(GL_LIGHT1, GL_DIFFUSE, Color);

		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT1);
		glEnable(GL_LIGHT0);
		glEnable(GL_COLOR_MATERIAL);

		//Textur initialisieren
		glGenTextures(1, &shadowMap);
		glBindTexture(GL_TEXTURE_2D,shadowMap);

		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);

		
		
		Position3d eye = Camera()->Eye();
		Position3d eyeLookAt = Camera()->Target();
		UnitVector3d eyeTopVec = Camera()->UpDirection();

		Vector3d _CameraPosition(eye[0],eye[1],eye[2]);
		Vector3d _CameraLookAt(eyeLookAt[0], eyeLookAt[1], eyeLookAt[2]);
		Vector3d _CameraTopVec(eyeTopVec[0], eyeTopVec[1], eyeTopVec[2]);


		GLViewPort view = Camera()->ViewPort();

		Vector4f _View(view[0],view[1],view[2],view[3]);

		if (view[3] != 0){

		glPushMatrix();
			glLoadIdentity();
			gluPerspective(45.0,(view[2]/view[3]),500.0,1050.0 );
			glGetDoublev(GL_MODELVIEW_MATRIX,lightProjectionMatrix);
			glPopMatrix();
		}
		else 
		{
			view[3]=1;
		glPushMatrix();
			glLoadIdentity();
			gluPerspective(45.0,(view[2]/view[3]),500.0,1050.0 );
			glGetDoublev(GL_MODELVIEW_MATRIX,lightProjectionMatrix);
			glPopMatrix();
		}

		glPushMatrix();
			glLoadIdentity();
			gluLookAt(_LightPosition[0], _LightPosition[1], _LightPosition[2],
					_LightLookAt[0], _LightLookAt[1], _LightLookAt[2],
					0.0, 0.0, 1.0);
			glGetDoublev(GL_MODELVIEW_MATRIX, lightViewMatrix);
			glPopMatrix();
		
		glPushMatrix();
			glLoadIdentity();
			gluLookAt(_CameraPosition[0],_CameraPosition[1],_CameraPosition[2],
						_CameraLookAt[0], _CameraLookAt[1], _CameraLookAt[2],
						_CameraTopVec[0], _CameraTopVec[1], _CameraTopVec[2]);
			glGetDoublev(GL_MODELVIEW_MATRIX, cameraViewMatrix);
			glPopMatrix();	
		 
		glPushMatrix();
			glLoadIdentity();
			gluPerspective(45.0f, view[2]/view[3], 1.0f, 500.0f);
			glGetDoublev(GL_MODELVIEW_MATRIX, cameraProjectionMatrix);
			glPopMatrix();


		
			//ShaderExtensions::glUseProgramObjectARB(0);
			//glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		
			glLoadIdentity();

			glViewport(0 ,0 ,view[2] ,view[3]);
			
			glMatrixMode(GL_PROJECTION);
			glPushMatrix();

			//Perspektive des Lichts
			
			glLoadMatrixd(lightProjectionMatrix);
			//glPushMatrix();
			glMatrixMode(GL_MODELVIEW);
			glLoadMatrixd(lightViewMatrix);
		
			glColorMask(0, 0, 0, 0);

			glPolygonOffset(1.1,4.0);
			glEnable(GL_POLYGON_OFFSET_FILL);
			glDisable(GL_LIGHTING);	

			//rendern
			_Root->Render(mode);

			//vom z-Buffer in textur kopieren
			glBindTexture(GL_TEXTURE_2D, shadowMap);
			glCopyTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT,0,0,view[2],view[3],0);

			
			glColorMask(1,1,1,1);
			//offsetting deaktivieren
			glDisable(GL_POLYGON_OFFSET_FILL);
			glDisable(GL_LIGHT1);

			//Model/ProjectionView Matrix zurücksetzen
			glMatrixMode(GL_PROJECTION);
			glPopMatrix();
			glMatrixMode(GL_MODELVIEW);
			glPopMatrix();

			glLoadIdentity();
			RenderSingleton::Instance()._GenerateShadowMap = true;]
}
else

	{//second render pass

	 const double mBias[] = {0.5, 0.0, 0.0, 0.0, 
						   0.0, 0.5, 0.0, 0.0,
						   0.0, 0.0, 0.5, 0.0,
						   0.5, 0.5, 0.5, 1.0};
        static Matrix4x4d biasMatrix(mBias);
       
	    Matrix4x4d textureMatrix=biasMatrix*lightProjectionMatrix*lightViewMatrix;

	    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	    glTexGenfv(GL_S, GL_EYE_PLANE, Vector4f(textureMatrix[0], textureMatrix[4], textureMatrix[8], textureMatrix[12])/*textureMatrix.GetRow(0)*/);
	    glEnable(GL_TEXTURE_GEN_S);

	    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	    glTexGenfv(GL_T, GL_EYE_PLANE, Vector4f(textureMatrix[1], textureMatrix[5], textureMatrix[9], textureMatrix[13])/*textureMatrix.GetRow(1)*/);
	    glEnable(GL_TEXTURE_GEN_T);

	    glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	    glTexGenfv(GL_R, GL_EYE_PLANE, Vector4f(textureMatrix[2], textureMatrix[6], textureMatrix[10], textureMatrix[14])/*textureMatrix.GetRow(2)*/);
	    glEnable(GL_TEXTURE_GEN_R);

	    glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
	    glTexGenfv(GL_Q, GL_EYE_PLANE, Vector4f(textureMatrix[3], textureMatrix[7], textureMatrix[11], textureMatrix[15])/*textureMatrix.GetRow(3)*/);
	    glEnable(GL_TEXTURE_GEN_Q);

		glBindTexture(GL_TEXTURE_2D, shadowMap);
	    glEnable(GL_TEXTURE_2D);

	    //Enable shadow comparison
	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);

	    //Shadow comparison should be true (ie not in shadow) if r<=texture
	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

	    //Shadow comparison should generate an INTENSITY result
	    glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
	
		 glAlphaFunc(GL_GEQUAL, 0.99f);
	    glEnable(GL_ALPHA_TEST);
		_Root->Render(mode);

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


	

        //reset matrices
	    glMatrixMode(GL_PROJECTION);
	    glPopMatrix();
	    glMatrixMode(GL_MODELVIEW);
	    glPopMatrix();

_Root->Render(mode);

}]

Share this post


Link to post
Share on other sites
What do you mean by not working?

Not working at all
Black screen
ugly shadows
flickering shadows
etc.

Provide some detail and/or pictures, because your code looks fine (besides that a lot of your code can be placed into initialization.)

EDIT: you even generate the map in every frame, which is a bad thing. Look into texture mapping a bit.
put this:
glGenTextures(1, &shadowMap);
glBindTexture(GL_TEXTURE_2D,shadowMap);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//NEAREST);

glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHDW_MAP_SIZE, SHDW_MAP_SIZE, 0,
GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);

into your initialization code. Did you call glTexImage anywhere?

Btw to offset the shadowmap, use the bias matrix instead of polygon offset:
const double mBias[] = {0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.49999, 1.0};

where are my whitespaces?

Share this post


Link to post
Share on other sites
You have glClear(GL_DEPTH);

That should be glClear(GL_DEPTH_BUFFER_BIT);
Same for the color buffer: glClear(GL_COLOR_BUFFER_BIT);

see this (GL.H)
#define GL_DEPTH_BUFFER_BIT               0x00000100
#define GL_DEPTH 0x1801

They are not the same at all!


That's my last effort :P

Share this post


Link to post
Share on other sites

This topic is 2953 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.

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