Android 3D Game Development tutorial-Part II

Source: Internet
Author: User

These Android 3D Game Development articles are originally written by Martin, a German, at lixinso is translated into Chinese.

The second part of this series is about how to add a triangle and rotate it.

The first thing is to initialize the triangle to be displayed. We will add a method inittriangle () in the vortexrenderer class ().

Let's start with the new object variable. _ vertexbuffer stores coordinates for our triangles. _ indexbuffer stores the index. the _ nrofvertices variable defines the number of vertices required. A triangle requires three vertices.
This method first allocates the required memory (Lines 14-22) for the two buffers. Next, we define some coordinates (lines 24-28) and the annotations next to them to explain the purpose.
In row 30, we fill the coords array with _ vertexbuffer. Similarly, in row 31, the indices array is filled with _ indexbuffer. Finally, set the position of both buffers to 0.

To prevent the triangle from being initialized every time, we only call it once for the number of rows before ondrawframe. A better choice is in the onsurfacecreated () function.

Glenableclientstate () sets OpenGL to use the vertex array for painting. This is important because OpenGL does not know how to process our data if it is not set. Next we will initialize our triangle.

Why don't we need to use different buffer? In the new ondrawframe () method, we must add some new OpenGL calls.

Well, let's take a look at it step by step.
Glclearcolor () and glclear () have been mentioned in section I of the tutorial. Use glcolor4f (red, green, blue, alpha) in row 10th to set the triangle to dark red.
In row 13th, we use glvertexpointer () to initialize vertex pointer. The first parameter is the size and the vertex dimension. We use 3D coordinates x, y, and z. The second parameter, gl_float, defines the data type used in the buffer. The third variable is 0, because our coordinates are compact in the array and no offset is used. Finally, Hu's fourth parameter vertex buffering.
Finally, gldrawelements () draws all these elements. The first parameter defines what elements will be drawn. The second parameter defines how many elements there are, and the third is the data type used by indices. The last one is the index buffer used to draw the vertex.

When you finally test the use of this application, you will see a triangle static in the middle of the screen. When you click the screen, the background color of the screen will change.

Now add a rotation to the triangle. The following code is written in the vortexrenderer class.

The glrotatef () method is called by ondrawframe () before glcolor4f.

At this time, we can rotate around the Y axis. To change the value, you only need to change the 0f in the glrotate () method. The value in this parameter represents a vector, marking the coordinate axis in which the triangle rotates.

To make it available, we must add a call to ontouchevent () in vortexview.

In the above Code, dividing by 10 is to reduce the angle conversion speed.

Compile and run this program. If you click on the far left of the screen, you will see a slight rotation of the triangle. If you move your finger to the right, the rotation will become fast.

Download the source code of the eclipse project here (original link): vortex Part II

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: 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.