Sign in to follow this  
Dragon_Strike

problems with shadowmapping

Recommended Posts

im having some problems with shadow mapping... been trying to figure this out for a few hours.. but im about to give up... this is my first time implementing it so i guess ive missed something... any ideas what i might have done wrong? thx...
    code:

       const GLdouble bias[] = {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};
    class CSHADOWMAP
    {
    	public:
    		
    		Init()
    		{
    			ShadowShader = new GLSLProgram("Shaders/TexDefault.vert", "Shaders/ShadowMap.frag", "Shaders/ShadowMap.log"); // all this shader does is to set fragcolor = vec4(1.0,1.0,1.0,1.0)

    			LightPos = vec3(500,500,500);
    			size = 1024;

    			glGenTextures(1, &shadowtex);
    			glBindTexture(GL_TEXTURE_2D, shadowtex);
    			glTexImage2D(GL_TEXTURE_2D, 0 , GL_DEPTH_COMPONENT, size, size, 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);


    			glPushMatrix();
    				glLoadIdentity();
    					gluPerspective(45.0f, 1.0f, 1.0f, 10000.0f);
    					glGetDoublev(GL_MODELVIEW_MATRIX, ProjMatrix);

    				glLoadIdentity();
    					gluLookAt(LightPos.x, LightPos.y, LightPos.z,
    							   0.0f, 0.0f, 0.0f,
    							   0.0f, 1.0f, 0.0f);

    					glGetDoublev(GL_MODELVIEW_MATRIX, ViewMatrix);
    			glPopMatrix();

    		}

    		GLSLProgram* ShadowShader;

    		int size;

    		GLuint shadowtex;

    		vec3 LightPos;

    		GLdouble ProjMatrix[16];
    		GLdouble ViewMatrix[16];		
    };

    void CWORLD::Init()
    {
    	ShadowMap.Init();	
    }

    void CWORLD::RenderShadowPass()
    {
    	
    	glActiveTexture(GL_TEXTURE9);
    	glBindTexture(GL_TEXTURE_2D, ShadowMap.shadowtex);

    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
    	
    	glMatrixMode(GL_PROJECTION);
    	glLoadMatrixd(ShadowMap.ProjMatrix);

    	glMatrixMode(GL_MODELVIEW);
    	glLoadMatrixd(ShadowMap.ViewMatrix);
        
    	glViewport(0, 0, ShadowMap.size, ShadowMap.size);	

    	ShadowMap.ShadowShader->use();

    		glCullFace(GL_FRONT);

    		Ground.Render(m_Cam->Position, LightPos, vec3(0.0,0.0,0.0)); //render plane

    		glEnable(GL_ALPHA_TEST);
    		glAlphaFunc(GL_GREATER, 0.3);

    		Veg.Render(m_Cam->Position, LightPos, vec3(0,0,0)); // render plants

    		glDisable(GL_ALPHA_TEST);
    		glCullFace(GL_BACK);

    		
    		glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, ShadowMap.size, ShadowMap.size);

    	ShadowMap.ShadowShader->disable();

    }


    void CWORLD::Render(bool* keys, vec2 MouseCoord, vec2 MouseButton)
    {
    	RenderShadowPass();

    	Update(keys, MouseCoord, MouseButton);

    	if (WireFrame)
    		glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    	else
    		glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

    	glViewport(0,0,1024,768);	
    	gluPerspective(45.0f, (GLfloat)1024/(GLfloat)768, 1.0f, 10000.0f);
    	
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();

    	m_Cam->Update(keys); // glulookat function

    	glMatrixMode(GL_TEXTURE);
    	glLoadMatrixd(bias);
    	glMultMatrixd(ShadowMap.ProjMatrix);
    	glMultMatrixd(ShadowMap.ViewMatrix);
    	glMatrixMode(GL_MODELVIEW);

    	glActiveTexture(GL_TEXTURE9);
    	glBindTexture(GL_TEXTURE_2D, ShadowMap.shadowtex);

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


    	Ground.ObjectShader->use();		
    		Ground.ObjectShader->sendUniform("ShadowMap", 9);
    		Ground.Render(m_Cam->Position, LightPos, vec3(0,0,0));
    	Ground.ObjectShader->disable();

    	glEnable( GL_BLEND );									
    	glBlendFunc(GL_SRC_ALPHA,		GL_ONE_MINUS_SRC_ALPHA);
    	glEnable(GL_ALPHA_TEST);
    	glAlphaFunc(GL_GREATER, 0.3);

    		Veg.ObjectShader->use();
    			Veg.Render(m_Cam->Position, LightPos, vec3(0,0.0,0));		
    		Veg.ObjectShader->disable();

    	glDisable(GL_ALPHA_TEST);
    	glDisable( GL_BLEND );				
    }

    // and the shaders

    //VERTEX
    varying vec4 ProjShadow;

    void main()
    {

    	
    	gl_TexCoord[0] = gl_MultiTexCoord0;
    	vec3 VertPos = gl_Vertex.xyz;
    	
    	gl_Position = gl_ModelViewProjectionMatrix * vec4(VertPos,1.0);	
    }

    // FRAGMENT

    #version 110

    uniform sampler2DShadow ShadowMap;
    varying vec4 ProjShadow;

    void main()
    {		 
     
    	Brightness = shadow2DProj(ShadowMap, ProjShadow).r;
    			
    	gl_FragColor = vec4(Brightness.xxxx, 1.0); 

    }

on the plane im rendering this results in a black area where z > 0 and white where z < 0. and the model shadows i render with Veg.Render are nowhere to be seen...

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