0

我在 XCode OpenGL ES 模板之上创建了一个项目。

有没有办法为每个对象自定义着色器颜色?

着色器.vsh:

attribute vec4 position;
attribute vec3 normal;

varying lowp vec4 colorVarying;

uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;

void main()
{
    vec3 eyeNormal = normalize(normalMatrix * normal);
    vec3 lightPosition = vec3(1.0, 1.0, 1.0);
    vec4 diffuseColor = vec4(1, 0.4, 1.0, 1.0);

    float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));

    colorVarying = diffuseColor * nDotVP;

    gl_Position = modelViewProjectionMatrix * position;
}

着色器.fsh:

varying lowp vec4 colorVarying;

void main()
{
    gl_FragColor = colorVarying;
}

我是否需要创建一个额外的属性Shader.vsh来解析颜色以替换diffuse变量,或者这是如何完成的?

4

1 回答 1

2

我建议对颜色使用统一变量

// put this in Shader.vsh, before the main()
uniform vec4 diffuseColor;

并在代码中设置值:

glUseProgram(progID);

// object 1 (R, G, B, A is a color)
glUniform4f(glGetUniformLocation(progID, "diffuseColor"), R, G, B, A);
draw_object_1();

// object 2 (R, G, B, A is a color)
glUniform4f(glGetUniformLocation(progID, "diffuseColor"), R, G, B, A);
draw_object_2();

光的位置也可以这样做。

于 2012-06-02T13:54:35.720 回答