   Shadow Mapping using OpenGL 3.2 Core Profile and GLSL 1.50
  Hi everyone,Since i just started programming with shaders, i started to implement simple Shadow Mapping using vertex and fragment shaders, along with Core OpenGL 3.2.I'm getting some weird results, and i cannot figure out what i did wrong, hope anyone can help me. Shader Code parts are similar to Cg Version from  "The Cg Tutorial". I added some light calculationsImage and code snippets as follows: Vertex Shader//uniform parameteruniform mat4 cgl_ProjectionMatrix;uniform mat4 cgl_GeometryMatrix;uniform mat4 cgl_CameraMatrix;uniform mat4 cgl_NormalMatrix; //=transpose(inverted(cameraMatrix*geometryMatrix))uniform mat4 cgl_TextureMatrix;uniform vec4 lightPos;//input parameterin vec4 cgl_Vertex;in vec4 cgl_Normal;in vec2 cgl_Texcoord;//output parameter//smooth = value for the fragment shader will be interpolatedsmooth out vec2 cgl_SmoothTexcoord;smooth out vec4 cgl_SmoothColor;out vec4 cgl_texCoordProj;... some light and material definitionsvoid main() {light.position=lightPos;vec3 ecPosition = (cgl_CameraMatrix * (cgl_GeometryMatrix * cgl_Vertex)).xyz;vec3 normalVec  = (cgl_NormalMatrix * cgl_Normal).xyz;vec3 lightVec   = normalize(light.position.xyz - ecPosition);vec3 halfVector = normalize(lightVec + vec3(0,0,1));float diffuse   = max(dot(normalVec, lightVec), 0.0);float spec = pow(max(dot(normalVec, halfVector), 0.0), material.shininess);vec4 Iamb  = vec4(0.4); vec4 Idiff = material.diffuse * light.diffuse * diffuse;vec4 Ispec = material.specular * light.specular * spec;cgl_SmoothColor    = Iamb + Idiff + Ispec;cgl_SmoothTexcoord = cgl_Texcoord;<b>//calc shadow projection//mat4 lightProjViewModelMatrix = cgl_TextureMatrix * cgl_GeometryMatrix;cgl_texCoordProj = cgl_TextureMatrix * cgl_Vertex;</b>//compute the effective vertex position using the projection, camera and geometry matrixgl_Position  = cgl_ProjectionMatrix * (cgl_CameraMatrix * (cgl_GeometryMatrix * cgl_Vertex));}Fragment Shader//uniform parameteruniform sampler2D cgl_textureSampler;uniform sampler2DShadow depthMap;//input parametersmooth in vec2 cgl_SmoothTexcoord;smooth in vec4 cgl_SmoothColor;in vec4 cgl_texCoordProj;//output parameterout vec4 cgl_FragColor;float shadowCoeff;void main(void) {shadowCoeff = textureProj(depthMap,cgl_texCoordProj);//compute effective fragment color as the product of the smoothcolor and the texel colorvec4 color = cgl_SmoothColor * textureProj( cgl_textureSampler, vec3(cgl_SmoothTexcoord.xy,1) );cgl_FragColor = shadowCoeff * color;}The texture matrix i calculated as follows (since OpenGL 3.2 doesn't support standard matrix calculations any more)void drawSceneWithShadows(void){//first render pass, draw from lights point of view//clear buffersglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//render scene from view of lightSourceMatrix currentCamera = cameraMatrix;camera->SetAspectRatio( 1.0f );//change viewPort to size of depthmap textueglViewport(0,0,512,512);BuildPerspectiveProjectionMatrix( &projectionMatrix, camera );cameraMatrix = Matrix::LookAtMatrix(Vector3(light0_pos[0],light0_pos[1],light0_pos[2]),Vector3(0,0,0),Vector3(0,1,0));//build texture projection MatrixtextureMatrix = biasMatrix * projectionMatrix * cameraMatrix;//draw the scenedrawScene(false);//save depths in depth buffer "the shadow map"copyDepthToTexture();//second render pass//clear buffersglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//set back camera and viewportcameraMatrix = currentCamera;if(width <= height) {camera->SetAspectRatio( (GLfloat)height/ (GLfloat)width ); } else {camera->SetAspectRatio( (GLfloat)width/ (GLfloat)height ); }glViewport(0, 0,(GLsizei) width,(GLsizei) height);//update our projection matrixBuildPerspectiveProjectionMatrix( &projectionMatrix, camera );//set depth map to texture unit 1glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D, depthMap);glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);//for debuggingcameraMatrix = Matrix::LookAtMatrix(Vector3(light0_pos[0],light0_pos[1],light0_pos[2]),Vector3(0,0,0),Vector3(0,1,0));//draw scene again, this time (shadows == true) shadow shaders will be useddrawScene(true);}Would appreciate your help :)   
  It's very difficult to see what's going on in your scene.  I would recommend putting your light source nearly overhead (make sure your camera's up vector is not collinear!).  Plus, make the shadows not 100% dark.  Perhaps use simpler objects too, like cubes, spheres, etc.  From there, try drawing the depthmap you've rendered (like to a 2D window on your screen) to make sure that it looks correct.  If it does, then it will be easier to track down the problem.  G   ;  Hi Geometrian,first thanks for your helpI tried your advice, the depth map is ok (it's exactly the scene from the lights view, of course with the closest things darker than the ones more far away).after trying the light scene with a fixed light source i tried it again with my moving lightand i think i did something wrong with the texture matrix, but don't have an idea what.I already checked that i didn't mixed up rows and colums in the bias matrix (combination of scale 0.5 and translate 0.5, 0.5, 0.5 so get in the range of texture coordinates),the other matrices (set up by hand due to OpenGL 3.2 core) should also work (otherwise i suppose also normal rendering would not work).Here some shots with the light coming from different angles:(used another tree to visualize where the lightsource is)approx 0° approx 90° approx 180° approx 270° I also changed some pieces of code: textureMatrix is now: textureMatrix = biasMatrix * projectionMatrix * cameraMatrix * currentCamera.Inverted() ;in the vertex shader i calculate the  lookup coordinates as followscgl_texCoordProj = cgl_TextureMatrix *(cgl_CameraMatrix *(cgl_GeometryMatrix* cgl_Vertex));.;  so finally i think i got the matrices right,forgot to invert the lightViews camera matrix:cpp:textureMatrix = biasMatrix * projectionMatrix * cameraMatrix.Inverted();vertexShader:cgl_texCoordProj = cgl_TextureMatrix  * cgl_GeometryMatrix * cgl_Vertex;but i still have some graphic errors: how can i resolve this :) ?   ;  got it, put an offset to the polygons so that selfshadowing won't occur, and i've seen that the shadowmap doesn't cover the whole scene, so i put the light source a little bit more far away -> everything fine now.I'm just wondering if there are better methods for that, but for now this is fine for me.   