首页 > 其他分享 >OpenGL 4.5+ 的 VAO、VBO、IBO写法

OpenGL 4.5+ 的 VAO、VBO、IBO写法

时间:2022-08-30 11:24:27浏览次数:82  
标签:4.5 vao IBO 绑定 bindingindex attrib GL VAO

写法

opengl4.5新增了DSA(direct_state_access), 可以不用glBindBuffer()glBindVertexArray()直接设置好VAO、VBO、IBO, 只需要draw之前bind即可

新旧函数对比(左侧为新函数)

  • glCreateBuffers = glGenBuffers + glBindBuffer(the initialization part)
  • glNamedBufferData = glBufferData, 但是glNamedBufferData不需要指定target类型,只需要bufferid
  • glVertexArrayVertexBuffer = glBindVertexArray + glBindBuffer, 把VBO绑定到VAO上
  • glVertexArrayElementBuffer = glBindVertexArray + glBindBuffer, 把IBO绑定到VAO上
  • glEnableVertexArrayAttrib = glBindVertexArray + glEnableVertexAttribArray, 不用bind VAO可以直接enable(这函数名有点难以区分......)
  • glVertexArrayAttribFormat + glVertexArrayAttribBinding = glVertexAttribPointer, opengl4.3开始加了 binding index 的概念,我们可以把VAO的某个属性与某个 binding index 关联起来,然后glVertexArrayVertexBuffer的时候把某个 binding index 关联的属性都绑定到VBO上
// 顶点属性
struct vertex
{
    vec3 loc;
    vec3 normal;
    vec2 texcoord;
};

// 创建vao
glCreateVertexArrays(1, &vao);
// 无需绑定,直接进行下面的函数

// 启用属性
glEnableVertexArrayAttrib(vao, loc_attrib);
glEnableVertexArrayAttrib(vao, normal_attrib);
glEnableVertexArrayAttrib(vao, texcoord_attrib);
// 设置格式
glVertexArrayAttribFormat(vao, loc_attrib,      3, GL_FLOAT, GL_FALSE, offsetof(vertex, loc));
glVertexArrayAttribFormat(vao, normal_attrib,   3, GL_FLOAT, GL_FALSE, offsetof(vertex, normal));
glVertexArrayAttribFormat(vao, texcoord_attrib, 2, GL_FLOAT, GL_FALSE, offsetof(vertex, texcoord));
// 把属性关联到bindingindex上
GLuint bindingindex = 0;
glVertexArrayAttribBinding(vao, loc_attrib,      bindingindex);
glVertexArrayAttribBinding(vao, normal_attrib,   bindingindex);
glVertexArrayAttribBinding(vao, texcoord_attrib, bindingindex);

// 创建vbo
glCreateBuffers(1, &vbo);
// 直接把vao上bindingindex关联的属性绑定到vbo上
glVertexArrayVertexBuffer(vao, bindingindex, vbo, 0, sizeof(vertex));

// 创建ibo
glCreateBuffers(1, &ibo);
// 绑定ibo到vao上
glVertexArrayElementBuffer(vao, ibo)

// 绘制前仍需绑定vao
glBindVertexArray(vao);
glDrawArrays(...);

参考

opengl doc
what-is-the-role-of-glbindvertexarrays-vs-glbindbuffer-and-what-is-their-relatio
Guide-to-Modern-OpenGL-Functions

标签:4.5,vao,IBO,绑定,bindingindex,attrib,GL,VAO
From: https://www.cnblogs.com/miyanyan/p/16638635.html

相关文章

  • CF633H Fibonacci-ish II 莫队 线段树 矩阵
    CF633HFibonacci-ishII题意很简明同时给人以不可做感。直接暴力大概是\(n^2log\)的优化一下提前排好序从小到大枚举数字再枚举询问可以完成\(n^2\)经过精细的优化......
  • OpenGL之渲染管线-VBO-VAO
    在OpenGL中,一切都是3D的,但屏幕或窗口是一个2D像素阵列,因此OpenGL的大部分工作是将所有3D坐标转换为适合屏幕的2D像素。这个过程由OpenGL的渲染管线管理。渲染管线可以分为......
  • vs2022安装framework 4.5
    默认VisualStudio2022不再支持安装.NETFramework4.5 当我们项目是4.5框架时,22不支持,需要我们自行安装框架包,下面是步骤:nuget下载4.5安装包下载地址:https://ww......
  • cf1718 B Fibonacci Strings
    solution当ai为Fib数的时候,他一定在串中是全部连续的,不然就g,因为把他分解成小的Fib数必定连续。一个数字不能拆成两个连续的fib数。\(f_i=\sum_{j=1}^{i-2}f_j+1\)考虑......
  • 我写的蓝宝石留言本php版 v4.5
    蓝宝石留言本php版v4.5采用原生php编写,在php5.6~php7.x下调试通过。本留言本使用了utf-8编码。include/config1.php是数据库连接参数的配置文件,include/config2是留言板......
  • doris-flink-connect1.14.5编译及问题处理
    1前提条件编译源码来自https://github.com/apache/doris-flink-connector,日期2022-08-161.1版本dorisflinkJDK1.1.11.14.51.81.2是否独立编译没有......