Android 3D Game Development tutorial-Part V

Source: Internet
Author: User
Tags xdiff

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

The fifth part of the series describes how to create your first complete 3D object. This case is a four-sided pyramid.

In order to make the subsequent development easier, we need to make some preparations here.

We must increase the size of the calculated buffer and the size of the created array.

To be more dynamic, we must change some variables so that we can continue to work. Let's take a closer look:

In the first line, you can see that we initialize _ nrofvertices to 0, because we can determine it by the coordinate size in the seventh line.

At the same time, we also changed _ indecesarray to the local variable indices and initialized it in line 13.

This buffer creation process is placed under the coordinates, color, and vertex array, because the buffer size depends on the array. Therefore, see rows 17-18, 22-23, and 27-28. In the comment, I explained the calculation method.

The main benefit is that we can create more vertices without manually re-calculating the number of vertices and the size of the array and buffer.

Next: you need to understand how OpenGL draws and determines what we see.

Compared with OpenGL, OpenGL ES has a major drawback that there are no primitive types except triangles. We have no other polygon, so all the objects we want to create must be made up of triangles. I quoted a blog post to illustrate this problem: iPhone developer and recommended his articles OpenGL ES series.

Here are more things about triangles that you need to know. In OpenGL, a concept is winding, which indicates the order of vertices. Unlike objects in the real world, the polygon in OpenGL generally has no two sides. They only have one face, usually the front. A triangle can be seen only when the front is facing the observer. You can configure OpenGL to use a polygon as both sides, but the triangle has only one visible surface by default. After knowing that there is the front of the polygon, OpenGL will be able to do less than half of the calculation workload. If both sides are visible, more computing is required.

Although sometimes a polygon is displayed independently, you may not need to display it on the back. A triangle is often a part of a larger object, the side of a polygon will be inside the object, so it will never be seen. This unspecified side is called the back. OpenGL uses the order of drawing to determine which side is the front and which is the back. Vertices are drawn on the front side in a counter-clockwise manner (this is the case by default, but can be changed ). Because OpenGL can easily determine which triangles are visible to users, it can avoid useless work for those polygon that are not displayed in the front by using backface culling. We will discuss the perspective in the next article, but now you can imagine it as a virtual camera, or observe the OpenGL world through a virtual window.

In the above, the green triangle on the left is the back and will not be drawn because it is clockwise relative to the observer. The triangle on the right is the front and will be drawn, because the order of the drawn vertex is counter-clockwise relative to the observer.

Because what we want to do is to create a beautiful pyramid, we first disable this glclearcolor (). We can delete the variables _ Red, _ Green, _ blue, and setcolor (). We also want to change the angle of view, so we will rotate the X and Y axes of the points.

To ensure that you have the same object variables as this one, I also paste them to the top of this class. You can see that we now have two float variables, _ xangle and _ yangle (Lines 15-16), as well as their setter and getter methods (lines 28-42)

Now let's calculate the angle Logic Based on the touch screen. To do this, we need to slightly change the vortexview class.

In rows 3rd and 4, we have two variables for the X and Y values. When moving, we set their values in the action_down event. We calculate the difference between the current value and the old value based on the motionevent. Calculate their differences and add them to the Applied perspective. Do not be confused when ydiff is added to X-angle or xdiff is added to Y-angle. As you can imagine, if you want to rotate only on the Y axis without changing the value of the X axis. The same applies to Y axes.

If we move our finger to the left or up, the value of xdiff/ydiff will become negative, and the rotation will rotate backward. Therefore, it is easier to rotate the two axes.

Now it is very interesting: Pyramid.

As we have referenced above, winding requires some settings. Some may be the default settings, but we still define them to ensure security.

In row 3, we enable the culling surface to ensure that there is only one side. In the first row, we defined the order above. Gl_ccw indicates the counter-clockwise. In row 3, we finally defined the surface as a culling surface. Grant gl_back to ensure that only the front is displayed. This may be a bit confusing. You can see what will happen if you use gl_front_and_back ...... You will not see anything.

On the fourth line, you can see that the background color is black, because we have deleted the previously set dynamic color. On Lines 13 and 14, you can see the Rotation Angle on each axis. The rest is the same as previously mentioned.

The last thing you need to do is to change the color array, coordinates, and indexes in the inittriangle () function. Our discussion of gold should be shown in this way.

As you can see in the figure, our pyramid has four corners. Each angle has its own coordinates, so we need to define four vertices. For example, 2-7 rows.

Each vertex has its own color defined in 10-15 rows.

In the middle section, the indexes required to define a triangle are given. Remember that winding, triangle 0, 1, 3, and 0, 3, and 1 are different.

Each index point points to the vertex defined in the coords array.

Compile and run the program to check what will happen if you change the indices sequence, or to check what will be seen after you change gl_ccw to gl_cw.

Download the source code of the eclipse project: vortex Part V

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.