I'm a beginner in OpenGL and I am trying to draw a colored square I followed the tutorial at OpenGL Book and I am using the example to draw here. Since this draws a triangle I modified the code to draw 4 vertices. I made a Rectangle class that can output it's data in array format. This is the data that I am trying to pass into the glBufferData function.
Unfortunately when I use my class's data it does not draw anything on the screen. I'm even checking in gDebugger, I'm looking at my VBO and the data is not correct.
To test, I extracted the vertices out of my class and used them in a local array instead of the pointer returned from my class.
My data is now :
Vertex Vertices[] =
{
{ { -0.5f, 0.5f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },
{ { 0.5f, 0.5f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } },
{ { -0.5f, -0.5f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } },
{ { 0.5f, -0.5f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }
};
instead of
Vertex* Vertices = rec->GetVertexData();
(I checked by hand both arrays, they have the exact same values, the problem is not in my Rectangle's code) and now the call glBufferData(GL_ARRAY_BUFFER, BufferSize, Vertices, GL_STATIC_DRAW); works and I see the correct rectangle on screen. With the data in gDebugger being
After some research I realized that sizeof does not report the correct size given a pointer so I fixed that. I hardcoded (for now) BufferSize = 128; in order to test quicker. Point is, it works with the Vertex array and still not with the Vertex pointer from my class.
I've googled examples of the glBufferData and literally none of them shows you how to use the data from a pointer, you'd think that would be a good example.
So how can I pass an array of Vertex to my glBufferData that's being returned as a pointer from another class ? I'm using OpenGL version 4 with GLEW and GLUT.
Update :
So the error was on my part, I'll post my debugging here in hopes it can help other people. I added a Watch of name Vertice, 8. Which showed me that my array had bogus values. Doing a step by step debugging showed me that when glGenBuffers was called it was overwriting my values. I went back and checked my code and I realized that my mistake was calling Vertex vec[4];
instead of Vertex *vec = new Vertex[4];
and the stack's values was overwritten by another function.
Vertices
toglBufferData
? One potential problem is that you have another level of indirection by using&Vertices
which would be wrong. Assuming that you're using MSVC, addVertices,8
to your watch, does it show the correct values? Otherwise your data is not consecutive. – Ylisar