Answer the question
In order to leave comments, you need to log in
Why does one texture work and load while others don't?
Please tell me why when I load one texture it works and is displayed, when I load any other texture (of the same format) an exception is thrown at 0x00007FFD52C23138 (nvoglv64.dll) in MOTOR.exe: 0xC0000005: access violation while reading at 0x000001AD861C6000.
Here is the texture loading code:
void GL::Texture::loadTextures(GLuint& texture, std::string WayToTexture)
{
data = stbi_load(WayToTexture.data(),&width,&height,&channels,0);
if (data != nullptr) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else {
std::cout << "texture"<< WayToTexture <<"couldn`t loaded"<<std::endl;
}
}
Answer the question
In order to leave comments, you need to log in
As the discussion in the comments showed, the exception is thrown from the glTexImage2D
.
Suspicion in this case can only fall on the fact that the size of the transferred data
memory is less than the calculated size, which is determined by the function based on the parameters width, height, GL_RGBA, GL_UNSIGNED_BYTE
.
This may mean that the parameters passed to glTexImage2D
the parameters do not match the read image. And since width
and height
obtained directly when reading the image, questions can only arise regarding the declared image format - GL_RGBA, GL_UNSIGNED_BYTE
.
In fact, that's exactly what it turned out to be. The read image has a different format and fewer channels, and therefore a smaller size than the calculated one.
Regarding the texture loading function itself, it is too simple. A true load function takes into account the mass of possible options for storing an image and always considers all possible points of failure. Handling all possible errors is much more important than the application code of the function.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question