gl_color 和 material 之间的相互依赖

Inter dependency between gl_color and material

  1. 在固定管道中,使用material时gl_color设置的颜色会怎样?

  2. 有没有办法在 glsl 片段着色器中检索 gl_color 设置的颜色?

OpenGL 2.1 API Specification - 2.15.3 Shader Variables, page 76

Vertex Attributes

Vertex shaders can access built-in vertex attribute variables corresponding to the per-vertex state set by commands such as Vertex, Normal, Color. .....

OpenGL Shading Language 1.20 Specification - 7.3 Vertex Shader Built-In Attributes, page 49:

The following attribute names are built into the OpenGL vertex language and can be used from within a vertex shader to access the current values of attributes declared by OpenGL.

attribute vec4 gl_Color;

这意味着,由glColor*设置的颜色可以通过顶点着色器中的属性attribute vec4 gl_Color;访问。


请参阅 [OpenGL 着色语言 1.20 规范 - 7.5 内置统一状态,第 50 页]:

As an aid to accessing OpenGL processing state, the following uniform variables are built into the OpenGL Shading Language.

.....

struct gl_MaterialParameters {
    vec4 emission; // Ecm
    vec4 ambient; // Acm
    vec4 diffuse; // Dcm
    vec4 specular; // Scm
    float shininess; // Srm
};
uniform gl_MaterialParameters gl_FrontMaterial;
uniform gl_MaterialParameters gl_BackMaterial;

这意味着,由glMaterial设置的material属性可以被内置统一变量gl_FrontMaterialgl_BackMaterial访问。


另见 OpenGL Shading Language (GLSL) Quick Reference Guide


参考评论中的附加问题:

And do you know which shader model (Gouraud, Phong...) is used by the old fixed pipeline, when only a color is set?

Phong 着色在 OpenGL 固定函数管线中是不可能的。参见 Legacy opengl - Why is phong shading not possible?

除非您在自己的着色器中实现 phong 着色。参见 Per Fragment Lighting

一般来说,着色模型可以通过glShadeModel设置,可以是GL_SMOOTHGL_FLAT


进一步说明:

如果启用了照明 (glEnable(GL_LIGHTING)),则必须通过 glMaterial.

设置颜色 (gl_Color)

但是如果另外启用GL_COLOR_MATERIAL (glEnable(GL_COLOR_MATERIAL)),那么您可以通过glColorMaterial.[=40 指定material参数跟踪当前颜色=]

例如

glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

How lighting works