LWJGL
May 23, 2013, 16:38:43 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.9.0 released!
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: Shadow mapping tutorial  (Read 1588 times)
Fir3cell
Newbie
*
Posts: 3


« on: July 11, 2012, 09:31:38 »

Hi!

I'm in search for a shadow mapping tutorial that uses LWJGL and GLSL. Could you recommend some?
Logged
Fir3cell
Newbie
*
Posts: 3


« Reply #1 on: July 13, 2012, 09:39:55 »

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

Set up of shadow texture:
Code:
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:
Code:
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:
Code:
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:
Code:
glUseProgram(shaderProgram);
glUniform1i(shadowtextureUniform, depthTextureID);
glUniformMatrix4(biasMatrixIn, false, BIAS_MATRIX);
drawFloor();
glCallList(modelDisplayList);
glUseProgram(0);

Shadow scene shaders:
Code:
#Vertex shader

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

#Fragment shader

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

Combined scene shaders:
Code:
#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.
Logged
Fir3cell
Newbie
*
Posts: 3


« Reply #2 on: July 26, 2012, 02:44:13 »

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.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!