Pregunta

Tengo una pregunta, tal vez alguien me pueda ayudar. Estoy tratando de hacer un efecto de espejo utilizando OpenGL. Dibujo un plano transparente, un " reflejado " Escena cortada a la plantilla, y una original. Pero tengo un " muro " En lugar del espejo. Sé que sucede debido a la primera representación del plano espejo (para obtener un búfer de plantilla). Pero no sé qué hacer con esto :( Aquí está el código:

  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();
}
¿Fue útil?

Solución

¿Qué sucede si no haces el último sorteo (es decir, si el problema sigue ahí, elimínalo ya que complica el ejemplo)?

Una cosa que está clara es que parece que no manejas nada relacionado con Z-buffer.

Cuando dibujes tu primer cuadrángulo para configurar la plantilla, suponiendo que Z-write esté activado, terminarás configurando los valores Z en tu espejo Z. Dibujar la escena que se supone que se refleja en el espejo será Z- rechazado.

Necesitas borrar el búfer Z de esa región de la pantalla de alguna manera Obviamente, un Clear (DEPTH_BIT) completo puede funcionar, pero depende de lo que ya hayas dibujado en tu pantalla.

Del mismo modo, no actualizar el Z-buffer al actualizar la plantilla puede funcionar dependiendo de si se ha dibujado algo allí anteriormente.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top