views:

133

answers:

1

Hi! A have a question, maybe someone can help me. I am trying to make a mirror effect using OpenGL. I draw a transparent plane, a "reflected" scene cut by stencil, and an original one. But I have a completely non-transparent "wall" instead of the mirror. I know it happens because of the first mirror plane rendering (to get a stencil buffer). But I don't know what to do with this:( Here is the code:

  void CMirror::draw(CSceneObject * curscene)
{
    glPushMatrix();
    glClearStencil(0.0f);

    glClear(GL_STENCIL_BUFFER_BIT);
    //Draw into the stencil buffer
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    glStencilFunc(GL_ALWAYS, 1, 0);
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
    glEnable(GL_STENCIL_TEST);
    glPushMatrix();
    glTranslatef(this->coords[0], this->coords[1], this->coords[2]);
    glScalef(this->size, this->size, this->size);
    glColor4f(1, 0, 1, 0);
    glBegin(GL_QUADS);
     glVertex3f(0.0f, this->height / 2.0f, this->width / 2.0f);
     glVertex3f(0.0f, this->height / -2.0f, this->width / 2.0f);
     glVertex3f(0.0f, this->height / -2.0f, this->width / -2.0f);
     glVertex3f(0.0f, this->height / 2.0f, this->width / -2.0f);
    glEnd();
    glPopMatrix();
    glDisable(GL_STENCIL_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);
    //glClear(GL_COLOR_BUFFER_BIT);
    //Draw the scene
    glEnable(GL_STENCIL_TEST); 
    glStencilFunc(GL_EQUAL, 1, 255);
    glPushMatrix();
    glTranslatef( 2*this->coords[0], 2*this->coords[1], 2*this->coords[2]);
    glScalef(-1.0f, 1.0f, 1.0f);
    ((CScene*)curscene)->draw();
    glColor4f(0.0f, 0.30f, 0, 0.9); 
    ((CScene*)curscene)->spline->draw();
    ((CScene*)curscene)->morph->draw();


    glPopMatrix();
    glDisable(GL_STENCIL_TEST);
    //the mirror itself:
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);
    glPushMatrix();
    glTranslatef(this->coords[0], this->coords[1], this->coords[2]);
    glScalef(this->size, this->size, this->size);
    glColor4f(0, 0, 0, 0.9);
    glBegin(GL_QUADS);
     glVertex3f(0.0f, this->height / 2.0f, this->width / 2.0f);
     glVertex3f(0.0f, this->height / -2.0f, this->width / 2.0f);
     glVertex3f(0.0f, this->height / -2.0f, this->width / -2.0f);
     glVertex3f(0.0f, this->height / 2.0f, this->width / -2.0f);
    glEnd();
    glPopMatrix();
    glDisable(GL_BLEND);

    glPopMatrix();
}
+1  A: 

What happens if you don't do that last draw (i.e. if the problem is still there, remove it as it complicates the example)?

One thing that's clear is that you don't seem to handle anything Z-buffer related.

When you draw your first quad to set the stencil, assuming Z-write is on, you end up setting the Z-values to your mirror Z. Drawing the scene that is supposed to be reflected in the mirror will be Z-rejected.

You need to clear the Z buffer for that region of the screen somehow. Obviously, a full Clear(DEPTH_BIT) can work but it depends on what you've already drawn on your screen.

Likewise, not updating the Z-buffer when updating the stencil can work depending on whether anything has been drawn there before.

Bahbar
Bahbar,thank you! glClear(GL_DEPTH_BUFFER_BIT) worked:)) And I removed the last draw, it really had no sense:))
Irene