View Original
Http://www.ibaiyang.org/2012/12/27/a-opengl-like-implementatio/
People who have learned OpenGL know that the design of OpenGL state machine is really impressive. Each State will continuously affect the rendering of subsequent objects until the state is changed.
OpenGL maintains two sets of matrices, the pattern view matrix, and the projection matrix. These two sets of matrices complete all the geometric changes from 3d to 2D, including rotation, scaling, translation, and projection. At the same time, OpenGL maintains two stacks, one storage mode view matrix and the other storage projection matrix.
Never underestimate the design of this stack. Let's take an example.
Draw_object (1) rotate (1) Translate (1) push_matrix () draw_object (2) rotate (2) Translate (2) pop_matrix () draw_object (3)
Among them, rotate (2) and translate (2) do not affect the draw_object (3) function, because the previous push_matrix will press the current into the stack. When draw_object (2), pop_matrix () the previous status is restored, so when draw_object (3), it is restored to the rotate (1), translate (1) status.
I took the time to use Python and pyqt to basically implement the 3D OpenGL function and simulate the two stacks.
CodePut it in my GitHub.
Https://github.com/baiyang/valleygl
The following shows the running effect of the Code: