Android OpenGL ES development tutorial (16): viewing and modeling (modelview) Transformation

Source: Internet
Author: User

The transformation between viewing and modeling is closely related to the camera to place the tripod and adjust the position and angle of the object to be photographed. Usually, these two transformations are defined using a modelview transformation matrix. For the same coordinate transformation, you can use different methods to imagine this transformation. For example, if you want to translate a distance from the camera to a certain direction, the effect is equivalent to that of the model to be taken) translate the same distance (relative motion) in the opposite direction ). Two different spatial imagination methods have their own advantages and disadvantages in understanding coordinate transformation. You can use a method suitable for your understanding to imagine spatial coordinate transformation.

Next we will use a simple example composed of two coordinate transformations to introduce the modelview Transformation: one is to rotate 45 degrees counterclockwise around the Z axis, and the other is to translate along the X axis. Assuming that the size and distance of the object to be drawn are much smaller, you can easily see the translation effect. The initial position of this object is at the origin of the coordinate system.

If the object is first rotated and then translated, the position of the object after rotation is above the X axis, but if the object is first translated and then rotated around the origin, the object will eventually appear in the Y = X line:

We can see that the order of Coordinate Transformations directly affects the final transformation result. All viewing and modeling transformation operations can be expressed using a 4x4 matrix.Glmultmatrix *()
Or other Coordinate Transformation commands will use a new transformation matrix m to multiply the current modelview matrix C to get a new transformation matrix cm. Then all vertex coordinates V are multiplied by the new transformation matrix. This process means that the last coordinate transformation command is actually the first application to the top point. Therefore, one way to understand the order of coordinate transformation is to use the reverse order to specify the coordinate transformation.

For exampleCode:

GL. glmatrixmode (gl_modelview); <br/> GL. glloadidentity (); <br/> // apply transformation n <br/> GL. glmultmatrixf (n); <br/> // apply transformation m <br/> glmultmatrixf (m); <br/> // apply transformation L <br/> GL. glmultmatrixf (l); <br/> // draw vertex <br/>...

In the code above, the modelview matrix is I (unit matrix), N, and NM are NML, and the final coordinate transformation result is nmlv, that is, n (m (LV )), V is first multiplied by L, then LV is multiplied by m, and mlv is then multiplied by N. We can see that the order of coordinate transformation is the opposite of the order specified by the command. When the actual code is running, the coordinate does not need to be converted three times. The vertex v only needs to be multiplied by the calculated final transformation matrix NML.

Therefore, if you use the world coordinate system (the origin and X, Y, and Z axes are fixed) to think about coordinate transformation, the order of the coordinate transformation commands in the Code is the opposite of the order of vertices and matrix multiplication. For example, if you want the final object to appear on the X axis, you must first rotate and then translate it. You can use the following code (r indicates the selection Matrix, t indicates the translation matrix)

GL. glmatrixmode (gl_modelview); <br/> GL. glloadidentity (); <br/> // translation <br/> GL. glmultmatrixf (t); <br/> // rotation <br/> GL. glmultmatrixf (r); </P> <p> draw_the_object ()

Another way to imagine coordinate transformation is to forget this fixed coordinate system, but to use the local coordinate system of the object itself, which is fixed with the relative position of the object. All Coordinate Transformation operations are the local coordinate system of objects. In this method, the order of matrix multiplication in the Code is the same as that of the coordinate transformation of the relative local coordinate system. (It doesn't matter which method is used to imagine coordinate transformation, and the code of the same transformation result is the same in the end, but the methods of understanding are different ). Or use the example of rotation and translation above. Imagine a local coordinate system connected to an object, such as a red coordinate system. Imagine that all the coordinate transformations are relative to this local coordinate system, so that the object will eventually appear on Y = x, it can be imagined that the system first rotates the object and its local coordinate system (R), and then translates the object and its local coordinate system (T). At this time, the order of the Code is the same as that of the local coordinate system relative to the object.

GL. glmatrixmode (gl_modelview); <br/> GL. glloadidentity (); <br/> // rotation <br/> GL. glmultmatrixf (r); <br/> // translation <br/> GL. glmultmatrixf (t); </P> <p> draw_the_object ()

By using the object's local coordinate system, you can better understand graphics systems such as robots and the solar system.

The glulookat sub-function of the glulookat package of Android OpenGL ES provides a more intuitive way to set the modelview transformation matrix:

Void glulookat (gl10 GL, float eyex, float eyey, float Eyez, float centerx, float centery, float centerz, float UPX, float upy, float upz)

      • Eyex, eyey, and Eyez specify the spatial coordinates of the observation point.
      • TARX, tary, and tarz are coordinates of the reference point of the object to be observed.
      • UPX, upy, and upz specify the vector with the observation point direction being "up.

      Note: These coordinates all use the world coordinate system.

       

       

       

      Related Article

      Contact Us

      The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

      If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

      A Free Trial That Lets You Build Big!

      Start building with 50+ products and up to 12 months usage for Elastic Compute Service

      • Sales Support

        1 on 1 presale consultation

      • After-Sales Support

        24/7 Technical Support 6 Free Tickets per Quarter Faster Response

      • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.