Shadow mapping tutorial

Started by Fir3cell, July 11, 2012, 16:31:38

Previous topic - Next topic

Fir3cell

Hi!

I'm in search for a shadow mapping tutorial that uses LWJGL and GLSL. Could you recommend some?

Fir3cell

I have write the following code, but there are now shadows, just the per-fragment lighting:

Set up of shadow texture:
depthTextureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, depthTextureID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_TEX_SIZE, SHADOW_TEX_SIZE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, (FloatBuffer)null);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glBindTexture(GL_TEXTURE_2D, 0);


Set up of FBO:
shadowFBOID = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, shadowFBOID);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTextureID, 0);
glDrawBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);


Render of the shadow scene:
glBindFramebuffer(GL_FRAMEBUFFER, shadowFBOID);
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, SHADOW_TEX_SIZE, SHADOW_TEX_SIZE);
glColorMask(false, false, false, false);
glUseProgram(shadowShaderProgram);
drawFloor();
glCallList(modelDisplayList);
glUseProgram(0);
glColorMask(true, true, true, true);
glViewport (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);


Render of combined scene:
glUseProgram(shaderProgram);
glUniform1i(shadowtextureUniform, depthTextureID);
glUniformMatrix4(biasMatrixIn, false, BIAS_MATRIX);
drawFloor();
glCallList(modelDisplayList);
glUseProgram(0);


Shadow scene shaders:
#Vertex shader

void main(){
	gl_Position = ftransform();
}

#Fragment shader

void main(){
  	gl_FragColor = vec4(1.0);
}


Combined scene shaders:
#Vertex shader

in vec3 AmbientColor;
in vec3 DiffuseColor;
in vec3 SpecularColor;

uniform mat4 BiasMatrix;

out vec3 Normal;
out vec3 Position;
out vec3 Ka;
out vec3 Kd;
out vec3 Ks;
out vec4 ShadowCoord;

void main(){
	mat4 MCtoLightMatrix = BiasMatrix * gl_ModelViewProjectionMatrix;
	ShadowCoord = MCtoLightMatrix * gl_Vertex;
	Ka = AmbientColor;
	Kd = DiffuseColor;
	Ks = SpecularColor;
	Normal = normalize(gl_NormalMatrix * gl_Normal);
	Position = vec3(gl_ModelViewMatrix * gl_Vertex);
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
#Fragment shader

uniform sampler2DShadow ShadowMap;

in vec3 Normal;
in vec3 Position;
in vec3 Ka;
in vec3 Kd;
in vec3 Ks;
in vec4 ShadowCoord;

vec3 ads(){
	vec3 n = normalize(Normal);
	vec3 s = normalize(gl_LightSource[0].position.xyz - Position);
	vec3 v = normalize(vec3(-Position));
	vec3 r = reflect(-s, n);
	return gl_LightModel.ambient.rgb * (Ka + Kd * max(dot(s, n), 0.0) + Ks * pow(max(dot(r, v), 0.0), gl_FrontMaterial.shininess));
}

void main(){
	float shadeFactor = textureProj(ShadowMap, ShadowCoord);
	shadeFactor = shadeFactor * 0.25 + 0.75;
	gl_FragColor = vec4(ads() * shadeFactor, 1.0);
}


Camera and light have the same position and orientation.
How could I see the data stored in texture that was created when rendering shadow scene?
Any help would be appreciated.

Fir3cell

Hi, still having problems with shadow mapping.
I have drawn depth texture on quad and looks like this (black dots on the floor):


Does it look right, or I'm doing something wrong?

Any help would be appreciated.