Original Post
I have some problems. I've followed the code from NeHe thingo (article) 26, and I've found that I get reflectivness only if I don't draw the surface that is supposed to be reflected off. So, I'm drawing a cube and the reflection should be shown in a section of floor. I can only see the reflection if I don't draw the floor. It is still drawn within the boundries of where the floor would be (when it's drawn into the stencil buffer). It's also kind of inside out, which I don't know if it's supposed to happen, but I think we should focus on being able to see the reflection before seeing if it's the right one. Okay, the call to "driver->render()" renders the scene graph, which at the moment only contains the cube. I have a fair feeling that these two lines of code are the problem:
// the texture of the floor
echo::video::ITexture* floor_tex = driver->createTexture("envwall.bmp");
// while the window has not received a WM_QUIT message
while( engine->run() ){
driver->beginScene();
// reset the world/modelview matrix to the identity matrix
driver->resetMatrix(echo::video::WORLD_MATRIX);
// my transformations
node->addRotationX(xrot);
node->addRotationY(yrot);
// set translation
node->setTranslation( echo::util::CVector3d(xtrans, ytrans, ztrans) );
// TEST - reflections
// plane equation for clipping
double plane[4] = {0.0f, -1.0f, 0.0f, 0.0f};
glLoadIdentity();
glTranslatef(0.0f, -0.5f, -5.0f);
glColorMask(0, 0, 0, 0);
// enable the stencil testing for the floor
glEnable(GL_STENCIL_TEST);
// set the stencil buffer so the test always passes, has a 1-bit plane, 1 as mask
// Keep if the test fails, keep if the test passes but the buffer test fails
// Replace if test passes
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// disable depth testing
glDisable(GL_DEPTH_TEST);
// apply floor texture
floor_tex->apply();
// draw the floor (draw it to the stencil buffer)
// we only want to mark it in the stencil buffer
DrawFloor();
// So, now we want to draw the cube, but only want the pixels to show up where the stencil buffer
// has the value of 1 (where the floor is)
/////// Draw the reflection ///////
glEnable(GL_DEPTH_TEST); // enable depth testing
glColorMask(1, 1, 1, 1);
glStencilFunc(GL_EQUAL, 1, 1); // we draw only where the stencil is 1 (where the floor was drawn)
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); // don't change the stencil buffer
glEnable(GL_CLIP_PLANE0); // enable the clipping plane
glClipPlane(GL_CLIP_PLANE0, plane); // equation for reflected objects
driver->pushMatrix();
// mirror the Y axis
glScalef(1.0f, -1.0f, 1.0f);
// draw the things to be reflected
driver->render();
driver->popMatrix();
glDisable(GL_CLIP_PLANE0); // disable the clip plane for drawing the floor
glDisable(GL_STENCIL_TEST); // disable the stencil testing
glEnable(GL_BLEND); // enable the blending so the reflected object will show
glDisable(GL_LIGHTING); // disable the lighting because we're using blending
glColor4f(1.0f, 1.0f, 1.0f, 0.8f); // set the color to white with 80% alpha
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// apply floor texture
floor_tex->apply();
DrawFloor();
//// Drawing the cube ////
glEnable(GL_LIGHTING);
glDisable(GL_BLEND);
// draw the cube
driver->render();
// TEST - reflections
// draw the console and the static messages to the screen
engine->drawConsole();
driver->endScene(); // actually renders the window
}
glColor4f(1.0f, 1.0f, 1.0f, 0.8f); // set the color to white with 80% alpha
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Because as far as I can see they define the blending for the floor. Am I doing something wrong? Have I left something out, as far as you guys can see?