gpt4 book ai didi

c++ - VAO 不渲染

转载 作者:行者123 更新时间:2023-11-28 05:04:52 25 4
gpt4 key购买 nike

我做了一个加载器类来加载 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 方法中加载的那个)时它渲染良好,但 vertexbuffer 不渲染任何东西。任何帮助将不胜感激

编辑:这是我试图遵循的教程 - http://www.opengl-tutorial.org/

最佳答案

您在函数内部的指针类型上使用了 sizeof,这几乎肯定不是您想要的。数组不是 first-class citizens在 C 和 C++ 等语言中,它们退化为指针,指针不携带有关它们存储的数据量的信息。您要做的是将数组的大小作为附加参数传递:

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 .

关于c++ - VAO 不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45060955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com