我制作了一个装载类,用于加载V.A.Os纹理等但是当我使用这种方法时
GLuint Loader::loadToVAO(GLfloat* vertices)
{
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
return vertexbuffer;
}
它不会渲染任何东西,这是我的渲染代码:
window.createWindow();
GLfloat vertices[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
GLuint vertexbuffer = loader.loadToVAO(vertices);
glClearColor(0, 1, 1, 0);
// It works fine when i generate the vao in the main method but not in the loader class
GLuint vertexbuffer1;
glGenBuffers(1, &vertexbuffer1);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer1);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
std::cout << vertexbuffer1 << std::endl;
std::cout << vertexbuffer << std::endl;
while (!window.shouldClose())
{
window.clearWindow();
// render here
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer1);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
window.updateWindow();
}
return 0;
当我尝试渲染vertexbuffer1
(在main方法中加载的那个)但顶点缓冲区不渲染任何内容时,它呈现正常。
任何帮助将不胜感激
修改 这是我试图关注的教程 - http://www.opengl-tutorial.org/
答案 0 :(得分:1)
您在函数内部的指针类型上使用sizeof
,这几乎肯定不是您想要的。在C和C ++等语言中,数组不是first-class citizens,它们会衰减为指针,指针不包含有关它们存储的数据量的信息。你想要做的是将数组的大小作为附加参数传递:
GLuint Loader::loadToVAO(GLfloat* vertices, size_t num_vertices)
{
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(*vertices) * num_vertices, vertices,
GL_STATIC_DRAW);
return vertexbuffer;
}
然后在你的主要功能中:
GLuint vertexbuffer = loader.loadToVAO(vertices, sizeof(vertices) / sizeof(vertices[0]));
我们鼓励您详细了解该主题here。