OpenGL quick rendering function gldrawelements instructions

Source: Internet
Author: User

Basic OpenGL drawing functions, such as glvertex and glnormal, run in debugging mode. If the number of vertices or triangles of the model is too large (for example, when the number exceeds 10 thousand), the program runs very slowly, normal debugging cannot be performed at all. For this reason, I checked the relevant information and found the glarrayelement and gldrawelements functions. Both functions can draw a large amount of data by calling a few statements, thus saving the resource usage of function calls.

The glarrayelement function is simple and straightforward to use. As long as all vertices, normal vectors, and other data are prepared in the order of triangles, it can be directly rendered. However, the disadvantage is that vertex indexes are not supported, therefore, the memory usage is large. For example, if a grid has 100 vertices, there will usually be about 200 triangles. If glarrayelement is used, you need to store 200x3 = 600 vertices, 5 times more than the original data. If the redundant data is striped, the redundant data may be more than five times. We decided to use the gldrawelements function. The gldrawelements function supports the vertex data list. more conveniently, it also supports vertex indexes, so it becomes the first choice for Fast Rendering. However, in the specific use process, I have never drawn any vertex data. I checked the relevant information. It was neither a data error nor a hardware failure, so I had to temporarily put it aside. By chance, I saw some sample code and found that the index data type parameter in gldrawelements is gl_unsigned_int, and the parameters I used earlier are all gl_int (because of the needs of the algorithm, sometimes you need to store a negative index to indicate the difference between positive and negative directions ). With a try, I changed the parameter to gl_unsigned_int, and the image was drawn. Check out msdn and explain gldrawelements as follows: Void gldrawelements (
Glenum
Mode ,
Glsizei Count ,
Glenum Type ,
Const glvoid * Indices
);

Parameters
Mode
The kind of primitives to render. It can assume one of the following symbolic values: gl_points, points, gl_line_loop, gl_lines, points, points, gl_triangles, points, gl_quads, and gl_polygon.
Count
The number of elements to be rendered.
Type
The type of the values in indices. Must be one of gl_unsigned_byte, gl_unsigned_short, or gl_unsigned_int.
Indices
A pointer to the location where the indices are stored.

Note the description of the type parameter and the index data type.RequiredIt is one of gl_unsigned_byte, gl_unsigned_short, and gl_unsigned_int. This explains why gldrawelements did not draw any elements. What's cool is that OpenGL didn't give any tips on this issue. I wonder if Microsoft intends to play down the role of OpenGL so far. If it has the opportunity to use the compiler in Linux to perform tests in another day. Finally, I attached some of the code I called during rendering. // Vertex
Glenableclientstate (gl_vertex_array );
Glvertexpointer (3, gl_float, 0, (float *) m_vdatacoord [0]); // Normal Vector
Glenableclientstate (gl_normal_array );
Glnormalpointer (gl_float, 0, (float *) m_vdatanormal [0]); // vertex color
Glableclientstate (gl_color_array );
Glcolorpointer (3, gl_float, 0, (float *) m_vdatacolor [0]); // texture coordinate
Glenableclientstate (gl_texture_coord_array );
Gltexcoordpointer (2, gl_float, 0, (float *) m_vdatautonomy [0]);
Gldrawelements (gl_triangles, (glsizei) m_vindexcoord.size () * 3, gl_unsigned_int, (glvoid *) m_vindexcoord [0]);

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.