2

I've a problem with the stbi library and I thought, maybe you have an idea why this isn't working. I have declared a function like this:

bool LoadTextureFile(std::string file, unsigned char ** pixel_data, int * width, int * height, int * n);

In this function I get the result of stbi_load directly saved in the *pixel_data variable:

*pixel_data = stbi_load(file.c_str(), width, height, n, 0);
// Do some more stuff till return
return true;

So, now my pixel_data pointer points to the memory of the result of stbi_load. Now I wanna use this result with the glTexImage2D method in my previous function. This function calls the LoadTextureFile method before calling the glTexImage2D method of OpenGL like this:

bool LoadTexture(...)
{
  int tex_width, tex_height, tex_n;
  unsigned char * pixel_data    = NULL;
  LoadTextureFile(filename, &pixel_data, &tex_width, &tex_height, &tex_n);
  // Do something special ...
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, &pixel_data);
  stbi_image_free(&pixel_data);
  // ...
}

But if I do it like that, then I get a memory violation message at the point of calling the glTexImage2D. If I move this whole magic into the LoadTextureFile, after loading a new texture file with stbi_load, then it works:

bool LoadTextureFile(std::string file, unsigned char ** pixel_data, int * width, int * height, int * n)
{
  unsigned char * = = stbi_load(file.c_str(), width, height, n, 0);
  // Do some magic ...
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 80, 80, 0, GL_RGB, GL_UNSIGNED_BYTE, pixel_data); 
  stbi_image_free(pixel_data);
  return true;
}

Can someone tell me why I get this message and how to solve this problem?

I guess, it is a matter of keep the reserved memory safe, but I'm not really sure, how to solve it. I tried this in a simple console application before, and there it works.

Thank you for your help!

MANiC
  • 103
  • 2
  • 9

1 Answers1

1

This:

unsigned char * pixel_data    = NULL;
[...]
glTexImage2D(..., &pixel_data);

is certainly not what you want. You are using the address of the pionter to your pixel data, not the value of the pointer, so you are basically telling the GL to use some random segment of your stack memory as source for the texture. It should be just

glTexImage2D(..., pixel_data);

In your second variant, what actually happens is unclear since the line

unsigned char * = = stbi_load(file.c_str(), width, height, n, 0);

just doesn't make sense and will never compile. So I assume it is copy and paste error when writing the question. But it is hard to guess what your real code would do.

derhass
  • 43,833
  • 2
  • 57
  • 78
  • Thank you, I don't know why I wrote "&pixel_data". I guess, after looking for so long for an error of my code makes me blind after some hours of searching. (-; – MANiC Jun 02 '14 at 23:03
  • And yeah, "unsigned char * = = stvi_load(...);" is nonsense. It must be "unsigned char * pixel_data = stvi_load(...);". Simple copy and paste error. (; – MANiC Jun 02 '14 at 23:08
  • There is another question I have, but I don't know if I should start a new topic: Is it better to use glTexImage2D or should I use gluBuild2DMipmaps? I tried with the last method, but it never worked for me ... /-: – MANiC Jun 02 '14 at 23:09
  • @MANiC: `glTexImage2D` and `gluBuild2DMipmaps` are different things. The first use specifies a single level, while the latter builds the whole mipmap pyramid. The glu stuff is deprecated and should not be used with modern GL, but it should still work. I don't know why it doesn't in your case. You should probably open another question if you have some issue with it. – derhass Jun 03 '14 at 14:32