照明在opengl中不起作用

时间:2017-06-26 17:43:05

标签: c opengl glut glew lighting

我无法让照明工作在我的地面下降部分。下面是我的地面和下降的代码(使它成为一个沟渠):

static void ground(double x, double y, double z, double dx, double dy, 
double dz){
 float white[] = {1,1,1,1};
 float Emission[]  = {0.0,0.0,0.01*emission,1.0};
 glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,shiny);
 glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
 glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,Emission);
 //  Save transformation
 glPushMatrix();
 //  Offset, scale and rotate
 glTranslated(x,y,z);
 glScaled(dx, dy, dz);

 glEnable(GL_TEXTURE_2D);
 glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

 glBindTexture(GL_TEXTURE_2D, textures[2]);
 glBegin(GL_QUADS);
 glColor3f(0.5, 1.0, 0.5);
 glNormal3f(0,0,0);
 glTexCoord2f(0.0,  0.0); glVertex3f(-100, 0, -300);
 glTexCoord2f(300.0,0.0); glVertex3f(-100,0,300);
 glTexCoord2f(300.0,300.0); glVertex3f(-3,0,300);
 glTexCoord2f(0.0,300.0); glVertex3f(-3,0,-300);
 glEnd();

 glBindTexture(GL_TEXTURE_2D, textures[2]);
 glBegin(GL_QUADS);
 glColor3f(1.0, 1.0, 0.5);
 glNormal3f(0,0,-1);
 glTexCoord2f(0.0,  0.0); glVertex3f(-2.99,0,-300);
 glTexCoord2f(300.0,0.0); glVertex3f(-2.99,0,300);
 glTexCoord2f(300.0,300.0); glVertex3f(-1,-1,300);
 glTexCoord2f(0.0,300.0); glVertex3f(-1,-1,-300);
 glEnd();

 glBindTexture(GL_TEXTURE_2D, textures[2]);
 glBegin(GL_QUADS);
 glColor3f(1.0, 1.0, 0.5);
 glNormal3f(0,0,1);
 glTexCoord2f(0.0,  0.0); glVertex3f(0.99,-1,-300);
 glTexCoord2f(300.0,0.0); glVertex3f(0.99,-1,300);
 glTexCoord2f(300.0,300.0); glVertex3f(2.99,0,300);
 glTexCoord2f(0.0,300.0); glVertex3f(2.99,0,-300);
 glEnd();

 glBindTexture(GL_TEXTURE_2D, textures[2]);
 glBegin(GL_QUADS);
 glColor3f(0.5, 1.0, 0.5);
 glNormal3f(0,0,0);
 glTexCoord2f(0.0,  0.0); glVertex3f(2.99, 0, -300);
 glTexCoord2f(300.0,0.0); glVertex3f(2.99,0,300);
 glTexCoord2f(300.0,300.0); glVertex3f(100,0,300);
 glTexCoord2f(0.0,300.0); glVertex3f(100,0,-300);
 glEnd();

 glPopMatrix();

 glDisable(GL_TEXTURE_2D);

}

因此,没有照明工作正常的代码是" glBegin(GL_QUADS)"

的中间两个片段

1 个答案:

答案 0 :(得分:1)

这只是GIGO的一个实例(垃圾输入,垃圾输出):

glNormal3f(0,0,0);

不。这不是一个有效的法向量,并且会完全破坏任何光照计算。

下一个

glNormal3f(0,0,-1);
glTexCoord2f(0.0,  0.0); glVertex3f(-2.99,0,-300);
glTexCoord2f(300.0,0.0); glVertex3f(-2.99,0,300);
glTexCoord2f(300.0,300.0); glVertex3f(-1,-1,300);
glTexCoord2f(0.0,300.0); glVertex3f(-1,-1,-300);

至少是一些非空的向量,但它与你描述的面不同,所以光照也是错误的。