Texture corruption under OS X and OpenGL

kberg
Unregistered
 
Post: #1
Wow, I can't believe how I could have missed such a relevent and active forum.

I'm working on an OS X multiplayer OpenGL based game. My game loads textures from TGA files using code taken from Nehe's tutorials, but is prone to all sorts of wierd texture corruption.

Here is an example of the types of corruption:
[Image: NotWorking.jpg]

The following is the code I'm using verbatim from my project:

Code:
typedef struct
{
    GLuint    texID;
   GLuint    bpp;
   GLuint    width;
   GLuint    height;
    GLubyte    *imageData;
} TextureImage;

bool LoadTGA(TextureImage &texture, char *filename, int UseMipMaps)
{
   GLubyte        TGAheader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
   GLubyte        TGAcompare[12];
   GLubyte        header[6];
   GLuint        bytesPerPixel;
   GLuint        imageSize;
   GLuint        temp;
   GLuint        type = GL_RGBA;

   FILE *file = fopen(filename, "rb");

    cout<<"Texture: "<<filename;

    if ( file != NULL )
    {
        if ((fread(TGAcompare, 1, sizeof(TGAcompare), file) != sizeof(TGAcompare)) ||
         (memcmp(TGAheader, TGAcompare, sizeof(TGAheader)) != 0 ) ||
         (fread(header, 1, sizeof(header), file) != sizeof(header)))
        {
             cout<<" failed to load"<<endl;
             fclose(file);
             return NULL;
        }
    }
    else
    {
        cout<<" failed to load"<<endl;
        return NULL;
    }

   texture.width  = header[1] * 256 + header[0];
   texture.height = header[3] * 256 + header[2];

   if (texture.width <= 0 || texture.height <= 0 || (header[4] != 24 && header[4] != 32))
   {
        cout<<" failed to load"<<endl;
        fclose(file);
        return false;
   }

   texture.bpp = header[4];
   bytesPerPixel = texture.bpp / 8;
   imageSize = texture.width * texture.height * bytesPerPixel;
   texture.imageData = (GLubyte *)malloc(imageSize);

   if (texture.imageData == NULL || fread(texture.imageData, 1, imageSize, file) != imageSize)
   {
        cout<<" failed to load"<<endl;
        if(texture.imageData != NULL)
            free(texture.imageData);
        fclose(file);
        return false;
   }

   for(GLuint i = 0; i < int(imageSize); i += bytesPerPixel)
   {
        temp = texture.imageData[i];
        texture.imageData[i] = texture.imageData[i + 2];
        texture.imageData[i + 2] = temp;
   }

   fclose (file);

   glGenTextures(1, &texture.texID);

   glBindTexture(GL_TEXTURE_2D, texture.texID);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   if (UseMipMaps)
   {
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture.width, texture.height, GL_RGB, GL_UNSIGNED_BYTE, texture.imageData);
   }
   else
    {
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }

   if (texture.bpp == 24)
   {
        type = GL_RGB;
   }

   glTexImage2D(GL_TEXTURE_2D, 0, type, texture.width, texture.height, 0, type, GL_UNSIGNED_BYTE, texture.imageData);
    
    cout<<" loaded succesfully"<<endl;
   return true;
}

It seems as though my textures are sometimes not respected by OpenGL, or the image data is not totally loaded or overlayed on existing textures? Once the textures are loaded they stay consistent and don't change; so I don't think this is an ongoing memory leak in my code.

The corrupted portions usually consists of bits of other textures from my game, or bits of quartz extreme windows, etc... It seems totally unrelated to the size or depth of the texture too. It is just as likely to corrupt a 32 x 32 pixel image with no alpha channel as a 512 x 512, 32 bit texture image.

It would be a great help if anyone has faced this problem before, or has an idea what might be causing it, it would be a great help!

If you want to download and try out what I've got so far, here is a link:

http://www.sfu.ca/~kaberg/Downloads/build4c7.tgz
Quote this message in a reply
ghettotek
Unregistered
 
Post: #2
downloaded and tried your game. very cool - very fast. i remember a few years back complaining to Ambrosia about EV not being multiplayer.

as for your texture problem...i have no clue. memory leak? looks like your code could easily yield problems with giving GL too much data.
Quote this message in a reply
Member
Posts: 49
Joined: 2002.05
Post: #3
Well, some problems could be:

1) the uv coordinates are messed

2) the textures aren't oriented correctly, which can have a similar effect to #1

3) you have a problem with your gl calls

4) you have updated the textures incorrectly using glTexSubImage2D()

5) you are using compressed TGA's

I looked at your code and it looks like your gl calls are in order so I don't think it's #3. You say that you can see bits of other textures mixed which makes me think that it's not #1 or #2 either. I'm pretty sure OpenGL clamps uv coordinates to 0, 1 so anything higher or lower than that shouldn't be responsible for the "mixed" textures.

Do you use glTexSubImage2D() anywhere?

Basically, I think that any problem in non-gl code would be before you call glTexImage2D(). After that I would think the only way to mess with the texture would be by calling gl functions.

One last question, are you absolutely sure you're TGA's aren't compressed?

-- Ian
Quote this message in a reply
Member
Posts: 79
Joined: 2002.08
Post: #4
The texture corruption problem on Jaguar is well known and unfortunately it's still not fixed in 10.2.4 Sad
It's very annoying!

KenD

CodeBlender Software - http://www.codeblender.com
Quote this message in a reply
kberg
Unregistered
 
Post: #5
I'm really certain that the textures are not compressed. As I said, they are regular TGA's, some 24 and some 32 bit, and I've made certain not to use RLE on them. The problems are intermittent. A certain texture may be fine one run, and then be corrupted the next time I launch the app, only to be fine again the next subsequent run...

If this is a well known bug under 10.2, are there any documents describing the problem? a discussion list, apple documentation, etc? The reason I ask is that I'm pretty certain it's a problem with my code. I haven't seen any other OpenGL apps that exhibit this texture corruption yet.

As for the project itself, this has been sort of a personal quest of mine for the last 3 years. I made the suggestion of multiplayer on Ambroisa's forums a few years back only to have dozens of responses basically telling me to do it myself. So I've been working on it ever since. Grin
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #6
There's something fishy about the call to gluBuild2DMipMaps... The red book uses the phrase "Assuming you have constructed the level 0, or highest-resolution map" about that function, but I'm not completely sure what that means. Are you sure the non-mipmapped path (glTexImage2D only) is also misbehaving?
Quote this message in a reply
Member
Posts: 49
Joined: 2002.05
Post: #7
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture.width, texture.height, GL_RGB, GL_UNSIGNED_BYTE, texture.imageData);

Don't you want GL_RGB to be type?

-- Ian
Quote this message in a reply
kberg
Unregistered
 
Post: #8
Thanks for that... that was an error, but I think it was unrelated.

Turning mipmaps on for all textures seems to aggravate the problem, and turning them totally off seems to reduce the probability of a texture being corrupted by quite a bit. I'd need to do a bit more testing to say that with any sort of certainty though. In any case I have observed cases where mipmaped textures have been corrupted, and where non-mipmaped textures have been corrupted.
Quote this message in a reply
Member
Posts: 79
Joined: 2002.08
Post: #9
The texture corruption bug has been discussed on Apple's OpenGL list and is acknowledged by Apple. The problem has been reported by a number of developers.

KenD

CodeBlender Software - http://www.codeblender.com
Quote this message in a reply
kberg
Unregistered
 
Post: #10
Heh, well that's a mixed I guess. On the one hand I can stop second guessing all my code, on the other... Does anyone know of any workarounds?Rasp
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
Texture corruption we're seeing on NVidia hardware seems to be related to using large (> 1024x1024) textures, and possibly to heavy texture-paging load. If either of those apply to you, that might be a place to start looking.

ATI claim to have eliminated the texture corruption bugs, I believe for 10.2.4.
Quote this message in a reply
kberg
Unregistered
 
Post: #12
Well,, neither of those applies to me then... I'm using 1024 x 512 max textures, and am developing on ATi hardware. As well, some of the textures I've seen get corrupted have been as small as 32 x 32 pixels.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #13
Quote:The texture corruption bug has been discussed on Apple's OpenGL list and is acknowledged by Apple. The problem has been reported by a number of developers.

As OSC says, the texture corruption issue you're discussing here is on ATI hardware. The fix for that is in 10.2.4.

I've seen one texture corruption problem on NVidia hardware when genmipmap is used. It looks like:

[Image: nv_corrupt.jpg]

Needless to say, that funny-looking square patch in the cloud layer is not supposed to be there.

Apple is currently looking at it, but there's certainly no guarantee it's a bug. It could easily be a problem in our code.

If you've got texture corruption on ATI with 10.2.4, I'd recommend going over your code again or working with ATI to find the cause of the problem.

Wade
Quote this message in a reply
Member
Posts: 79
Joined: 2002.08
Post: #14
Well, I still see the exact same random corruption on 10.2.4 and ATI as on earlier 10.2.x so it does not appear to be fixed. And if the ATI fixes were still being beta tested a few days before the 10.2.4 update I doubt it made into the update. The update has probably been in testing for a while too so I don't think some last minute drivers are being included.

KenD

CodeBlender Software - http://www.codeblender.com
Quote this message in a reply
kberg
Unregistered
 
Post: #15
Hi, my texture corruption problems do persist in 10.2.4. I don't have the use of an nVidia system to see if similar problems exist on those cards; however if anyone has access to an nVidia based system, I would be really grateful if they would let me know of any anomalies they see in gameplay.

Does anyone know who or how to contact ATi to begin discussions about my problems?

Thanks for all your help.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES Texture Masking airfire 6 14,405 Mar 17, 2014 07:07 PM
Last Post: baioses
  OpenGL ES Texture Compression ajrs84 9 3,765 May 7, 2013 03:36 PM
Last Post: ajrs84
  OpenGL ES Texture Masking dalasjoe sin 0 3,782 Apr 13, 2012 12:17 AM
Last Post: dalasjoe sin
  Texture in OpenGL ES 2 looks pixelated vunterslaush 18 22,578 Aug 30, 2011 09:44 PM
Last Post: Frogblast
  Lighting and changing texture colors in OpenGL agreendev 2 7,536 Aug 13, 2010 03:47 PM
Last Post: agreendev