Setup VAO and VBO from class constructor

I’m new to OpenGL and I’m wondering why this is not working.
util.h

    #pragma once

    #include <glad/glad.h>
    #include <glm/glm.hpp>

    #include "shader.h"

    #include <iostream>
    #include <vector>

    class Triangle
    {
    public:
        GLuint VAO, VBO;
        std::vector<float> planeVertices;

        Triangle(std::vector<float> vertices)
        {
            planeVertices = vertices;

            glGenVertexArrays(1, &VAO);
            glGenBuffers(1, &VBO);

            glBindVertexArray(VAO);

            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &planeVertices, GL_STATIC_DRAW);

            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
            glEnableVertexAttribArray(0);

            glBindBuffer(GL_ARRAY_BUFFER, 0);

            glBindVertexArray(0);
        }

        void drawTriangle(Shader shader)
        {
            shader.use();
            glBindVertexArray(VAO);
            glDrawArrays(GL_TRIANGLES, 0, 3);
        }

        void clear()
        {
            glDeleteVertexArrays(1, &VAO);
            glDeleteBuffers(1, &VBO);
        }
    };

main.cpp

    #include <glad/glad.h>
    #include <GLFW/glfw3.h>

    #define STBI_IMAGE_IMPLEMENTATION
    #include "stb_image.h"
    #include "camera.h"
    #include "shader.h"
    #include "util.h"

    #include <iostream>
    #include <vector>

    void framebuffer_size_callback(GLFWwindow* window, int width, int height);
    void processInput(GLFWwindow* window);

    // settings
    const unsigned int SCR_WIDTH = 800;
    const unsigned int SCR_HEIGHT = 600;

    int main()
    {
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

        GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
        if (window == NULL)
        {
            std::cout << "Failed to create GLFW window" << std::endl;
            glfwTerminate();
            return -1;
        }
        glfwMakeContextCurrent(window);
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        {
            std::cout << "Failed to initialize GLAD" << std::endl;
            return -1;
        }

        std::vector<float> verticesCpp = {
             0.5f,  0.5f, 0.0f,  // top right
             0.5f, -0.5f, 0.0f,  // bottom right
            -0.5f, -0.5f, 0.0f,  // bottom left
        };

        Triangle triangle = Triangle(verticesCpp);
        Shader shader = Shader("shader.vert", "shader.frag");

        while (!glfwWindowShouldClose(window))
        {
            processInput(window);

            glClearColor(0.49f, 0.7f, 0.68f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            shader.use();
            triangle.drawTriangle(shader);

            glfwSwapBuffers(window);
            glfwPollEvents();
        }

        triangle.clear();

        glfwTerminate();
        return 0;
    }

    void processInput(GLFWwindow* window)
    {
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, true);
    }

    void framebuffer_size_callback(GLFWwindow* window, int width, int height)
    {
        glViewport(0, 0, width, height);
    }

The result I expected was to draw a triangle with the given vertices.

There are many things wrong here:

sizeof(vertices)

This is not how you get the byte-size of the storage of a std::vector. That just gives you the size of the object, which is immutable and has no relationship to how much data is stored there.

What you’re looking for is sizeof(float) * vertices.size().

Similarly:

&planeVertices

That is not how you get the address of the array of data stored in the vector. That’s just a pointer to the vector itself, not its internal array.

To get a pointer to the storage/first element, you use planeVertices.data().

1 Like

Yeah, I’m not used to C++ vectors. This worked perfectly, thanks for the answer.