- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在阅读一些有关 VAO 如何与 VBO 配合使用的内容。据我了解,当我们调用 glVertexAttribPointer 时,VAO 仅存储有关 VBO 的状态信息。我的问题是,它何时存储有关 EBO 的状态(用于索引绘图),当调用 glVertexAttribPointer 时是否保存两者的状态?
//Vertices of the rectangle
std::vector<GLfloat> vertices
{
0.5f, 0.5f, 0.0f, // Top Right
0.5f, -0.5f, 0.0f, // Bottom Right
-0.5f, 0.5f, 0.0f, // Top Left
-0.5f, -0.5f, 0.0f, // Bottom Left
};
//Indices of the triangle
std::vector<GLuint> indices
{
0, 1, 3,
0, 3, 2
};
GLuint VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO); //Bind the VAO
//Bind the buffers
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0); //Unbind the VAO
//Supply Index Buffer information
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//Custom shader class that stores the program to be used at render time
Shader shader("..path_to_shader\\shaders\\vertexshader.vert", "..path_to_shader\\shaders\\fragmentshader.frag");
while (!glfwWindowShouldClose(window))
{
glUseProgram(shader.Program());
glBindVertexArray(VAO); //Bind the VAO to draw.
glClearBufferfv(GL_COLOR, 0, &color[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
glfwPollEvents();
glfwSwapBuffers(window);
glBindVertexArray(0);
glUseProgram(0);
}
最佳答案
当您使用以下方式绑定(bind)时,GL_ELEMENT_ARRAY_BUFFER
绑定(bind)将成为 VAO 状态的一部分:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...);
当 VAO 已绑定(bind)时。
请注意,在没有 VAO 绑定(bind)的情况下绑定(bind)元素数组缓冲区也是合法的。这仅在使用 glBufferData()
或类似调用填充数据时才真正有用。由于在 Core Profile 中,您需要为任何绘制调用绑定(bind) VAO,因此您需要在绑定(bind) VAO 时绑定(bind)要用于渲染的元素数组缓冲区。
关于您的代码,您使用的调用顺序不会给出所需的结果。以下是您接到的关键电话,并附有解释结果的评论:
glBindVertexArray(VAO);
// VAO is now bound. If this is the first time, it will have the default VAO state.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// The element array buffer is bound, and this binding becomes part of the VAO state.
glBindVertexArray(0);
// The VAO is unbound. Since the element array buffer binding was part of the
// VAO state, the element array buffer is also unbound.
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
&indices[0], GL_STATIC_DRAW);
// This will not work, since you do not have an element array buffer bound.
要使其正常工作,最简单的解决方案是更改最后两个调用的顺序:
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
&indices[0], GL_STATIC_DRAW);
// The currently bound element array buffer is filled with data.
glBindVertexArray(0);
// The VAO is unbound. Since the element array buffer binding was part of the
// VAO state, the element array buffer is also unbound.
为了进一步探索这一点,以下序列也可以:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// The element array buffer is bound, Since no VAO is bound, the binding becomes
// part of the global state (or more precisely, of the default VAO 0).
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint),
&indices[0], GL_STATIC_DRAW);
// The currently bound element array buffer is filled with data.
glBindVertexArray(VAO);
// VAO is now bound. If this is the first time, it will have the default VAO state.
// Since the element array binding is part of the VAO state, the previously bound
// element array buffer is not bound anymore.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
// The element array buffer is bound, and this binding becomes part of the VAO state.
glBindVertexArray(0);
// The VAO is unbound. Since the element array buffer binding was part of the
// VAO state, the element array buffer binding is restored to the binding that
// was current before the VAO was bound.
注意,在这个序列中,绑定(bind)VAO后必须再次绑定(bind)元素数组缓冲区,因为一旦绑定(bind)VAO,VAO状态下的元素数组缓冲区绑定(bind)就会覆盖当前绑定(bind)。
关于opengl - VAO 和元素缓冲区对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863426/
我遇到了一个恐怕很难解决的问题,至少就谷歌搜索而言是这样。 我有一个使用 Qt 的编辑器实用程序,它为编辑器中的不同工具创建多个 OpenGL 上下文,它们是一个“世界”编辑器,它托管我的游戏场景的
我正在再次尝试 OpenGL,我想知道如何设计稍后呈现的类。现在,只有一种类型的形状,所以我创建了一个 Shape 类,它有一个静态 VAO 成员,当第一个时调用 glGenVertexArrays(
环境:OpenGL 3.3+、C 编程、Windows。 我有一个函数,可以从模型加载所有顶点数据,但只返回 VAO id 和顶点数。在此过程中,它生成 VBO id,并将顶点数组数据传递到缓冲区..
假设我有 2 个不同的对象,每个对象都有自己的 VAO 和绘制调用。像这样的事情: void Object::Draw() { glBindVertexArray(vao); glDr
1 VAO 是 OpenGL 技术中提出来的 参考: 外链 其中有一段文字记录了 VAO 是什么: A Vertex Array Object (VAO) is an object which con
我最近使用顶点数组对象 (VAO) 编写了一些 OpenGL 3.3 代码,后来在英特尔图形适配器上对其进行了测试,令我失望的是,元素数组缓冲区绑定(bind)显然不是 VAO 状态的一部分,因为调用
我正在使用 OpenGL 编写一个小图形引擎(通过 OpenTK 和 C#)。 为了定义顶点属性,我有一个 VertexDeclaration 类,其中包含一组 VertexElement 结构,这些
我面临一个问题,我认为该问题与 VAO 相关,但我不确定.. 我不确定 VAO 的正确用法,我在 GL 初始化期间所做的很简单 glGenVertexArrays(1,&vao) 后跟 glBindV
假设我们将两个程序上的顶点属性位置绑定(bind)到相同的值。使用相同的顶点数组对象来使用这两个程序进行绘制是否正确? 最佳答案 定义“正确”。 如果两个程序对象使用兼容的属性位置,则它们使用相同的属
我一直在阅读一些有关 VAO 如何与 VBO 配合使用的内容。据我了解,当我们调用 glVertexAttribPointer 时,VAO 仅存储有关 VBO 的状态信息。我的问题是,它何时存储有关
我做了一个加载器类来加载 V.A.Os 纹理等但是当我使用这个方法时 GLuint Loader::loadToVAO(GLfloat* vertices) { GLuint VertexAr
据我所知,顶点获取阶段由 VAO 封装,并且 VAO 需要包含顶点获取阶段状态,以便在缓冲区对象和顶点属性之间进行管道传输以及格式化缓冲区对象中的数据。 我一直在阅读的关于这个主题的两本书,红皮书,蓝
我最近开始学习 OpenGL,我一直在尝试编写一个程序,使用带有着色器的 VAO 和 VBO 在屏幕上显示钻石。我的代码主要基于本教程: https://www.opengl.org/wiki/Tut
我刚刚开始使用 OpenGL,并且我正在尝试仅使用 3.x 及更高版本的功能。我不明白的一件事是 VAO。 我知道 VAO 封装了渲染状态,所以我可以在渲染循环之前调用所有设置函数,然后绑定(bind
VAO 是顶点数组对象,VBO 是顶点缓冲区对象。创建和绑定(bind)/解除绑定(bind) VAO 和 VBO 的调用具有如下一般格式: GLuint VAO, VBO; glGenVertexA
我希望在 Cocoa 上制作一个自定义的 NSOpenGL View 。但是,我在使用 VAO 时遇到了问题。特别是,运行时,这个虚拟测试 View : /// OpenGLTestView.h: @
我和我的 friend 正在开发一个使用 C++ 和 OpenGL 的项目。我们为“ModelObject”创建了一个C++类,每个ModelObject都有一个GLuint vao作为成员变量。然后
我很难理解 VAO 到底是如何处理缓冲区映射的。我正在做的事情可以用这个伪代码来描述: SetUp: BindVAO BindArrayBuffer glBufferData(GL_ARR
我想知道如果我想画画最好做什么超过约 6000 个不同的 VAO 使用相同的着色器。 目前,我绑定(bind)我的着色器,然后为其提供所需的所有信息(统一),然后循环遍历每个 VAO 来绑定(bind
目前我有一个包含 2 个缓冲区的系统,一个创建 CPU 端并设置为一个缓冲区。然后一个来自 ssbo 并从另一个着色器填充。 这是 ssbo 数据的结构: struct ssbo_data_t {
我是一名优秀的程序员,十分优秀!