Pergunta

Estou trabalhando para obter uma iluminação simples na minha cena do iPhone OpenGL ES. Estou exibindo um objeto simples centrado na origem e usando uma arcball para girá -lo tocando na tela. Tudo isso funciona bem, exceto que eu tento adicionar uma luz fixa (posição fixa do olho WRT) e está mal parafusada: todo o objeto (um icosaedro neste exemplo) é iluminado uniformemente, ou seja, tudo aparece na mesma cor.

Simplifiquei meu código o máximo possível, para que seja independente e ainda reproduz o que eu experimento:

    glClearColor (0.25, 0.25, 0.25, 1.);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable (GL_DEPTH_TEST);

    glEnable(GL_LIGHTING);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glOrthof(-1, 1, -(float)backingWidth/backingHeight, (float)backingWidth/backingHeight, -10, 10);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();

    GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
    GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    GLfloat position[] = { -1.5f, 1.0f, -400.0f, 0.0f };

    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
    glLightfv(GL_LIGHT0, GL_POSITION, position);

    glShadeModel(GL_SMOOTH);
    glEnable(GL_NORMALIZE);

    float currRot[4];
    [arcball getCurrentRotation:currRot];
    glRotatef (currRot[0], currRot[1], currRot[2], currRot[3]);

    float f[4];
    f[0] = 0.5; f[1] = 0; f[2] = 0; f[3] = 1;
    glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, f);
    glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, f);

    f[0] = 0.2;     f[1] = 0.2; f[2] = 0.2; f[3] = 1;
    glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, f);

    glEnableClientState (GL_VERTEX_ARRAY);
    drawSphere(0, 0, 0, 1);

Onde a função Drawsphere realmente desenha um icosaedro:

static void drawSphere (float x, float y, float z, float rad)
{ 
    glPushMatrix ();
    glTranslatef (x, y, z);
    glScalef (rad, rad, rad);

    // Icosahedron
    const float vertices[] =
    { 0., 0., -1., 0., 0., 1., -0.894427, 0., -0.447214, 0.894427, 0.,
            0.447214, 0.723607, -0.525731, -0.447214, 0.723607, 0.525731,
            -0.447214, -0.723607, -0.525731, 0.447214, -0.723607, 0.525731,
            0.447214, -0.276393, -0.850651, -0.447214, -0.276393, 0.850651,
            -0.447214, 0.276393, -0.850651, 0.447214, 0.276393, 0.850651,
            0.447214 };
    const GLubyte indices[] =
    { 1, 11, 7, 1, 7, 6, 1, 6, 10, 1, 10, 3, 1, 3, 11, 4, 8, 0, 5, 4, 0,
            9, 5, 0, 2, 9, 0, 8, 2, 0, 11, 9, 7, 7, 2, 6, 6, 8, 10, 10, 4, 3,
            3, 5, 11, 4, 10, 8, 5, 3, 4, 9, 11, 5, 2, 7, 9, 8, 6, 2 };

    glVertexPointer (3, GL_FLOAT, 0, vertices);
    glDrawElements (GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_BYTE, indices);
    glPopMatrix ();
}

Um filme do que eu vejo como resultado é aqui. Obrigado a quem pode lançar alguma luz sobre isso (sem brincadeira!). Tenho certeza de que parecerá embaraçosamente trivial para alguém, mas juro que olhei para muitos tutoriais de iluminação antes disso e estou preso.

Foi útil?

Solução

Tente adicionar alguns normais de vértice usando glNormalPointer(). Parece que o OpenGL ES está apenas usando o Normal padrão para tudo.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top