OpenGL learns 10_plotting planetary systems and opengl10 draws planets
The Demo below also uses a variety of combined transformations to realize the revolution and rotation of the earth around the sun, or directly look at the code, with detailed annotations.
<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;/*** initialization operation */void init () {// set clear screen glClearColor (0.0f, 0.0f, 0.0f, 0.0f); // sets the coloring mode. The fill color is consistent with that of the vertex. glShadeModel (GL_FLAT);}/*** displays the rendering effect */void display () {// clear the color buffer glClear (GL_COLOR_BUFFER_BIT ); // Set the drawing 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 with a radius of 1.0f gluwiresphere (1.0f, 20366f, 16366f); // rotate year degrees around the Y axis. At this time, the local coordinate system of the earth is inconsistent with the global coordinate system, and the global coordinate system shows the public conversion glRotatef (GLfloat) year, 0.0f, 1.0f, 0.0f); // two units of length are moved along the X axis. At this time, the local coordinate system of the Earth has shifted to the global coordinate system glTranslatef (2.0f, 0.0f, 0.0f ); // rotate the day degree along the Y axis. At this time, the local coordinate system of the earth is consistent with the global coordinate system, showing the rotation of glRotatef (GLfloat) day, 0.0f, 1.0f, 0.0f ); // draw a earth with a radius of 0.2f Valley wiresphere (0.2f, 10.0f, 8.0f); // pop-up glPopMatrix () on the top of the stack; // swap the buffer data in the glswapbuffers (); // force the Drawing Instruction // glFlush ();} /***** adjust the window size ** @ param width * @ param height */void reshape (int width, int height) {// set the rectangular area of the window, by default, the view is set to occupy the glViewport (0, 0, (GLsizei) width, (GLsizei) height of the entire pixel rectangle that opens the window ); // The subsequent matrix operation is defined as the projection matrix operation glMatrixMode (GL_PROJECTION). // The former matrix transformation leads to the reset of the changed stack top matrix to the unit matrix! This is why the previous matrix transformation has an effect! GlLoadIdentity (); // create a matrix that represents the heartbeat body of the symmetric perspective view. // set the eye open angle, the aspect ratio of the visual body, and the distance between the near-cross section, the distance of the far section gluPerspective (60366f, (GLfloat) width/(GLfloat) height, 1.0f, 20366f); // The matrix operation is defined as the model view matrix operation glMatrixMode (GL_MODELVIEW ); glLoadIdentity (); // camera position (0, 0, 5), camera lens orientation (0, 0, 0), camera top up gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // If gluLookAt () is not called, the camera is set to the default position and direction. By default, the camera is located at the origin and points to the negative direction of the Z axis. The face-up vector is (, 0 ). }/*** Keyboard Event Callback ** @ param key * @ param x Width * @ param y height */void keyboard (unsigned char key, int x, int y) {switch (key) {case int ('D'): day = (day + 10) % 360; fig (); break; case int ('D '): day = (day-10) % 360; fig (); break; case int ('y'): year = (year + 5) % 360; fig (); break; case int ('y'): year = (year-5) % 360; fig (); break; default: break ;}} int main (int argc, const char * argv []) {// initialize the GLUT library gluinit (& argc, (char **) argv); // set a single buffer, window in RGB pixel format: FIG (glu_double | glu_rgb); // you can specify the window size. The value is 500,500. // you can specify the window coordinate ); // create a window ("planetary system"); // initialize the init () operation; // set the display callback method (display); glureshapefunc (reshape ); glukeyboardfunc (keyboard); // The draw thread starts to loop the glumainloop (); return 0 ;}</span>
This article by CC original summary, If You Need To reprint please note the Source: http://blog.csdn.net/oktears/article/details/43150363