2
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You also need to reflect the lighting environment (i.e. light positions and directions) for the lighting on the reflection to be correct.

It's useful to think about the reflection as being applied to the camera, i.e. as part of the view transformation, rather than as part of the model transformation. That way, the logic you already have to ensure lights remain fixed in world space as you move the camera around will also make them do the right thing when the camera gets reflected.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .