I
I
IliaNeverov2021-07-21 20:46:27
C++ / C#
IliaNeverov, 2021-07-21 20:46:27

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;
  }
}

In addition, if you replace the path with an incorrect one, then a message will simply be displayed in the console that the texture is not loaded, but if the path is correct (to other pictures), then an exception is thrown
. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2021-07-22
@IliaNeverov

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 datamemory 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 glTexImage2Dthe parameters do not match the read image. And since widthand heightobtained 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 question

Ask a Question

731 491 924 answers to any question