Im trying to make my own mesh class without copying the code from a tutorial and I ran into some trouble. As of now, Im not actually importing any info, I have all the vertices stored in a c++ vector class and I have a render function to render the model. The problem is that once I call the render function, my GLuint VBO gets a value of 0 and apperantly the size of my vertices drops from 5 or whatever value it was before to 0. Im not sure why, I figured it might be something to do with how OpenGL handles buffers since I can't modify these values outside of my class (theyre private). Does anyone have an idea as to why these values get modified and become 0? I call CreateMesh just before GlutMainLoop is called which is were my tutorial creates theyre model. The // after cout functions simply state the values the cout functions display.

Are you sure that you're really using that same instance of that class? Maybe you haven't assigned the result of CreateMesh to Model, so it's just using the default constructor? Try printing out the pointer inside the constructor and render method, printf("%p\n", this);

Btw, your glBufferData takes a pointer to std::vector<Vertex>, I'd change it to &vertices[0].pos.x

Well I encountered a similar problem like this before when I was making a physics simulator for collisions in 2 space and the way I solved it was by putting the class that didn't work into the same header that was using it, in this case id put mesh class into the main function but that makes the code clunky and unorganized.

I don't think its actually the default constructor but Ill give it a test. The reason for that is because the Create Mesh function gets called and creates the mesh after the default constructor was called so it should override it. The value does get assigned because the model variable does get the proper values of VBO being equal to 1 and having the vertices equal to the actual number of vertices not 0.

I could post the whole program but I did a lot of changing with the GLUT interface. It now all runs in a Application class which also has a list of all the models in my application. I then add a new model and all models get rendered in a for loop in the RenderScene function. The variables all still get called the way they did previously so the emsh stuff still runs the same, just under a more dynamic interface.

EDIT: After setting the values of create mesh, the mesh being rendered does indeed get the changed values generated by create mesh which are the ones I want. But once render function gets called it all gets set to 0 and Im really lost as to why because the only thing happening is that another function is being called in another header but that function doesn't actually change any of those values, they just become modified O.o

Btw I did change the BufferData to &vertices[0].pos.x but what difference does it make if it has the .pos.x or if it doesn't? Won't it work properly either way?

Btw I did change the BufferData to &vertices[0].pos.x but what difference does it make if it has the .pos.x or if it doesn't? Won't it work properly either way?

That line of code is fine. Sponji was just having a funny five minutes.....

Right then. Where to begin with this. I'd probably recommend changing your ctor to take a const ref, rather than duplicating the array... i.e.

Mesh::Mesh(const std::vector<Vertex>& _vertices)

If you ever find yourself putting using namespace std; in a header file, you're doing it wrong. Using namespace must only be used in a CPP file, after all #includes. This does mean you need to use the std:: prefix in all headers, but that's a good habit to get into. You're just heading for another set of hideous problems if you keep doing that.

they just become modified O.o

Yeah, hate to break it to you, but stuff doesn't just randomly modify itself. It means you have a bug, and you're modifying the data somewhere.

Hit compile. Has it spat out a bunch of compile errors? Good. Those are the locations of your bug(s).

If you ever find yourself writing a function declaration like this:

Mesh CreateMesh()
{
}

Take yourself outside, slap yourself around a bit, and go and read the chapter on C++ pointers, followed by the chapter on C++ references. Learn how to use them, and your problems will be greatly reduced.

That Mesh you're returning. Does it have a destructor? Does that destructor delete the vertex buffer? Is it therefore a good idea to be creating a temporary Mesh, which you're then assigning to another Mesh object elsewhere, which is then deleted when it falls out of scope? (but since you haven't defined a copy ctor or assingment op, the VBO's are being killed off). Have you considered maybe reading that chapter on pointers again? Now is probably a good time to do so!

Alright so Im revisiting my C++ book on Pointers and References but I have just a few questions.

Why exactly is it wrong to create a constructor for Mesh the way I did? If I do add a deconstructor which I did that calls glDeleteBuffers on the VBO, would it just make returning a pointer more efficient? I did end up adding a deconstructor and I don't get the VBO drawn on the screen.

Also, in your example of the what I should put for the Mesh class and solve the errors I get, why exactly is there an operator =? Wouldn't C++ copy all the variables for me by default if I didn't make a operator=?

Also, whats with the facepalm for using std::vector<Meshes> as a list? Is it supposed to be a pointer to the meshes rather than being on the stack?

Yeah I added the clear depth buffer part, it wasn't there in the first place because the tutorial I was looking up for OpenGL didn't have the clear depth buffer until shadow mapping was introduced I think, or somewhere further down the line.

Except..... you're only doing half of the above (which is required if you're going to pass these things around by reference)

I did end up adding a deconstructor and I don't get the VBO drawn on the screen.

Good. Your Mesh class is now releasing the GPU resources when it is called (which is a very good thing)

Also, in your example of the what I should put for the Mesh class and solve the errors I get, why exactly is there an operator =? Wouldn't C++ copy all the variables for me by default if I didn't make a operator=?

It will indeed copy the variables blindy, without any understanding of what those variables mean. Now this is all well and good when you have a simple Vec3 / Matrix. But it's not so good when your class contains a handle to resources allocated on the GPU (i.e. your VBO). If you really think it's a good idea to allow copying of mesh data, you should implement the copy ctor & assignment operators. I'd argue that allowing copying of Mesh data is a bit pointless - it would be far easier to simply draw the same Mesh twice, rather than drawing two meshes that are exactly the same.

Also, whats with the facepalm for using std::vector<Meshes> as a list? Is it supposed to be a pointer to the meshes rather than being on the stack?

They aren't on the stack, they're on the heap, and std::vector is not a list. If it was a list you could get away with it, but not when you're using a std::vector (where push_back may cause the entire array to be reallocated, which would cause all meshes to be reallocated, including all of their vertex arrays, and all GPU resources.

Sorry for the alte reply but thanks, I think I understand this a lot more. I decided to remove the GL functions from the constructor and destructor and instead I added functions to bind the buffer and remove it so that not all meshes created get loaded onto the GPU. I realized from these posts that the error came from having the meshes not passed properly via refrences/pointers. I fixed that part but now the triangle doesn't appear unless its GL_POINTS and it will always be in the centre of the screen. I assume its because I need shaders but by default, on OpenGL 3.0 or higher, do I need shaders to properly display triangles or will it always go to (0,0)?