Sign in to follow this  
Shawn619

OpenGL Position shadow based of lighting

Recommended Posts

Shawn619    342

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

Share this post


Link to post
Share on other sites
DishSoap    698

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

Share this post


Link to post
Share on other sites
Shawn619    342

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 this post


Link to post
Share on other sites
DishSoap    698

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now