Code not working when inside class constructor?

This is my program:


#include <GL\glew.h>
#include <GLFW\glfw3.h>

#include <iostream>

int main()
{
    glfwWindowHint(GLFW_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_VERSION_MINOR, 4);

    if (!glfwInit())
        std::cerr << "Failed to initialize GLFW." << std::endl;

    GLFWwindow* window;
    window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
    if (!window) 
        std::cerr << "Failed to create Window." << std::endl;

    glfwMakeContextCurrent(window);

    GLenum err;
    if ((err = glewInit()) != GLEW_OK)
    {
        std::cerr << "Failed to initialize GLEW" << std::endl;
        std::cerr << err << std::endl;
    }

    float data[] = { -0.5, -0.5, 0.1,
                    0.0, 0.5, 0.1,
                    0.5, -0.5, 0.1 };

    GLuint vaoID;
    GLuint vboID;

    glGenVertexArrays(1, &vaoID);

    glBindVertexArray(vaoID);
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data[0], GL_STATIC_DRAW);
    glBindVertexArray(0);

    while (!glfwWindowShouldClose(window))
    {

        glClear(GL_COLOR_BUFFER_BIT);

        glBindVertexArray(vaoID);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

        glDrawArrays(GL_TRIANGLES, 0, 3);
        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
}

It produces a white triangle like you might expect. BUT. when i place some of the vao and vbo code in a class like this:


#pragma once
#include <GL\glew.h>

class Model
{
public:
    Model(float* data) {
        glGenVertexArrays(1, &vaoID);

        glBindVertexArray(vaoID);
        glGenBuffers(1, &vboID);
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glBufferData(GL_ARRAY_BUFFER, sizeof(data), &data[0], GL_STATIC_DRAW);
        glBindVertexArray(0);
    }
    ~Model();

    GLuint vaoID;
    GLuint vboID;
};

And the main code looks like this:


#include <GL\glew.h>
#include <GLFW\glfw3.h>

#include <iostream>

#include "Model.h"

int main()
{
    glfwWindowHint(GLFW_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_VERSION_MINOR, 4);

    if (!glfwInit())
        std::cerr << "Failed to initialize GLFW." << std::endl;

    GLFWwindow* window;
    window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
    if (!window) 
        std::cerr << "Failed to create Window." << std::endl;

    glfwMakeContextCurrent(window);

    GLenum err;
    if ((err = glewInit()) != GLEW_OK)
    {
        std::cerr << "Failed to initialize GLEW" << std::endl;
        std::cerr << err << std::endl;
    }

    float data[] = { -0.5, -0.5, 0.1,
                    0.0, 0.5, 0.1,
                    0.5, -0.5, 0.1 };

    Model model(data);

    while (!glfwWindowShouldClose(window))
    {

        glClear(GL_COLOR_BUFFER_BIT);

        glBindVertexArray(model.vaoID);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, model.vboID);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

        glDrawArrays(GL_TRIANGLES, 0, 3);
        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
}

It does not.

Why is this it has been foiling my projects for ages and i’m really fed up with it.

wrong use of pointers … try printing “sizeof(data)” in your constructor, it wont give you “sizeof(float) * 9” as you expect

use model(const float* data) otherwise do us john_connor said. for me personaly i hate using flat out pointers it’s 2017 and std::vector has been here for long time and almost few who use it
. it’s simpler less messy and controllable

This will work as long as you compile with at least a C++ 03 compliant compiler.
This is thus not guarantee to work as you transparently expect it to work on C++ 98 compilers.

Thank you i’ve changed to using vectors as suggested and also watch my sizeof usage and it all works great now thanks!