Realizing a cube that can follow the mouse to switch the viewing angle and perform dynamic blanking can help to better realize the realization of three-dimensional three-dimensional geometry in two dimensions.
The system mainly involves the drawing of three-dimensional graphics in the two-dimensional plane, the rotation of graphics, the translation of graphics, the scaling of graphics and the algorithm of blanking graphics.
1. Three-dimensional presentation of the cube
First label the vertices, and correspond to the coordinates of each vertex by setting the xyz coordinate array of the vertices.
x[8] = { a+o, a+o, 0+o, 0+o, a+o, a+o, 0+o, 0+o };
y[8] = { 0+o, a+o, a+o, 0+o, 0+o, a+o, a+o, 0+o };
z[8] = { a+o, a+o, a+o, a+o, 0+o, 0+o, 0+o, 0+o };
(The reason for +o will be mentioned later)
Through the point-plane matrix, gather all the points of the same plane, put them into a matrix, and label the six planes with serial numbers.
f[6] = { 0,1,2,3,4,5 }; //face number
fp[6][5] = { {0,1,2,3,0},{1,0,4,5,1},{4,0,3,7,4},{2,1, 5,6,2}, {6,7,3,2,6},{6,5,4,7,6} }; //The vertex order of the face
2. Graphic rotation
Use the mouseDown mouse trigger event to trigger the graphic rotation, the mouse is activated, and the camera can convert coordinates and angles, using:
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(1.0f, y rot, 0.0f, 0.0f); //Set how the object rotates. The parameter is the rotation angle
The function gets the rotation angle.
3. Graphic translation
x[8] = { a+o, a+o, 0+o, 0+o, a+o, a+o, 0+o, 0+o };
When initially initializing the coordinates, use the 'modifiable variable o' to assign a value to the array, modify the value of o when the keyboard event is triggered, and redraw the picture to obtain the image of the initial coordinate transformation, that is, the translation of the image is completed.
4. Scaling of graphics
x[8] = { a+o, a+o, 0+o, 0+o, a+o, a+o, 0+o, 0+o };
When initially initializing the coordinates, use the modifiable variable a to assign a value to the array, modify the value of a when the keyboard event is triggered, and redraw the image to obtain an enlarged/shrinked image, which means that the zooming of the image is completed.
5. Real-time dynamic blanking of graphics
Based on what you have learned in class:
(1) The rear face is always invisible;
(2) do not render other edges invisible solely due to occlusion by the rear face;
(3) All these backward faces can be removed without affecting the blanking result.
Calculate the viewpoint vector S of the square at the current viewing angle and the normal vector (V) of each face of the cube
① If V·S<0, the polygon is called the front face. ② If V·S>0, the polygon is called the back face.