I am trying to call a function on my Model3D class (my created class for OBJ models) wherein I load the model’s texture but Visual Studio tells me there is an error–
Exception thrown at 0x00007FFC0E3CFD97 (ig11icd64.dll) in glfwsetup.exe: 0xC0000005: Access violation reading location 0x000001C7B701C000.
Here is the function in question:
void Model3D::loadTexture(int i) {
std::string strTexPath = texturePath;
stbi_set_flip_vertically_on_load(true);
unsigned char* tex_bytes =
stbi_load(texturePath,
&img_width,
&img_height,
&colorChannels,
0);
// Generate a reference
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, texture);
if (tex_bytes) {
// All rendered textures are .png files for now
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_width, img_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_bytes);
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR)
std::cout << "OpenGL Error " << err << std::endl;
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cout << "Failed to load image" << std::endl;
}
stbi_image_free(tex_bytes);
}
So far, what I’ve noticed is that the error is isolated into one Model3D variable. But given the application stops after the exception was thrown, I have no idea how to properly debug this without using glGetError().
// glfwsetup.cpp OR main.cpp
// ...
glfwMakeContextCurrent(window);
gladLoadGL();
cSteve.loadTexture(0); // This works
cFire.loadTexture(1); // This does NOT work, even with the previous function call commented out
glEnable(GL_DEPTH_TEST);