SharpGL learning notes (6) cropping and transformation, sharpgl learning notes
In OpenGL, in addition to the six cropping planes defined by the visual object (top, bottom, left, right, and back), you can also define one or more additional cropping planes to remove irrelevant targets in the scenario.
The additional flat cropping function is prototype as follows:
ClipPlane (OpenGL. GL_CLIP_PLANEi, double [] equation );
Equation is an array with four coefficients, which defines a cropping plane. The equation parameter points to the four coefficients of the plane equation Ax + By + Cz + D = 0.
Equation = (0,-, 0), the first three parameters (0,-) can be understood as normal downward, only downward, that is, Y <0 can be displayed, the last parameter 0 indicates starting from the z = 0 plane. This is to crop the half plane.
The corresponding equation = (0, 1, 0, 0) indicates the cropping of the lower half plane,
Equation = (,) indicates to crop the left half plane,
Equation = (-,) indicates cropping out the right half plane,
Equation = (,-) indicates to crop the first half plane,
Equation = (,) indicates to crop the half plane
In the previous sections, we have discussed Perspective Projection and normal projection. They constitute two kinds of visual objects, including the cropping function. This additional cropping function is shown in:
It may not be intuitive. I will first reference the cropping Effect of 3dsmax to give you a subjective impression on the so-called cropping effect.
In 3dsmax, the camera has a cut plane option. After this option is selected, you can set the close cut position (the red line in the Left view is the close cut line, its position is 85.493). After setting a value, you can see the effect of cropping, as shown in.
If you move the camera, the cut position will change accordingly.
In OpenGL, the author tested that the viewpoint transformation does not affect the cropping result, but the geometric Transformation (moving, rotating, scaling) of the model affects the cropping result.
The code is used to describe the usage of this ClipPlane () function. First, go to the Code:
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 using SharpGL; 10 11 namespace clipPlane 12 {13 14 public partial class SharpGLForm: Form 15 {16 17 public SharpGLForm () 18 {19 InitializeComponent (); 20} 21 22 private void openGLControl_OpenGL Draw (object sender, PaintEventArgs e) 23 {24 OpenGL gl = openGLControl. openGL; 25 gl. clear (OpenGL. GL_COLOR_BUFFER_BIT | OpenGL. GL_DEPTH_BUFFER_BIT); 26 gl. loadIdentity (); 27 28 double [] eqn = new double [4] {1f, 0f, 0f, 0f}; 29 30 gl. color (1.0, 1.0, 1.0); 31 32 gl. pushMatrix (); 33 {34 gl. translate (-2,-2,-3); 35 gl. rotate (-90.0, 1.0, 0.0, 0.0); 36 drawSphere (gl); 37} 38 gl. popMatrix (); 39 40 Gl. pushMatrix (); 41 {42 // gl. clipPlane (OpenGL. GL_CLIP_PLANE0, eqn); 43 // gl. enable (OpenGL. GL_CLIP_PLANE0); 44 drawGrid (gl); 45} 46 gl. popMatrix (); 47 48 gl. flush (); 49} 50 51 private void openGLControl_OpenGLInitialized (object sender, EventArgs e) 52 {53 OpenGL gl = openGLControl. openGL; 54 gl. clearColor (0, 0, 0, 0); 55} 56 57 private void openGLControl_Resized (object sender, EventArgs e) 58 {59 60 OpenGL gl = openGLControl. openGL; 61 gl. matrixMode (OpenGL. GL_PROJECTION); 62 gl. loadIdentity (); 63 gl. perspective (601_f, (double) Width/(double) Height, 1,100.0); 64 gl. lookAt (0, 5, 10, 0, 0, 0, 0, 1, 0); 65 gl. matrixMode (OpenGL. GL_MODELVIEW); 66} 67 68 void drawSphere (OpenGL gl) 69 {70 // draw a Quadratic Surface sphere Drawing Process 71 gl. pushMatrix (); 72 gl. translate (2f, 1f, 2f); 73 74 // draw Quadratic Surface 75 var sphere = gl. newQu Adric (); 76 // you can specify a secondary drawing style. GluQuadricDrawStyle. Generally, the GLU_FILL style is used, and the polygon is used to simulate 77 gl. QuadricDrawStyle (sphere, OpenGL. GLU_LINE); 78 // set the normal style. GluQuadricNormals. Generally, the GLU_SMOOTH style is used to calculate the normal vector for each vertex. The default mode is 79 gl. QuadricNormals (sphere, OpenGL. GLU_SMOOTH); 80 // you can specify the direction of the quadratic surface. GluQuadricOrientation. Generally, GLU_OUTSIDE is used to draw a line by pointing all the normal lines to the outside. The default method is 81 gl. QuadricOrientation (sphere, (int) OpenGL. GLU_OUTSIDE); 82 // you can specify the texture. GluQuadricTexture. Set whether to automatically calculate the texture. The default value is GLU_FALSE. Change to GLU_TRUE when texture is required. 83 gl. quadricTexture (sphere, (int) OpenGL. GLU_FALSE); 84 85 gl. sphere (sphere, 3f, 20, 10); 86 gl. deleteQuadric (sphere); 87 gl. popMatrix (); 88} 89 90 void drawGrid (OpenGL gl) 91 {92 // raster Line Drawing Process 93 gl. pushAttrib (OpenGL. GL_CURRENT_BIT); // Save the current attribute 94 gl. pushMatrix (); // press the stack 95 gl. translate (0f, 0f, 0f); 96 gl. color (0f, 0f, 1f); 97 98 // draw a grid 99 for (float I =-50; I <= 50; I + = 1) on the X and Z planes) 100 {101 // draw line 102 gl. begin (OpenGL. GL_LINES); 103 // the X axis direction is 104 gl. vertex (-50f, 0f, I); 105 gl. vertex (50f, 0f, I); 106 // Z axis 107 gl. vertex (I, 0f,-50f); 108 gl. vertex (I, 0f, 50f); 109 gl. end (); 110} 111 gl. popMatrix (); 112 gl. popAttrib (); 113} 114 115 116 117} 118}
In the code above, I commented out lines and 43 of the Code for cropping. The running effect is as follows:
Generate a sphere and a raster plane. My cropping will affect both objects, so that you can observe the effect easily.
Line is enabled. After running, cropping takes effect as follows:
The left half plane is removed. Because the cropping will affect all objects in the scenario, the raster plane is also exhausted.
If you want to move the position of the cropping plane, you need to perform geometric transformation on the sphere. Just change the parameters of the following line of code.
gl.Translate(-2, -2, -3);
However, in general, after an object in a scenario is fixed, it cannot undergo geometric transformation. What should we do if we want to crop it?
I don't know what to do now! If you know what to do in the future, I will complete this knowledge point here. (If you know, thank you for returning this post to teach me !)
Below is the crop in the 45-degree direction. It is not possible to crop the appearance from any angle.
Download the source code in this section