我正在使用通用OpenGL游戏项目,它自动创建2个旋转立方体。我试图找出如何将每个面/顶点更改为不同的颜色。目前,它只允许您更改整个多维数据集的颜色。
答案 0 :(得分:1)
将颜色值添加到完整数据
GLfloat gCubeVertexData[324] =
{
// Data layout for each line below is:
// positionX, positionY, positionZ, normalX, normalY, normalZ, your color
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0, 1.0, 0.5,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0, 0.5, 0.5,
....
};
在setupGL
更新为:
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 36, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 36, BUFFER_OFFSET(12));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, 36, BUFFER_OFFSET(24));
找到字符串glBindAttribLocation(_program, GLKVertexAttribNormal, "normal");
并添加:
glBindAttribLocation(_program, GLKVertexAttribColor, "color");
和Shader.vsh:
attribute vec4 position;
attribute vec3 normal;
attribute vec4 color;
varying lowp vec4 colorVarying;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
void main()
{
vec3 eyeNormal = normalize(normalMatrix * normal);
vec3 lightPosition = vec3(0.0, 0.0, 1.0);
vec4 diffuseColor = vec4(0.4, 0.4, 1.0, 1.0);
float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));
colorVarying = color;// * diffuseColor * nDotVP;
gl_Position = modelViewProjectionMatrix * position;
}
但是如果你明白为什么需要做那些操作会更好!