Correcting Lighting in Stencil Reflections

Posted by Reanimation on Game Development See other posts from Game Development or by Reanimation
Published on 2014-04-02T03:00:53Z Indexed on 2014/06/01 9:43 UTC
Read the original article Hit count: 254

Filed under:
|
|
|
|

I'm just playing around with OpenGL seeing how different methods of making shadows and reflections work.

I've been following this tutorial which describes using GLUT_STENCIL's and MASK's to create a reasonable interpretation of a reflection.

Following that and a bit of tweaking to get things to work, I've come up with the code below.

Unfortunately, the lighting isn't correct when the reflection is created.

glPushMatrix();
plane();                                                //draw plane that reflection appears on

    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    glDepthMask(GL_FALSE);
    glEnable(GL_STENCIL_TEST);
    glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

plane();                                                //draw plane that acts as clipping area for reflection

    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    glDepthMask(GL_TRUE);
    glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

    glDisable(GL_DEPTH_TEST);
    glPushMatrix();
    glScalef(1.0f, -1.0f, 1.0f);
    glTranslatef(0,2,0); 
    glRotatef(180,0,1,0);

sphere(radius, spherePos);                              //draw object that you want to have a reflection

    glPopMatrix();
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_STENCIL_TEST);

sphere(radius, spherePos);                              //draw object that creates reflection
glPopMatrix();

It looked really cool to start with, then I noticed that the light in the reflection isn't correct.

first reflection second reflection

I'm not sure that it's even a simple fix because effectively the reflection is also a sphere but I thought I'd ask here none-the-less.

I've tried various rotations (seen above the first time the sphere is drawn) but it doesn't seem to work. I figure it needs to rotate around the Y and Z axis but that's not correct.

Have I implemented the rotation wrong or is there a way to correct the lighting?

© Game Development or respective owner

Related posts about opengl

Related posts about c++