I’m trying to create a model class for my project and use one vaos with several vbos to save resources, but I encountered a crash when using the function. Don’t pay attention to the offset values and others mistakes, since I’m just figuring out how to work with them. Just tell me what could be the reason for the crash. VS output this error:
An exception was thrown at address 0x0000000000000000 in gl.exe: 0xC0000005: Execution access violation at address 0x00000000000000000.
Model.cpp
Model::Model(const std::string& mtl, const std::string& obj){
std::ifstream mtlFile;
std::ifstream objFile;
try
{
mtlFile.open(mtl);
objFile.open(obj);
}
catch (std::ifstream::failure& e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() << std::endl;
}
materialReader(Materials, mtlFile);
for (int i = 0; i < Materials.size(); i++)
{
Vertices.push_back({});
VBO_array.push_back(0);
Materials[i].diffuse_texture_ID = loadTexture(Materials[i].diffuse_texture.c_str());
Materials[i].specular_texture_ID = loadTexture(Materials[i].specular_texture.c_str());
}
meshReader(Vertices, objFile, Materials);
glGenVertexArrays(1, &VAO1);
glBindVertexArray(VAO1);
glGenBuffers(1, &VBO_array[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBO_array[0]);
glBufferData(GL_ARRAY_BUFFER, Vertices[0].size() * sizeof(float), &Vertices[0][0], GL_DYNAMIC_DRAW);
glGenBuffers(1, &VBO_array[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBO_array[1]);
glBufferData(GL_ARRAY_BUFFER, Vertices[1].size() * sizeof(float), &Vertices[1][0], GL_DYNAMIC_DRAW);
glGenBuffers(1, &VBO_array[2]);
glBindBuffer(GL_ARRAY_BUFFER, VBO_array[2]);
glBufferData(GL_ARRAY_BUFFER, Vertices[2].size() * sizeof(float), &Vertices[2][0], GL_DYNAMIC_DRAW);
glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
glVertexAttribBinding(0, 0);
glEnableVertexAttribArray(0);
glVertexAttribFormat(1, 3, GL_FLOAT, GL_FALSE, 0);
glVertexAttribBinding(1, 0);
glEnableVertexAttribArray(1);
glVertexAttribFormat(2, 3, GL_FLOAT, GL_FALSE, 0);
glVertexAttribBinding(2, 0);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
Model::~Model()
{
glDeleteVertexArrays(1, &VAO1);
for (auto i : VBO_array) { glDeleteBuffers(1, &i); }
}
void Model::Draw(Shader shader, glm::vec3 cam_position, glm::mat4 matrix)
{
shader.use();
shader.setVec3("viewPos", cam_position[0], cam_position[1], cam_position[2]);
shader.setMat4("matrix", matrix);
shader.setVec3("light.position", 1.2f, 1.0f, 2.0f);
shader.setVec3("light.ambient", 0.5f, 0.5f, 0.5f);
shader.setVec3("light.diffuse", 0.5f, 0.5f, 0.5f);
shader.setVec3("light.specular", 0.5f, 0.5f, 0.5f);
shader.setFloat("material.shininess", Materials[0].shininess);
glBindVertexArray(VAO1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Materials[0].diffuse_texture_ID);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, Materials[0].diffuse_texture_ID);
glBindVertexBuffer(0, VBO_array[0], 0, 3 * sizeof(float));
glDrawArrays(GL_TRIANGLES, 0, Vertices[0].size());
}
Model.h
#ifndef MODEL_H
#define MODEL_H
struct Material {
std::string materialName;
std::string diffuse_texture = "C:\\Users\\Gigabyte\\source\\repos\\gl\\textures\\grass.jpg";
float diffuse_color[3] = { 1.0, 1.0, 1.0 };
unsigned int diffuse_texture_ID;
std::string specular_texture = "C:\\Users\\Gigabyte\\source\\repos\\gl\\textures\\grass.jpg";
float specular_color[3] = { 1.0, 1.0, 1.0 };
unsigned int specular_texture_ID;
float shininess = 1.0;
};
void pathCorrector(std::string& path);
std::vector<std::string> split(std::string& string, char splitter);
unsigned int loadTexture(char const* path);
void materialReader(std::vector<Material>& Materials, std::ifstream& mtlFile);
void meshReader(std::vector<std::vector<float>>& vertices, std::ifstream& in, std::vector<Material>& Materials);
class Model {
public:
std::vector<Material> Materials;
std::vector<std::vector<float>> Vertices;
std::vector<unsigned int> VBO_array;
unsigned int VAO1;
Model(const std::string& mtl, const std::string& obj);
~Model();
void Draw(Shader shader, glm::vec3 cam_position, glm::mat4 matrix);
};
#endif;