OpenGL笔记八之EBO和EBO绘制流程
—— 2024-07-07 晚上
总结自bilibili赵新政老师的教程
code review!
文章目录
- OpenGL笔记八之EBO和EBO绘制流程
- 1.EBO
- 2.glDrawElements:如果使用了ebo,最后一个参数可以写0
- 3.glDrawElements:如果使用了ebo,假设最后一个参数是数字,表示ebo内偏移量
- 3.1.count改为3,偏移量为0
- 3.2.count改为3,偏移量为3
- 4.glDrawElements:如果没有使用ebo,可以直接将cpu端的indices数组传输进去(不推荐这种方式)
- 5.main.cpp
1.EBO
代码
void prepareVAO() {//1 准备positionsfloat positions[] = {-0.5f, -0.5f, 0.0f,0.5f, -0.5f, 0.0f,0.0f, 0.5f, 0.0f,0.5f, 0.5f, 0.0f,};unsigned int indices[] = {0, 1, 2,2, 1, 3};//2 VBO创建GLuint vbo;glGenBuffers(1, &vbo);glBindBuffer(GL_ARRAY_BUFFER, vbo);glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);//3 EBO创建GLuint ebo;glGenBuffers(1, &ebo);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);//4 VAO创建glGenVertexArrays(1, &vao);glBindVertexArray(vao);//5 绑定vbo ebo 加入属性描述信息//5.1 加入位置属性描述信息glBindBuffer(GL_ARRAY_BUFFER, vbo);glEnableVertexAttribArray(0);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);//5.2 加入ebo到当前的vaoglBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);glBindVertexArray(0);
}
对比VAO
2.glDrawElements:如果使用了ebo,最后一个参数可以写0
运行
关键代码
// glDrawArrays(GL_LINE_STRIP, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0);
3.glDrawElements:如果使用了ebo,假设最后一个参数是数字,表示ebo内偏移量
3.1.count改为3,偏移量为0
运行
关键代码
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
3.2.count改为3,偏移量为3
运行
关键代码
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(int)*3));
4.glDrawElements:如果没有使用ebo,可以直接将cpu端的indices数组传输进去(不推荐这种方式)
运行
关键代码
//5.2 加入ebo到当前的vao
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); //需要注释掉
unsigned int indices[] = {0, 1, 2,2, 1, 3
};
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indices);
5.main.cpp
代码
#include <iostream>#define DEBUG//注意:glad头文件必须在glfw引用之前引用
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string>
#include <assert.h>//断言
#include "wrapper/checkError.h"
#include "application/Application.h"/*
*┌────────────────────────────────────────────────┐
*│ 目 标: 学习EBO进行绘制
*│ 讲 师: 赵新政(Carma Zhao)
*│ 拆分目标:
*│
*│ 1 创建EBO,灌入数据,与vao绑定
*│ 2 练习glDrawElements
* -如果使用了ebo,最后一个参数可以写0;
* -如果使用了ebo,假设最后一个参数是数字,表示ebo内偏移量
* -如果没有使用ebo,可以直接将cpu端的indices数组传输进去
*└────────────────────────────────────────────────┘
*/GLuint vao, program;void OnResize(int width, int height) {GL_CALL(glViewport(0, 0, width, height));std::cout << "OnResize" << std::endl;
}void OnKey(int key, int action, int mods) {std::cout << key << std::endl;
}void prepareVAO() {//1 准备positionsfloat positions[] = {-0.5f, -0.5f, 0.0f,0.5f, -0.5f, 0.0f,0.0f, 0.5f, 0.0f,0.5f, 0.5f, 0.0f,};unsigned int indices[] = {0, 1, 2,2, 1, 3};//2 VBO创建GLuint vbo;glGenBuffers(1, &vbo);glBindBuffer(GL_ARRAY_BUFFER, vbo);glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);//3 EBO创建GLuint ebo;glGenBuffers(1, &ebo);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);//4 VAO创建glGenVertexArrays(1, &vao);glBindVertexArray(vao);//5 绑定vbo ebo 加入属性描述信息//5.1 加入位置属性描述信息glBindBuffer(GL_ARRAY_BUFFER, vbo);glEnableVertexAttribArray(0);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);//5.2 加入ebo到当前的vaoglBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);glBindVertexArray(0);
}void prepareShader() {//1 完成vs与fs的源代码,并且装入字符串const char* vertexShaderSource ="#version 330 core\n""layout (location = 0) in vec3 aPos;\n""void main()\n""{\n"" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n""}\0";const char* fragmentShaderSource ="#version 330 core\n""out vec4 FragColor;\n""void main()\n""{\n"" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n""}\n\0";//2 创建Shader程序(vs、fs)GLuint vertex, fragment;vertex = glCreateShader(GL_VERTEX_SHADER);fragment = glCreateShader(GL_FRAGMENT_SHADER);//3 为shader程序输入shader代码glShaderSource(vertex, 1, &vertexShaderSource, NULL);glShaderSource(fragment, 1, &fragmentShaderSource, NULL);int success = 0;char infoLog[1024];//4 执行shader代码编译 glCompileShader(vertex);//检查vertex编译结果glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);if (!success) {glGetShaderInfoLog(vertex, 1024, NULL, infoLog);std::cout << "Error: SHADER COMPILE ERROR --VERTEX" << "\n" << infoLog << std::endl;}glCompileShader(fragment);//检查fragment编译结果glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);if (!success) {glGetShaderInfoLog(fragment, 1024, NULL, infoLog);std::cout << "Error: SHADER COMPILE ERROR --FRAGMENT" << "\n" << infoLog << std::endl;}//5 创建一个Program壳子program = glCreateProgram();//6 将vs与fs编译好的结果放到program这个壳子里glAttachShader(program, vertex);glAttachShader(program, fragment);//7 执行program的链接操作,形成最终可执行shader程序glLinkProgram(program);//检查链接错误glGetProgramiv(program, GL_LINK_STATUS, &success);if (!success) {glGetProgramInfoLog(program, 1024, NULL, infoLog);std::cout << "Error: SHADER LINK ERROR " << "\n" << infoLog << std::endl;}//清理glDeleteShader(vertex);glDeleteShader(fragment);
}void render() {//执行opengl画布清理操作GL_CALL(glClear(GL_COLOR_BUFFER_BIT));//1 绑定当前的programGL_CALL(glUseProgram(program));//2 绑定当前的vaoGL_CALL(glBindVertexArray(vao));//3 发出绘制指令
// glDrawArrays(GL_LINE_STRIP, 0, 6);glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);glBindVertexArray(0);
}int main() {if (!app->init(800, 600)) {return -1;}app->setResizeCallback(OnResize);app->setKeyBoardCallback(OnKey);//设置opengl视口以及清理颜色GL_CALL(glViewport(0, 0, 800, 600));GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));prepareShader();prepareVAO();while (app->update()) {render();}app->destroy();return 0;
}