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

如果你不做最后一次抽签会发生什么(即如果问题仍然存在,将其删除,因为它使示例复杂化)?

很清楚的一件事是,您似乎没有处理任何与 Z 缓冲区相关的内容。

当您绘制第一个四边形以设置模板时,假设 Z 写入已打开,您最终会将 Z 值设置为镜像 Z。绘制应该在镜像中反射的场景将被 Z 拒绝。

您需要以某种方式清除屏幕该区域的 Z 缓冲区。显然,完整的Clear(DEPTH_BIT)可以工作,但这取决于您已经在屏幕上绘制的内容。

同样,在更新模板时不更新 Z 缓冲区可能会起作用,具体取决于之前是否在此处绘制过任何内容。

于 2009-11-27T08:19:11.237 回答