OpenGL Lighting通过相机改变亮度

时间:2012-09-14 01:46:58

标签: opengl light

所以我刚开始玩OpenGL,并决定制作一个小体素渲染的东西。我试图点亮它,但是当我围绕y轴旋转相机时会发生奇怪的效果。例如,当我第一次产卵时,灯光看起来像这样:

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

有人知道发生了什么事吗?

1 个答案:

答案 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();