2010-05-12 101 views
1

我正在努力在我的OpenGL ES iPhone場景中獲得簡單的照明。我正在顯示一個以原點爲中心的簡單對象,並使用一個弧形球通過觸摸屏幕來旋轉它。除了我嘗試添加一個固定光源(固定的眼睛位置)並且擰得很緊:所有這些都很好地工作,但是整個對象(本例中爲二十面體)均勻點亮,即它們全都顯示爲相同的顏色。照明和OpenGL ES

我簡化了我的代碼儘可能所以它是獨立的,仍然再現我的經歷:

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); 

其中drawSphere函數實際上繪製了一個二十面體:

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

電影是什麼我看到的結果是here。感謝任何能夠闡明這一點的人(不要開玩笑!)。我敢肯定它對於某個人來說看起來會很尷尬,但是我發誓在這之前我已經看過很多照明教程,並且陷入困境。

回答

1

嘗試使用glNormalPointer()添加一些頂點法線。它看起來像OpenGL ES只是使用default normal的一切。

+1

你說得對。我也是對的,我現在感到羞愧:) – 2010-05-12 22:27:28