VA, Vao, and VBO API memos

Source: Internet
Author: User
Tags arrays
Origin

Contact OpenGL for some time, most of the time is the use of OSG in the development, a few days ago, colleagues asked about OpenGL VBO related content, only to find a lot of OpenGL knowledge unfamiliar. The two smallpox had a little time to re-review the content related to Vbo, recorded here for forgetting. Most of the content comes from the web. Overview

OpenGL has divided the API into two types from OpenGL 3.0: The Legacy OpenGL (Legacy OpenGL) and the new OpenGL (Core profile), OpenGL3.3 's official API document that completely describes the new OpenGL API, the legacy OpenGL API can be viewed in OpenGL 2.1.

The first station for OpenGL rendering: The introduction of vertex data (including vertex position, vertex normals, vertex colors, texture coordinates, etc.) into OpenGL, which is implemented in the following ways in legacy OpenGL: Immediate Mode (glbegin/glend) This is probably the first API to learn OpenGL exposure, at least I do. But unfortunately, it's been abandoned by new OpenGL.
A vertex array (Va:vertex array) reduces the call cost of the function compared to the immediate mode. It is also currently being compared to VA by the new OpenGL discard buffer Object (Vbo:vertex), which stores the data on the server side (VA is stored on the client). is currently the only data incoming method supported by new OpenGL
The display list compiles several OpenGL commands, which are called directly by the video card. Because the compiled modules cannot be modified, the flexibility is lost. And abandoned by new OpenGL.

In addition, VBO is used in legacy OpenGL and core profile OpenGL in different ways. VA API

(1) Turn VA on and off

Turn on and off client status
void Glenableclientstate (glenum cap);
void Gldisableclientstate (glenum cap);

Parameter Cap Value
gl_vertex_array					//Vertex position		
gl_color_array					//Vertex color
gl_edge_flag_array				//vertex boundary line identification
Gl_fog_coord_array				//vertex fog coordinates
gl_index_array					//Vertex index
gl_normal_array					//vertex normals
Gl_ Secondary_color_array			//vertex auxiliary color
gl_texture_coord_array				//vertex texture coordinates

(2) Set data to vertex

The following is a series of API//parameter values for setting vertex data (  The default parameter represents the default value in OpenGL)://Size Description Data Dimension (2D\3D)//Type description Each data types//Stride describes the span of each vertex data//pointer point to actual data//set vertex position data void 
Glvertexpointer (Glint size=4, Glenum type=gl_float, Glsizei stride=0, const glvoid *pointer=0); 
Set vertex color data void glcolorpointer (Glint size=4, Glenum type=gl_float, Glsizei stride=0, const glvoid *pointer=0); 
Set vertex boundary line identification data void Gledgeflagpointer (Glsizei stride=0, const glvoid *pointer=0); 
Set vertex fog coordinate data void Glfogcoordpointer (Glenum type=gl_float, Glsizei stride=0, glvoid *pointer=0); 
Set vertex index data void Glindexpointer (Glenum type=gl_float, Glsizei stride=0, const glvoid *pointer=0); 
Set vertex normals data void Glnormalpointer (Glenum type=gl_float, Glsizei stride=0, const glvoid *pointer=0); Set vertex secondary color data void glsecondarycolorpointer (Glint size=3, Glenum type=gl_float, Glsizei stride=0, const glvoid *pointer= 
0); Set vertex texture coordinate data void gltexcoordpointer (glint size=4, Glenum type=gl_float, Glsizei stride=0, const glvoid *pointer=0);  

(3) VA drawing

//////////////////////////////////////////////////////////////////////////////////////////////
Draw with set-up vertex data: You can use the following APIs to draw void Gldrawarrays (Glenum mode, glint first, glsizei count); Parameter Description//mode The start index position of the//first vertex data (gl_points, Gl_line_strip, Gl_triangle, etc.) of the drawing geometry type (corresponding to (2) pointer)//count needs to be rendered 
Vertex data number void Gldrawelements (glenum mode, Glsizei count, glenum type, const glvoid *indices); Parameter description//mode drawing geometry type (gl_points, Gl_line_strip, Gl_triangle, etc.)//count fourth parameter indices number of indexes participating in rendering//type fourth parameter in  The Dices data type//indices requires an additional incoming index array pointer void gldrawrangeelements (glenum mode, gluint start, Gluint end, Glsizei count,
Glenum type, const glvoid * indices); Parameter description (same as gldrawelements but two more parameters)//mode drawing geometry type (gl_points, Gl_line_strip, Gl_triangle, etc.)//count fourth parameter indices participate in rendering The number of indexes//type the data type of the fourth parameter indices//indices an index array pointer that needs to be passed in//start Index array indices the minimum index value//end index array Indices The maximum index value in the 

The above is the basic API involved in VA, the use of the process is as follows

First, the client Vertexarray state is turned on, then the data is bound to the vertex state and finally drawn:

static float Vertices[][3] = {
	1.0f, 0.0f, 0.0f,
	0.0f, 1.0f, 0.0f,
	-1.0f, 0.0f, 0.0f
};

static float Colors[][4] = {
	1.0f, 0.0f, 0.0f, 1.0f,
	0.0f, 1.0f, 0.0f, 1.0f,
	0.0f, 0.0f, 1.0f, 1.0f
};
  
   //draw function
void Renderscene ()
{
	Glclear (gl_color_buffer_bit | Gl_depth_buffer_bit);

	Glmatrixmode (Gl_modelview);
	Glloadidentity ();
	Gltranslatef (0.0f, 0.0f, -5.0f);
	//Turn on VA status
	Glenableclientstate (Gl_vertex_array);
	Glenableclientstate (Gl_color_array);
	Binding Data
	Glvertexpointer (3, gl_float, 0, vertices);
	Glcolorpointer (4, gl_float, 0, colors);
	Draw
	Gldrawarrays (gl_triangles, 0, 3);
	Turn off VA status
	gldisableclientstate (Gl_vertex_array);
	Gldisableclientstate (Gl_color_array);
	//////////////////////////////////////////////////////////////////////////
}	
  

VBO API

The previous article described VBO in Legacy and core profile in two different ways: Legacy OpenGL VBO API

(1) creating (initializing) Vbo objects

Create VBO API
//////////////////////////////////////////////////////////////////////////
void Glgenbuffers (Glsizei   N, gluint *   buffers); 
Generate buffer ID
//Parameter description
//n			Generate ID number
//buffers storage buffer ID array

void glbindbuffer (Glenum   Target,  gluint   buffer); 
Sets the buffer to the current operation's buffer, and sets the buffer type
//Parameter description
//target			buffer type (including 4 kinds)
Gl_array_buffer
Gl_element_ Array_buffer
gl_pixel_pack_buffer
gl_pixel_unpack_buffer
//buffer        using glgenbuffers generated buffer ID

void Glbufferdata (glenum   target,  glsizeiptr   size,  const glvoid *   data,  glenum   usage); 
Sets the data
//parameter description for the buffer
//target			buffer type (including 4 types)
gl_array_buffer
gl_element_array_buffer
Gl_pixel_pack_buffer
gl_pixel_unpack_buffer
//size             buffer size (how many bytes)
//data		   actually pointer
to an array Usage		   mode of the current buffer [a hint for optimization] (9 values below)
gl_stream_draw
gl_stream_read
gl_stream_copy
Gl_static_draw
gl_static_read
gl_static_copy
gl_dynamic_draw
gl_dynamic_read
gl_ Dynamic_copy
(2) Turn Vbo on/off

When the VBO is created, we don't know whether the vertex position, the vertex color, or the vertex normals are stored in VBO, so use the following API to describe exactly what the vertex is in a vbo.

//Turn off VBO status
void Glenableclientstate (glenum cap);
void Gldisableclientstate (glenum cap);
Refer to the description in VA


void glvertexpointer (Glint   size,  glenum   type,  glsizei   stride,  const Glvoid *   pointer); 
Refer to the description in VA:
//But the pointer parameter is different:
//When using Glbindbuffer to bind Gl_array_buffer to a VBO ID other than 0, then the pointer
/ /represents an offset value for the actual data in the Vbo ID object (the data parameter in Glbufferdata), which means that this time pointer
//is not a pointer, just an offset value. At the same time gl_array_buffer_binding this state will be saved in the client

//synthesis above VA explanation: You can know the pointer in this glvertexpointer two meanings:
//(1) When the Glbindbuffer is bound to an ID other than 0:
//pointer represents the offset, and the client glclientstate is saved gl_array_buffer_binding the hint is now in VBO
//(2) When Glbindbuffer is bound to the ID of 0:
//pointer represents a pointer to an array, which is not saved in the client glclientstate the gl_array_buffer_binding hint is now in VA

/ /Similarly, for a series of functions used in VA have the same interpretation, no longer repeat:
glcolorpointer
gledgeflagpointer
glfogcoordpointer
Glindexpointer
glnormalpointer
glsecondarycolorpointer
gltexcoordpointer
(3) Vbo drawing
//Draw Vbo

void Gldrawarrays ( Glenum mode,  Glint First,  glsizei count);
The reference VA describes
//differs in that the data is now in the buffer, not the pointer array that the VA points to in the

void Gldrawelements (glenum mode,  Glsizei count,  Glenum type,  const glvoid *indices);
Reference VA Description
//The difference is indices set to null
//Because the buffer type has a gl_element_array_buffer (see type description in Glgenbuffers)
// We will store the indices index value in buffer of type Gl_element_array_buffer, no additional array is required
//directly with two buffers
These are the APIs that implement VBO in legacy OpenGL, using the following procedure:

First create the buffer and set the buffer type and fill the buffer, then specify what is inside the buffer (whether it is the position or color or normal), and then draw

static float Vertices[][3] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f};

static float Colors[][4] = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f}; void Renderscene () {Glclear (Gl_color_buffer_bit |

	Gl_depth_buffer_bit);
	Glmatrixmode (Gl_modelview);
	Glloadidentity ();
	
	Gltranslatef (0.0f, 0.0f, -5.0f); Create VBO (typically in the initialization function) Gluint
	Vertexbufferid, Colorbufferid;
	Glgenbuffers (1, &vertexbufferid);
	Glbindbuffer (Gl_array_buffer, Vertexbufferid);
	Glbufferdata (Gl_array_buffer, sizeof (vertices), vertices, gl_static_draw);
	Glgenbuffers (1, &colorbufferid);
	Glbindbuffer (Gl_array_buffer, Colorbufferid);

	Glbufferdata (gl_array_buffer, sizeof (colors), colors, gl_static_draw);
	Open Vbo mode and set Vbo how to parse Glenableclientstate (Gl_vertex_array);
	Glenableclientstate (Gl_color_array);
	Glbindbuffer (Gl_array_buffer, Vertexbufferid);
	Glvertexpointer (3, gl_float, 0, 0);Glbindbuffer (Gl_array_buffer, Colorbufferid);

	Glcolorpointer (4, gl_float, 0, 0);

	Draw Gldrawarrays (gl_triangles, 0, 3);
	Close the VBO state after drawing is complete//set Glbindbuffer to 0 so that subsequent glvertexpointer similar functions play the role of VA gldisableclientstate (Gl_vertex_array);
	Gldisableclientstate (Gl_color_array);
	Glbindbuffer (gl_array_buffer, 0);	 //////////////////////////////////////////////////////////////////////////
}

Core Profile VBO

(1) Create Vbo

Refer to the contents of legacy VBO, both of which are the same

(2) Turn Vbo on/off

void Glenablevertexattribarray ( Gluint   index); 
void Gldisablevertexattribarray (Gluint   index); 
Turns on and off a common buffer object
//Parameter description
//index Buffer Object index value

void Glvertexattribpointer (gluint   index,  
											  Glint            size,  
											  Glenum           type,  
											  glboolean        normalized,  
											  Glsizei          stride,  
											  const glvoid *   pointer
											  ); 
Sets how to parse the data in the buffer
//Parameter Description
//index The				current operation's Buffer object index value
//size				the dimension of the data is 2d/3d/4d
//type				The data type Gl_int/gl_float
//normalized	has been
//stride				data spacing
//pointer			Reference Legacy VBO explained ( Glbindbuffer is the index value when the pointer is open when it is not turned on)

(3) Drawing

Refer to the contents of legacy VBO, both of which are the same

These are the VBO under core profile, using the following methods:

The Vbo object is created first, followed by the VBO and used to describe what the data in the VBO is organized (but does not indicate whether the data is vertex position, vertex color, or normal), unlike Legacy VBO, because its description section (which Vbo stores the location, which VBO stores the color, Which Vbo store the normals, etc.) is indicated in the shading language shader, and is then drawn using the same API:

Glfloat vverts[] = { -0.5f, 0.0f, 0.0f,

0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f};
Glfloat Vcolors [] = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f}; void Renderwidget::p aintgl () {////////
	Generate Vbo glgenbuffers (1, &_vertexbuffer);
	Glbindbuffer (Gl_array_buffer, _vertexbuffer);

	Glbufferdata (Gl_array_buffer, sizeof (vverts), Vverts, Gl_static_draw);
	Glgenbuffers (1, &_colorbuffer);
	Glbindbuffer (Gl_array_buffer, _colorbuffer);

	Glbufferdata (Gl_array_buffer, sizeof (vcolors), vcolors, Gl_static_draw);

	Setshaders ();
	Turn on the VBO and set the data mode stored in the Vbo glenablevertexattribarray (0);
	Glbindbuffer (Gl_array_buffer, _vertexbuffer);
	Glvertexattribpointer (0, 3, gl_float, Gl_false, 0, (void*) 0);
	Glenablevertexattribarray (1); GlbindbuffeR (Gl_array_buffer, _colorbuffer);

	Glvertexattribpointer (1, 4, gl_float, Gl_false, 0, (void*) 0);

	Draw Vbo Gldrawarrays (gl_triangles, 0, 3);
	Close Vbo Gldisablevertexattribarray (1);
	Gldisablevertexattribarray (0);
Glbindbuffer (gl_array_buffer, 0); }
Shader part :

Vertex shader

#version
(location = 0) in vec4 Vertex;
Layout (location = 1) in VEC4 color;
Out VEC4 Infragcolor;

void main (void)
{
	gl_position = vertex;
	Infragcolor = color;
}
piece element Shader

#version
vec4 Infragcolor;
Out VEC4 Outfragcolor;

void main (void)
{
	outfragcolor = Infragcolor;
}

With vertex shader you can know that an index value of 0 VBO is interpreted as the vertex position, which is passed to Gl_position, and Vbo with an index value of 1 is interpreted as the color of the vertex

VAO API

Vao The introduction can refer to <<ab is a? Vao and Vbo>>

(1) Initialize

//Initialize Vao

void Glgenvertexarrays (Glsizei N,  gluint *arrays);
Create Vao ID
//Parameter description
//n					generate Vao ID number
//arrays			        save Vao ID array

void Glbindvertexarray (gluint array) ;
Sets the ID of the Vao object
//Parameter Description
//array              Vao for the current operation

(2) Turn Vao on/off

Vao Open when using Glbindvertexarray, using Glbindvertexarray (0), passing in a 0 value can be considered as closing the Vao

The process of setting up Vao is to call the procedure in VBO (2)

(3) Drawing

Draw with Vbo (enable Vao before drawing)

These are the APIs that VAO involves, using the following procedures:

First create the Vao, then set the Vao (Vao Auto-complete setting during the call to Vbo's data setup), and finally turn on Vao and draw:

Glfloat vverts[] = { -0.5f, 0.0f, 0.0f,

0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f};
Glfloat Vcolors [] = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f}; void Renderwidget::p aintgl () {////////
	Generate Vbo glgenbuffers (1, &_vertexbuffer);
	Glbindbuffer (Gl_array_buffer, _vertexbuffer);

	Glbufferdata (Gl_array_buffer, sizeof (vverts), Vverts, Gl_static_draw);
	Glgenbuffers (1, &_colorbuffer);
	Glbindbuffer (Gl_array_buffer, _colorbuffer);

	Glbufferdata (Gl_array_buffer, sizeof (vcolors), vcolors, Gl_static_draw);

	
    Setshaders ();
	Initialize Vao gluint vaobuffer;
	Glgenvertexarrays (1, &vaobuffer);

	Glbindvertexarray (Vaobuffer);
	Complete the Vao setting by setting the data mode stored in the Vbo glenablevertexattribarray (0);
	Glbindbuffer (Gl_array_buffer, _vertexbuffer); GlvertexattribPointer (0, 3, gl_float, Gl_false, 0, (void*) 0);
	Glenablevertexattribarray (1);
	Glbindbuffer (Gl_array_buffer, _colorbuffer);
	Glvertexattribpointer (1, 4, gl_float, Gl_false, 0, (void*) 0);
	Glbindvertexarray (0);

	Glbindbuffer (gl_array_buffer, 0); All of the above code is called in the initialization function//Only the drawing code is called in the Render function//
	Use Vao to draw Glbindvertexarray (Vaobuffer);
	Gldrawarrays (gl_triangles, 0, 3);
Glbindvertexarray (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.