質問
質問があります。誰かが助けてくれるかもしれません。 OpenGLを使用してミラー効果を作成しようとしています。 「反射」した透明な平面を描きます。ステンシルでカットされたシーンと、オリジナルのシーン。 しかし、私は完全に不透明な「壁」を持っています。ミラーの代わりに。最初のミラープレーンレンダリング(ステンシルバッファーを取得するため)が原因で発生することを知っています。しかし、私はこれで何をすべきかわかりません:( コードは次のとおりです。
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つは、Zバッファーに関連するものを一切処理していないように見えることです。
最初のクワッドを描画してステンシルを設定すると、Z書き込みがオンになっていると仮定して、ミラーZにZ値を設定することになります。拒否されました。
画面のその領域のZバッファを何らかの方法でクリアする必要があります。明らかに、完全な Clear(DEPTH_BIT)
は機能しますが、画面に既に描画したものに依存します。
同様に、ステンシルを更新するときにZバッファを更新しないことは、以前に何かが描画されているかどうかに応じて機能します。
所属していません StackOverflow