如何获取片段的当前颜色? [英] How do I get the current color of a fragment?

查看:134
本文介绍了如何获取片段的当前颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用GLSL中的着色器来处理,我找到了一些有用的资源和教程,但是我不断碰壁,这本应该是基础和琐碎的:片段着色器如何检索当前片段的颜色?

I'm trying to wrap my head around shaders in GLSL, and I've found some useful resources and tutorials, but I keep running into a wall for something that ought to be fundamental and trivial: how does my fragment shader retrieve the color of the current fragment?

您可以说gl_FragColor = whatever来设置最终颜色,但是显然这是仅输出值.如何获得输入的原始颜色,以便可以对其进行计算?那一定是在某个地方的变量中,但是如果那里有人知道它的名字,那么他们似乎还没有将其记录在我到目前为止所见过的任何教程或文档中,这使我无所适从. /p>

You set the final color by saying gl_FragColor = whatever, but apparently that's an output-only value. How do you get the original color of the input so you can perform calculations on it? That's got to be in a variable somewhere, but if anyone out there knows its name, they don't seem to have recorded it in any tutorial or documentation that I've run across so far, and it's driving me up the wall.

推荐答案

片段着色器接收gl_Colorgl_SecondaryColor作为顶点属性.它还获得了四个可以写入值的变量:gl_FrontColorgl_FrontSecondaryColorgl_BackColorgl_BackSecondaryColor.如果要直接传递原始颜色,可以执行以下操作:

The fragment shader receives gl_Color and gl_SecondaryColor as vertex attributes. It also gets four varying variables: gl_FrontColor, gl_FrontSecondaryColor, gl_BackColor, and gl_BackSecondaryColor that it can write values to. If you want to pass the original colors straight through, you'd do something like:

gl_FrontColor = gl_Color;
gl_FrontSecondaryColor = gl_SecondaryColor;
gl_BackColor = gl_Color;
gl_BackSecondaryColor = gl_SecondaryColor;

顶点着色器之后的管道中固定的功能会将其固定在[0..1]范围内,并确定顶点是正面的还是背面的.然后,它将像往常一样插值所选的(正面或背面)颜色.然后,片段着色器将接收选择的,钳位的,插值的颜色为gl_Colorgl_SecondaryColor.

Fixed functionality in the pipeline following the vertex shader will then clamp these to the range [0..1], and figure out whether the vertex is front-facing or back-facing. It will then interpolate the chosen (front or back) color like usual. The fragment shader will then receive the chosen, clamped, interpolated colors as gl_Color and gl_SecondaryColor.

例如,如果您绘制标准的死亡三角形",例如:

For example, if you drew the standard "death triangle" like:

glBegin(GL_TRIANGLES);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-1.0f, 0.0f, -1.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, -1.0f);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3d(0.0, -1.0, -1.0);
glEnd();

然后是这样的顶点着色器:

Then a vertex shader like this:

void main(void) {
    gl_Position = ftransform();
    gl_FrontColor = gl_Color;
}

具有这样的片段着色器:

with a fragment shader like this:

void main() {
    gl_FragColor = gl_Color;
}

会像通过固定功能管道一样传输颜色.

will transmit the colors through, just like if you were using the fixed-functionality pipeline.

这篇关于如何获取片段的当前颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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