2012-02-13 161 views
0

我想使用OpenGL移動並旋轉透明的BGRA圖像(文字覆蓋圖)。這裏是代碼片段我用:OpenGL:爲什麼複製紋理時alpha通道設置爲255?

glViewport(0, 0, iWidth, iHeight); // Reset The Current Viewport 
glEnable(GL_TEXTURE_2D); 

glGenTextures(1, &arTex[0].iName); 
glBindTexture(GL_TEXTURE_2D, arTex[0].iName); 

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 

glTexImage2D(GL_TEXTURE_2D, 0, 4, imf.iWidth, imf.iHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, ib.GetData()); 

glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Note: Transparent alpha value 
glClear(GL_COLOR_BUFFER_BIT); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 

glOrtho(-dAspectCanvas/2.0, dAspectCanvas/2.0, -0.5, 0.5, -1.0, 1.0); // Note: Using (-1.0; 1.0) for Z-planes 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 
glScaled(Motion.Scale.GetValue(dPosition), Motion.Scale.GetValue(dPosition), 1.0); 
glTranslated(Motion.OffsetX.GetValue(dPosition) * dAspectCanvas, Motion.OffsetY.GetValue(dPosition), 0.0); 
glRotated(Motion.Rotation.GetValue(dPosition), 0.0, 0.0, 1.0); 
glTranslated(Motion.PositionX.GetValue(dPosition) * dAspectCanvas, Motion.PositionY.GetValue(dPosition), 1.0); 

// Rotating and moving 

glBindTexture(GL_TEXTURE_2D, arTex[iTexIndex].iName); // has to be called outside glBegin/glEnd scope 

glBegin(GL_TRIANGLE_STRIP); // "Many OpenGL applications avoid quads altogether because of their inherent rasterization problems" 

glTexCoord2d(rcTextureIntersection.left, rcTextureIntersection.top); 
glVertex2d(rcVertexIntersection.left, rcVertexIntersection.top); 
glTexCoord2d(rcTextureIntersection.right, rcTextureIntersection.top); 
glVertex2d(rcVertexIntersection.right, rcVertexIntersection.top); 
glTexCoord2d(rcTextureIntersection.left, rcTextureIntersection.bottom); 
glVertex2d(rcVertexIntersection.left, rcVertexIntersection.bottom); 
glTexCoord2d(rcTextureIntersection.right, rcTextureIntersection.bottom); 
glVertex2d(rcVertexIntersection.right, rcVertexIntersection.bottom); 
glEnd(); 

glDisable(GL_TEXTURE_2D); 
glFlush(); 
glReadBuffer(GL_BACK); 
glReadPixels(0, 0, imf.iWidth, imf.iHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, ibOut.GetData()); 

但無論我嘗試在glTexImage2D,我透明的黑色圖像變成一個完全不透明的黑色圖像。輸入BGRA圖像包含字節:0,0,0,0,0 ....在旋轉之後,輸出圖像包含0,0,0,255,0,0,0,255,0 .... A始終設置爲255,我不明白爲什麼。

+0

「旋轉後,輸出圖像包含0,0,0,0,0,0,0,0,0,05,0 ....」當您創建OpenGL上下文時,您是否要求幀緩存具有alpha在裏面? – 2012-02-13 22:53:17

回答

0

阿爾法並不意味着「透明」。阿爾法本身並不意味着任何東西。只有當你使用這個值來做某件事時,它纔會有意義。

如果要呈現對象並使用alpha控制對象的透明度,則需要激活混合和set up a proper blend mode

+0

我可以使用這些行開始混合: glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); – Kirill 2012-02-13 23:00:15

+0

@ user1207865:嗯,我希望你能閱讀那個頁面,並理解這些行實際上做了什麼。 – 2012-02-13 23:01:32

0

您在這裏面臨兩個問題。一個是,你沒有啓用混合,其效果是紋理的alpha值對透明度沒有影響。

另一個問題 - 對你來說更重要的是 - 你的framebuffer(你正在繪製紋理的地方)可能沒有alpha通道,因此會默認爲最大值。您不需要啓用混合來使紋理影響幀緩衝區alpha值(它只會替換它)。但是你需要一個帶alpha通道的幀緩衝來完成這項工作。請注意,在屏幕上的幀緩衝區獲取alpha緩衝區是可能的(我甚至推薦它)。但是對於圖像處理來說,幀緩衝僅僅是錯誤的地方。您需要一個安全的環境:A Framebuffer Object

0

沒有必要混合任何東西,但幀緩衝區需要使用阿爾法位設置。這是一個問題,我完全忘了wglCreateContext。

感謝您的幫助!

0

可能使用正確的alpha顏色設置glClearColor。

相關問題