Easy OpenGL Directional Lighting Question

Started by
11 comments, last by Terin 11 years, 9 months ago
Hi dpadam450,

Here's what I have so far.

My shader for building the depth buffer and shadows is done as follows and works correctly:

[source lang="cpp"] //Vertex
varying vec4 LightPosition;

void main()
{
gl_Position = ftransform(); // Get the actual in-scene vector position.
LightPosition = gl_Position; // Share this position with the fragment shader.
}

//Fragment
varying vec4 LightPosition;

void main()
{
gl_FragColor = vec4(LightPosition.z / LightPosition.w); // The color is the depth ratio.
}[/source]

This builds everything fine, and I've checked.

The actual code for the shaders that draw the scene are as follows:

[source lang="cpp"] //Vertex
uniform mat4 LightModelViewProjectionMatrix;
uniform mat4 WorldMatrix;

varying vec3 Normal;
varying vec4 LightCoordinate; // Position in model view projection space from the lights view.
varying vec4 WorldPosition; // Position in world space.

void main()
{
Normal = gl_Normal;
WorldPosition = WorldMatrix * gl_Vertex;
LightCoordinate = LightModelViewProjectionMatrix * gl_Vertex;
gl_Position = ftransform(); // Transform via fixed function into the viewer's view
gl_TexCoord[0] = gl_MultiTexCoord0;
}

//Fragment
uniform sampler2D DiffuseMap;
uniform sampler2D ShadowMap;
uniform mat4 WorldMatrix;
uniform float MinimumShadow;

varying vec3 Normal;
varying vec4 LightCoordinate;
varying vec4 WorldPosition;

void main()
{
// Direct lighting
// ------------------------------
vec4 Color = gl_LightSource[0].ambient;
vec3 l = normalize(gl_LightSource[0].position.xyz - WorldPosition.xyz); // direction to the light source

vec3 view_normal = normalize(gl_NormalMatrix * Normal);
vec3 view_light_direction = normalize(vec3(gl_LightSource[0].position)); //(WorldMatrix * vec4( LightPosition.x, LightPosition.y, LightPosition.z, 0 ) ).xyz;

//float lambert = max(dot(view_normal, view_light_direction), 0.2);
float lambert = max(dot(Normal, l), MinimumShadow);
Color.xyz *= lambert;

//Blend in Color from primary texture unit
Color.wxyz *= texture2D(DiffuseMap, vec2(gl_TexCoord[0])).wxyz;

// Shadow mapping
// ------------------------------
vec4 lcoord = LightCoordinate; // Fragment position in light space.
lcoord /= lcoord.w; // Project to cartesian space
lcoord.xy = lcoord.xy * 0.5 + 0.5; // Scale since light clipping space is in [-1,1] but texture space is [0,1]

float fragmentDepth = lcoord.w; // Depth of the fragment in light space.
float shadowMapDepth = texture2D(ShadowMap, lcoord.xy).x; // Depth in the shadow map.

float eps = 0.001; // depth bias
float shadow = fragmentDepth - eps > shadowMapDepth ? 0.5: 1.0;

gl_FragColor = Color * shadow;
}[/source]

This appears to be where I start having the screw-ups. Everything in the scene renders right -- except for the light. The Shadow Mapping part works great at the bottom, but the top part seems to be screwing up somehow. Mind you, I realize the correct code should be the first lambert (that is commented out). However, there are no shadows in this situation.

Now, I've got the position building correctly to what I believe it should be. In fact, it's at the point where I can render from the Light's viewpoint and it shows how I expect it to.

[source lang="csharp"] Single LightX = (Single)( Math.Cos( Radians ) );
Single LightY = 0.0f;
Single LightZ = (Single)( Math.Sin( Radians ) );
Double LightAngle = Radians / Math.PI * 180.0d;

Single[] LightDirection = new Single[] { LightX, LightY, LightZ, 0.0f };
Gl.glLightfv( Gl.GL_LIGHT0, Gl.GL_POSITION, LightDirection );[/source]

Does anything appear to be outwardly wrong?
Advertisement
So what I would suggest again, is instead of binding the shadow texture, Bind an image of a tree or anything and see if you can see any of the image at all noticeable.

The Shadow Mapping part works great at the bottom, but the top part seems to be screwing up somehow.[/quote]
So you have shadows but not lighting? Post a pic.

You mentioned you have a 0 for the w coordinate but you are subtracting light position from world position as if it is a point light.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This current code actually renders the shadows and light (but the light is incorrect).

Here's what that looks like:

https://plus.google....sts/GkWqzumYmKv

That is the problem I have with that code. When I do it with the other stuff that's commented out, I have flat shading with no shadows. It's as if I had just put a film over the screen and changed the color of it to darken the entire scene.

EDIT:

Just want to note that I changed the discussion of this and posted to a different topic (since this is no longer an accurate title and is a completely different issue altogether) : http://www.gamedev.net/topic/628307-help-with-shadow-mapping-and-multi-texturing/

This topic is closed to new replies.

Advertisement