OpenGl大作业

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

八大行星与太阳代码:#includeglut.h#includemath.h//LightingvaluesGLfloatwhiteLight[]={0.2f,0.2f,0.2f,1.0f};GLfloatsourceLight[]={0.8f,0.8f,0.8f,1.0f};GLfloatlightPos[]={0.0f,0.0f,0.0f,1.0f};//EarthandMoonangleofrevolutionGLfloatfMoonRot=10.0f;GLfloatfEarthRot=29.786f;GLfloatfMercuryRot=47.874f;GLfloatfVenusRot=35.024f;GLfloatfMarsRot=24.131f;GLfloatfJupiterRot=13.059f;GLfloatfSaturnRot=9.644f;GLfloatfUranusRot=6.799f;GLfloatfNeptuneRot=5.433f;voidNeptune(){glColor3ub(162,56,90);glutSolidSphere(22.0f,30,30);}voidUranus(){glColor3ub(146,48,47);glutSolidSphere(22.5f,30,30);}voidSaturn(){glColor3ub(100,190,59);glutSolidSphere(29.0f,30,30);}voidJupiter(){glColor3ub(49,100,112);glutSolidSphere(30.0f,30,30);}voidMars(){glColor3ub(40,90,10);glutSolidSphere(7.50f,30,30);}voidVenus(){glColor3ub(46,112,12);glutSolidSphere(7.0f,30,30);}voidMercury(){glColor3ub(18,46,19);glutSolidSphere(5.0f,30,30);}voidMoon(){glColor3ub(0,200,0);glutSolidSphere(5.0f,30,30);}voidEarth(){glColor3ub(46,10,0);glutSolidSphere(15.0f,30,30);glPushMatrix();glRotatef(fMoonRot,0.0f,1.0f,0.0f);glTranslatef(25.0f,0.0f,0.0f);Moon();glPopMatrix();}voidSolar(){glColor3ub(255,63,0);glutSolidSphere(40.0f,30,30);glPushMatrix();glRotatef(fMercuryRot,0.0f,1.0f,0.09f);glTranslatef(48.50f,0.0f,0.0f);Mercury();glPopMatrix();glPushMatrix();glRotatef(fVenusRot,0.0f,1.0f,0.07f);glTranslatef(54.0f,0.0f,0.0f);Venus();glPopMatrix();glPushMatrix();glRotatef(fEarthRot,0.0f,1.0f,0.05f);glTranslatef(69.50f,0.0f,0.0f);Earth();glPopMatrix();glPushMatrix();glRotatef(fMarsRot,0.0f,1.0f,0.09f);glTranslatef(118.50f,0.0f,0.0f);Mars();glPopMatrix();glPushMatrix();glRotatef(fJupiterRot,0.0f,1.0f,.06f);glTranslatef(778.0f/2,0.0f,0.0f);Jupiter();glPopMatrix();glPushMatrix();glRotatef(fSaturnRot,0.0f,1.0f,0.12f);glTranslatef(1426.0f/6,0.0f,0.0f);Saturn();glPopMatrix();glPushMatrix();glRotatef(fUranusRot,0.0f,1.0f,0.08f);glTranslatef(2870.0f/6,0.0f,0.0f);Uranus();glPopMatrix();glPushMatrix();glRotatef(fNeptuneRot,0.0f,1.0f,0.14f);glTranslatef(4497.0f/6,0.0f,0.0f);Neptune();glPopMatrix();}//Calledtodrawscenevoiddisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glPushMatrix();glTranslatef(0.0f,0.0f,-400.0f);Solar();glPopMatrix();glutSwapBuffers();}//Thisfunctiondoesanyneededinitializationontherendering//context.voidinit(){//LightvaluesandcoordinatesglEnable(GL_DEPTH_TEST);//HiddensurfaceremovalglFrontFace(GL_CCW);//Counterclock-wisepolygonsfaceoutglEnable(GL_CULL_FACE);//Donotcalculateinsideofjet//EnablelightingglEnable(GL_LIGHTING);//Setupandenablelight0glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);glLightfv(GL_LIGHT0,GL_POSITION,lightPos);glEnable(GL_LIGHT0);//EnablecolortrackingglEnable(GL_COLOR_MATERIAL);//SetMaterialpropertiestofollowglColorvaluesglColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);//BlackbluebackgroundglClearColor(0.0f,0.0f,0.0f,1.0f);}voidmove(void){fMoonRot+=0.50f;if(fMoonRot360.0f)fMoonRot=0.0f;//Stepearthorbit.25degreesfEarthRot+=.29786f;if(fEarthRot360.0f)fEarthRot=0.0f;fMercuryRot+=.47874f;if(fMercuryRot360.0f)fMercuryRot=0.0f;fVenusRot+=.35024f;if(fVenusRot360.0f)fVenusRot=0.0f;fMarsRot+=.24131f;if(fMarsRot360.0f)fMarsRot=0.0f;fJupiterRot+=.13059f;if(fJupiterRot360.0f)fJupiterRot=0.0f;fSaturnRot+=.09644f;if(fSaturnRot360.0f)fSaturnRot=0.0f;fUranusRot+=.06799f;if(fUranusRot360.0f)fUranusRot=0.0f;fNeptuneRot+=.05433f;if(fNeptuneRot360.0f)fNeptuneRot=0.0f;glutPostRedisplay();}voidreshape(intw,inth){GLfloatfAspect;//Preventadividebyzeroif(h==0)h=1;//SetViewporttowindowdimensionsglViewport(0,0,w,h);//CalculateaspectratioofthewindowfAspect=(GLfloat)w/(GLfloat)h;//SettheperspectivecoordinatesystemglMatrixMode(GL_PROJECTION);glLoadIdentity();//fieldofviewof90degrees,nearandfarplanes1.0and425gluPerspective(90.0f,fAspect,1.0,1000.0);//ModelviewmatrixresetglMatrixMode(GL_MODELVIEW);glLoadIdentity();}voidInitAWindow(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);glutInitWindowSize(800,800);glutCreateWindow(SunSystem:theeightandtheone);return;}intmain(intargc,char*argv[]){InitAWindow(argc,argv);glutReshapeFunc(reshape);glutDisplayFunc(display);glutIdleFunc(move);init();glutMainLoop();return0;}

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功