first i need to get the logics right...
First i do a normal render of the scene filling depth mask etc..., but with setting first glColorMask( FALSE, FALSE, FALSE, FALSE );
Now i set stencil rendering with no depth mask
glEnable( GL_STENCIL_TEST );
glDepthMask( FALSE );
glStencilFunc( GL_ALWAYS, 0, 0 );
Next i do a showdow render (needs some calculation with regard to the light position?):
glStencilOp( GL_KEEP, GL_KEEP, GL_INCR );
glCullFace( GL_BACK );
buildShadowVolume( 10, objlightpos, 0 ); //10 is mesh with id 10 objlight pos is the light position 0 is optional
So the buildshadowvolume needs to be done for each mesh that casts shadows....
This should have written a shadow in the stencil buffer, but it looks wrong
Now i reset rendering to normal:
glDepthMask( TRUE );
glDepthFunc( GL_LEQUAL );
glColorMask( TRUE, TRUE, TRUE, TRUE );
glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
Nexit i do a render of the scene for the shadows parts (lights of) glStencilFunc( GL_EQUAL, 1, 1 );
Next i do normal render (lights on) glStencilFunc( GL_EQUAL, 0, 1 );
The buildshadowvolume looks like this
Code: procedure buildShadowVolume( MeshID: Integer;lightposit: T3DPoint; ext: GlfLoat ); var i: integer; e0, e1: integer; vExtended: T3DPoint; v1: T3DPoint; begin glDisable( GL_LIGHTING ); glBegin( GL_TRIANGLES ); glColor3f( 0.2, 0.8, 0.4 ); //set shadow color // // For each vertex of the shadow casting object, find the edge // that it helps define and extrude a quad out from that edge. // for i:=0 to scene.Mesh[meshid].NumFaces-1 do begin // Define the edge we're currently working on extruding... e0 := i; e1 := i+1; // If the edge's second vertex is out of array range, // place it back at 0 if( e1 >= scene.Mesh[meshid].NumFaces-1 ) then e1 := 0; // v0 of our extruded quad will simply use the edge's first // vertex or e0. glVertex3f( scene.Mesh[meshid].Vertex[scene.Mesh[meshid].Face[e0]].x, scene.Mesh[meshid].Vertex[scene.Mesh[meshid].Face[e0]].y, scene.Mesh[meshid].Vertex[scene.Mesh[meshid].Face[e0]].z ); // v1 of our quad is created by taking the edge's first // vertex and extending it out by some amount. vExtended:=extendVertex( lightPosit, scene.Mesh[meshid].Vertex[scene.Mesh[meshid].Face[e0]], ext ); glVertex3f( vExtended.x, vExtended.y, vExtended.z ); // v2 of our quad is created by taking the edge's second // vertex and extending it out by some amount. vExtended:=extendVertex( lightPosit, scene.Mesh[meshid].Vertex[scene.Mesh[meshid].Face[e1]], ext ); glVertex3f( vExtended.x, vExtended.y, vExtended.z ); // v3 of our extruded quad will simply use the edge's second // vertex or e1. glVertex3f( scene.Mesh[meshid].Vertex[scene.Mesh[meshid].Face[e1]].x, scene.Mesh[meshid].Vertex[scene.Mesh[meshid].Face[e1]].x, scene.Mesh[meshid].Vertex[scene.Mesh[meshid].Face[e1]].x ); end; glEnd(); glEnable( GL_LIGHTING ); end;
the extendVertex looks like:
Code: function extendVertex( lightPosit, vert: T3DPoint; ext: GLFLoat ): T3DPoint; var lightdir: T3DPoint; begin // Create a vector that points from the light's position to the original vertex. lightDir.x := vert.x - lightPosit.x; lightDir.y := vert.y - lightPosit.y; lightDir.z := vert.z - lightPosit.z; // Then use that vector to extend the original vertex out to a new position. // The distance to extend or extrude the new vector is specified by t. result.x := lightPosit.x + lightDir.x * ext; result.y := lightPosit.y + lightDir.y * ext; result.z := lightPosit.z + lightDir.z * ext; end;
Obviously i am doing something wrong as all i get is wild bunch of triangles, but no shadow....
Any ideas on what i do wrong, most probably i am doing the calculations for rendering the shadows wrong.
PS: this is on of these things that look simple when you start.......
|