OpenGL模拟宇宙场景,可是无法显示材质
OpenGL模拟宇宙场景,可是无法显示材质,背景是贴图,三个球是加了材质的,但是一旦运行,三个球的颜色一闪而过,变了黑色。。。各位大侠。。。求指教啊。。。我可是在贴图那里加了法线的啊。。。
主要代码:
GLfloat light1_position[] = {2.0f, 5.0f, 2.0f, 1.0f};
GLfloat light1_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,0.01);
glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.01);
glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION,0.10);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
//3个行星
glPushMatrix();
GLfloat ball1_mat_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat ball1_mat_diffuse[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat ball1_mat_specular[] = {0.0f, 0.8f, 1.0f, 1.0f};
GLfloat ball1_mat_emission[] = {0.0f, 0.8f, 0.0f, 1.0f};
GLfloat ball1_shininess = 500.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, ball1_mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, ball1_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, ball1_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, ball1_mat_emission);
glMaterialf (GL_FRONT, GL_SHININESS, ball1_shininess);
glRotatef(day,0.0,0.0,1.0);
glTranslatef(1.0f, 3.0f, 4.0f);
glutSolidSphere(0.5, 20, 20);
GLfloat ball2_mat_ambient[] = {0.2f, 0.0f, 0.2f, 1.0f};
GLfloat ball2_mat_diffuse[] = {0.2f, 0.0f, 0.2f, 1.0f};
GLfloat ball2_mat_specular[] = {0.2f, 0.0f, 1.0f, 1.0f};
GLfloat ball2_mat_emission[] = {0.2f, 0.0f, 0.2f, 1.0f};
GLfloat ball2_shininess = 200.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, ball2_mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, ball2_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, ball2_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, ball2_mat_emission);
glMaterialf (GL_FRONT, GL_SHININESS, ball2_shininess);
glRotatef(day,0.0,1.0,0.0);
glTranslatef(-1.0f, -1.0f, 2.0f);
glRotatef(year,0.0,1.0,0.0);
glutSolidSphere(0.5, 20, 20);
GLfloat ball3_mat_ambient[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat ball3_mat_diffuse[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat ball3_mat_specular[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat ball3_mat_emission[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat ball3_shininess = 1000.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, ball3_mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, ball3_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, ball3_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, ball3_mat_emission);
glMaterialf (GL_FRONT, GL_SHININESS, ball3_shininess);
glTranslatef(-5.0f, -5.0f, -2.0f);
glRotatef(month,0.0,1.0,0.0);
glutSolidSphere(0.5, 20, 20);
glPopMatrix();glPopMatrix();glPopMatrix();
glPushMatrix();
//背景图加载,因为希望和第一个行星一起运动:
GLfloat light2_position[] = {2.0f, 5.0f, -2.0f, 1.0f};
GLfloat light2_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
glLightf(GL_LIGHT2,GL_CONSTANT_ATTENUATION,0.01);
glLightf(GL_LIGHT2,GL_LINEAR_ATTENUATION,0.01);
glLightf(GL_LIGHT2,GL_QUADRATIC_ATTENUATION,0.10);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHTING);
glRotatef(year,0.0,1.0,0.0);
glBindTexture(GL_TEXTURE_2D, texback);
glBegin(GL_QUADS);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-8.0f, -8.0f, -2.0f) ;
glTexCoord2f(0.0f, 1.0f); glVertex3f(-8.0f, 8.0f, -2.0f) ;
glTexCoord2f(1.0f, 1.0f); glVertex3f(8.0f, 8.0f, -2.0f) ;
glTexCoord2f(1.0f, 0.0f); glVertex3f(8.0f, -8.0f, -2.0f) ;
glEnd();
grab() ;
glPopMatrix();
glutSwapBuffers();
glFlush();
主要代码:
GLfloat light1_position[] = {2.0f, 5.0f, 2.0f, 1.0f};
GLfloat light1_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,0.01);
glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0.01);
glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION,0.10);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
//3个行星
glPushMatrix();
GLfloat ball1_mat_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat ball1_mat_diffuse[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat ball1_mat_specular[] = {0.0f, 0.8f, 1.0f, 1.0f};
GLfloat ball1_mat_emission[] = {0.0f, 0.8f, 0.0f, 1.0f};
GLfloat ball1_shininess = 500.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, ball1_mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, ball1_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, ball1_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, ball1_mat_emission);
glMaterialf (GL_FRONT, GL_SHININESS, ball1_shininess);
glRotatef(day,0.0,0.0,1.0);
glTranslatef(1.0f, 3.0f, 4.0f);
glutSolidSphere(0.5, 20, 20);
GLfloat ball2_mat_ambient[] = {0.2f, 0.0f, 0.2f, 1.0f};
GLfloat ball2_mat_diffuse[] = {0.2f, 0.0f, 0.2f, 1.0f};
GLfloat ball2_mat_specular[] = {0.2f, 0.0f, 1.0f, 1.0f};
GLfloat ball2_mat_emission[] = {0.2f, 0.0f, 0.2f, 1.0f};
GLfloat ball2_shininess = 200.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, ball2_mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, ball2_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, ball2_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, ball2_mat_emission);
glMaterialf (GL_FRONT, GL_SHININESS, ball2_shininess);
glRotatef(day,0.0,1.0,0.0);
glTranslatef(-1.0f, -1.0f, 2.0f);
glRotatef(year,0.0,1.0,0.0);
glutSolidSphere(0.5, 20, 20);
GLfloat ball3_mat_ambient[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat ball3_mat_diffuse[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat ball3_mat_specular[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat ball3_mat_emission[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat ball3_shininess = 1000.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, ball3_mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, ball3_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, ball3_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, ball3_mat_emission);
glMaterialf (GL_FRONT, GL_SHININESS, ball3_shininess);
glTranslatef(-5.0f, -5.0f, -2.0f);
glRotatef(month,0.0,1.0,0.0);
glutSolidSphere(0.5, 20, 20);
glPopMatrix();glPopMatrix();glPopMatrix();
glPushMatrix();
//背景图加载,因为希望和第一个行星一起运动:
GLfloat light2_position[] = {2.0f, 5.0f, -2.0f, 1.0f};
GLfloat light2_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
glLightf(GL_LIGHT2,GL_CONSTANT_ATTENUATION,0.01);
glLightf(GL_LIGHT2,GL_LINEAR_ATTENUATION,0.01);
glLightf(GL_LIGHT2,GL_QUADRATIC_ATTENUATION,0.10);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHTING);
glRotatef(year,0.0,1.0,0.0);
glBindTexture(GL_TEXTURE_2D, texback);
glBegin(GL_QUADS);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-8.0f, -8.0f, -2.0f) ;
glTexCoord2f(0.0f, 1.0f); glVertex3f(-8.0f, 8.0f, -2.0f) ;
glTexCoord2f(1.0f, 1.0f); glVertex3f(8.0f, 8.0f, -2.0f) ;
glTexCoord2f(1.0f, 0.0f); glVertex3f(8.0f, -8.0f, -2.0f) ;
glEnd();
grab() ;
glPopMatrix();
glutSwapBuffers();
glFlush();
作者: LOU09020207YES 发布时间: 2011-06-15
对于初学者,最好是在一些示例程序上做改动。
http://www.codesampler.com/code/
http://www.codesampler.com/code/
作者: dizuo 发布时间: 2011-06-15