problemas de mistura (OpenGL)
-
05-07-2019 - |
Pergunta
A tem uma pergunta, talvez alguém possa me ajudar. Eu estou tentando fazer um efeito de espelho usando OpenGL. Eu desenhar um avião transparente, um "refletida" corte de cena por stencil, e um original. Mas eu tenho uma "parede" completamente não-transparente em vez do espelho. Eu sei que isso acontece por causa da primeira prestação plano do espelho (para obter um stencil buffer). Mas eu não sei o que fazer com este :( Aqui está o 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();
}
Solução
O que acontece se você não fizer esse último sorteio (ou seja, se o problema ainda está lá, removê-lo, uma vez que complica o exemplo)?
Uma coisa que é claro é que você não parecem lidar com qualquer coisa Z-tampão relacionados.
Quando você desenha o seu primeiro quad para definir o stencil, assumindo Z-gravação está ligada, você acaba definindo os valores de Z para o seu Z. espelho Desenhando a cena que é suposto ser refletida no espelho será Z- rejeitada.
Você precisa limpar o buffer Z para aquela região da tela de alguma forma. Obviamente, um Clear(DEPTH_BIT)
completo pode funcionar, mas isso depende do que você já tenha desenhado em sua tela.
Da mesma forma, não atualizar o Z-tampão ao atualizar o estêncil pode funcionar, dependendo se alguma coisa foi tirada lá antes.