2009-11-26 106 views
0

有一个问题,也许有人可以帮助我。 我想使用OpenGL制作镜面效果。我画了一个透明的飞机,一个由模具切割的“反射”场景,以及一个原始的飞机。 但我有一个完全不透明的“墙”而不是镜子。我知道这是因为第一次镜像平面渲染(获取模板缓冲区)。但我不知道该怎么做这个:( 下面是代码:混合问题(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拒绝。

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

同样,更新模板时不更新Z缓冲区可以工作取决于之前是否有任何东西被绘制。

+0

Bahbar,谢谢! glClear(GL_DEPTH_BUFFER_BIT)工作:))我删除了最后一个画,它真的没有意义:)) – Irene 2009-11-27 14:55:54