The role of the stack in OpenGL is to save the current matrix.
Pushmatrix ()
Declare the 3D coordinate changes in the graphics:
In OpenGL, the current matrix (including projection matrix, transformation matrix, and so on) is saved under the coordinate change process.CodeIt simply imitates the storage and restoration of the matrix.
Code: (I have applied the stack data structure in STL. Please follow the instructions for self-implementation)
1 # Include " Stdafx. h " 2 # Include <iostream> 3 # Include <stack> 4 # Include " Windows. h " 5 Using Namespace STD; 6 /* **************************************** ****************************** */ 7 /* The simulation of stack operations in OpenGL simply simulates the operations of the world coordinate system. 8 Where: press the stack to save the current matrix transformation 9 In this example, initfunc () is used to simulate the initialization transformfunc () to represent the coordinate transformation. */ 10 /* **************************************** ****************************** */ 11 12 Struct Vector3f { 13 14 Float X, Y, Z; // Coordinates x, y, z 15 }; 16 17 Vector3f currentvector; // The global coordinate representation is used to represent the world coordinates (global) 18 Stack <vector3f> Savevectorstatic; 19 // Initialization Function 20 Void Showvector (); 21 Void Initfunc () 22 { 23 Currentvector. x = 0 ; 24 Currentvector. Y = 0 ; 25 Currentvector. z = 0 ; 26 } 27 // Transform Functions 28 Void Transformfunc ( Float X, Float Y, Float Z) 29 { 30 Currentvector. x = currentvector. x + X; 31 Currentvector. Y = currentvector. Y + Y; 32 Currentvector. z = currentvector. Z +Z; 33 } 34 35 Int _ Tmain ( Int Argc, _ tchar * Argv []) 36 { 37 Initfunc (); 38 Cout < " I want to see the current coordinates: " <Endl; 39 Showvector (); 40 41 Cout < " I want to Pan 6 units up and save the current Matrix !! " < Endl; 42 Cout < " Execute: "savevectorstatic. Push (currentvector); To save !" " < Endl; 43 Cout < " Run "transformfunc (0.0f, 6.0f, 0.0f);" to move it! " < Endl; 44 Savevectorstatic. Push (currentvector ); 45 Transformfunc ( 0.0f , 6.0f , 0.0f ); 46 47 Cout < " I want to see the current coordinates: " < Endl; 48 Showvector (); 49 50 Cout < " I want to move 3 units to the left based on the origin !! " < Endl; 51 Currentvector = Savevectorstatic. Top (); 52 Transformfunc (3.0f , 0.0f , 0.0f ); 53 54 Cout < " Run "currentvector = savevectorstatic. Top ();" to restore the instance! " < Endl; 55 Cout < " Run "transformfunc (3.0f, 0.0f, 0.0f);" to move it! " <Endl; 56 57 58 59 Cout < " I want to see the current coordinates: " < Endl; 60 Showvector (); 61 Return 0 ; 62 } 63 Void Showvector () 64 { 65 Cout < " Number of elements in the current stack: " <Savevectorstatic. Size () < Endl; 66 Cout < " Coordinates: " < " X " < " Y " < " Z " < Endl; 67 Cout < " " <Currentvector. x < " " <Currentvector. Y < " " <Currentvector. z < Endl; 68 }
Original connection :( we create a forum): http://bochuang.sinaapp.com/forum.php? MoD = viewthread & tid = 55