Project description: using C++ to implement the OpenGL GLUT to implement a simple planetary system in the solar system. It will involve some mathematical foundation of 3d graphics technology, 3d coordinate system in OpenGL, illumination model in OpenGL, and keyboard event processing of GLUT.

OpenGL includes many rendering functions, but they are designed to be independent of any windowing system or operating system. Therefore, it does not contain functions to create open Windows or read the time from the keyboard or mouse, or even the most basic display window function, so it is completely impossible to create a complete graphics program using OpenGL alone. And most programs need to interact with the user (responding to keyboard and mouse actions, etc.). GLUT provides this convenience.

! [](https://upload-images.jianshu.io/upload_images/24762785-dc113eae2820ea35.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

GLUT is an acronym for The OpenGL Utility Toolkit. GLUT is a tool library that handles calls and I/O operations with the underlying operating system. With GLUT, you can mask some of the details of the underlying OPERATING system GUI implementation. Using only the GLUT API, you can create application Windows, handle mouse and keyboard events, and so on across platforms.

In the whole celestial system, they are all one Star. Planets and stars can be distinguished only by whether they have parent nodes. Secondly, for different planets, they usually have their own materials, and different materials will show whether they shine or not, thus we have a preliminary object model. Therefore, we can divide the planets into stars that can rotate and orbit around a certain point, planets with special materials, and planets that can give off light.

! [](https://upload-images.jianshu.io/upload_images/24762785-32f19b3cc186856e.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

I. Programming model assumptions:

Planets move in circular orbits;

The rotation speed remains the same;

Each time you refresh your drawing, assume that a day has passed.

Two, the overall idea:

Initialize a planet object.

Initialization of OpenGL engine, onDraw and onUpdate;

Planets should be responsible for their own properties, devolution, and transform correlation drawing, so provide a draw draw() method when designing planets’ classes;

The planet should also handle its own rotation and revolution of the update display, so when designing the planet class should also provide an update method update();

Call the draw() method of the planet in onDraw();

If you have a problem with C/C++ one item is a warm one (● ‘◡’ ●). A full item: one item for free and a live programme.

! [](https://upload-images.jianshu.io/upload_images/24762785-b767c8ff49521162.gif? imageMogr2/auto-orient/strip)

Call the update() method of the planet in onUpdate();

Adjust the display throughout the solar system on the onKeyboard().

3. Each planet has the following properties:

Color color

Radius of revolution RADIUS

Rotation speed selfSpeed

Revolution speed Speed

Distance from the center of the sun

Orbiting planet parentStar

Current rotation Angle of alphaSelf

The current revolution Angle alpha

So there are the following classes:

! [](https://upload-images.jianshu.io/upload_images/24762785-8a45ecdb4ab3d61f.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

In the solar system, the solar system is obviously made up of planets; And, for the solar system, the view refresh after the motion of the planets in the solar system should be completed by the solar system. Therefore, the SolarSystem class can be designed as follows:

! [](https://upload-images.jianshu.io/upload_images/24762785-f552b0e29491c133.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

The basic framework of the program: main.cpp

! [](https://upload-images.jianshu.io/upload_images/24762785-231c72c03637b83b.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

The framework and ideas of this experiment come from the experimental building, and the operation effect is shown in the figure below:

! [](https://upload-images.jianshu.io/upload_images/24762785-03f40a1e8bdfc492.jpg?imageMogr2/auto-orient/strip%7CimageView 2/2/w/1240)

prompt

Single buffering, is to execute all the drawing instructions in the window, is directly on the window drawing, such drawing efficiency is relatively slow, if the use of single buffering, and computer processing performance is not enough, the screen will appear flashing.

Double buffering, drawing instructions will be completed in a buffer, drawing here is very fast, after the completion of drawing instructions, and then through the exchange of instructions to complete the graphics immediately displayed on the screen, so as to avoid the incomplete drawing, high efficiency.

Double buffer is mainly divided into foreground buffer and background buffer, foreground buffer that we say see the screen, background buffer is maintained in memory, invisible to the user. With double buffering, all drawing takes place in the background, and when the drawing is complete, the results are copied to the screen.

The advantage of this is that if we let the drawing operation operate with the graphics card in real time, when the drawing task is complex, the IO operation will also become complex, resulting in low performance. Double buffering will only swap the buffer will be completed after the drawing results directly sent to the graphics card for rendering, IO significantly reduced.