OpenGL笔记五之VBO与VAO
总结自bilibili赵新政老师的教程
code review!
文章目录
- OpenGL笔记五之VBO与VAO
- 1.VBO
- 2.VAO
- 3.VBO与VAO对比
1.VBO
代码
void prepareVBO() {//1 创建一个vbo *******还没有真正分配显存*********GLuint vbo = 0;GL_CALL(glGenBuffers(1, &vbo));//2 销毁一个vboGL_CALL(glDeleteBuffers(1, &vbo));//3 创建n个vboGLuint vboArr[] = {0, 0, 0};GL_CALL(glGenBuffers(3, vboArr));//4 销毁n个vboGL_CALL(glDeleteBuffers(3, vboArr));
}void prepare() {float vertices[] = {-0.5f, -0.5f, 0.0f,0.5f, -0.5f, 0.0f,0.0f, 0.5f, 0.0f};//1 生成一个vboGLuint vbo = 0;GL_CALL(glGenBuffers(1, &vbo));//2 绑定当前vbo,到opengl状态机的当前vbo插槽上//GL_ARRAY_BUFFER:表示当前vbo这个插槽GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo));//3 向当前vbo传输数据,也是在开辟显存GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW));
}void prepareSingleBuffer() {//1 准备顶点位置数据与颜色数据float positions[] = {-0.5f, -0.5f, 0.0f,0.5f, -0.5f, 0.0f,0.0f, 0.5f, 0.0f};float colors[] = {1.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f,0.0f, 0.0f, 1.0f};//2 为位置&颜色数据各自生成一个vboGLuint posVbo = 0, colorVbo = 0;GL_CALL(glGenBuffers(1, &posVbo));GL_CALL(glGenBuffers(1, &colorVbo));//3 给两个分开的vbo各自填充数据//position填充数据GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, posVbo));GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW));//color填充数据GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, colorVbo));GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW));
}void prepareInterleavedBuffer() {float vertices[] = {-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f};GLuint vbo = 0;GL_CALL(glGenBuffers(1, &vbo));GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo));GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW));}
2.VAO
代码
void prepareSingleBuffer() {//1 准备positions colors数据float positions[] = {-0.5f, -0.5f, 0.0f,0.5f, -0.5f, 0.0f,0.0f, 0.5f, 0.0f};float colors[] = {1.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f,0.0f, 0.0f, 1.0f};//2 使用数据生成两个vbo posVbo, colorVboGLuint posVbo, colorVbo;glGenBuffers(1, &posVbo);glGenBuffers(1, &colorVbo);glBindBuffer(GL_ARRAY_BUFFER, posVbo);glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);glBindBuffer(GL_ARRAY_BUFFER, colorVbo);glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);//3 生成vao并且绑定GLuint vao = 0;glGenVertexArrays(1, &vao);glBindVertexArray(vao);//4 分别将位置/颜色属性的描述信息加入vao当中//4.1描述位置属性glBindBuffer(GL_ARRAY_BUFFER, posVbo);//只有绑定了posVbo,下面的属性描述才会与此vbo相关glEnableVertexAttribArray(0);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);//4.2 描述颜色属性glBindBuffer(GL_ARRAY_BUFFER, colorVbo);glEnableVertexAttribArray(1);glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);glBindVertexArray(0);
}void prepareInterleavedBuffer() {//1 准备好Interleaved数据(位置+颜色)float vertices[] = {-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f};//2 创建唯一的vboGLuint vbo = 0;GL_CALL(glGenBuffers(1, &vbo));GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo));GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW));//3 创建并绑定vaoGLuint vao = 0;GL_CALL(glGenVertexArrays(1, &vao));GL_CALL(glBindVertexArray(vao));GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo));//4 为vao加入位置和颜色的描述信息//4.1 位置描述信息GL_CALL(glEnableVertexAttribArray(0));GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0));//4.2 颜色描述信息GL_CALL(glEnableVertexAttribArray(1));GL_CALL(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))));//5 扫尾工作:解绑当前vaoglBindVertexArray(0);
}