#include<gl/glut.h>#include<math.h>#define PI 3.14159265358979323846#define PI2 6.28318530717958647692
GLsizei width =600,height =600;//球的大小int uStepsNum =50,vStepNum =50;//线的条数,从一个极点到另外一个极点的条数classPoint//点的数据结构{public:Point(){};Point(double a,double b,double c):x(a),y(b),z(c){};public:double x;double y;double z;//xyz三个坐标};
Point getPoint(double u,double v)//参数坐标(u,v)转换成时机坐标(x,y,z)函数{double x =sin(PI*v)*cos(PI2*u);double y =sin(PI*v)*sin(PI2*u);double z =cos(PI*v);returnPoint(x,y,z);//球面参数坐标的转换公式}voiddrawWire()//绘制球面{double ustep =1/(double)uStepsNum, vstep =1/(double)vStepNum;//离散化处理,设置增量大小double u =0,v =0;//初始化位置,v是球面点与原点的连线与z轴正向的夹角,u表示连线在xy平面的投影与x轴正向的夹角//绘制下端三角形组for(int i =0;i<uStepsNum;i++){glBegin(GL_LINE_LOOP);//GL_LINE_LOOP绘制的
Point a =getPoint(0,0);glVertex3d(a.x,a.y,a.z);//绘制三维空间坐标点
Point b =getPoint(u,vstep);glVertex3d(b.x,b.y,b.z);
Point c =getPoint(u+ustep,vstep);glVertex3d(c.x,c.y,c.z);
u += ustep;glEnd();}//下端三角形组主要是绘制极点附近的图形//绘制中间四边形组
u =0, v = vstep;for(int i=0;i<vStepNum-1;i++){for(int j=0;j<uStepsNum;j++){glBegin(GL_LINE_LOOP);
Point a =getPoint(u,v);
Point b =getPoint(u+ustep,v);
Point c =getPoint(u+ustep,v+vstep);
Point d =getPoint(u,v+vstep);glVertex3d(a.x,a.y,a.z);glVertex3d(b.x,b.y,b.z);glVertex3d(c.x,c.y,c.z);glVertex3d(d.x,d.y,d.z);
u += ustep;glEnd();}
v += vstep;}//绘制下端三角形组
u =0;for(int i=0;i<uStepsNum;i++){glBegin(GL_LINE_LOOP);
Point a =getPoint(0,1);
Point b =getPoint(u,1-vstep);
Point c =getPoint(u+ustep,1-vstep);glVertex3d(a.x,a.y,a.z);glVertex3d(b.x,b.y,b.z);glVertex3d(c.x,c.y,c.z);glEnd();}}voidinit(){glClearColor(0,0,1,1);//使用指定颜色清除颜色缓冲区glClearDepth(0.0);//指定深度缓冲区的清除值glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//要清除颜色缓冲以及深度缓冲glClearColor(0,1,1,1);glClearDepth(1.0);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat light_position []={1.0f,1.0f,1.0f,0.0f};//光源位置
GLfloat light_ambient []={0.2f,0.2f,0.2f,0.2f};//环境光
GLfloat light_diffuse []={0.5f,0.5f,0.5f,0.2f};//漫反射光
GLfloat light_specular []={0.5f,0.5f,0.5f,0.2f};//镜面反射光glLightfv(GL_LIGHT0, GL_POSITION, light_position);//创建指定的光源,light可以是GL_LIGHT0、GL_LIGHT1,pname定义的是光源的属性,它指定了一个命名参数。params表示表示pname属性将要被设置的值glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glEnable (GL_COLOR_MATERIAL);//开启材质颜料设置功能glEnable(GL_LIGHTING);//启动光源glEnable(GL_LIGHT0);//光源的颜色为白色glEnable(GL_AUTO_NORMAL);//执行后,图形能把光反射到各个方向
glEnable (GL_NORMALIZE);//根据函数glNormal的设置条件,启用法向量glEnable(GL_DEPTH_TEST);//启用深度测试。根据坐标的远近自动隐藏被遮住的图形(材料)
glDepthFunc (GL_LESS);//深度小的时候才渲染}voiddisplayFunc(){glMatrixMode(GL_MODELVIEW);//对模型视景矩阵操作glLoadIdentity();glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glColor3f(1.0,0.0,0.0);glPointSize(1.0);//指定栅格化点的直径glRotated(30,1,0,0);//相乘旋转矩阵的当前的矩阵glRotated(60,0,1,0);glRotated(90,0,0,1);drawWire();glutSwapBuffers();//交换两个缓冲区指针}intmain(int argc,char* argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowPosition(100,100);glutInitWindowSize(width,height);glutCreateWindow("傻逼球体");init();glutDisplayFunc(displayFunc);glutMainLoop();}