Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
3 replies to this topic

#1 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

Posted 02 June 2013 - 01:12 PM

I'm following swiftless' tutorial on shadows: http://www.swiftless.com/tutorials/opengl/basic_shadows.html

 

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:

30jnpz4.jpg

 

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);
glLoadIdentity();
gluPerspective(60.0, (
 
float)windowWidth / (float)windowHeight, 0.4, 9000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
 
 
//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
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
 
//disable the color mask
glDepthMask(GL_FALSE); 
 
//disable the depth mask
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();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
 
//enable the color mask
glDepthMask(GL_TRUE); 
 
//enable the depth mask
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); 
 
//disable texturing of the shadow
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); 
 
//flip the shadow vertically(from tutorial)
glTranslatef(0,0,0); 
 
//translate the shadow onto our drawing plane(from tutorial)
glRotatef(angle,0,1,0); 
 
//rotate the shadow accordingly(from tutorial)
glColor4f(0.9,0.1,0.9,1); 
 
//color the shadow black(from tutorial)
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); 
 
//flip the shadow vertically(from tutorial)
glTranslatef(0,0,0); 
 
//translate the shadow onto our drawing plane(from tutorial)
glRotatef(angle,0,1,0); 
 
//rotate the shadow accordingly(from tutorial)
glColor4f(0.9,0.1,0.9,1); 
 
//color the shadow black(from tutorial)
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.


Sponsor:

#2 DishSoap   Members   -  Reputation: 353

Like
1Likes
Like

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
shadowMatrix(GLfloat shadowMat[4][4],GLfloat groundplane[4], GLfloat lightpos[4])
{
  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:

 

http://stackoverflow.com/questions/1114742/how-do-i-create-cheap-shadows-in-opengl


Edited by DishSoap, 02 June 2013 - 07:37 PM.


#3 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

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



#4 DishSoap   Members   -  Reputation: 353

Like
1Likes
Like

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.



PARTNERS