Some suggestions for converting OpenGL program into OpenGL ES: gldrawarray and gldrawelements

Source: Internet
Author: User

It took four days to transfer an FPS game engine fly3d to OpenGL ES. As we all know, ES is intended for mobile devices, so for efficiency, it performs a lot of screening and subtraction on OpenGL, throwing away all inefficient functions such as (glbegin) and throwing away some of the drawn types such as gl_polygon,

Here we will share with you the alternative method of using gldrawarray to draw polygon.
And problems that occur when gldrawelements is used to draw a triangle mesh.

Gldrawarray (gl_polygon, index, nvert) is used to draw a polygon under OpenGL. The third parameter is the number of points, and the second parameter is the index (Label) of the current polygon point ), this function will find the index point from the array, and find the nvert point backward. Use these points to draw a polygon in sequence,

But under OpenGL ES, I'm sorry, I can't use it anymore, but I can only draw triangles. So, to draw the original polygon, you can only perform triangle division on this polygon (such as the Adaboost, of course, it may be silly to do this, because in fly3d, the data originally exists as a triangle, just for convenience, we export it into a polygon ).

For example, when exporting data, we export four vertices (V1, V2, V3, V4 ),
Now, we need to split it into two triangles and convert it to (V1, V2, V3), (V1, V3, V4) during export ),
When drawing,
Gldrawarray (gl_triangles, index, 6); // six points, two triangles
That's all.

Gldrawelements:
Note that in OpenGL, we use the following:

Gldrawelements (gl_triangles, ntrivert, gl_unsigned_int, trivert );
The first parameter is of type.
Number of the second Vertex
The third parameter indicates the type of the fourth parameter.
The fourth parameter is the index data of the triangle.

It can be understood that the quadrilateral is used as an example. A bit (V1, V2, V3, V4) is saved in the array, and the second parameter is 4, that is, four vertex data,
How can these four vertex data form two triangles? Trivert is actually an index array, and its number should be six. Each value records a point in the corresponding quadrilateral data. for example, trivert [0] = 0, trivert [1] = 1, trivert [2] = 3, that is, the first triangle is composed of (V1, V2, V3)

It's all nonsense. You should know that here, we should pay attention to the third parameter. gl_unsigned_int is not supported in OpenGL ES. Currently, only gl_unsigned_byte is supported, the gl_unsigned_short parameters are four bytes at the int point, one byte, and two short parameters,

Therefore, you must note that the trivert index array type must be glubyte or glushort.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zhucde/archive/2008/12/18/3547442.aspx

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.