Problemi di fusione (OpenGL)
-
05-07-2019 - |
Domanda
A hai una domanda, forse qualcuno mi può aiutare. Sto cercando di creare un effetto specchio usando OpenGL. Traccio un piano trasparente, un "riflesso" scena ritagliata dallo stencil e originale. Ma ho un "muro" completamente non trasparente invece dello specchio. So che succede a causa del primo rendering del piano speculare (per ottenere un buffer di stencil). Ma non so cosa fare con questo :( Ecco il codice:
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();
}
Soluzione
Cosa succede se non si esegue l'ultima estrazione (ovvero se il problema persiste, rimuoverlo perché complica l'esempio)?
Una cosa chiara è che non sembri gestire nulla relativo al buffer Z.
Quando disegni il tuo primo quad per impostare lo stencil, supponendo che Z-write sia attivo, finisci per impostare i valori Z sul tuo specchio Z. Disegnare la scena che dovrebbe essere riflessa nello specchio sarà Z- respinto.
In qualche modo è necessario cancellare il buffer Z per quella regione dello schermo. Ovviamente, un Clear (DEPTH_BIT)
completo può funzionare ma dipende da ciò che hai già disegnato sullo schermo.
Allo stesso modo, non aggiornare lo Z-buffer durante l'aggiornamento dello stencil può funzionare a seconda che qualcosa sia stato disegnato lì prima.