Opengl es 在GLKit中启用light0会导致颜色丢失

Opengl es 在GLKit中启用light0会导致颜色丢失,opengl-es,glkit,glkbaseeffect,Opengl Es,Glkit,Glkbaseeffect,我一直在修改OpenGL ES 2教程中的一些示例代码。我可以将GLKBaseEffect设置为使用恒定颜色。现在,当我去给场景添加灯光时,颜色消失了,剩下2个黑色立方体 以下是绘制方法: - (void)draw { GLKMatrix4 xRotationMatrix = GLKMatrix4MakeXRotation(rotation.x); GLKMatrix4 yRotationMatrix = GLKMatrix4MakeYRotation(rotation.y);

我一直在修改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;
    }
}
+(void)初始化{
如果(!已初始化){
顶点[0]=GLKVector3Make(-0.5,-0.5,0.5);//左下前
顶点[1]=GLKVector3Make(0.5,-0.5,0.5);//右下前
顶点[2]=GLKVector3Make(0.5,0.5,0.5);//右上前方
顶点[3]=GLKVector3Make(-0.5,0.5,0.5);//左上前
顶点[4]=GLKVector3Make(-0.5,-0.5,-0.5);//左下背部
顶点[5]=GLKVector3Make(0.5,-0.5,-0.5);//右下背部
顶点[6]=GLKVector3Make(0.5,0.5,-0.5);//右上背部
顶点[7]=GLKVector3Make(-0.5,0.5,-0.5);//左上背部
颜色[0]=GLKVector4Make(1.0,0.0,0.0,1.0);//红色
颜色[1]=GLKVector4Make(0.0,1.0,0.0,1.0);//绿色
颜色[2]=GLKVector4Make(0.0,0.0,1.0,1.0);//蓝色
颜色[3]=GLKVector4Make(0.0,0.0,0.0,1.0);//黑色
颜色[4]=GLKVector4Make(0.0,0.0,1.0,1.0);//蓝色
颜色[5]=GLKVector4Make(0.0,0.0,0.0,1.0);//黑色
颜色[6]=GLKVector4Make(1.0,0.0,0.0,1.0);//红色
颜色[7]=GLKVector4Make(0.0,1.0,0.0,1.0);//绿色
整数顶点[36]={
//正面
0, 1, 2,
0, 2, 3,
//对
1, 5, 6,
1, 6, 2,
//背
5, 4, 7,
5, 7, 6,
//左
4, 0, 3,
4, 3, 7,
//顶
3, 2, 6,
3, 6, 7,
//底部
4, 5, 1,
4, 1, 0,
};
对于(int i=0;i<36;i++){
三角形杠杆[i]=顶点[vertexIndices[i]];
三角形颜色[i]=颜色[顶点[i]];
}
效应=[[glkbaseffect 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);
初始化=是;
}
}
以下是禁用照明(红色)和禁用照明(黑色)的屏幕截图


照明要求顶点同时具有位置和法线属性——看起来只有位置。

照明要求顶点同时具有位置和法线属性——看起来只有位置。

照明要求顶点同时具有位置和法线属性——看起来只有位置具有位置。

照明要求顶点同时具有位置和法线属性——您似乎只有位置。

您可能还需要启用颜色材质属性:

self.effect = [[GLKBaseEffect alloc] init];
self.effect.colorMaterialEnabled = GL_TRUE;

您可能还需要启用“颜色材质”属性:

self.effect = [[GLKBaseEffect alloc] init];
self.effect.colorMaterialEnabled = GL_TRUE;

您可能还需要启用“颜色材质”属性:

self.effect = [[GLKBaseEffect alloc] init];
self.effect.colorMaterialEnabled = GL_TRUE;

您可能还需要启用“颜色材质”属性:

self.effect = [[GLKBaseEffect alloc] init];
self.effect.colorMaterialEnabled = GL_TRUE;

你说得对!非常感谢你,你是如此的直率!非常感谢你,你是如此的直率!非常感谢你,你是如此的直率!多谢各位much@genpfault我很好奇你为什么编辑我的问题?删除并修复了。@genpfault Ok。我理解背后的逻辑。不过在我看来似乎有点冷淡……@genpfault我很好奇你为什么编辑我的问题?删除并修复了。@genpfault好的。我理解背后的逻辑。不过在我看来似乎有点冷淡……@genpfault我很好奇你为什么编辑我的问题?删除并修复了。@genpfault好的。我理解背后的逻辑。不过在我看来似乎有点冷淡……@genpfault我很好奇你为什么编辑我的问题?删除并修复了。@genpfault好的。我理解背后的逻辑。不过在我看来似乎有点冷淡。。。。