# OpenGL Position shadow based of lighting

This topic is 1934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

However, the tutorial is basic and doesn't include how to position the stenciled shadow correctly on the desired surface based on the lights' position.

This is the swiftless tutorial implementation:

And this is the code:

void display(){
//draw scene
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClearStencil(0);
glViewport(0, 0, windowWidth, windowHeight);
glClearColor (0.7,0.7,0.7,1);
glMatrixMode(GL_PROJECTION);
gluPerspective(60.0, (

float)windowWidth / (float)windowHeight, 0.4, 9000.0);
glMatrixMode(GL_MODELVIEW);

//set camera
gluLookAt(cam.posX,cam.posY,cam.posZ,cam.viewX,cam.viewY,cam.viewZ,cam.upX,cam.upY,cam.upZ);

//set light
glPushMatrix();
glTranslatef(cam.posX,cam.posY,cam.posZ);
GLfloat positionalLightPos[] = {0.0f, 0.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, positionalLightPos);
glPopMatrix();
?

//start

glEnable(GL_STENCIL_TEST);

//enable the stencil testing
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

//set the stencil buffer to replace our next lot of data
?

drawSceneGrid();

glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

//set the stencil buffer to keep our next lot of data
glDisable(GL_TEXTURE_2D);

glDisable(GL_DEPTH_TEST);

//disable depth testing of the shadow
glPushMatrix();

/*shadow coords that i want to be based on light[0]*/
glScalef(1.0f, -1.0f, 1.0f);

glTranslatef(0,0,0);

//translate the shadow onto our drawing plane(from tutorial)
glRotatef(angle,0,1,0);

glColor4f(0.9,0.1,0.9,1);

square();

//draw our square as the shadow(from tutorial)
glColor3f(1.0f,1.0f,1.0f);
glPopMatrix();
glEnable(GL_DEPTH_TEST);

//enable depth testing
glEnable(GL_TEXTURE_2D);

//enable texturing
glDisable(GL_STENCIL_TEST);

//disable the stencil testing

//end
?
glEnable(GL_BLEND);

//enable alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

//set the alpha blending

drawSceneGrid();
glDisable(GL_BLEND);

//disable alpha blending
?
glPushMatrix();
glRotatef(angle,0,1,0);

//rotate the square
square();

//draw the square
glPopMatrix();
?
glutSwapBuffers();
}


My question is how do I find the right translation for the shadow onto the shadowed surface? The tutorial code simply vertically flips the square to show users the basics:

glPushMatrix();

/*shadow coords that i want to be based on light[0]*/
glScalef(1.0f, -1.0f, 1.0f);

glTranslatef(0,0,0);

//translate the shadow onto our drawing plane(from tutorial)
glRotatef(angle,0,1,0);

glColor4f(0.9,0.1,0.9,1);

square();

//draw our square as the shadow(from tutorial)
glColor3f(1.0f,1.0f,1.0f);
glPopMatrix();

Edited by Shawn619

##### Share on other sites

So I researched this a little bit. Looks like you need the plane that represents the ground and a vector representing the light position and run it through this function:

/* Create a matrix that will project the desired shadow. */
void
{
GLfloat dot;

/* Find dot product between light position vector and ground plane normal. */
dot = groundplane[X] * lightpos[X] +
groundplane[Y] * lightpos[Y] +
groundplane[Z] * lightpos[Z] +
groundplane[W] * lightpos[W];

shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];

shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];

shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];

shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
shadowMat[3][3] = dot - lightpos[W] * groundplane[W];

}


Push your matrix then multiply by the shadowMat and try drawing. Let me know how that works.

I found this info from:

Edited by DishSoap

##### Share on other sites

I haven't attempted this, but nonetheless I forgot to mention that I need it to work for all surfaces, not just flat.

However I did enjoy reading this little tid-bit of information in your link that got me thinking of ways to implement shadow mapping:

"If I were trying to accomplish this, I would be tempted to use ray casting. For each triangle in B, create a vector from the triangle to the light. If it hits anything along the way, it is in shadow. This would be kind of slow unless you were using a decent acceleration structure and a fast triangle hit test. I like bounding volume hierarchies; many programs use them for collision detection as well."

##### Share on other sites

You could perhaps treat the light as a seperate eye from the viewer. Give it a viewing frustum that represents the area lit by that point light.

Detect which triangles are not in view within the frustum of the light and which are in view of the viewer, cast those in shadow.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 10
• 21
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007464
• ### Who's Online (See full list)

There are no registered users currently online

×