#include <gl/glut.h>
#include <windows.h>
GLfloat light_position[]={0.0,3.0,6.0,0.0};
GLfloat material_diffuse[]={0.8,0.2,0.5,1.0};
GLfloat material_specular[]={1.0,1.0,1.0,1.0};
GLfloat no_shininess[]={0.0};
GLfloat low_shininess[]={5.0};
GLfloat high_shininess[]={100.0};
GLfloat no_mat[]={0.0,0.0,0.0,1.0};
GLfloat mat_grey_ambient[]={0.5,0.5,0.5,1.0};
GLfloat mat_red_ambient[]={0.0,0.0,1.0,1.0};
void Initial(void)
{
glLightfv(GL_LIGHT2,GL_POSITION,light_position);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,material_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,material_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,high_shininess);
glMaterialfv(GL_FRONT,GL_EMISSION,no_mat);
glColorMaterial(GL_FRONT,GL_AMBIENT);
glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
glColor3fv(no_mat);//glColor3f(1.0,0.0,0.0);等价于
//float color_array[]={1.0,0.0,0.0};
//glColor3fv(color_array);
glTranslatef(-2.5,1.5,0.0);
glRotatef(15.0,1.0,0.0,0.0);//void glRotatef(float angle,float x,float y,float z)分别代表 角度 X ,Y Z的旋转
glutSolidSphere(1.2,20.0,20.0);
glPopMatrix();
glPushMatrix();
glColor3fv(mat_grey_ambient);
glRotatef(15.0,1.0,0.0,0.0);
glutSolidSphere(1.2,20.0,20.0);
glPopMatrix();
glPushMatrix();
glColor3fv(mat_red_ambient);
glTranslatef(2.5,-1.5,0.0);
glRotatef(15.0,1.0,0.0,0.0);
glutSolidSphere(1.2,20.0,20.0);
glPopMatrix();
glPopMatrix();
glFlush();
}
void myReshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.5,5.5,-5.5,5.5,-5.5,5.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(100,100);
glutCreateWindow("LIGHT&Material");
Initial();
glutReshapeFunc(myReshape);
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
三个不同材质球体
最新推荐文章于 2021-04-03 16:15:21 发布
这篇博客通过OpenGL展示了如何创建并展示三个具有不同材质和光照效果的球体。使用了glutSolidSphere函数绘制球体,并通过glMaterialfv设置材质参数,包括漫射、镜面反射和光泽度。此外,还设置了光源位置并启用光照和深度测试。
摘要由CSDN通过智能技术生成