我已经通过了几个允许抽象网格的解决方案,我已经决定了: RenderObject实例包含指向网格,着色器和纹理的指针。它还包含一个GLuint,它是VAO的ID。
struct RenderObject
{
GLuint __vao, bufferStart, length;
Mesh *mesh;
Shader *shader;
Texture *tex;
RenderObject();
void constructVAO();
};
RenderObject::constructVAO()
函数的实现如下:
void RenderObject::constructVAO()
{
glBindVertexArray(__vao);
mesh->constructVAO(__vao, false);
shader->bind();
tex->push(1, glGetUniformLocation(shader->getID(), "tex_sampler"));
glBindVertexArray(0);
}
和Mesh::constructVAO(GLuint &id, bool createNew)
是这样的:
void Mesh::constructVAO(GLuint &id, bool createNew)
{
if(createNew)
{
if(id != 0) glDeleteVertexArrays(1, &id);
glGenVertexArrays(1, &id);
}
glBindVertexArray(id);
if(_vertbuf == 0)
{
glGenBuffers(1, &_vertbuf);
}
glBindBuffer(GL_VERTEX_ARRAY, _vertbuf);
glBufferData(GL_VERTEX_ARRAY, sizeof(GLfloat) * _vert.size(), &_vert[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, _vertbuf);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
}
使用此代码时我得到的只是一个黑色窗口。但是,如果我像这样手动创建VAO,它可以完美地运行:
GLuint vertex_arr_id;
glGenVertexArrays(1, &vertex_arr_id);
glBindVertexArray(vertex_arr_id);
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
shader.bind();
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*) 0);
tex.push(1, glGetUniformLocation(shader.getID(), "tex_sampler"));
glBindVertexArray(0);
ro.__vao = vertex_arr_id;