OpenGL ES 3.0 Vertexattributes,vertex arrays,and Buffer Objects (ix)

Source: Internet
Author: User

Vertex data, also known as vertex attributes, refers to each vertex data. Refers to the data that can be used to describe each vertex, or a constant value that can be used by all vertices. For example, you want to draw a cube triangle with color. You specify a constant value for all three vertex colors of the triangle. But the three vertex positions of the triangles are different, you need to specify a vertex matrix to store three position values.

specifying vertex attribute Data

Vertex property data You can specify each vertex data using vertex arrays or constant values, and OpenGL ES 3.0 must support at least 16 vertex attributes. The application should be able to query the exact number of attributes supported by the compiler. The following program indicates how to query.

// n'll be >= 8glgetintegerv (Gl_max_vertex_attribs, &maxvertexattribs);

  

Constant vertex properties
A constant vertex property is the same for all vertex properties of a primitive, so only all vertices of the primitive need to specify a value. Vertex property constants are specified using the following function:

voidglvertexattriblf (gluint Index, glfloat x);voidglvertexattrib2f (gluint Index, glfloat x, glfloat y);voidglvertexattrib3f (gluint Index, Glfloat x, glfloat y,glfloat z);voidglvertexattrib4f (gluint Index, Glfloat x, Glfloat y,glfloat Z, glfloat W);voidGLVERTEXATTRIBLFV (Gluint Index,ConstGlfloat *values);voidGLVERTEXATTRIB2FV (Gluint Index,ConstGlfloat *values);voidGLVERTEXATTRIB3FV (Gluint Index,ConstGlfloat *values);voidGLVERTEXATTRIB4FV (Gluint Index,ConstGlfloat *values);

 

Glvertexattrib* are loaded in general vertex properties by index, GLVERTEXATTRIBLF and GLVERTEXATTRIBLFV load (x, 0.0, 0.0, 1.0) to vertex properties, glvertexattrib2f and GLVERTEXATTRIB2FV load (x, Y, 0.0, 1.0), glvertexattrib3f and GLVERTEXATTRIB3FV load (x, Y, Z, 1.0), glvertexattrib4f and
GLVERTEXATTRIB4FV load (x, Y, Z, W)

Vertex array

The vertex array specifies that the property data for each vertex is the data stored in the application address space (OpenGL ES called clientspace) buffer. They provide an efficient and flexible way to specify vertex attribute data. The vertex array is specified using the Glvertexattribpointer or Glvertexattribipointer function:

Stores all vertex properties in a buffer, this method of storing vertex attributes is called a structure array, which describes all the properties of each vertex. Storing each vertex attribute to a separate buffer, this method of storing vertex properties is called the array structure each vertex has four properties-position, normals, two mapping coordinates, these properties are stored in a buffer, shared by all vertices. The vertex position property is a vector of three floating-point numbers (x, Y, z). Normals are also vectors of three floating-point numbers, and each map coordinate is a vector of two floating-point numbers.

Structure Array code example

#defineVertex_pos_size 3//x, y, and Z#defineVertex_normal_size 3//x, y, and Z#defineVertex_texcoord0_size 2//s and T#defineVertex_texcoordl_size 2//s and T#defineVertex_pos_indx 0#defineVertex_normal_indx 1#defineVertex_texcoord0_indx 2#defineVertex_texcoordl_indx 3//The following 4 defines is used to determine the locations//of various attributes if vertex data is stored as an array//of Structures#defineVertex_pos_offset 0#defineVertex_normal_offset 3#defineVertex_texcoord0_offset 6#defineVertex_texc00rd1_0ffset 8#defineVertex_attrib_size (vertex_pos_size + \vertex_normal_size+vertex_texcoord0_size+vertex_texc00rd1_size)
float*p = (float*) malloc (numvertices *vertex_attrib_size*sizeof(float));//position is vertex attribute 0Glvertexattribpointer (Vertex_pos_indx, Vertex_pos_size, Gl_float, Gl_false, Vertex_attrib_si ZE*sizeof(float), p);//Normal is vertex attribute 1Glvertexattribpointer (Vertex_normal_indx, Vertex_normal_size, Gl_float, Gl_false, vertex_attr Ib_size*sizeof(float), (P+vertex_normal_offset));//Texture coordinate 0 is vertex attribute 2Glvertexattribpointer (Vertex_texcoordo_indx, Vertex_texcoord0_size, Gl_float, Gl_false, Vertex_attrib_size*sizeof(float), (P+vertex_texcoord0_offset));//texture coordinate 1 is vertex attribute 3Glvertexattribpointer (Vertex_texcoordl_indx, Vertex_texc00rd1_size, Gl_float, Gl_false, Vertex_attrib_size*sizeof(float), (P+ Vertex_texc00rd1_0ffset));

Array Structure Sample code

float*position = (float*) malloc (numvertices *vertex_pos_size*sizeof(float));float*normal = (float*) malloc (numvertices *vertex_normal_size*sizeof(float));float*texcoordo = (float*) malloc (numvertices *vertex_texcoord0_size*sizeof(float));float*TEXCOORDL = (float*) malloc (numvertices *vertex_texc00rd1_size*sizeof(float));//position is vertex attribute 0Glvertexattribpointer (Vertex_pos_indx, Vertex_pos_size, Gl_float, Gl_false, vertex_pos_size *sizeof(float), position);//Normal is vertex attribute 1Glvertexattribpointer (Vertex_normal_indx, Vertex_normal_size, Gl_float, Gl_false, Vertex_nor Mal_size*sizeof(float), normal);//Texture coordinate 0 is vertex attribute 2Glvertexattribpointer (Vertex_texcoordo_indx, Vertex_texcoord0_size, Gl_float, Gl_false, Vertex_texcoord0_size*sizeof(float), Texcoordo);//texture coordinate 1 is vertex attribute 3Glvertexattribpointer (Vertex_texcoordl_indx, Vertex_texc00rd1_size, Gl_float, Gl_false, Vertex_texc00rd1_size*sizeof(float), texcoordl);

Performance Tips

1. How to store different vertex properties.

Using an array of structures is better than using an array structure, because the property data of each vertex can be read continuously, and this memory structure is more efficient. But using the array of structures is bad when we want to modify the specified property. If a vertex property needs to be modified (like mapping coordinates), this will have to update the vertex buffer. When the vertex buffer is used as a buffer object, the entire

The vertex attribute buffers will need to be updated to load

2. What data format is used for vertex attributes

The vertex attribute data format is specified by calling the parameter type of the Glvertexattribpointer function, which affects not only the storage requirements for the drawing data of the vertex attribute, but also all execution, which is the memory bandwidth requirement when rendering the frame. The smaller the amount of data, the less bandwidth is required. OpenGL ES 3 supports 16-bit floating point vertex format naming gl_half_float, it is recommended to use gl_half_float,texture coordinates, normals, binormals, tangent Vectors are suitable for storing with gl_half_float, the color uses four gl_unsigned_byte to store each vertex color, and the vertex position should be stored as gl_float.

3. How to standardize glvertexattribpointer work

The vertex property is stored in memory as a single-precision floating-point value before being used by the vertex shader. If the data types of the vertex properties are not floating-point numbers, their values are converted to floating-point values before the shader is used. The normalized flag indicates that non-floating vertex attribute data is converted to a single precision floating point value. If the normalized character is false, the vertex values are converted directly to floating-point values, and the conversion of non-floating-point variables is similar to floating-point types

// F represents values in the range [ -128.0,//  127.0]

If normalized is true, the vertex data type is gl_byte, Gl_short or gl_fixed is matched to [ -1.0,1.0], data type if Gl_unsigned_byte or Gl_unsigned_short is matched to [0.0,1.0]. The following describes the non-floating-point value data type normalized conversion process

It is also possible to access vertex shaders whose integer vertex property data is integers, without converting them to floating-point numbers. In this case, the Glvertexattribipointer feature should use vertex properties that should be declared as an integer type for vertex coloring.

4. Select between the constant vertex attribute and the vertex array

intInit (Escontext *Escontext) {UserData*userdata = (userdata*) escontext->UserData;Const CharVshaderstr[] ="#version es \ n""Layout (location = 0) in vec4 a_color; \ n""Layout (location = 1) in vec4 a_position; \ n""Out vec4 v_color; \ n""void main () \ n""{\ n""v_color = A_color; \ n""gl_position = a_position; \ n""}";Const CharFshaderstr[] ="#version es \ n""precision mediump float; \ n""In vec4 v_color; \ n""Out vec4 o_fragcolor; \ n""void main () \ n""{\ n""o_fragcolor = V_color; \ n""}" ; Gluint Programobject;//Create The program objectProgramobject =Esloadprogram (Vshaderstr, fshaderstr);if(Programobject = =0 )returnGl_false;//Store The program objectUserdata->programobject =Programobject; Glclearcolor (0.0f,0.0f,0.0f,0.0f );returnGl_true;}
voidDraw (Escontext *Escontext) {UserData*userdata = (userdata*) escontext->UserData; Glfloat color[4] = {1.0f,0.0f,0.0f,1.0f };//3 vertices, with (x, Y, z) Per-vertexGlfloat vertexpos[3*3] ={0.0f,0.5f,0.0f,//V0-0.5f, -0.5f,0.0f,//v10.5f, -0.5f,0.0f //v2}; Glviewport (0,0, Escontext->width, escontext->height);  Glclear (Gl_color_buffer_bit); Gluseprogram (UserData-programobject); GLVERTEXATTRIB4FV (0, color); Glvertexattribpointer (1,3, Gl_float, Gl_false,0, Vertexpos); Glenablevertexattribarray (1 ); Gldrawarrays (Gl_triangles,0,3 ); Gldisablevertexattribarray (1 );}

OpenGL ES 3.0 Vertexattributes,vertex arrays,and Buffer Objects (ix)

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.