以下是我的一些代码
光明:
glEnable(GL_LIGHT0);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f);
glEnable(GL_COLOR_MATERIAL);
GLfloat ambient_light0[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position_light0[] = { 0.0f, 8.0f, 0.0f, 0.9f };
GLfloat spotDirection_light0[] = {0.0, -1.0, 0.0 };
GLfloat specularLightcolor[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, position_light0 );
glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection_light0 );
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.5);
glLightfv( GL_LIGHT0, GL_AMBIENT, ambient_light0 );
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLightcolor);
glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, 60.0f );
glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 50.0f );
理由:
void drawGround(int id) {
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
getTexture(id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLfloat material_diffuse[] = {0.9, 0.9, 0.9, 1.0 };
GLfloat material_specular[] = { 1, 1, 1, 1 };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
glBegin(GL_QUADS);
for(int i = 0; i<3; i++){
for(int j= 0; j<3; j++){
glNormal3f(0.0f, 1.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 80.0);
glTexCoord2f(1, 1);
glVertex3f(-floorSize +((j)*(2*floorSize)/3), 0 ,-floorSize +((i)*(2*floorSize)/3));
glTexCoord2f(1, 0);
glVertex3f(-floorSize +((j)*(2*floorSize)/3), 0 ,-floorSize +((i+1)*(2*floorSize)/3));
glTexCoord2f(0,0);
glVertex3f(-floorSize +((j+1)*(2*floorSize)/3), 0 ,-floorSize +((i+1)*(2*floorSize)/3));
glTexCoord2f(0, 1);
glVertex3f(-floorSize +((j+1)*(2*floorSize)/3), 0 ,-floorSize +((i)*(2*floorSize)/3));
}
}
glDepthMask( 1.0 );
glDisable(GL_TEXTURE_2D);
glEnd();
}
在调用光线后发生地面绘制。 有谁知道为什么我的地面没有点亮?
这是我的问题的屏幕截图---&gt;
答案 0 :(得分:8)
问题是,默认的固定函数OpenGL光照仅在顶点处进行照明,然后只是插值得到的颜色。由于地面的所有4个顶点都是昏暗的,所以整个四边形都变暗了。
解决方案:
或
你正在遇到一个典型的OpenGL新手陷阱,BTW。
更新
旁注:你也没有为飞机提供法线。因此,照明根本不起作用。