The following demo also uses a variety of combination transformations to achieve the earth around the sun and rotation of the realization, or directly look at the code, there are detailed comments.
<span style= "FONT-SIZE:12PX;" >////main.cpp//opengl_11_planet////Created by Apple on 15/1/20.//Copyright (c) 2015 CC. All rights reserved.//#include <iostream> #include <glut/glut.h>static int year = 0, day = 0;/** * Initialize Operation */voi D init () {//Set clear screen color Glclearcolor (0.0f, 0.0f, 0.0f, 0.0f); Sets the shading mode, the fill color is consistent with the vertex glshademodel (Gl_flat);} /** * Display Drawing effect */void display () {//Cleanup color buffer glclear (gl_color_buffer_bit); Set Paint color glcolor3f (1.0, 1.0, 1.0); Copy the current matrix and save it to the top of the stack Glpushmatrix (); Render a sun, radius 1.0f glutwiresphere (1.0f, 20.0f, 16.0f); Rotate the year degree around the Y axis, at which time the local coordinate system of the earth is inconsistent with the global coordinate system, showing the Revolution Glrotatef ((glfloat) year, 0.0f, 1.0f, 0.0f); Move 2 unit lengths along the x-axis, at which point the earth's local coordinate system is offset with the global coordinate system Gltranslatef (2.0f, 0.0f, 0.0f); Rotates the day degree along the Y axis, at which time the local coordinate system of the earth is consistent with the global coordinate system, showing the Glrotatef ((glfloat) day, 0.0f, 1.0f, 0.0f); Draw a globe with a radius of 0.2f glutwiresphere (0.2f, 10.0f, 8.0f); Pop-up stack top matrix Glpopmatrix (); Swap buffer data glutswapbuffers (); Force finish paintingCommand//Glflush ();} /** * Resize Window * * @param width Width * @param height */void reshape (int width, int height) {//Set viewport rectangle area, by default, viewport is set Glviewport (0, 0, (Glsizei) width, (glsizei) height) for the entire pixel rectangle that occupies the open window; The following matrix operation is defined as the projection matrix operation Glmatrixmode (gl_projection); is equal to the previous matrix transformation caused the change of the stack top matrix back to bits, set as the unit matrix! is equal to the effect of the previous matrix transformation. Glloadidentity (); Create a matrix that represents a symmetric perspective tiling truncated body//Set the eye open angle, the aspect ratio of the scene, the distance from the near section, the distance from the far section gluperspective (60.0f, (glfloat) Width/(glfloat) height, 1.0f , 20.0f); The matrix operation is then defined as the Model View matrix Operation Glmatrixmode (Gl_modelview); Glloadidentity (); Camera position (0, 0, 5), camera lens facing (0, 0, 0), top of camera glulookat (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); If Glulookat () is not called, then the camera is set to the default position and orientation. By default, the camera is at the origin point, pointing to the negative direction of the z axis and the upward vector (0,1,0). }/** * Keyboard Event Callback * * @param key keyed * @param x Width * @param y height */void keyboard (unsigned char key, int x, int y) { Switch (key) {case int (' d '): Day = (day + 10)% 360; Glutpostredisplay (); Break case int (' D '): Day = (day-10)% 360; Glutpostredisplay (); Break case int (' Y '): Year = (year + 5)% 360; Glutpostredisplay (); Break case int (' Y '): Year = (year-5)% 360; Glutpostredisplay (); Break Default:break; }}int Main (int argc, const char * argv[]) {//Initialize GLUT library Glutinit (&ARGC, (char**) argv); Set the single-buffered, RGB-pixel-formatted window glutinitdisplaymode (glut_double | GLUT_RGB); Set window Size Glutinitwindowsize (500, 500); Set window coordinates glutinitwindowposition (100, 100); Create Window Glutcreatewindow ("planetary system"); Initialization Operation Init (); Set the display callback method Glutdisplayfunc (display); Glutreshapefunc (reshape); Glutkeyboardfunc (keyboard); Draw thread Start loop glutmainloop (); return 0;} </span>
this article by cc original summary, if need reprint please indicate source: http://blog.csdn.net/oktears/article/details/ 43150363
OpenGL Learning 10_ Mapping Planetary Systems