Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualShawn619

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();

#1Shawn619

Posted 02 June 2013 - 01:12 PM

I'm following swiftless's 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();

PARTNERS