我在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

最佳答案

似乎您将错误的法线 vector 传递给了墙。

如果您的墙所有点的Z = -10(墙平行于X),则它的法向 vector 必须指向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();

关于c++ - C++ OpenGL照明仅照亮某些墙,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14766346/

10-14 09:12