我有一个应该模仿阳光的光源,但效果不是很好。目前正从每个物体后面稍微传来,就像在 4 点钟位置。我想为不同的对象设置不同的光泽度和材质,但是即使当我更改 1 个参数或添加新材质时功能是分开的,它似乎也会为渲染场景中的每个对象更改它
void setLight(void)
{
GLfloat L0position[]={30.0, 10.0, 40.0, 1.0};
GLfloat whitelight[]={1.0, 1.0, 1.0, 1.0}; // white
GLfloat blacklight[]={0.0, 0.0, 0.0, 1.0}; // black
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amblight); //background lighting
glLightfv(GL_LIGHT0, GL_AMBIENT, blacklight);
glLightfv(GL_LIGHT0, GL_SPECULAR, whitelight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, whitelight);
glLightfv(GL_LIGHT0, GL_POSITION, L0position);
//glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100);
//glLightf(GL_LIGHT0,GL_SHININESS,100);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
例如,我有 drawknight() 和 drawdragon() 函数,当我更改它们的材质值时,它也会影响其他对象,即使我最后禁用了“GL_COLOR_MATERIAL”(drawknight 与 drawdragon 几乎相同)
void drawKnight() {
GLfloat specular[]={1, 1, 1,1};
GLfloat emission[]={0, 0, 0,1};
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_BACK, GL_SPECULAR, specular);
glMaterialfv(GL_BACK, GL_EMISSION, emission);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 1000000);
glColor3f(0.99f, 0.765f, 0.890f);
// Draw Body
glPushMatrix();
glTranslatef(0.0f ,0.0f, 0.0f);
glRotatef(270.0f,1.0f, 0.0f, 0.0f);
gluCylinder(objCylinder, 1,2, 3, 10, 2);
glPopMatrix();
// Draw Head
glPushMatrix();
glTranslatef(0.0f, 4.0f, 0.0f);
glRotatef(270.0f,1.0f, 0.0f, 0.0f);
glutSolidSphere(2,20,20);
glPopMatrix();
//Draw sword
glPushMatrix();
glColor3f(0.0f,0.0f,1.0f);
glTranslatef(-2.1f, 2.0f, 0.0f);
glScalef(0.5,0.5,0.5);
glRotatef(240.0f,1.0f, 0.0f, 0.0f);
glutSolidCone(2,10,10,10);
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
}
这里是渲染场景
void renderScene(void) {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
setLight();
GLfloat specular[]={0.3, 0.3, 0.3};
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_BACK, GL_SPECULAR, specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0);
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-500.0f, 0.0f, -500.0f);
glVertex3f(-500.0f, 0.0f, 500.0f);
glVertex3f( 500.0f, 0.0f, 500.0f);
glVertex3f( 500.0f, 0.0f, -500.0f);
glEnd();
glPushMatrix();
glTranslatef(75,0,-80);
glRotatef(270.0f,1.0f, 0.0f, 0.0f);
glColor3f(1.0f,0.4f,0.2f);
gluCylinder(objCylinder, 5, 5, 10, 10, 2);
glPopMatrix();
.
.
.
//KNIGHT
glPushMatrix();
glTranslatef(x_knight, y_knight,z_knight);
drawKnight();
glPopMatrix();
glutSwapBuffers();
}
如何解决这个问题,如何设置每个对象的材料和光泽度?