2009-11-26 107 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