SharpGL learning notes (2) model transformation, sharpgl learning notes
(2) model transformation
Mode Transformation refers to three operations: Moving, rotating, and scaling in the world space coordinate system.
First of all, in Opengl, we use a 4x4 matrix for coordinate transformation. OpenGL's 4x4 matrix is arranged by column, as shown below.
The so-called model transformation is to transform this matrix.
To describe the 3D world, you must first design the 3D model. When designing a 3D model, it is in the coordinate system of the model, and eventually it will be placed in the world coordinate system. If this model is well handled during design, in the world coordinate system, the center of the model is located at the center of the world coordinate system by default.
Here is an extended knowledge point: when a model is placed in the world coordinate system, the center is not in the center of the world coordinate system. What is the problem? What are the consequences? In the future, we will discuss the scalability.
The above describes how to import a model made by other design software, such as a model made by 3dsMax. If we use code to draw a model, it is naturally at the center of the world coordinate system by default.
The world coordinate system is like:
The direction of the x-axis arrow is positive, and the reverse direction is negative.
The Y-axis arrow is in the positive direction, and the reverse direction is negative.
The arrow on the Z axis points to our eyes, so the figure is just a small dot, invisible. It is directed to the screen in the negative direction.
The center of the 3D body is just the center of the world coordinate system.
The following demo Code draws a triangle in the world coordinate system and then performs some transformation operations on it.
1 using System; 2 using System.Windows.Forms; 3 using SharpGL; 4 5 namespace blankTest 6 { 7 public partial class Form1 : Form 8 { 9 private int mtype = 0; 10 public Form1() 11 { 12 InitializeComponent(); 13 14 } 15 16 private void Form1_Load(object sender, EventArgs e) 17 { 18 19 } 20 21 private void openGLControl1_OpenGLInitialized(object sender, EventArgs e) 22 { 23 24 } 25 26 private void openGLControl1_Resize(object sender, EventArgs e) 27 { 28 29 } 30 31 private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e) 32 { 33 SharpGL.OpenGL gl = this.openGLControl1.OpenGL; 34 35 gl.ClearColor(0, 0, 0, 1); 36 gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); 37 38 switch (mtype) 39 { 40 case 0: 41 translateSample(gl); 42 break; 43 case 1: 44 rotateSample(gl); 45 break; 46 case 2: 47 scaleSample(gl); 48 break; 49 } 50 51 gl.Finish(); 52 gl.Flush(); 53 } 54 55 private void scaleSample(SharpGL.OpenGL gl) 56 { 57 gl.LoadIdentity(); 58 gl.Scale(1f, 1f, 1f); 59 gl.Translate(0, 0, -9f); 60 drawPT(gl); 61 62 gl.LoadIdentity(); 63 gl.Scale(5f, 1f, 1f); 64 gl.Translate(0, 1, -9f); 65 drawPT(gl); 66 } 67 68 private void rotateSample(SharpGL.OpenGL gl) 69 { 70 gl.LoadIdentity(); 71 gl.Rotate(0,0, 45); 72 gl.Translate(0, 0, -8f); 73 drawPT(gl); 74 75 gl.Rotate(0, 0, 45); 76 gl.Translate(-1, 1,0f); 77 drawPT(gl); 78 } 79 80 private void translateSample(SharpGL.OpenGL gl) 81 { 82 gl.LoadIdentity(); 83 gl.Translate(0f, 0f, -3f); 84 drawPT(gl); 85 86 gl.LoadIdentity(); 87 gl.Translate(0f, 1f, -3f); 88 drawPT(gl); 89 } 90 91 private void drawPT(SharpGL.OpenGL gl) 92 { 93 gl.PointSize(5f); 94 gl.Begin(OpenGL.GL_TRIANGLES); 95 { 96 gl.Vertex(0.0f, 0f, 0.0f); 97 gl.Vertex(-1.0f, -1f, 0.0f); 98 gl.Vertex(1.0f, -1f, 0.0f); 99 }100 gl.End();101 }102 103 private void btnTranslate_Click(object sender, EventArgs e)104 {105 switch (((Button)sender).Name)106 {107 case "btnTranslate":108 mtype = 0;109 break;110 case "btnRotate":111 mtype = 1;112 break;113 case "btnScale":114 mtype = 2;115 break;116 }117 }118 119 }120 }
The three functions translateSample (), rotateSample (), and scaleSample () demonstrate "pan", "Rotate", "zoom" respectively ".
Let's talk about some important points:
In the "Translation" function, if there is no gl. the LoadIdentity () function resets the coordinate system to the origin location, and the execution result will be different. The second gl. translate (0f, 1f,-3f) will be relative to the previous gl. the position of the Translate (0f, 0f,-3f) is moved.
Gl. LoadIdentity () is performed before a triangle is drawn every time, which is equivalent to the center point coordinate of each drawing from the origin.
private void translateSample(SharpGL.OpenGL gl) { gl.LoadIdentity(); gl.Translate(0f, 0f, -3f); drawPT(gl); gl.LoadIdentity(); gl.Translate(0f, 1f, -3f); drawPT(gl); }
If the Z axis in gl. Translate is set to 0, the object is invisible because the triangle drawn at this time is attached to the Z axis of the world coordinate system. It's like your eyes are close to it.
You can imagine that the camera is in the world coordinate system (0, 0, 0) and is in the negative direction of the Z axis.
To illustrate this, the blogger draws the following:
The raster plane in the figure is the gridline in the world coordinate system, and the bottom (red) of the three-dimensional box is pasted on the depth of the Z axis 0 in the world coordinate system. You have to move the Box to the negative direction of the Z axis-1 or more to see this Box.
The running effect of this program is as follows:
Translation
Rotate
Zoom
Download the source code in this section