更改顶点着色器中顶点的颜色

发布于 2024-08-27 09:31:32 字数 60 浏览 4 评论 0原文

是否可以使用 GLSL 顶点着色器程序设置单个顶点的颜色,就像 gl_Position 更改顶点位置一样?

Is it possible to set the color of a single vertex using a GLSL vertex shader program, in the same way that gl_Position changes the position of a vertex ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

嘿咻 2024-09-03 09:31:32

对于 GLSL 1.30 之前的版本,您需要写入 gl_FrontColorgl_BackColor 内置函数,它们可以在顶点着色器中进行访问。了解 GLSL 1.10 规范中的变化 (http://www .opengl.org/registry/doc/GLSLangSpec.Full.1.10.59.pdf)了解有关它们的更多信息,或 GL_ARB_vertex_shader 扩展规范。

gl_FrontColorgl_BackColor 是采用归一化浮点标量的 4D RGBA 向量。

但这会将所有顶点设置为红色,而不仅仅是一个顶点。这是因为对所有顶点运行相同的顶点着色器。如果要设置单独的颜色,请将 glColorPointerglDrawArraysglDrawElementsglDrawRangeElementsglMultiDrawElements 一起使用。由glColorPointer设置的顶点颜色可以在顶点着色器中读取为gl_Color。顶点着色器中的gl_Color是每个顶点的属性。

要读取您在顶点着色器中编写的颜色,请在片段着色器中读取内置的变量 gl_Color。完成的片段应写入gl_FragColor

顶点着色器示例:

void main()
{
    gl_FrontColor = gl_Color;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

片段着色器示例:

void main()
{
    gl_FragColor = gl_Color;
}

另外,要使顶点着色器像 OpenGL 固定功能管道一样设置变量,请调用函数 ftransform()。

void main()
{
    ftransform();
}

For versions of GLSL prior version 1.30, you want to write to the gl_FrontColor or gl_BackColor built-ins, which are varyings accessible in the vertex shader. Read about varyings in the GLSL 1.10 specification (http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.10.59.pdf) to learn more about them, or the GL_ARB_vertex_shader extension specification.

gl_FrontColor and gl_BackColor are 4D RGBA vectors which take normalized floating point scalars.

But this will set all the vertices to red, not just one vertex. This is because the same vertex shader is run for all the vertices. If you want to set individual colours, use glColorPointer together with glDrawArrays, glDrawElements, glDrawRangeElements or glMultiDrawElements. The vertex color set by glColorPointer can be read as gl_Color in the vertex shader. gl_Color in the vertex shader is a per-vertex attribute.

To read the color you wrote in the vertex shader, in the fragment shader, read the built-in varying gl_Color. Finished fragments should be written to gl_FragColor.

Vertex shader example:

void main()
{
    gl_FrontColor = gl_Color;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment shader example:

void main()
{
    gl_FragColor = gl_Color;
}

Also, to make the vertex shader set the varyings just like the OpenGL fixed-function pipeline, call the function ftransform().

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