OpenGL Learning Note III (simple example Sun Moon Earth)

Source: Internet
Author: User

#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.