Jump to content
  • Advertisement
Sign in to follow this  
Yhonatan

Problem with shadow mapping and glsl

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

Hey all, first of all I must say that i'm already trying a couple of days to achieve shadow in my game with no success. finally today I've found a good tutorial that worked but it's not working for me. this is what i get: http://img367.imageshack.us/img367/9365/get2al7.jpg this is what I suppose to get (in the toturial): http://img367.imageshack.us/img367/7820/supposefx8.jpg IF i switch the shaders order (Second is first and first is second) this is what i get: http://img529.imageshack.us/img529/9580/getgm4.jpg I really have no idea what's going on and i'm kinda desperate on this one. this is my rending code:

	glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, g_frameBuffer );
			glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_shadowMapID, 0 );
			glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, g_depthRenderBuffer );
			glViewport( 0, 0, RENDERBUFFER_WIDTH, RENDERBUFFER_HEIGHT );
			glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
			glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

			
			glMatrixMode( GL_MODELVIEW );
			glLoadIdentity();
			g_light_pos[0]=Light[0];
			g_light_pos[1]=Light[1];
			g_light_pos[2]=Light[2];
			gluLookAt( g_light_pos[0],g_light_pos[1],g_light_pos[2], // Look from the light's position
               0.0f, 0.0f, 0.0f,   // Towards the teapot's position
               0.0f, 1.0f, 0.0f ); 
			glGetFloatv(GL_MODELVIEW_MATRIX, g_lightproj_matrix);	
    glMatrixMode( GL_TEXTURE );
    glLoadIdentity();
    glTranslatef( 0.5f, 0.5f, 0.5f );                      // Offset
    glScalef( 0.5f, 0.5f, 0.5f );                          // Bias
	gluPerspective( 45.0, (GLdouble)1024 / 768, 0.1, 1500.0 );
    glMultMatrixf( g_lightproj_matrix ); 
	glMatrixMode( GL_MODELVIEW );

glUseProgramObjectARB(first);
			
			DrawEntireScene();
			


glUseProgramObjectARB(0);

			
			glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
			glViewport( 0, 0, 1024, 768 );
	glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );

	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	
	glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
gluLookAt( 0.0f,0.0f,0.0f, // Look from the light's position
               0.0f, 0.0f, 0.0f,   // Towards the teapot's position
               0.0f, 1.0f, 0.0f ); 


glUseProgramObjectARB(Second);
glBindTexture( GL_TEXTURE_2D, g_shadowMapID );
DrawEntireScene();


As you can see it looks Really horrible and it even changes while i move. this is my shaders code: --First.Vertex--

// We will need to send only one var to the fragment shader.
varying vec3 vertPos;

void main(void)
{

	// Set and send OutPut data
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

	// This is just setting 'vertPos' to the vertex's position
	// in view space. This way, it will be easy to get the distance
	// between the light and the vert, as we assume the camera
	// is positioned at the light source.
	vertPos = vec4(gl_ModelViewMatrix * gl_Vertex).xyz;

	    

}

-First.Fragment-
varying vec3 vertPos;

void main(void)
{

    // The hardest part to understand is how we will store
    // the depth of the fragment.
    // This will be done in kind of a pseudo-bitshift
    // fashion, ie the red,green and blue components will
    // each store 8bits of the 24bit depth.
    // NOTE: Both 16bit or 32bit can be done in this fashion
    // with no speed gain or loss, however 24bit seems to
    // be enough for most depth buffers, so we will use that.


    // Firstly, we create the constant 'bit' vectors.
    // 'bitSh' is the bit shift, designed to 'shift' the 
    // depth value so that only a certain part lies within
    // the current 8bit component.
    // The second is the bit mask, designed to cull all numbers
    // below the 8bit component. Any value above will be culled by
    // the 'fract()' function.
    const vec3 bitSh = vec3(	256*256,	256,			1);
    const vec3 bitMsk = vec3(	0,		1.0/256.0, 	1.0/256.0);
    

    // We will need to calculate and store the distance between the
    // light and the fragment. 
    // NOTE: This value needs to be normalized, this can usually be
    // done by dividing the length by the lights range, but for simplicity here
    // we are dividing it by 100, which is the 'zFar' value set in 'gluPerspective()'.
    float dist = length(vertPos)/100.0;
    vec3 comp;
    

    // Now, we apply the bit shift, the commented code is what is
    // happening under the hood. If you dont understand bit shifting
    // or masking, just trust me on this. It will move a different section
    // of the 'distance' over the 0.0f-1.0f range of each component, so that
    // a 24bit float can be stored in 3*8bit floats.

    //comp.x=dist*(256*256);
    //comp.y=dist*256;
    //comp.z=dist;

    comp=dist*bitSh;


    // The next part (again, commented code shows what is happening)
    // is simply culling all unwanted 'bits'...all we want is a value
    // between 0.0f and 1.0f, with a precision of 1.0f/256.0f.
    // If the precision is better (1.0f/512.0f for example) the value
    // may be rounded off and we will get a bad value. And of course if the
    // value is above 1.0f the value will be clamped, which
    // again will produce a bad value.
    
    //comp.x=fract(comp.x);
    //comp.y=fract(comp.y)-comp.x/256;
    //comp.z=fract(comp.z)-comp.y/256;

    comp=fract(comp);
    comp-=comp.xxy*bitMsk;


    // For a simple one-texture projectsion spot light, this is perhaps not 
    // the best method (GL_DEPTH_COMPONENT should produce nicer results), but for cube
    // shadow mapping, this is the best way.

    // This also removes the need for floating point
    // buffers (GL_RGBA_32F) or for single component rendering (GL_LUMINANCE).
    // Which in some cases can be a plus for compatability.
    
    gl_FragColor=vec4(comp,1);

}

-Second.Vertex-

uniform mat4 lightMatrix;

varying vec4 lightPos;
varying vec4 lightProj;

void main( void)
{

	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;




	lightProj= gl_TextureMatrix[0]* gl_Vertex;
	lightPos=gl_LightSource[1].position- gl_Vertex;

}

-Seconds.Fragment-

uniform sampler2D shadowMap;

varying vec4 lightPos;
varying vec4 lightProj;

// Use use another bitshift constant, notice that
// its the reciprocal of the other bit shift constant
// in the shadow.frag shader.
const vec3 bitShifts = vec3(1.0/(256.0*256.0), 1.0/256.0, 1);

void main(void)
{

	// Firstly, we get the 2D coords of the shadow map,
	// and get its value.
	vec3 pos = lightProj.xyz/lightProj.w;
	vec4 shadmap = texture2D(shadowMap,pos.xy );
//vec4 shadmap = texture2D(shadowMap,gl_TexCoord[0]);

	// Next we just have some constants, the bias is used to
	// 'push' the shadow back, to reduce 'Z-fighting' on the
	// shadow maps. The range is used to normalize the distance
	// between the fragment and the light, (just like we did
	// in shadow.frag, notise the value 100).
	const float bias = 0.1;
	const float range = 100.0;

	// We calculate the distance between the fragment and the light.
	// Then we apply the bias and the normalization by dividing
	// it by the range;
	float lightLen=length(lightPos);
	float lightDist=(lightLen-bias)/range;

	// Now we apply the bit shift, and add all of the components 
	// together. A dot product does this very cleanly.
	// This is all we need to do to convert the shadow map into
	// a distance value.
	float shad = dot(shadmap.xyz , bitShifts);
        
	// Perform the actual shadow map test. If shad is larger than
	// light dist, then the current fragment is outside of the shadow.
	float shadow = float(shad >lightDist);
	
	// NOTE: For show purposes, the shadow map is also drawn.
	// The only value we need is 'shadow'.
	// This is just to show you how freaky the unconverted
	// shadow map looks.
	gl_FragColor=shadmap *shadow ;

}

Does anyone know what Should i do? Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!