Android3d game development series (2) color rendering and Rotation

Source: Internet
Author: User

 

Android3d game development series (2) color rendering and Rotation

Next we will perform a simple operation for the triangles and squares we draw, such as color rendering and rotation. First, let's look at the running effect:

1. color rendering of the image

There are two main ways to color a graphic: Smooth coloring and plane coloring. As shown in, the triangle uses smooth coloring to combine different colors of the three vertices of the triangle to create a beautiful color mix. The quadrilateral uses flat coloring (monotonic coloring) to apply a fixed color.

(1) first, we will learn smooth coloring ). First, set a color for each vertex of the triangle. The color is the same as the clear screen color. The following Code defines a color array for the triangle:

In the ondrawframe method, use the following code to enable color rendering:

Gl. glableclientstate (gl10.gl _ color_array );

The glcolorpointer method is provided in Android to set the color. The defined color for the triangle is as follows:

Gl. glcolorpointer (4, gl10.gl _ fixed, 0, colorbuffer );

The first parameter is the four attributes (R, G, B, A) used to set the color, so it is 4. Because the data in this example is fixed, therefore, the second parameter uses gl_fixed to indicate a fixed value. stride describes the step size. The default value is 0. The fourth parameter is the color cache, which is the color array we created.

Finally, you must note that do not forget to disable the function after the color is used. After using the glenableclientstate in OpenGL, you must use gldisableclientstate to disable or cancel the corresponding function. The Code is as follows:Gl. gldisableclientstate (gl10.gl _ color_array );

(2) After learning smooth coloring, monotonous coloring is simple. monotonous coloring is actually to set the current color, that is, to set all the content drawn after the current color to use the current color. The code for setting the current color is as follows:

// Color (R, G, B,)

Gl. glcolor4f (0.5f, 0.5f, 1.0f, 1.0f );

2. Image Rotation

After learning how to color, we will think about how to rotate these color objects around the coordinate axis? Next we will take the triangle rotating along the Y axis and the Quadrilateral rotating along the X axis as an example to explain the implementation process.

It is easy to implement rotation. First, add two variables to control the rotation of the two objects. They are floating-point variables that allow us to rotate objects very accurately. The floating point number contains decimal places, which means that 1, 2, and 3 are not required... . Floating Point number is the basis of OpenGL programming. The rotatetri in the new variable is used to rotate the triangle and rotatequad is used to rotate the Quadrilateral. The Code is as follows:

Float rotatetri; // used for the triangle Angle

Float rotatequad; // used for the Quadrilateral Angle

The following describes the rotation function. Used in AndroidGlrotatef (float angle, float X, float y, float Z)Method to rotate an object along the specified axis. The angle parameter is usually a variable, representing the angle of the object. The X, Y, and Z parameters jointly determine the direction of the rotation axis. For example, the vector described in (, 0) goes through one unit of the X axis and is directed to the right. The detailed code is as follows:

// Rotate the triangle

Gl. glrotatef (rotatetri, 0.0f, 1.0f, 0.0f );

// Rotate the Quadrilateral

Gl. glrotatef (rotatequad, 1.0f, 0.0f, 0.0f );

Here we have completed the rotation of the object. In order to enable the object to rotate continuously, the ondrawframe method continuously changes the angle variable, as shown in the following code:

// Change the Rotation Angle

Rotatetri + = 0.5f;

Rotatequad-= 0.5f;

By now, the entire project has been basically implemented. The Code is as follows:

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.