【问题标题】:Blending problems (OpenGL)混合问题(OpenGL)
【发布时间】:2009-11-26 22:42:04
【问题描述】:

有一个问题,也许有人可以帮助我。 我正在尝试使用 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();
}

【问题讨论】:

    标签: opengl alphablending


    【解决方案1】:

    如果您最后一次抽签没有进行,会发生什么情况(即如果问题仍然存在,请将其删除,因为这会使示例复杂化)?

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

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

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

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

    【讨论】:

    • 巴巴尔,谢谢! glClear(GL_DEPTH_BUFFER_BIT) 工作:)) 我删除了最后一个抽奖,它真的没有意义:))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多