3

I am trying to catch the error that comes from incompatible image texture resolution in

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, imgdata);


1. Compatible resolution, working fine = 1024x1024, 1920x1200, 1920x1080, 704x891
2. Incompatible resolutions, all giving error = 2058x1734, 1018x1280, 591x602, etc.

I don't know why glTexImage2D is working fine with #1 resolutions and giving error with #2 resolutions. I couldn't extract the resolution pattern that it follows means that some resolution are working fine and some not. I don't know why.

Now the issue is, if user try to create a texture with incompatible resolution then OpenGL should catch the error and I should get 0 if texture is not created but glTexImage2D is not returning anything so we are unable to make something secure for user OK. "This resolution is not supported, do not use it." But the issue is OpenGL doesn't not catch the glTexImage2D error.

During run-time execution

cout<<until here working fine..<<endl;

glTexImage2D(......) run-time execution stops here and windows gives not responding error.

cout<<"output of something that never executed"<<endl;
Here If I use glGetError() it never works. Because windows gives error in glTexImage2D.

So, is anybody know how can I catch glTexImage2D error. If it gives error then I display OK "Dont use this Image, Use another one." and return 0.

How to achieve this task ? I also thought about to restrict the user in only some resolutions but this isn't the solution. Right ?

maxpayne
  • 1,111
  • 2
  • 21
  • 41
  • 2
    Pattern wise, I can only see that the non-working textures are not a multiple of 4 pixels wide. – JasonD Dec 22 '12 at 10:37
  • I think so, you are right. So do you think I can restrict in this way ? if(imgW%4 == 0) then create texture. Right ? – maxpayne Dec 22 '12 at 10:41
  • You shouldn't have to, it seems like there's a different underlying problem. Are you sure your image data is valid for those non-working sizes? i.e. is your image loader passing a bogus pointer for glTexImage? – JasonD Dec 22 '12 at 10:42
  • (though if it really is a driver issue, and restricting the texture size works, what the heck - a hack is better than a crash...) – JasonD Dec 22 '12 at 10:44
  • OK what I just did is, I resize the incompatible image to compatible one, and it works fine. So, what the issue now ? – maxpayne Dec 22 '12 at 10:46

2 Answers2

4

Those image formats not accepted in your case are not multiples of 4. This leads me to the question: Do you set the pixel storage parameters? Set all GL_UNPACK_… parameters to match your image data right before the call to glTexImage. GL_UNPACK_ALIGNMENT is of particular interest for your problem.

http://www.opengl.org/sdk/docs/man/xhtml/glPixelStore.xml

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I got your point but where to put glPixelStorei(GL_UNPACK_ALIGNMENT, 1); just before glTexImage2D() ? or where ? – maxpayne Dec 22 '12 at 13:17
  • You are right. I solved it with glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // this is so important in order to avoid invalid image resolution. Image width should be divided by 4 in glTexImage2D (image->width%4 == 0). glPixelStorei makes texture compatible. – maxpayne Dec 22 '12 at 13:58
  • @furqan: I did write in my answer, where to put the calls to glPixelStore. I re-formatted the already present stanza in bold for emphasis. – datenwolf Dec 22 '12 at 14:53
0

This looks like some odd driver bug. OpenGL calls should never crash, no matter what you pass (as long as it's not an invalid pointer or wrong sizes with pointer operations). Also I don't see any pattern on the textures failing to load.

Usually there should only be three limiting capabilities regarding valid textures: aspect ratio and dimensions:

  • A texture can be too high or too wide to be processed.
  • Older cards might require textures to be square (width = height).
  • Older cards might require texture dimensions to be powers of two (width = 2n; height = 2m).

I can't see any real pattern in your texture dimensions.

The texture format (i.e. color depth/color channels) might be another issue, but don't think that's actually involved here.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • I am not using older GPU, its Nvidia GeForce GT 220 512 MB. colors or Channels are not issue here because the same image is working after resizing into compatible resolution. JasonD found out that might is pattern wise width should be divided by 4. – maxpayne Dec 22 '12 at 10:52