Jes*_*der 4 alpha opengl-es 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);
Run Code Online (Sandbox Code Playgroud)
有什么我在这里做错了吗?你需要更多代码才能搞清楚吗?这可能会发生什么?
我只猜测:
绘制第一个纹理可以在RGB和alpha通道中提供204(0.8*255).当您第二次绘制时(启用了GL_BLEND,我推测),您将获得具有80%alpha的浅灰色204 RGB,从而为您提供中灰色.
解决方案:使用glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)并预乘你的颜色.