Blending & Avoid Texture Mixed with Previous Color in OpenGL (L4)

2015-11-13_160211

● Blending

本篇介紹如何將 texture 與其他顏色的混和或者將 texture 做程度上的透明化。實現 Blending 很簡單,只要在 initializeGL() 函式裡面加入以下四行程式碼就完成了。

glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);

glColor4f() 四個參數中,分別代表(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha),除了 RGB 外多了一個參數叫做 Alpha,Alpha 用意為表示透明度,1 表示完全不透明的,0 則表示完全透明;所以你可以選擇不同顏色及不同透明程度的 blending 數值。

glBlendFunc(GL_SRC_ALPHA, GL_ONE) 為設定 blending type 的函式。glBlendFunc (GLenum sfactor, GLenum dfactor) 裡面 sfactor 表示 source blending 的強度( source 為新加入的顏色或紋理),dfactor 則代表 destination 的強度(原本在 buffer 裡面的顏色或紋理),這兩個 factor 都在 [ 0, 1 ] 的範圍內。這裡的 GL_SRC_ALPHA 表示 source 本身 alpha 的強度, GL_ONE (dfactor) 為疊加上去的強度。如果今天不想要讓 texture 混和,就把 factor 設為 GL_ZERO。 

glEnable(GL_BLEND) 將 GL_BLEND 給啟動。

glDisable(GL_DEPTH_TEST) 為告訴 OpenGL 要繪製後面像素的內容,意思是說當啟動時,OpenGL 只會繪畫前面的圖形而已,躲在屏幕後方的像素是不會被繪製的,所以當調用 glEnable(GL_BLEND) 時,都要把 GL_DEPTH_TEST 給關閉,才會看到混和效果。

2015-11-13_160539[ 可以看到GL_DEPTH_TEST 開啟時,左後方與下方的資訊 OpenGL 是不做任何繪製的,只會看到前方所顯現的資訊而已。]

 


● Avoid Texture Mixed with Previous Color

在 texture mapping 後,如果有再繪製其他顏色的幾何圖形時,這時候會使 GL_BLEND 混和的顏色被蓋掉。以下為例,當我在立方體旁邊再繪製一個綠色的三角形時,GL_BLEND 所認定的顏色會變成綠色,如下圖。2015-11-13_163554

所以在 Blending 之前最好加上一行當初設定好的顏色與透明度( glColor4f(1.0f, 1.0f, 1.0f, 0.5f) ),就可以避免掉上述的問題了。

2015-11-13_164039

glPushMatrix();
glTranslatef(-10.0f, 0.0f, -50.f);
glRotatef(rota_x, 1.0f, 0.0f, 0.0f);
glRotatef(rota_y, 0.0f, 1.0f, 0.0f);
glRotatef(rota_z, 0.0f, 0.0f, 1.0f);
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture[2]);	

// Front Face
glBegin( GL_QUADS );
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-D, -D,  D);  // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( D, -D,  D);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( D,  D,  D);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-D,  D,  D);  // Top Left Of The Texture and Quad
glEnd();
//Back Face
glBegin( GL_QUADS );
glNormal3f( 0.0f, 0.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-D, -D, -D);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-D,  D, -D);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( D,  D, -D);  // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( D, -D, -D);  // Bottom Left Of The Texture and Quad
glEnd();
// Top Face
glBegin( GL_QUADS );
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-D,  D, -D);  // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-D,  D,  D);  // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( D,  D,  D);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( D,  D, -D);  // Top Right Of The Texture and Quad
glEnd();
// Bottom Face
glBegin( GL_QUADS );
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( D, -D,  D);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-D, -D,  D);  // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-D, -D, -D);  // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( D, -D, -D);  // Bottom Right Of The Texture and Quad
glEnd();
// Right face
glBegin( GL_QUADS );
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( D, -D, -D);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( D,  D, -D);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( D,  D,  D);  // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( D, -D,  D);  // Bottom Left Of The Texture and Quad
glEnd();
// Left Face
glBegin( GL_QUADS );
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-D, -D, -D);  // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-D, -D,  D);  // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-D,  D,  D);  // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-D,  D, -D);
glEnd();
glPopMatrix();

glPushMatrix();
glTranslatef(10.0f, 0.0f, -30.0f);
double T = 5.0;
glDisable(GL_TEXTURE_2D);
glColor3f(  0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex3f( 0, T, 0);
glVertex3f(-T,-T, 0);
glVertex3f( T,-T, 0);
glEnd();
glPopMatrix();

 

 


reference : http://nehe.gamedev.net/tutorial/blending/16001/

Leave a Reply

Your email address will not be published. Required fields are marked *