OpenGL:渲染到 FBO 时是否支持纹理组合器函数?
我有一个渲染到第一个 FBO 的 OpenGL 纹理,我需要编辑其亮度,渲染到第二个 FBO,然后将其绘制在屏幕上。
如果我将纹理渲染到第二个 FBO(不修改其亮度),然后将其绘制在屏幕上,它会正确显示。
如果我尝试编辑渲染纹理的亮度(使用像 glTexEnv
这样的纹理组合器函数),我会得到错误的输出,例如全黑或全白纹理、改变对比度等。
如果我在绘制时应用亮度调节屏幕而不是 FBO,效果很好......亮度按预期修改。
所以我的问题是:当我渲染到 FBO 时,我可以使用像 glTexEnv
这样的纹理组合器函数吗?
这是我在绘制到 FBO 时用来修改亮度的代码(它不起作用):
// "secondFBO" is the FBO I'm intended to render to after brightness regulation.
// "textureId" is the OpenGL texture attached to the first FBO that contains
// the unmodified texture.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, secondFBO);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId);
// apply alpha filter
double t = brightnessValue;
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
if (t > 1.0f)
{
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);
glColor4f(t-1, t-1, t-1, t-1);
}
else
{
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT);
glColor4f(1-t, 1-t, 1-t, 1-t);
}
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);
// ... draw texture ...
glDisable(GL_TEXTURE_RECTANGLE_ARB);
// restore previous state
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
// draw on screen
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
// "textureId2" is the texture attached to the second FBO
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId2);
// ... draw texture ...
glDisable(GL_TEXTURE_RECTANGLE_ARB);
I have an OpenGL texture rendered to a first FBO and I need to edit its brightness, render to a second FBO and then draw it on screen.
If I render the texture to the second FBO (without modifying its brightness) and then draw it on screen, it shows up correctly.
If I try to edit brightness of rendered texture (using texture combiner functions like glTexEnv
) I get wrong output like all black or all white texture, altered contrast, ecc..
If I apply brightness regulation while drawing on screen instead of the FBO, it works great..brightness is modified as expected.
So my question is: can I use texture combiner functions like glTexEnv
when I render to FBO?
This is the code which I use to modify brightness while drawing to the FBO (it doesn't work):
// "secondFBO" is the FBO I'm intended to render to after brightness regulation.
// "textureId" is the OpenGL texture attached to the first FBO that contains
// the unmodified texture.
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, secondFBO);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId);
// apply alpha filter
double t = brightnessValue;
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
if (t > 1.0f)
{
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);
glColor4f(t-1, t-1, t-1, t-1);
}
else
{
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT);
glColor4f(1-t, 1-t, 1-t, 1-t);
}
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE);
// ... draw texture ...
glDisable(GL_TEXTURE_RECTANGLE_ARB);
// restore previous state
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
// draw on screen
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
// "textureId2" is the texture attached to the second FBO
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textureId2);
// ... draw texture ...
glDisable(GL_TEXTURE_RECTANGLE_ARB);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么你不使用着色器?
我认为编写代码会更容易
您正在使用 FBO,这意味着您的硬件支持着色器 - 至少 GLSL 1.2
Why you are not using shaders?
I think it would be much easier to write the code
You are using FBO and that means that Shaders - at least GLSL 1.2 are supported by your hardware