2013-11-13 6 views
0

Я модифицировал некоторый пример кода из учебников OpenGL ES 2. Я могу установить GLKBaseEffect для использования постоянного цвета. Теперь, когда я иду, чтобы добавить свет к сцене, цвет теряется, и я остаюсь с двумя черными кубиками.Включение light0 в GLKit приводит к потере цвета

Ниже приведен метод рисования:

- (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; 
    } 
} 

Ниже приведены скриншоты, когда освещение выключено (красный цвет) и инвалидов (черный цвет)

enter image description here

enter image description here

+0

@genpfault Мне любопытно, почему вы редактировали на мой вопрос? – Lancophone

+0

Удалено [noise] (http://meta.stackexchange.com/a/3021/150045) и исправлено [теги] (http://meta.stackexchange.com/a/145080/150045). – genpfault

+0

@genpfault Хорошо. Я понимаю логику этого. По-моему, кажется немного холодным, хотя .... – Lancophone

ответ

3

Освещение требует, чтобы ваши вершины имели как положение, так и нормальные атрибуты - у вас есть только позиция.

+0

Правильно вы были! большое спасибо – Lancophone

0

Вам также может понадобиться включить свойство цвета материал:

self.effect = [[GLKBaseEffect alloc] init]; 
self.effect.colorMaterialEnabled = GL_TRUE; 
Смежные вопросы