用alpha渲染纹理问题

当我渲染纹理,然后绘制相同的图像时,它似乎会使一切变暗。 为了得到这个图像:

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);

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


我只是在猜测:

绘制第一个纹理会在RGB和alpha通道中给出204(0.8 * 255)。 当你第二次画画(启用GL_BLEND时,我假设),你会得到浅灰色的204 RGB和80%alpha,这会给你一个中等灰度。

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

链接地址: http://www.djcxy.com/p/5805.html

上一篇: Render to texture problem with alpha

下一篇: Animating a texture across a surface in OpenGL