当前位置:网站首页>OpenGL's distinction and understanding of VAO, VBO and EBO
OpenGL's distinction and understanding of VAO, VBO and EBO
2022-07-07 15:39:00 【Vegetable chicken on the road】
1.VBO
VBO Buffer objects for vertices , It is a memory buffer area opened up in the storage space of the graphics card , Used to store various attribute information of vertices , Such as vertex coordinates , Vertex normal vector , Vertex color data, etc . At the time of rendering , Can be directly from VBO Take out various attribute data of vertices .
So it can be interpreted as VBO Is a storage area in the video memory , You can keep a lot of vertex attribute information . And can open up many VBO, Every VBO stay OpenGL It has its unique identification in ID, This ID Corresponding to the specific VBO The video memory address of , Through this ID It can be used for specific VBO Access the data in the .


Pictured above , Define two vbo, We draw vbo Triangle under data , But what appears is vbo2 Lower triangle , Why? ?
because , Before we draw , The location in the video memory has been defined 0 Points to memory vbo, But then we show the location in the middle 0 Points to vbo2, So what we see is the last triangle .
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); // from layout by 0 Start , A vertex contains three data , Span 6 A set of vertices , Start with 0 Read
So how to solve this problem ?

Above , We only need to specify a temporary memory location to draw when drawing , You can see that the drawn triangle is normal .
1.VAO
VAO Attributes of vertices are defined in , Each attribute has a specific read VBO Methods , It's equivalent to a VBO A collection of data objects , Use glVertexAttribPointer Settings for vertex attributes , So we know one vbo Which information should be specific . Pictured :

Here we can solve the above vbo When drawing two triangles , The last problem that always appears , The following code :

This is the time , What we see is , He draws the first triangle .
EBO
for instance : Suppose we draw a rectangle . We can draw two triangles to form a rectangle . So we need six vertex sets , And there will be two overlapping vertices ( Points in the lower left corner and upper right corner ). So when there are more vertices , There will be a lot of waste . So the best solution is to save each vertex only once , When we need to use these vertices , Only call the index of the vertex .
EBO The content stored in is the index of vertex position indices,EBO Follow VBO similar , It is also a memory buffer in video memory , It's just EBO Save the index of vertices .
When used EBO When drawing a model by binding vertex indexes , Need to use glDrawElements instead of glDrawArrays: glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- The first parameter specifies the mode to draw ;
- The second parameter specifies the number of vertices to paint ;
- The third parameter is the data type of the index ;
- The fourth parameter is optional EBO Medium offset setting .
as follows , Use EBO Draw a rectangle consisting of two triangles :
// Use EBO Draw a rectangle ( Two triangles )
#include <GL/glew.h>
#include <GL/freeglut.h>
void userInit(); // Custom initialization
void reshape(int w, int h); // Repaint
void display(void);
void keyboardAction(unsigned char key, int x, int y); // Keyboard exit event
GLuint eboId;//element buffer object Handle
GLuint vboId;//vertext buffer object Handle
GLuint vaoId;//vertext array object Handle
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(512, 512);
glutCreateWindow("Rectangle demo");
// Use glew, You need to perform glewInit, Otherwise, an error will be reported during operation
//glewInit Put it on glut After basic initialization, execute
glewInit();
// Custom initialization , Generate VAO,VBO,EBO
userInit();
// Redraw function
glutReshapeFunc(reshape);
glutDisplayFunc(display);
// Register the keyboard key exit event
glutKeyboardFunc(keyboardAction);
glutMainLoop();
return 0;
}
// Custom initialization function
void userInit()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
// Create vertex data
const GLfloat vertices[] = {
-0.5f,-0.5f,0.0f,1.0f,
0.5f,-0.5f,0.0f,1.0f,
0.5f,0.5f,0.0f,1.0f,
-0.5f,0.5f,0.0f,1.0f,
};
// Index data
GLshort indices[] = {
0, 1, 3, // The first triangle
1, 2, 3 // The second triangle
};
// establish VAO object
glGenVertexArrays(1, &vaoId);
glBindVertexArray(vaoId);
// establish VBO object , Copy the vertex array into a vertex buffer , for OpenGL Use
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// establish EBO object
glGenBuffers(1, &eboId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
// Pass in EBO data
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Explain how vertex data is processed
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
// Unbundling VAO
glBindVertexArray(0);
// Unbundling EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// Unbundling VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// Resize window callback function
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
}
// Drawing callback functions
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
// binding VAO
glBindVertexArray(vaoId);
// Draw a model
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL);
glutSwapBuffers();
}
// Keyboard key callback function
void keyboardAction(unsigned char key, int x, int y)
{
switch (key)
{
case 033: // Escape key
exit(EXIT_SUCCESS);
break;
}
}
Divide a rectangle into triangles with six vertices , Respectively 0,1,2,1,2,3 Draw again 
边栏推荐
- [data mining] visual pattern mining: hog feature + cosine similarity /k-means clustering
- 【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
- Gd32 F3 pin mapping problem SW interface cannot be burned
- Oracle控制文件丢失恢复归档模式方法
- Starting from 1.5, build a microservice framework link tracking traceid
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
- 【OBS】RTMPSockBuf_ Fill, remote host closed connection.
- Connecting FTP server tutorial
- Mathematical modeling -- what is mathematical modeling
- Getting started with webgl (1)
猜你喜欢
MongoD管理数据库的方法介绍

Ctfshow, information collection: web1

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Bye, Dachang! I'm going to the factory today
Configure mongodb database in window environment

Keil5 does not support online simulation of STM32 F0 series

如何在opensea批量发布NFT(Rinkeby测试网)

2022全开源企业发卡网修复短网址等BUG_2022企业级多商户发卡平台源码
![[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset](/img/69/9dadeb92f8d6299250a894690c2845.png)
[deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
![[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)](/img/3f/40475f9f6e0fcd3f58c93164f65674.png)
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
随机推荐
MySQL bit类型解析
What is Base64?
避坑:Sql中 in 和not in中有null值的情况说明
Ida Pro reverse tool finds the IP and port of the socket server
Why do we use UTF-8 encoding?
Ctfshow, information collection: web13
HW初级流量监控,到底该怎么做
[quick start of Digital IC Verification] 24. AHB sramc of SystemVerilog project practice (4) (AHB continues to deepen)
大表delete删数据导致数据库异常解决
Configure mongodb database in window environment
什麼是數據泄露
Nacos一致性协议 CP/AP/JRaft/Distro协议
Create lib Library in keil and use lib Library
Connecting FTP server tutorial
摘抄的只言片语
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
Typescript release 4.8 beta
【原创】一切不谈考核的管理都是扯淡!
Getting started with webgl (2)
A need to review all the knowledge, H5 form is blocked by the keyboard, event agent, event delegation