0

質問があります、多分誰かが私を助けることができます。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();
}
4

1 に答える 1

1

最後の描画を行わないとどうなりますか(つまり、問題がまだ残っている場合は、例が複雑になるため、問題を削除してください)。

明らかなことの1つは、Zバッファに関連するものを処理していないように見えることです。

最初のクワッドを描画してステンシルを設定するとき、Z書き込みがオンになっていると仮定すると、Z値をミラーZに設定することになります。ミラーに反射するはずのシーンの描画はZ拒否されます。

どういうわけか、画面のその領域のZバッファをクリアする必要があります。明らかに、フルClear(DEPTH_BIT)は機能しますが、それはあなたがすでにあなたのスクリーンに描いたものに依存します。

同様に、ステンシルを更新するときにZバッファを更新しないことは、以前にそこに何かが描画されたかどうかによっては機能します。

于 2009-11-27T08:19:11.237 に答える