OpenGL 3.0帧缓冲输出到附件而无需我指定吗? [英] OpenGL 3.0 Framebuffer outputting to attachments without me specifying?

查看:79
本文介绍了OpenGL 3.0帧缓冲输出到附件而无需我指定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我有一个带有一堆附件的帧缓冲区.附件是颜色",绽放",速度"和深度".

Ok so I have a framebuffer with a bunch of attachments attached. The attachments are Color, Bloom, Velocity and Depth.

我首先使用以下代码将帧缓冲区清除为自己选择的值.

I start by clearing the framebuffer to the values of my choosing with the following code.

// Clear Color Buffer
float colorDefaultValue[4] = { 0.5, 0.5, 0.5, 1.0 };
glClearBufferfv(GL_COLOR, 0, colorDefaultValue);

// Clear Bloom Buffer
float bloomDefaultValue[4] = { 0.0, 0.0, 1.0, 1.0 };
glClearBufferfv(GL_COLOR, 1, bloomDefaultValue);

// Clear Depth Buffer
float depth[1] = { 1.0 };
glClearBufferfv(GL_DEPTH, 0, depth);

然后,我继续使用主着色器渲染场景.如下面的代码所示,Ive指定了输出.

Then I proceed to render the scene using my main shader. As you can see in the code below, Ive specified the outputs.

// Layouts
layout (location = 0) out vec4 o_vFragColor;
layout (location = 1) out vec4 o_vBloomColor; 

然后我将片段着色器中的值输出给它们.

And then i output values in the fragment shader to them.

o_vFragColor = vec4(color, alpha);
float brightness = dot(o_vFragColor.rgb, vec3(0.2126, 0.2126, 0.2126)); 
if(brightness > 1.0)
    o_vBloomColor = vec4(o_vFragColor.rgb, 1.0);

现在,我的问题是:如果一个片段不够亮,为什么它会向bloom附件输出黑色?我尚未指定要输出任何内容,但无论如何它都会对其进行调整.

Now, my question is: If a fragment is not bright enough, why does it output black to the bloom attachment? I haven't specified for it to output anything yet it adjusts it anyway.

例如,如果我将Bloom缓冲区清除为绿色

For example, if I clear the bloom buffer to green

// Clear Bloom Buffer
float bloomDefaultValue[4] = { 0.0, 1.0, 0.0, 1.0 };
glClearBufferfv(GL_COLOR, 1, bloomDefaultValue);

,并且我不向片段着色器中的bloom附件输出任何值,在bloom缓冲区中出现黑色.您可以在下图中看到它.

and I dont output any value to the bloom attachment in the fragment shader, I get a black in the bloom buffer. You can see it it the following image.

BloomBuffer

图像的缓冲区为绿色的部分是未绘制几何图形的部分.黑色部分是图像中包含几何图形的部分,该几何图形不够明亮,因此不应向bloom缓冲区输出任何内容,但显然它们确实输出了某些内容,在这种情况下为黑色.紫色部分是没有亮度阈值且可以正常工作的碎片.

The parts of the image where the buffer is green is where no geometry was drawn. The black parts are parts of the image that contained geometry that was not bright enough and therefore should have not outputted anything to the bloom buffer, yet they clearly did output something, black in this case. Purple parts are fragments that are beyind the brightness threshold and working as intended.

黑色呢?

推荐答案

您不能有条件地从片段着色器中写入颜色缓冲区.

You cannot write to a color buffer conditionally from within a fragment shader.

输出片段始终具有每个活动颜色缓冲区的值,即使您没有写.即使您没有为该输出值声明变量,也是如此.如果没有值写入特定的片段输出位置,则使用的值将是不确定的.但这会是某事.

An output fragment always has values for each active color buffer, even if you didn't write them. This is true even if you don't declare a variable for that output value. If no value gets written to a particular fragment output location, then the value used will be undefined. But it will be something.

您可以根据颜色遮罩进行条件书写.但这是按绘制调用的事情,而不是片段着色器中可以完成的事情.您可以在该颜色缓冲区中进行混合,使用0的alpha表示不"t write",并以1的alpha值替换其中的内容.

You can do conditional writing based on color masking. But that is a per-draw-call thing, not something that can be done in the fragment shader. You could employ blending in that color buffer, using an alpha of 0 to mean "don't write", and an alpha of 1 to replace what's there.

这篇关于OpenGL 3.0帧缓冲输出到附件而无需我指定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆