2

I have a problem that I don't know how to solve. I am using C++ with GLEW and GLFW, I initialise GLFW, create the window, make the window the current context, then I initialise GLEW. All of this happens on the main function.

The problem appears when the code to create the VAO and the VBO is organised in methods, in classes, I see that the VAO is generated, that the VBO is generated, but when the data is sent to the GPU, using glBufferData, it does not work. If I take this piece of code and move it in the main function, everything works just fine, but outside it, it does not work.

This code works perfectly on the main function, but if I move it in other functions, just to make things clear, it stops working. So I think the problem is somehow related with that context. I make the window, the current context, on the main function.

Any Idea?

// MAIN FUNCTION

unsigned int vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);

// if I move this code in a method of a class and pass the vao, to bind it again //there it does not work
// if I leave it here, like this, it works
unsigned int vboID;
unsigned int attribPointer = 0;
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(attribPointer, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

// the drawing part, just the idea, it's in a while, of course.
glBindVertexArray(vaoID);
glEnableVertexAttribArray(attribPointer);
glDrawArrays(GL_TRIANGLES, 0, 6);

glDisableVertexAttribArray(attribPointer);
glBindVertexArray(0);
//if I move it here, it's dead
void storeDataInAttributeList(int attributeNumber, float data[], int vaoID)
    {
        glBindVertexArray(vaoID);
        unsigned int vboID;
        glGenBuffers(1, &vboID);
        vbos.push_back(vboID);
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
        glVertexAttribPointer(attributeNumber, 3, GL_FLOAT, GL_FALSE, 0, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

    }
Pocsan Jr
  • 25
  • 4
  • Very hard to diagnose problems with code when you can't see the code. – john Sep 16 '20 at 12:50
  • 2
    Do you use classes? Have you thought about [OpenGL object in C++ RAII class no longer works](https://stackoverflow.com/questions/46839586/opengl-object-in-c-raii-class-no-longer-works) – Rabbid76 Sep 16 '20 at 12:51
  • I added some code and comments, to get a better idea. – Pocsan Jr Sep 16 '20 at 12:53
  • 1
    @PocsanJr You added the code that works, to fix your problem we need to see the code that **doesn't work**. – john Sep 16 '20 at 12:54
  • That's the idea, it works if I leave it like that, if I move that pieace of code about the VBO, in a method of a class, it does not work, that's the only change. – Pocsan Jr Sep 16 '20 at 12:56
  • Well lets see that change. You're making a mistake somewhere, but we can only guess what it is unless we can see the code. – john Sep 16 '20 at 12:56
  • @PocsanJr Please *show* the broken code rather than trying to describe it. – G.M. Sep 16 '20 at 12:57
  • Note that `sizeof(data)` in the call to `glBufferData` doesn't do what you think it does. – G.M. Sep 16 '20 at 13:00

1 Answers1

3

The problem is here sizeof(data).

Within your method data is a pointer, and sizeof(data) returns the size of the pointer, not the size of the original array.

To solve pass the size as a parameter to the method along with the data. Or use a std::vector<float> instead of an array of floats.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you! There could be no way for me to see this, I thought all the time that it's a problem with the window context. Silly me. – Pocsan Jr Sep 16 '20 at 13:04
  • It's a common beginner mistake, if I'd been on the ball I might have guessed the problem even from your working code, but always post the non-working code please. It's surprising how many posters are reluctant to do this. – john Sep 16 '20 at 13:06
  • Yes, it is, now I will never forget this :)) I was too sure that the problem was somehow related with the window context, so that when I was calling those GL functions, they would have had no effect on the current window context. Thank you!!! And I tried to debug it for an hour, I was close to just give up and put the code in the main function in spite of the fact that it would have made the code more messy. – Pocsan Jr Sep 16 '20 at 14:16