Computer Graphics OpenGL-Solar System

Source: Internet
Author: User

In implementation, I use the OpenGL function library and some important header files: glut. H, windows. H, math. H, etc. Use the custom macro deg_to_rad. Then, use textureimage to define the externally referenced graphical variables required by the program. Then, use glunit to create a textureobjects object. The details are as follows: # Include <Gl/glut. h> # Include <windows. h> # Include <mmsystem. h> # Include <math. h> # Include "RF. H" # Include "MF. H" # Include "draw. H" # Define deg_to_rad 0.017453  Static gluint texname; Bool g_borbiton = true; Float g_fspeedmodifier = 1.0f; Float g_felpasedtime; Double g_dcurrenttime; Double g_dlasttime;  Static gldouble viewer [] = {-sin (10 * deg_to_rad), 10 * Cos (10 * deg_to_rad),-25};/* Initial viewer location */ Static int year = 0, Day = 0; Float angles = 0.0;The material used in the program defines the physical performance of the image to be constructed, including ambient light, diffuse light, mirror light, and light source position: # Define num_textures 10 Gluint textureobjects [num_textures];  Material material [num_textures]; // Illumination Light = { {0.0, 0.0, 0.0, 1.0 },// Environment light Parameters {1.0, 1.0, 1.0, 1.0 },// Diffuse Light Parameters {1.0, 1.0, 1.0, 1.0 },// Mirror Parameters {0.0, 0.0, 0.0, 1.0 }// Light source location }; // Material Material materialspace = { {0.4, 0.4, 0.4, 1.0 }, {0.0, 0.0, 0.0, 1.0 }, {0.0, 0.0, 0.0, 1.0 }, 0.0 };The initialization Init () and preprocessing of the image are as follows: Void Init () { Glclearcolor (0.0, 0.0, 0.0, 0.0 ); Glenable (gl_depth_test ); Glshademodel (gl_flat );  Material [0] = materialsun; Material [1] = materialearth; Material [2] = materialmoon; Material [3] = materialspace;  Glfloat light_ambient [] = {1.0, 1.0, 1.0, 1.0 }; Glfloat light_diffuse [] = {1.0, 1.0, 1.0, 1.0 }; Glfloat light_specular [] = {1.0, 1.0, 1.0, 1.0 }; Gllightfv (gl_light0, gl_ambient, light_ambient ); Gllightfv (gl_light0, gl_diffuse, light_diffuse ); Gllightfv (gl_light0, gl_specular, light_specular ); Glable (gl_lighting ); Glable (gl_light0 ); }  Void reshape (int w, int H) { Glviewport (0, 0, (glfloat) W, (glfloat) H ); Glmatrixmode (gl_projection ); Glloadidentity (); Gluperspective (60.0, (glfloat) W/(glfloat) h, 1.0, 200.0 ); Glmatrixmode (gl_modelview ); Glloadidentity (); Glulookat (viewer [0], viewer [1], viewer [2], 0, 0, 0, 0, 1 ); } Void shutdownrc (void) { // Delete the textures Gldeletetextures (num_textures, textureobjects ); }Then the display () function is used to plot the entire solar system: Glloadidentity (); // Set the position and direction of the observation point Glulookat (viewer [0], viewer [1], viewer [2], 0, 0, 0, 0, 1 );  // Obtain system time to make the Solar System Dynamic G_dcurrenttime = timegettime (); G_felpasedtime = (float) (g_dcurrenttime-g_dlasttime) * 0.001 ); G_dlasttime = g_dcurrenttime;  Glfloat light_position [] = {0.0, 0.0, 0.0, 1.0 };Then initialize the rotation speed of the Sun and the rotation and revolution speed of other planets. For example: Static float fsunspin = 0.0f ;// Sun rotation speed Static float fmercuspin = 0.0f ;// Mercury rotation speed Static float fmercuorbit = 0.0f ;// Mercury revolution speed  Static float fjupiterspin = 0.0f ;// Jupiter rotation speed Static float fjupiterorbit = 0.0f ;// Jupiter's public transfer speed  Static float fsaturnspin = 0.0f ;// Saturn's rotation speed Static float fsaturnorbit = 0.0f ;// Saturn revolution speed  Static float fplutospin = 0.0f ;// Pluto rotation speed Static float fplutoorbit = 0.0f ;// Pluto revolution speed  // Static float fspace = 0.0f ;// Space Rotation  Float spacewidth = 28.50; Float spacedepth = 18.0;Then define the distance between each planet: for example: Static float mercutosun =-3.0 ;// Distance from the sun Static float ventosun =-5.0 ;// Distance between Venus and the sun Static float neptunetosun =-15.0 ;// Distance from Neptune to the sun Static float plutosun =-17.0 ;// The distance from Pluto to the sunThen there are specific limitations on the speed of each planet to reflect the specificity and authenticity of each planet. Next, we enter the actual plotting phase for each planet: first, the whole space, then the sun, and then draw one by one based on the actual situation of the solar system. For example, Uranus: // The location, size, speed, and texture of Uranus are roughly the same as those of the Earth. // Uranus uses roughly the same material as Earth. Glcolor3f (192,122, 24 ); Glpushmatrix (); Glrotatef (furanusorbit, 0.0, 1.0, 0.0 ); Gltranslatef (0.0, 0.0, uranustosun ); Glrotatef (furanusspin, 0.0, 1.0, 0.0 ); Glrotatef (0.0, 0.0 ); Mf (uranusimg, & textureobjects [0]); Gltdrawsphere (0.5, 150,200 ); Glpopmatrix ();  Glbegin (gl_line_loop ); For (angle = 0; angle <= 360; angle ++) Glvertex3f (uranustosun * sin (deg_to_rad * angle), 0, uranustosun * Cos (deg_to_rad * angle )); Glend (); Gldeletetextures (1, & textureobjects [0]);The next step is to control the keyboard () and mouse () of user input, so as to realize the conversion of the Angle of View and the control of the rotation speed of the planet, so that users can have good performance. Then the main function main (): In this function, the "skin" image of the planet to be drawn is first loaded to make the planet more authentic. Then, call the initialization function to initialize the image, space, and form. Then, we call the previous function to draw images and ultimately implement the Solar System: Gluinit (& argc, argv ); Fig ); Gluinitwindowsize (800,600 ); Valley createwindow ("Sean "); Init (); Gludisplayfunc (Display ); Glutidlefunc (Display ); Glureshapefunc (reshape ); Glukeyboardfunc (keyboard ); Glumousefunc (Mouse ); Glumainloop (); Shutdownrc ();


Related Article

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.