OpenGL:渲染到 FBO 时是否支持纹理组合器函数?

发布于 2025-01-04 09:31:38 字数 1727 浏览 1 评论 0原文

我有一个渲染到第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2025-01-11 09:31:38

为什么你不使用着色器?
我认为编写代码会更容易

您正在使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文