Question

Vous avez une question, peut-être que quelqu'un peut m'aider. J'essaie de créer un effet miroir en utilisant OpenGL. Je dessine un plan transparent, un "réfléchi" scène coupée au pochoir, et une originale. Mais j'ai un "mur" totalement non transparent. au lieu du miroir. Je sais que cela se produit à cause du premier rendu du plan miroir (pour obtenir un tampon de stencil). Mais je ne sais pas quoi faire avec ça :( Voici le 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();
}
Était-ce utile?

La solution

Que se passe-t-il si vous ne faites pas ce dernier tirage (c'est-à-dire si le problème persiste, supprimez-le, car cela complique l'exemple)?

Une chose est claire, c'est que vous ne semblez pas gérer quoi que ce soit en rapport avec le tampon Z.

Lorsque vous dessinez votre premier quad pour définir le gabarit, en supposant que l'écriture Z est activée, vous définissez les valeurs Z sur votre miroir Z. Le dessin de la scène censée être reflétée dans le miroir sera Z- rejeté.

Vous devez effacer le tampon Z pour cette région de l'écran d'une manière ou d'une autre. Évidemment, un Clear (DEPTH_BIT) complet peut fonctionner, mais cela dépend de ce que vous avez déjà dessiné sur votre écran.

De même, ne pas mettre à jour le tampon Z lors de la mise à jour du gabarit peut fonctionner si quelque chose a été dessiné auparavant.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top