OpenGL VAO无法正常工作 [英] OpenGL VAO not working as intended

查看:242
本文介绍了OpenGL VAO无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,VAO应该存储渲染所需的状态,例如缓冲区和属性指针. 但是我遇到的问题是,在渲染之前,我需要每次设置VAO并将数据加载到缓冲区中,否则绘制的东西是错误的.

From what I understand VAOs should store the states needed for rendering like the buffers and the attribute pointers. But the problem I am having is, I need to set-up the VAO and load the data to the buffers every time before rendering or else the things being drawn is wrong.

我正在使用一个名为mesh的类,该类将VAO和VBO句柄作为受保护的GLuint变量进行初始化,并将顶点数据加载到GPU中.

I am using a class called mesh that hold the VAO and VBO handles as protected GLuint variables to initialize and load the vertex data into the GPU.

我搜索了几个小时,但没有其他人遇到同样的问题.

I searched for hours but it seams no one else is having the same problem.

所以如果我这样初始化并像这样渲染:

So if I initialize like this and render like this :

mesh coneMesh, sphereMesh, boxMesh;

coneMesh.setup(coneVertex, GL_STATIC_DRAW, 1, ShaderA);
sphereMesh.setup(*sphereVertex, GL_STATIC_DRAW,1, ShaderA);
boxMesh.setup(boxVertex, GL_STATIC_DRAW, 1, ShaderA);

,并在渲染循环中:

//sphereMesh.setup(*sphereVertex, GL_STATIC_DRAW,1, ShaderA);
sphereMesh.render(GL_TRIANGLE_STRIP,0,sphereVertex->size(),0);
sphereMesh.render(GL_TRIANGLE_STRIP,0,sphereVertex->size(),1);

//coneMesh.setup(coneVertex, GL_STATIC_DRAW, 1, ShaderA);
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
coneMesh.render(GL_TRIANGLE_STRIP,0,coneVertex.size(),2);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);

//boxMesh.setup(boxVertex, GL_STATIC_DRAW, 1, ShaderA);
boxMesh.render(GL_LINE_LOOP, 0, boxVertex.size());


glfwSwapBuffers(window);

如果我删除他的评论并重新初始化VAO并重新加载数据,则一切正常,有人可以告诉我我做错了什么以及如何解决?谢谢

If I take out he comments and reinitialize the VAO and reload the data every thing works perfectly, can someone please tell me what I am doing wrong and how to fix it? Thank You

int mesh::setup(std::vector<vertex> &vert, GLenum BufferDataUsage, GLuint        nuberOfAttribute, shader &shad)
{

this->nuberOfAttribute = nuberOfAttribute;
this->shad = &shad;

std::vector<glm::vec3> position;
std::vector<glm::vec2> textureCoord;

position.reserve(vert.size());
textureCoord.reserve(vert.size());

for(unsigned int i; i<vert.size(); i++)
{
    position.push_back(vert[i].pos());
    textureCoord.push_back((vert[i].textureCoordinate));
}

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1,&vbo[0]);
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);
glBufferData(GL_ARRAY_BUFFER,(GLsizeiptr)vert.size() * sizeof (position[0]), &position[0], BufferDataUsage);
glEnableVertexAttribArray(0);
glVertexAttribPointer ( ( GLuint ) 0, 3, GL_FLOAT, GL_FALSE,0,0);


glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);

std::cout <<vbo[0]<<std::endl;
return 0;
}

渲染功能:

void mesh::render(GLenum DrawHint,  GLint first,GLsizei count)
{
shad->bind();//glUseProgram(id)
//set up transform matrix and set up uniform

glBindVertexArray(vao);
glDrawArrays(DrawHint, first, count);
glBindVertexArray(0); 

修改

1.)我已经在另一台机器上运行了代码,但问题并没有消失.因此,这可能不是驱动程序错误.

1.)I have ran the code in another machine and the problem didn't go away. So it's probably not a driver bug.

2.)我可以确认不同的VAO和VBO不会相互覆盖,因为它们各自的句柄都是唯一的.

2.)I can confirm that the different VAO and VBO are not overwriting each other since the handles on each of them is unique.

3.)现在我不知道有什么可能解决这个问题.

3.) Now I have no idea what could possibly course this problem.

推荐答案

glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);

取消绑定VBO必须先 取消绑定VAO,否则VAO会记住您的VBO没有被绑定.

Unbinding the VBO has to go after unbinding the VAO, as otherwise VAO will remember your VBO not being bound.

编辑

如注释中所指出的,在解除绑定VAO之前,可以解除对GL_ARRAY_BUFFER的绑定. GL_ELEMENT_ARRAY_BUFFER可能不行,尽管本示例中未使用它.

As pointed out in comments, it's OK to unbind GL_ARRAY_BUFFER before unbinding the VAO. It wouldn't be OK for GL_ELEMENT_ARRAY_BUFFER, which is not used in this example though.

因此,问题必须出在其他地方.

Therefore, the problem must lie elsewhere.

这篇关于OpenGL VAO无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆