gpt4 book ai didi

c++ - 当抽象到一个类中时,我的 OpenGL 顶点数组不再有效

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

<分区>

我正在使用 OpenGL 渲染一些立方体,我已经设法渲染了大约 10 个立方体,但是我现在想将代码抽象到类中。我从顶点数组对象开始。

我查看了代码并一直跟踪到着色器,以确保顶点数据到达需要的位置。

#include "cube_vertex_array.h"

cube_vertex_array::cube_vertex_array(float* vertex_buffer, const std::string& texture_file)
{
GLCall(glGenVertexArrays(1, &va_ID));
GLCall(glBindVertexArray(va_ID));

GLCall(glGenBuffers(1, &vb_ID));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, vb_ID));
GLCall(glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer), vertex_buffer, GL_STATIC_DRAW));
GLCall(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(0 * sizeof(float))));
GLCall(glEnableVertexAttribArray(0));
GLCall(glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))));
GLCall(glEnableVertexAttribArray(1));

GLCall(glGenTextures(1, &texture));
GLCall(glBindTexture(GL_TEXTURE_2D, texture));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
int width, height, nrChanells;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(texture_file.c_str(), &width, &height, &nrChanells, 0);
if (data)
{
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data));
GLCall(glGenerateMipmap(GL_TEXTURE_2D));
}
else { std::cout << "failed to load texture" << std::endl; }
stbi_image_free(data);

GLCall(glBindVertexArray(0));
}

void cube_vertex_array::bind()
{
GLCall(glBindVertexArray(va_ID));
}

此外,这是使用此类的 main() 部分:

main()
{
cube_vertex_array cube_1(vertices, "resources/test.png");
cube_1.bind();
while (!glfwWindowShouldClose(window))
{
cube_1.bind();
GLCall(glDrawArrays(GL_TRIANGLES, 0, 36));
}
GLCall(glDeleteVertexArrays(1, &cube_1.va_ID));
GLCall(glDeleteBuffers(1, &cube_1.vb_ID));
}

我希望看到多个立方体渲染,但这并没有发生。如果我将顶点数组直接实现到主文件中,代码会呈现立方体,但是当抽象到一个类中时它不会呈现任何东西。这可能是什么问题?

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