I want to draw the reflection of a motorbike on a water puddle, moreover I want to draw the shadow only when I'm not on this puddle. For this reasons I am trying to use the stencil test, following this example. However on my physical installation of Fedora 30 (opengl core profile version 4.5) it works, but it fails on VM with Ubuntu 19.10 (opengl core profile version 3.3). The relevant code is the one below
void Waterpuddle::DrawWaterpuddle(Motorbike mbike, float posx, float posy, float posz)
{
glPushMatrix();
glTranslatef(posx, posy, posz);
drawFloorPuddle(posy);
glPopMatrix();
/* Clear; default stencil clears to zero. */
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
/* Don't update color or depth. */
glDisable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
/* Draw 1 into the stencil buffer. */
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
glPushMatrix();
glTranslatef(posx, posy, posz);
puddleMesh.RenderNxV();
glPopMatrix();
/* Re-enable update of color and depth. */
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_DEPTH_TEST);
/* Now, only render where stencil is set to 1. */
glStencilFunc(GL_EQUAL, 1, 0xffffffff); /* draw if ==1 */
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glPushMatrix();
/* The critical reflection step: Reflect motorbike through the floor
(the Y=0 plane) to make a reflection. */
glScalef(1.0, -1.0, 1.0);
mbike.Render();
glPopMatrix();
glDisable(GL_STENCIL_TEST);
/* Draw "top" of floor. Use blending to blend in reflection. */
glPushMatrix();
glTranslatef(posx, posy, posz);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.372549, 0.623529, 0.623529, 0.3);
puddleMesh.RenderNxV();
glDisable(GL_BLEND);
glPopMatrix();
}
void Motorbike::Render() const{
glPushMatrix();
glTranslatef(px,py,pz);
glRotatef(facing, 0,1,0);
DrawHeadlight(0,0,-1, 0, useHeadlight); // accendi faro
RenderAllParts(true);
// shadow!
if(useShadow)
{
glPushMatrix();
glEnable(GL_STENCIL_TEST);
glShadowProjection(lightPosition, e, n);
glDisable(GL_LIGHTING);
glColor3f(0.2, 0.2, 0.2);
//do not draw shadow if I'm on the water puddle
glStencilFunc(GL_NOTEQUAL, 1, 0xffffffff); /* draw if !=1 */
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
RenderAllParts(false);
glEnable(GL_LIGHTING);
glDisable(GL_STENCIL_TEST);
glPopMatrix();
}
glPopMatrix();
glPopMatrix();
}
These images are the one where the stencil test succeeds (on Fedora 30):
These are instead the images obtained with the same code on my Ubuntu VM:
What did I do wrongly?
User contributions licensed under CC BY-SA 3.0