使用alpha渲染纹理问题

时间:2009-08-18 18:26:32

标签: opengl-es alpha render-to-texture

当我渲染到纹理,然后绘制相同的图像时,它似乎使一切变暗。要获得此图像:

http://img24.imageshack.us/img24/8061/87993367.png

我将颜色为(1, 1, 1, .8)的左上角正方形渲染到纹理,然后渲染该纹理,再将中间正方形(相同颜色)渲染到另一个纹理,然后最终纹理加上右下角(相同的颜色)到屏幕。

正如您所看到的,每次渲染到纹理时,一切都会变得更暗。

我的渲染到纹理代码如下所示:(我在iPhone上使用OpenGL ES)

// gen framebuffer
GLuint framebuffer;
glGenFramebuffersOES(1, &framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);

// gen texture
GLuint texture;
glGenTextures(1, &texture);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);

// hook it up
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture, 0);
if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES))
   return false;

// set up drawing
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
glViewport(0, 0, Screen::Width, Screen::Height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Screen::Width, 0, Screen::Height, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4f(1, 1, 1, 1);

// do whatever drawing we'll do here
Draw();

glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);

我在这里做错了吗?你需要更多代码才能搞清楚吗?可能会发生什么?

1 个答案:

答案 0 :(得分:6)

我只是在猜测:

绘制第一个纹理可以在RGB和alpha通道中提供204(0.8 * 255)。当您第二次绘制时(启用GL_BLEND,我猜),您将获得浅灰色204 RGB和80%alpha,这将为您提供中灰色。

解决方案:使用glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)并预乘您的颜色。