Не работает класс VAO

Я написал классы VAO и VBO для того, чтобы облегчить себе жизнь:

// VBO.h
class VBO
{
    GLuint id;
    GLsizeiptr size;
public:
    VBO(GLfloat* vertices, GLsizeiptr size);
    ~VBO();
    GLsizeiptr getSize();

    void bind();
    void unbind();
    void draw(GLenum mode);
};

// VBO.cpp
VBO::VBO(GLfloat* vertices, GLsizeiptr size)
{
    glGenBuffers(1, &id);
    glBindBuffer(GL_ARRAY_BUFFER, id);
    glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
    this->size = size;
}

GLsizeiptr VBO::getSize() {
    return size;
}

void VBO::bind() {
    glBindBuffer(GL_ARRAY_BUFFER, id);
}

void VBO::unbind() {
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

VBO::~VBO() {
    glDeleteBuffers(1, &id);
}

void VBO::draw(GLenum mode) {
    glDrawArrays(mode, 0, size);
}
// VAO.h
class VAO
{
    GLuint id;
public:
    VAO();
    ~VAO();

    void linkVBO(VBO vbo, GLuint* attrs);
    void bind();
    void unbind();
};

// VAO.cpp
VAO::VAO() {
    glGenVertexArrays(1, &id);
    bind();
}

void VAO::linkVBO(VBO vbo, GLuint* attrs)
{
    GLsizei stride = 0;
    for (GLint i = 0; attrs[i]; i++)
        stride += attrs[i];
    GLint offset = 0;
    for (GLint i = 0; attrs[i]; i++)
    {
        glVertexAttribPointer(i, attrs[i], GL_FLOAT, GL_FALSE, stride * sizeof(GLfloat), (GLvoid*)(offset * sizeof(GLfloat)));
        glEnableVertexAttribArray(i);
        offset += i;
    }
}

void VAO::bind() {
    glBindVertexArray(id);
}

void VAO::unbind() {
    glBindVertexArray(0);
}

VAO::~VAO() {
    glDeleteVertexArrays(1, &id);
}

При выполнении этого кода я получаю вращающийся квадрат:

#include <glad/glad.h>
#include "shader.h"
#include "vao.h"
#include "ebo.h"
#include "tools/stb_image.h"
#include "texture.h"
#include "window.h"
#include "camera.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>

GLfloat vertices[] = {
        -1.0f,-1.0f, 0.0f, 0.0f, 0.0f,
        1.0f,-1.0f, 0.0f, 1.0f, 0.0f,
        -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,

        1.0f,-1.0f, 0.0f, 1.0f, 0.0f,
        1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
};

GLuint attrs[] = {
        3, 2, 0
};

int main()
{
    Window::init(800, 600, "Window");
    auto shader = Shader("main.vert", "main.frag");
    auto texture = Texture("img.png");
    auto camera = Camera(glm::vec3(0.0f, 0.0f, 4.0f), glm::radians(45.0f), 1000.0f);

    unsigned int vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    VBO vbo = VBO(vertices, sizeof(vertices));
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (GLvoid*)(0 * sizeof(float)));
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (GLvoid*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);
    vbo.unbind();
    glBindVertexArray(0);

    float rotation = 0.0f;
    double prevTime = glfwGetTime();

    glClearColor(0.07, 0.13, 0.17, 1.0);
    glEnable(GL_DEPTH_TEST);
    while (!Window::shouldClose())
    {
        Window::update();
        shader.use();

        double crntTime = glfwGetTime();
        if (crntTime - prevTime >= 1.0f / 60)
        {
            rotation += 0.5f;
            prevTime = crntTime;
        }

        shader.uniformMat4("view", camera.getView());
        shader.uniformMat4("proj", camera.getProj());
        glm::mat4 model = glm::mat4(1.0f);
        model = glm::rotate(model, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));
        shader.uniformMat4("model", model);

        texture.use(shader, "tex", 0);

        glBindVertexArray(vao);
        vbo.draw(GL_TRIANGLES);
        glBindVertexArray(0);
        Window::swapBuffers();
    }
    Window::destroy();
}

введите сюда описание изображения

Но когда я хочу использовать класс VAO:

#include <glad/glad.h>
#include "shader.h"
#include "vao.h"
#include "ebo.h"
#include "tools/stb_image.h"
#include "texture.h"
#include "window.h"
#include "camera.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>

GLfloat vertices[] = {
        -1.0f,-1.0f, 0.0f, 0.0f, 0.0f,
        1.0f,-1.0f, 0.0f, 1.0f, 0.0f,
        -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,

        1.0f,-1.0f, 0.0f, 1.0f, 0.0f,
        1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
};

GLuint attrs[] = {
        3, 2, 0
};

int main()
{
    Window::init(800, 600, "Window");
    auto shader = Shader("main.vert", "main.frag");
    auto texture = Texture("img.png");
    auto camera = Camera(glm::vec3(0.0f, 0.0f, 4.0f), glm::radians(45.0f), 1000.0f);

    VAO vao = VAO();
    VBO vbo = VBO(vertices, sizeof(vertices));
    vao.linkVBO(vbo, attrs);
    vao.unbind();
    vbo.unbind();

    float rotation = 0.0f;
    double prevTime = glfwGetTime();

    glClearColor(0.07, 0.13, 0.17, 1.0);
    glEnable(GL_DEPTH_TEST);
    while (!Window::shouldClose())
    {
        Window::update();
        shader.use();

        double crntTime = glfwGetTime();
        if (crntTime - prevTime >= 1.0f / 60)
        {
            rotation += 0.5f;
            prevTime = crntTime;
        }

        shader.uniformMat4("view", camera.getView());
        shader.uniformMat4("proj", camera.getProj());
        glm::mat4 model = glm::mat4(1.0f);
        model = glm::rotate(model, glm::radians(rotation), glm::vec3(0.0f, 1.0f, 0.0f));
        shader.uniformMat4("model", model);

        texture.use(shader, "tex", 0);
        vao.bind();
        vbo.draw(GL_TRIANGLES);
        vao.unbind();
        Window::swapBuffers();
    }
    Window::destroy();
}

Я получаю вот это: введите сюда описание изображения

Что я делаю не так?


Ответы (0 шт):