This case is OpenGL in a more classic comprehensive case, combined with most knowledge points in OpenGL, the following to understand this case

Let’s take a look at the final result

The overall process is as follows

The most important logic is in the following three functions

  • SetupRC function
  • RenderScene function
  • SpecialKeys function

The following details the realization of the overall effect, which can be roughly divided into four parts

  • The floor
  • Big ball (spin)
  • Ball (contains 50 static balls +1 dynamic ball orbiting the large ball)
  • Movement (triggered by special key up, down, left and right)

The preparatory work

ChangeSize and main functions in the flow chart will not be explained too much. I believe you have been very impressed by these two functions. If you still don’t understand them, you can first understand what work is done in these two functions by combining case 03: drawing pyramid, hexagon and circle

The floor

  • SetupRC function: Prepares the vertex data of the floor
  • RenderScene function: Draws the floor with a flat shader

The flow chart of the two functions at this point is roughly as follows

A big ball

On the basis of the completion of the floor drawing, drawing the big ball, and realize its rotation function, the main steps are as follows

  • SetupRC function: Creates large spheres using system model classes
  • RenderScene function: Divided into three parts
    • Set timer: Records the current time Angle based on the time change
    • Set the big ball transformation (translation only once, and with the timer rotation) and draw the big ball
    • On timer: By submitting a re-render request, the timer trigger effect is realized

The flow charts of the two functions are as follows. The red box part of the flow is related to the big sphere

Big ball transform The code for big ball transform is as follows

/ / 5. Translation makes the ball position (3.0) to the screen inside modelViewMatrix. Translate (0.0 f to 0.0 f, 3.0 f); / / 6. Pressure stack (copy stack) modelViewMatrix. PushMatrix (); Rotate(yRot, 0.0f, 0.0f, 0.0f); Rotate(yRot, 0.0f, 0.0f, 0.0f);Copy the code
  • Translate: The goal is to have a better view of the big ball, because the big ball is created at the origin by default, and the current observer is also at the origin, making it difficult to observe
  • PushMatrix: copy the top of the stack and push the stack. In this case, you only need to move the big ball once, and then rotate around the Y-axis based on the shifted coordinate to realize rotation
  • Rotate: The large ball rotates around the y axis

The principle of rotation Only for the individual to understand: can see time as a circle, big ball is mapped to a xoy plane is also a circle, and then based on the change in the time of Angle, the ball mapping circle in the same point of view, and then the ball is based on the last point of view, continue to rotate the Angle of alpha, every 360 degrees is a cycle, so as to realize the ball’s rotation

A ball

After drawing the big ball, continue to draw the small ball

  • SetupRC function: initializes ball data
  • RenderScene function: Draws static + dynamic balls
    • Draw 50 static balls: Push and POP are required for each ball drawn
    • The matrix stack records translation + rotation transformations
    • Draw dynamic ball

The flow chart of the two functions is as follows:

In fact, the revolution of the ball can be understood as: Rotation + translation, if only the rotate transformation, then the ball is in the same spot on the y axis rotation, but the ball with the ball is in the same position, in order to get a better look, you need to ball, translation between the ball and has a certain interval, and each rotation, small ball rotation Angle is the ball twice, means the ball than the ball go fast, The principle of revolution is shown in figure

  • Note: hererotate+tranalateThe order of is not interchangeable, because matrix multiplication is a cross product that does not satisfy the commutative law8. Understand vectors and matrices in OpenGLIt’s mentioned in the model transformation section that if you swap it you get a different result, when you test it, if the order istranalate+rotateAt this time, the ball is rotated after moving, which means that the ball is rotated in situ after changing its position. Specific can modify their own code to try.

mobile

At this step, the main functions have been basically realized, and only the movement of special keys is needed. At this time, the movement is applied to all graphs, so an observer needs to be added before drawing graphs to record the transformation of all graphs

At this point, the overall flow chart of setupRc, RenderScene, and SpecialKeys in the project is as follows

At this point, the comprehensive project is complete.

See the githuB-06_OpengL_ synthesis project for the complete code