Android OpenGL ES concise development tutorial 04

Source: Internet
Author: User

This article introduces the Coordinate Transformation transformations in the 3D coordinate system.

Coordinate System

OpenGL uses the right-hand coordinate system. The method for determining the right-hand coordinate system is as follows: In the space Cartesian coordinate system, place the right-hand thumb in the positive direction of the X axis and the index finger in the positive direction of the Y axis, if the middle finger points to the positive direction of the Z axis, the coordinate system is called the right-hand Cartesian coordinate system.

Translate Translation

Method public abstract void gltranslatef (float
X, float y, float z) is used for coordinate translation transformation.

In the previous example, we moved the square to be displayed four units behind, that is, the coordinate translation transformation, which can be used for multiple translation transformations, the results are the cumulative results of multiple translation matrices. The order of the matrices is not important and can be exchanged.

Rotate Rotation

Method public abstract void glrotatef (float
Angle, float X, float y, float z) is used to realize the select coordinate transformation, in the unit of angle. (X, y, z) defines the direction of the reference vector for rotation. The sequence of multiple rotations is very important.

For example, if you select a dice, first select three times in the following order:

gl.glRotatef(90f, 1.0f, 0.0f, 0.0f);gl.glRotatef(90f, 0.0f, 1.0f, 0.0f);gl.glRotatef(90f, 0.0f, 0.0f, 1.0f);

Then, we intend to reverse the rotation back to the original initial state, which requires the following rotation:

gl.glRotatef(90f, -1.0f, 0.0f, 0.0f);gl.glRotatef(90f, 0.0f, -1.0f, 0.0f);gl.glRotatef(90f, 0.0f, 0.0f, -1.0f);

Or rotate as follows:

gl.glRotatef(90f, 0.0f, 0.0f, -1.0f);gl.glRotatef(90f, 0.0f, -1.0f, 0.0f);gl.glRotatef(90f, -1.0f, 0.0f, 0.0f);

The Rotation Transformations glrotatef (angle,-X,-y,-z) and glrotatef (-angle, x, y, z) are equivalent, however, the sequence of transformation directly affects the final coordinate transformation result. If the angle is positive, it indicates the clockwise direction.

Translate & rotate (combined translation and rotation conversion)

The sequence of coordinate transformation directly affects the final result when the mesh (mesh, which constitutes the basic unit of three-dimensional form) is translated and selected at the same time.

For example, the system first translates and then rotates, and the center of the rotation is the coordinate after the translation.

Select and then pan:
To translate, it is relative to the coordinate system after rotation:

A basic principle is that the coordinate transformation is performed relative to the coordinate system of the transformed mesh.

Scale)

Method public abstract void glscalef (float
X, float y, float z) is used for scaling and transformation.

To use the Gl. glscalef (2f, 2f, 2f) transform, it is equivalent to multiplying each coordinate value by 2.

Translate
& Scale)

Similarly, when translation and scaling are required, the order of transformation also affects the final result.

For example, First Pan and then Zoom:

gl.glTranslatef(2, 0, 0);gl.glScalef(0.5f, 0.5f, 0.5f);

If the order is changed:

gl.glScalef(0.5f, 0.5f, 0.5f);gl.glTranslatef(2, 0, 0);

The results are different:

Matrix operation, unit matrix

During translation, rotation, and scaling transformations, all transformations are for the current matrix (multiplied by the current matrix). If you need to restore the current matrix to the original non-transformed matrix, you can use the unit matrix (no translation, scaling, or rotation ).

Public abstract void glloadidentity ().

Save the current matrix in the stack and restore the existing matrix from the stack. You can use

Public abstract void glpushmatrix ()

And

Public abstract void glpopmatrix ().

A good habit of performing coordinate transformation is to use glpushmatrix to save the current matrix before the transformation. After completing the Coordinate Transformation Operation, call glpopmatrix to restore the original matrix settings.

Finally, we use the Coordinate Transformation knowledge introduced above to draw three squares A, B, and C. Perform scaling and transformation to make B 50% smaller than a and c 50% smaller than B. Then rotate a in the center of the screen counterclockwise, and B in the center clockwise, C in the center clockwise rotation at the same time in the center of their own high-speed clockwise rotation.

The ondrawframe modification code is as follows:

public void onDrawFrame(GL10 gl) { // Clears the screen and depth buffer. gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // Replace the current matrix with the identity matrix gl.glLoadIdentity(); // Translates 10 units into the screen. gl.glTranslatef(0, 0, -10); // SQUARE A // Save the current matrix. gl.glPushMatrix(); // Rotate square A counter-clockwise. gl.glRotatef(angle, 0, 0, 1); // Draw square A. square.draw(gl); // Restore the last matrix. gl.glPopMatrix(); // SQUARE B // Save the current matrix gl.glPushMatrix(); // Rotate square B before moving it, //making it rotate around A. gl.glRotatef(-angle, 0, 0, 1); // Move square B. gl.glTranslatef(2, 0, 0); // Scale it to 50% of square A gl.glScalef(.5f, .5f, .5f); // Draw square B. square.draw(gl); // SQUARE C // Save the current matrix gl.glPushMatrix(); // Make the rotation around B gl.glRotatef(-angle, 0, 0, 1); gl.glTranslatef(2, 0, 0); // Scale it to 50% of square B gl.glScalef(.5f, .5f, .5f); // Rotate around it's own center. gl.glRotatef(angle*10, 0, 0, 1); // Draw square C. square.draw(gl); // Restore to the matrix as it was before C. gl.glPopMatrix(); // Restore to the matrix as it was before B. gl.glPopMatrix(); // Increse the angle. angle++; }

Download this sample code

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.