• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Position shadow based of lighting

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Shawn619  Members

Posted 02 June 2013 - 01:12 PM

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, 02 June 2013 - 01:12 PM.

### #2DishSoap  Members

Posted 02 June 2013 - 07:35 PM

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, 02 June 2013 - 07:37 PM.

### #3Shawn619  Members

Posted 03 June 2013 - 01:28 PM

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."

### #4DishSoap  Members

Posted 07 June 2013 - 08:28 AM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.