我试图了解在 opengl 中更改指向光源位置的效果,但遇到了麻烦。有人可以解释不同的位置如何影响模型的照明吗?
我使用的代码如下 -
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat lightPos0[] = {0.0f,0.0f, -1.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//gluLookAt(0, 6, 6.0, 0, -2, 0, 0.0f, 1.0f, 0.0f);
glTranslatef(0,0,-6);
GLfloat ambientColor[] = {0.1f, 0.1f, 0.1f, 1.0f};
glRotatef(50,1,0,0);
glRotatef(-45,0,1,0);
glBegin(GL_QUADS);
glNormal3f(-1,0,0);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
glNormal3f(1,0,0);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
glNormal3f(0,-1,0);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
glNormal3f(0,0,-1);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
glNormal3f(0,0,1);
glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
glEnd();
glTranslatef(-1,1,0);
glRotatef(a,0,0,1);
glTranslatef(1,-1,0);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
glEnd();
glFlush();
if(a>90)
a=a-90;
a=a+1;
glutSwapBuffers();
glutPostRedisplay();
}
我期待 light(0,0,-1) 会从前面照射立方体,因为我已经将立方体转换为 -6 。但光是从背后射来的。