#include <glut.h>//the Sun, the Earth and the moon//let's say every month is 30 days .//12 months a year, a total of 360 daysStatic intDay = Max; changes in//day: from 0 to 359 voidMydisplay (void) {gldepthfunc (gl_lequal);//set depth <= through, related to the front and back of the objectGlenable (gl_depth_test);//Depth DetectionGlclear (Gl_color_buffer_bit |gl_depth_buffer_bit); Glmatrixmode (gl_projection);//The matrix is the projection transformation modeGlloadidentity ();//get the Unit matrixGluperspective ( the,1,1,400000000);//Perspective Transformation Angle 75 degrees, aspect ratio 1:1, the nearest visible distance 1, the farthest 4,000,000,002 times times the earth Revolution radiusGlmatrixmode (gl_modelview);//matrix is view modeglloadidentity (); Glulookat (0,-200000000,200000000,0,0,0,0,0,1);//overall layout, angle of view (here is 45 degrees inclination), object position, z-axis forward//Draw Red SunGLCOLOR3F (1.0f,0.0f,0.0f); Glutsolidsphere (69600000, -, -); //drawing the Blue EarthGLCOLOR3F (0.0f,0.0f,1.0f); Glrotatef ( Day/360.0* the,0.0f,0.0f,-1.0f); Gltranslatef (150000000,0.0f,0.0f); Glutsolidsphere (15945000, -, -); //draw a yellow "moon"GLCOLOR3F (1.0f,1.0f,0.0f); Glrotatef ( Day/30.0* the-day/360.0* the,0.0f,0.0f,-1); Gltranslatef (38000000,0.0f,0.0f); Glutsolidsphere (4345000, -, -); Glflush ();}intMainintargcChar*argv[]) {Glutinit (&argc, argv); Glutinitdisplaymode (Glut_rgb | glut_single); Glutinitwindowposition ( -, -); Glutinitwindowsize ( -, -); Glutcreatewindow ("Celestial Bodies"); Glutdisplayfunc (&mydisplay); Glutmainloop (); return 0;}
Effect:
To animate and use a double cache:
#include <glut.h>//the Sun, the Earth and the moon//let's say every month is 30 days .//12 months a year, a total of 360 daysStatic intDay = Max;//changes in day: from 0 to 359voidMydisplay (void) {gldepthfunc (gl_lequal);//set depth <= through, related to the front and back of the objectGlenable (gl_depth_test);//Depth DetectionGlclear (Gl_color_buffer_bit |gl_depth_buffer_bit); Glmatrixmode (gl_projection);//The matrix is the projection transformation modeGlloadidentity ();//get the Unit matrixGluperspective ( the,1,1,400000000);//Perspective Transformation Angle 75 degrees, aspect ratio 1:1, the nearest visible distance 1, the farthest 4,000,000,002 times times the earth Revolution radiusGlmatrixmode (gl_modelview);//matrix is view modeglloadidentity (); Glulookat (0,-200000000,200000000,0,0,0,0,0,1);//overall layout, angle of view position (here is 45 degrees inclination), object position, z-axis forward//Draw Red SunGLCOLOR3F (1.0f,0.0f,0.0f); Glutsolidsphere (69600000, -, -); //drawing the Blue EarthGLCOLOR3F (0.0f,0.0f,1.0f); Glrotatef ( Day/360.0* the,0.0f,0.0f,-1.0f); Gltranslatef (150000000,0.0f,0.0f); Glutsolidsphere (15945000, -, -); //draw a yellow "moon"GLCOLOR3F (1.0f,1.0f,0.0f); Glrotatef ( Day/30.0* the-day/360.0* the,0.0f,0.0f,-1); Gltranslatef (38000000,0.0f,0.0f); Glutsolidsphere (4345000, -, -); Glflush (); glutswapbuffers ();}voidMyidle () {++Day ; if(day>= the) Day=0; Mydisplay ();}intMainintargcChar*argv[]) {Glutinit (&argc, argv); Glutinitdisplaymode (Glut_rgb | glut_double); Glutinitwindowposition ( -, -); Glutinitwindowsize ( the, the); Glutcreatewindow ("Celestial Bodies"); Glutdisplayfunc (&mydisplay); Glutidlefunc ( & myidle); Glutmainloop (); return 0;}
OpenGL Learning Note III (simple example Sun Moon Earth)