2012-09-14 101 views
1

所以我剛開始玩OpenGL,並決定做一個體素渲染的東西。我試圖點亮它,但是當我將相機繞y軸旋轉時發生奇怪的效果。例如,當我第一次產卵中,光看起來是這樣的:OpenGL燈光相機改變亮度

http://i.stack.imgur.com/zQ49y.png

但是,當我有點繞旋轉我得到這個:

http://i.stack.imgur.com/PWvVo.png

下面是做到這一點的代碼東西:

glRotatef(xrot, 1.0f, 0.0f, 0.0); 
    glRotatef(yrot, 0.0f, 1.0f, 0.0); 

    glTranslatef(-3.5f-xcam, ycam, -3.5f-zcam); 

    glEnable (GL_DEPTH_TEST); //enable the depth testing 
    glEnable (GL_LIGHTING); //enable the lighting 
    glEnable (GL_LIGHT0); 

    GLfloat specular[] = {1.0f-xcam, ycam+1.0, 1.0f-zcam, 1.0}; 
    glLightfv(GL_LIGHT0, GL_SPECULAR, specular); 

    glEnable(GL_TEXTURE_2D); 

    glBindTexture(GL_TEXTURE_2D, grnd); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 

    glBegin(GL_QUADS); 

    glTexCoord2i(0, 0); 
    glVertex3f(0, 0, 0); 

    glTexCoord2i(0, 1); 
    glVertex3f(0, 0, 7.0f); 

    glTexCoord2i(1, 1); 
    glVertex3f(7.0f, 0, 7.0f); 

    glTexCoord2i(1, 0); 
    glVertex3f(7.0f, 0, 0); 

    glEnd(); 

    glDisable(GL_TEXTURE_2D); 

    glLoadIdentity(); 

    drawVoxel(2.f, 0.f, 2.f, tex); 
    drawVoxel(1.f, 0.f, 1.f, tex); 
    drawVoxel(2.f, 0.f, 2.f, tex); 
    drawVoxel(3.f, 0.f, 3.f, tex); 

    drawVoxel(4.f, 0.f, 4.f, tree); 
    drawVoxel(4.f, 1.f, 4.f, tree); 
    drawVoxel(4.f, 2.f, 4.f, tree); 
    drawVoxel(4.f, 3.f, 4.f, tree); 

    test.Flip(); 

有沒有人有任何想法是怎麼回事?

+3

您未設置法線。 –

+0

設置法線如何工作? –

+0

我已經添加了答案。 –

回答

4

您還需要使用glNormal3f()。

. 
. 
. 

glBegin(GL_QUADS); 

glTexCoord2i(0, 0); 
glNormal3f(0,1,0); 
glVertex3f(0, 0, 0); 

glTexCoord2i(0, 1); 
glNormal3f(0,1,0); 
glVertex3f(0, 0, 7.0f); 

glTexCoord2i(1, 1); 
glNormal3f(0,1,0); 
glVertex3f(7.0f, 0, 7.0f); 

glTexCoord2i(1, 0); 
glNormal3f(0,1,0); 
glVertex3f(7.0f, 0, 0); 

glEnd(); 
+0

它似乎運作良好,它仍然是奇怪的東西,但這解決了我問。謝謝! –