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.
Mesh.h
#include <vector>
#include <assert.h>
#include "Vector.h"
#include "glew.h"
#include <iostream>
using namespace std;
struct Vertex {
Vector3f pos;
Vertex () {};
Vertex (const Vector3f &_pos) {
pos = _pos;
}
};
struct Mesh {
public:
Mesh ();
Mesh (vector<Vertex>);
void Render ();
private:
GLuint VBO; // Vertex Buffer Object
vector<Vertex> vertices;
};
Mesh.cpp
#include "Mesh.h"
Mesh::Mesh () {}
Mesh::Mesh (vector<Vertex> _vertices) {
vertices.resize(_vertices.size());
vertices = _vertices;
//cout << vertices.size() << endl; // 5
//cout << VBO << endl; // 3435973836
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
//cout << vertices.size() << endl; // 5
//cout << VBO << endl; // 1
}
void Mesh::Render () {
//cout << vertices.size() << endl; // 0
//cout << VBO << endl; // 0
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, vertices.size());
glDisableVertexAttribArray(0);
}
Render Function:
Mesh CreateMesh () {
vector<Vertex> Vertices (5, Vector3f());
Vertices[0].pos = Vector3f(0.0f, 0.0f, 0.0f);
Vertices[1].pos = Vector3f(0.0f, 0.0f, 0.0f);
return Mesh(Vertices);
}
static void RenderScene () {
// VBO = 0 here too
glClear(GL_COLOR_BUFFER_BIT);
Model.Render();
glutSwapBuffers();
}