您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > opengl实验四【太阳系】
学院专业班学号姓名:教师评定:#includestdlib.h#includeGL/glut.h#includeGL/glaux.h#includestdio.h#includemath.h#pragmacomment(lib,glaux.lib)//将glaux.lib连接到工程enum//枚举鼠标变量{BUTTON_LEFT,//鼠标左键};intmButton=-1;intmOldY,mOldX;floatz=0,c=0;floateye[3]={0.0f,0.0f,2.0f};//观察视角floatrot[3]={45.0f,45.0f,0.0f};//旋转变量constintGL_WIN_WIDTH=800;//窗口大小constintGL_WIN_HEIGHT=600;constintGL_WIN_INITIAL_X=0;constintGL_WIN_INITIAL_Y=0;constintn=1000,m=20;constfloatR=1.2f,r=0.15f;//半径constfloatPi=3.1415926536f;GLuintg_TexturesArray[7];//纹理指针intiMode=1;voidProcessMenu(intvalue)//弹出处理函数{iMode=value;glutPostRedisplay();//强制刷新显示}voidCreatePopMenu()//生成弹出式菜单函数{intnMainMenu;//主菜单变量nMainMenu=glutCreateMenu(ProcessMenu);//生成一个主菜单并指定菜单处理函数glutAddMenuEntry(填充,1);//添加一个主菜单glutAddMenuEntry(轮廓,2);//添加一个主菜单glutAttachMenu(GLUT_RIGHT_BUTTON);//指定激活菜单的鼠标右键}boolLoadBMP(char*filename,GLuint&texture)//调贴图大小为2的幂{AUX_RGBImageRec*pImage=NULL;pImage=auxDIBImageLoad(filename);//装入位图if(pImage==NULL)returnfalse;//位图没装入返回错误glGenTextures(1,&texture);//生成贴图(纹理)glBindTexture(GL_TEXTURE_2D,texture);//捆绑贴图(纹理)gluBuild2DMipmaps(GL_TEXTURE_2D,4,//建立图形pImage-sizeX,//图形宽pImage-sizeXpImage-sizeY,//图形高pImage-sizeY,GL_RGB,GL_UNSIGNED_BYTE,pImage-data//图形数据);free(pImage-data);//释放位图数据占据的内存资源free(pImage);returntrue;//返回成功}voidInit(){glEnable(GL_DEPTH_TEST);//开启深度缓冲glClearColor(0.0,0.0,0.0,1.0);glEnable(GL_TEXTURE_2D);//启用二维文理//----------------载入图片--------------LoadBMP(data/11.bmp,g_TexturesArray[0]);LoadBMP(data/12.bmp,g_TexturesArray[1]);LoadBMP(data/13.bmp,g_TexturesArray[2]);LoadBMP(data/14.bmp,g_TexturesArray[3]);LoadBMP(data/15.bmp,g_TexturesArray[4]);LoadBMP(data/16.bmp,g_TexturesArray[5]);LoadBMP(data/17.bmp,g_TexturesArray[6]);}voidglutResize(intwidth,intheight)//重置OpenGL窗口大小{glViewport(0,0,width,height);//重置当前视口glMatrixMode(GL_PROJECTION);//选择投影矩阵glLoadIdentity();//重置投影矩阵gluPerspective(45.0,(float)width/(float)height,1.0,300.0);//设置视口大小}voidglutDisplay(void)//显示函数{glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);//表示物体的前向和后向面用填充面显示glPushMatrix();//入栈glTranslatef(-eye[0],-eye[1],-10);//视口glBindTexture(GL_TEXTURE_2D,g_TexturesArray[0]);//----------------背景------------glBegin(GL_QUADS);glTexCoord2f(1.0f,0.0f);glVertex3f(8,-8,0);glTexCoord2f(1.0f,1.0f);glVertex3f(8,8,0);glTexCoord2f(0.0f,1.0f);glVertex3f(-8,8,0);glTexCoord2f(0.0f,0.0f);glVertex3f(-8,-8,0);glEnd();glPopMatrix();//出栈glPushMatrix();//入栈glTranslatef(-eye[0],-eye[1],-eye[2]);glRotatef(rot[0],1.0f,0.0f,0.0f);glRotatef(rot[1],0.0f,1.0f,0.0f);glRotatef(rot[2],0.0f,0.0f,1.0f);//---------------太阳--------------if(iMode==1){GLUquadricObj*quadric1;//建立一个曲面对象指针quadric1=gluNewQuadric();//建立一个曲面对象指针gluQuadricTexture(quadric1,GLU_TRUE);//建立纹理坐标gluQuadricDrawStyle(quadric1,GLU_FILL);//用面填充绘制场景glBindTexture(GL_TEXTURE_2D,g_TexturesArray[1]);//绑定纹理gluSphere(quadric1,0.3f,64,64);//画一个填充太阳}elseif(iMode==2){glDisable(GL_TEXTURE_2D);//取消纹理glColor3f(1.0,1.0,1.0);glutWireSphere(0.3f,32,32);//画一个线条太阳glEnable(GL_TEXTURE_2D);//启用纹理}//-----------土星公转轨道------------glDisable(GL_TEXTURE_2D);//取消纹理glColor3f(1.0,1.0,1.0);glBegin(GL_LINE_LOOP);for(intj=0;jn;j++)glVertex3f(R*cos(2*Pi/n*j),0,R*sin(2*Pi/n*j));glEnd();glEnable(GL_TEXTURE_2D);//启用纹理//-----------地球公转轨道---------------glDisable(GL_TEXTURE_2D);//取消纹理glColor3f(1.0,1.0,1.0);glBegin(GL_LINE_LOOP);for(j=0;jn;j++)glVertex3f(0.7*cos(2*Pi/n*j),0,0.7*sin(2*Pi/n*j));glEnd();glEnable(GL_TEXTURE_2D);//启用纹理//------------------土星---------------------------glPushMatrix();//入栈glRotatef(z,0.0f,1.0f,0.0f);glTranslatef(1.2,0,0);glPushMatrix();//入栈glRotatef(c,0.0f,1.0f,0.0f);glRotatef(90,1.0f,0.0f,0.0f);if(iMode==1){GLUquadricObj*quadric2;////建立一个曲面对象指针quadric2=gluNewQuadric();//建立一个曲面对象指针gluQuadricTexture(quadric2,GLU_TRUE);//建立纹理坐标gluQuadricDrawStyle(quadric2,GLU_FILL);//用面填充绘制场景glBindTexture(GL_TEXTURE_2D,g_TexturesArray[2]);//绑定纹理gluSphere(quadric2,0.12f,64,64);//画一个填充土星}elseif(iMode==2){glDisable(GL_TEXTURE_2D);//取消纹理glColor3f(1.0,1.0,1.0);glutWireSphere(0.12f,25,25);//画一个线条土星glEnable(GL_TEXTURE_2D);//启用纹理}glPopMatrix();//出栈//---------------土星一号卫星-----------------glPushMatrix();//入栈glRotatef(c,0.0f,0.0f,1.0f);glTranslatef(0.25,0,0);if(iMode==1){GLUquadricObj*quadric3;////建立一个曲面对象指针quadric3=gluNewQuadric();//建立一个曲面对象指针gluQuadricTexture(quadric3,GLU_TRUE);//建立纹理坐标gluQuadricDrawStyle(quadric3,GLU_FILL);//用面填充绘制场景glBindTexture(GL_TEXTURE_2D,g_TexturesArray[3]);//绑定纹理gluSphere(quadric3,0.03f,64,64);//画一个填充卫星}elseif(iMode==2){glDisable(GL_TEXTURE_2D);//取消纹理glColor3f(1.0,1.0,1.0);glutWireSphere(0.03f,25,25);//画一个线条卫星glEnable(GL_TEXTURE_2D);//启用纹理}glPopMatrix();//出栈//-----------------土星一号卫星轨道--------------glDisable(GL_TEXTURE_2D);//取消纹理glColor3f(1.0,1.0,1.0);glBegin(GL_LINE_LOOP);for(j=0;jn;j++)glVertex3f(0.25*cos(2*Pi/n*j),0.25*sin(2*Pi/n*j),0);glEnd();glEnable(GL_TEXTURE_2D);//启用纹理//------------------土星二号卫星--------------------glPushMatrix();//入栈glRotatef(c,1.0f,0.0f,0.0f);glTranslatef(0,0,0.25);if(iMode==1){GLUquadricObj*quadric4;////建立一个曲面对象指针quadric4=gl
本文标题:opengl实验四【太阳系】
链接地址:https://www.777doc.com/doc-5155703 .html