我一直在修改OpenGL ES 2教程中的一些示例代码。我可以设置GLKBaseEffect使用常量颜色。现在,当我去为场景添加光线时,颜色会丢失,我会留下2个黑色立方体。
以下是绘制方法:
- (void)draw
{
GLKMatrix4 xRotationMatrix = GLKMatrix4MakeXRotation(rotation.x);
GLKMatrix4 yRotationMatrix = GLKMatrix4MakeYRotation(rotation.y);
GLKMatrix4 zRotationMatrix = GLKMatrix4MakeZRotation(rotation.z);
GLKMatrix4 scaleMatrix = GLKMatrix4MakeScale(scale.x, scale.y, scale.z);
GLKMatrix4 translateMatrix = GLKMatrix4MakeTranslation(position.x, position.y, position.z);
GLKMatrix4 modelMatrix =
GLKMatrix4Multiply(translateMatrix,
GLKMatrix4Multiply(scaleMatrix,
GLKMatrix4Multiply(zRotationMatrix,
GLKMatrix4Multiply(yRotationMatrix, xRotationMatrix))));
GLKMatrix4 viewMatrix = GLKMatrix4MakeLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
effect.transform.modelviewMatrix = GLKMatrix4Multiply(viewMatrix, modelMatrix);
effect.transform.projectionMatrix = GLKMatrix4MakePerspective(0.125*M_TAU, 2.0/3.0, 2, -1);
effect.useConstantColor = YES;
effect.constantColor = redColor;
[effect prepareToDraw];
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices);
// Sticking with constant color as opposed to per-vertex vertex shading for now...
// glEnableVertexAttribArray(GLKVertexAttribColor);
// glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, triangleColors);
glDrawArrays(GL_TRIANGLES, 0, 36);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribColor);
}
我在初始化静态方法中启用灯光:
+ (void)initialize {
if (!initialised) {
vertices[0] = GLKVector3Make(-0.5, -0.5, 0.5); // Left bottom front
vertices[1] = GLKVector3Make( 0.5, -0.5, 0.5); // Right bottom front
vertices[2] = GLKVector3Make( 0.5, 0.5, 0.5); // Right top front
vertices[3] = GLKVector3Make(-0.5, 0.5, 0.5); // Left top front
vertices[4] = GLKVector3Make(-0.5, -0.5, -0.5); // Left bottom back
vertices[5] = GLKVector3Make( 0.5, -0.5, -0.5); // Right bottom back
vertices[6] = GLKVector3Make( 0.5, 0.5, -0.5); // Right top back
vertices[7] = GLKVector3Make(-0.5, 0.5, -0.5); // Left top back
colors[0] = GLKVector4Make(1.0, 0.0, 0.0, 1.0); // Red
colors[1] = GLKVector4Make(0.0, 1.0, 0.0, 1.0); // Green
colors[2] = GLKVector4Make(0.0, 0.0, 1.0, 1.0); // Blue
colors[3] = GLKVector4Make(0.0, 0.0, 0.0, 1.0); // Black
colors[4] = GLKVector4Make(0.0, 0.0, 1.0, 1.0); // Blue
colors[5] = GLKVector4Make(0.0, 0.0, 0.0, 1.0); // Black
colors[6] = GLKVector4Make(1.0, 0.0, 0.0, 1.0); // Red
colors[7] = GLKVector4Make(0.0, 1.0, 0.0, 1.0); // Green
int vertexIndices[36] = {
// Front
0, 1, 2,
0, 2, 3,
// Right
1, 5, 6,
1, 6, 2,
// Back
5, 4, 7,
5, 7, 6,
// Left
4, 0, 3,
4, 3, 7,
// Top
3, 2, 6,
3, 6, 7,
// Bottom
4, 5, 1,
4, 1, 0,
};
for (int i = 0; i < 36; i++) {
triangleVertices[i] = vertices[vertexIndices[i]];
triangleColors[i] = colors[vertexIndices[i]];
}
effect = [[GLKBaseEffect alloc] init];
effect.lightingType = GLKLightingTypePerVertex;
effect.lightModelAmbientColor = GLKVector4Make(0.3f, 0.3f, 0.33f, 1.0f);
effect.light0.enabled = GL_TRUE;
effect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f);
initialised = YES;
}
}
以下是禁用灯光(红色)和禁用(黑色)
时的屏幕截图
答案 0 :(得分:3)
照明要求你的顶点同时具有位置和普通属性 - 你似乎只有位置。
答案 1 :(得分:0)
您可能还需要启用颜色材质属性:
self.effect = [[GLKBaseEffect alloc] init];
self.effect.colorMaterialEnabled = GL_TRUE;