1

我在 opengl 中点亮我的场景,有 4 面墙,其中只有 2 面被点亮。我认为这是正常的问题,但我不确定。

这是照明代码:

glShadeModel(GL_SMOOTH);
//glEnable(GL_CULL_FACE);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);

GLfloat lightpos0[] = {-5, 1, 0, 0.};
GLfloat AmbientLight0[] = {0.2, 0.2, 0.2,1.0}; 
GLfloat DiffuseLight0[] = {0.8, 0.8, 0.8,1.0};
GLfloat SpecularLight0[] = {1.0, 1.0, 1.0,1.0};

glLightfv(GL_LIGHT0, GL_POSITION, lightpos0);
glLightfv(GL_LIGHT0, GL_AMBIENT, AmbientLight0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, DiffuseLight0);
glLightfv(GL_LIGHT0, GL_SPECULAR, SpecularLight0);

GLfloat lightpos1[] = {5, 1, 0, 0};
GLfloat AmbientLight1[] = {0.2, 0.2, 0.2,1.0}; 
GLfloat DiffuseLight1[] = {0.8, 0.8, 0.8,1.0};
GLfloat SpecularLight1[] = {1.0, 1.0, 1.0,1.0};

glLightfv(GL_LIGHT1, GL_POSITION, lightpos1);
glLightfv(GL_LIGHT1, GL_AMBIENT, AmbientLight1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, DiffuseLight1);
glLightfv(GL_LIGHT1, GL_SPECULAR, SpecularLight1);

这是绘制 4 面墙的代码。

    //north
    glBegin(GL_QUADS);
glNormal3f(1.0,0,0);
glVertex3f(-10,0,-10);
glVertex3f( 10,0,-10);
glVertex3f( 10,5,-10);
glVertex3f(-10,5,-10);
glEnd();
//south
glBegin(GL_QUADS);
glNormal3f(-1.0, 0, 0);
glVertex3f(-10,0, 10);
glVertex3f( 10,0, 10);
glVertex3f( 10,5, 10);
glVertex3f(-10,5, 10);
glEnd();
//east
glBegin(GL_QUADS);
glNormal3f(0,0, -1.0);
glVertex3f( 10,0,-10);
glVertex3f( 10,0,10);
glVertex3f( 10,5,10);
glVertex3f( 10,5,-10);
glEnd();
//west
glBegin(GL_QUADS);
glNormal3f(0,0,1.0);
glVertex3f( -10,0,-10);
glVertex3f( -10,0,10);
glVertex3f( -10,5,10);
glVertex3f( -10,5,-10);
glEnd();

如果有帮助,这里是正在发生的事情的图片:http: //i.stack.imgur.com/xRgVL.png

4

2 回答 2

2

正如 Vladislav Khorev 所指出的,法线必须指向所有点共有的坐标(鉴于这种特殊的正交排列)。但也应该考虑多边形的缠绕:

 // Polygon A --  north     Polygon B -- south
 glVertex3f(-10,0,-10);     glVertex3f(-10,0, 10); 
 glVertex3f( 10,0,-10);     glVertex3f( 10,0, 10);
 glVertex3f( 10,5,-10);     glVertex3f( 10,5, 10);
 glVertex3f(-10,5,-10);     glVertex3f(-10,5, 10);
 Normal(0,0,1),             Normal(0,0,-1);

两个多边形(南北),是不同的绕组。第一个是从点 0,0,0 观察到的顺时针,另一个是逆时针。一个应该与这些一致。

于 2013-02-08T06:08:43.790 回答
1

似乎您将错误的法线向量传递给墙壁。

如果你的墙的所有点的 Z = -10(墙平行于 X),那么它的法向量必须指向 Z 的正方向:(0,0,1)。所有方向都是如此。

尝试这个:

glBegin(GL_QUADS);
glNormal3f(0,0,1);
glVertex3f(-10,0,-10);
glVertex3f( 10,0,-10);
glVertex3f( 10,5,-10);
glVertex3f(-10,5,-10);
glEnd();
//south
glBegin(GL_QUADS);
glNormal3f(0, 0, -1);
glVertex3f(-10,0, 10);
glVertex3f( 10,0, 10);
glVertex3f( 10,5, 10);
glVertex3f(-10,5, 10);
glEnd();
//east
glBegin(GL_QUADS);
glNormal3f(-1,0, -0);
glVertex3f( 10,0,-10);
glVertex3f( 10,0,10);
glVertex3f( 10,5,10);
glVertex3f( 10,5,-10);
glEnd();
//west
glBegin(GL_QUADS);
glNormal3f(1, 0, 0);
glVertex3f( -10,0,-10);
glVertex3f( -10,0,10);
glVertex3f( -10,5,10);
glVertex3f( -10,5,-10);
glEnd();
于 2013-02-08T06:02:25.163 回答