I’m writing an OpenGL program and have been implementing the model loading functionality via assimp but have been having a hard time making it work.
I’ve tracked the issue down to one of the member variables of my mesh class not updating correctly for some reason.
I’ve tried everything I can think of but can’t seem to figure out why it’s not updating correctly.
Before the gpu_gen_mesh method call, the values of all the buffer handles are 0, which is to be expected. Afterwards, the values are all 1 for some reason. This is despite the values of the intermediate variables I am using being set to what seems like the correct values.
I must be doing some stupid mistake in my class definition but can’t seem to figure out where.
Here is the class:
struct Vertex
{
lnal::vec3 position;
lnal::vec3 normal;
lnal::vec2 tex_coords;
};
class Mesh
{
private:
std::vector<unsigned int> m_indices;
std::vector<Vertex> m_vertices;
std::vector<Texture> m_textures;
bool gpu_gen_mesh(const std::vector<Vertex>& vertices,const std::vector<unsigned int>& indices);
public:
uint32_t m_vao;
uint32_t m_vbo;
uint32_t m_ebo;
Mesh();
~Mesh();
//When not loading from file
bool load(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture> textures);
//Draws the mesh with the given shader.
//NOTE - DOES NOT BIND THE SHADER FOR PERFORMANCE REASONS
//MAKE SURE TO BIND THE SHADER BEFORE ANY CALLS TO draw()
//@param shader shader to draw with
void draw(Shader& shader);
};
And here is the method that is called when generating the opengl buffers:
bool Mesh::gpu_gen_mesh(const std::vector<Vertex>& vertices,const std::vector<unsigned int>& indices)
{
uint32_t vao, vbo, ebo;
//Generate the buffers
glGenVertexArrays(1, &vao);
//std::cout << vao << std::endl;
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, tex_coords));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
this->m_vao = vao;
this->m_vbo = vbo;
this->m_ebo = ebo;
return true;
}
The method scoped variables vao, vbo, and ebo are all set to the correct values each time the function is called (I checked), but for whatever reason when the member variables are set to them the result is always 1. Both are uint32_t so there shouldn’t be any weird type conversions (I’ve tried casting just in case).
I’ve also tried getting rid of the intermediate variables and the member variables are still set to 1 no matter what (which is why I think this is a class definition problem and not anything to do with my actualy opengl code).
Any help would be greatly apprecited.