Texture crashing issues

Moderator
Posts: 608
Joined: 2002.04
Post: #1
Why are almost all the problem with OpenGL texture-related? Here's my problem: I made a C++ class called GLTexture to handle (suprise!) my OpenGL textures. I use the code from the QTValuePak 2 to load the texture but it always crashes with a type 10 error.
Code:
void GLTexture::Load(char *path)
{
    FSSpec spec;
    Rect natbounds;
    GraphicsImportComponent gi;
    ComponentResult cr;
    void *buffer;
    GWorldPtr gw;
    OSStatus err;
    
    MakeFSSpecFromPath(path, &spec);
    
    err = GetGraphicsImporterForFile(&spec, &gi);

    if(err != noErr)
        return;
    
    cr = GraphicsImportGetNaturalBounds(gi, &natbounds);

    buffer = malloc(4 * natbounds.bottom * natbounds.right);

    if(buffer == NULL)
        return;

    err = QTNewGWorldFromPtr(&gw, k32ARGBPixelFormat, &natbounds, NULL, NULL, 0, buffer, 4 * natbounds.right);

    if(err != noErr)
        return;

    cr = GraphicsImportSetGWorld(gi, gw, NULL);
    cr = GraphicsImportDraw(gi);

    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, natbounds.right, natbounds.bottom, 0, GL_BGRA_EXT, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);

    err = CloseComponent(gi);

    if(err != noErr)
        return;
    
    DisposeGWorld(gw);
    
    free(buffer);
}
According to the Console, it crashes at glGenTextures. Sad And yes, this is a mutilated version of the texture loading code in the QTValuePak 2.
Quote this message in a reply
Member
Posts: 440
Joined: 2002.09
Post: #2
That should work (just make sure textureID is a GLuint) - but I actually prefer to maintain my own texture names and do away glGenTextures all together, so I've never run into this problem myself.

Then again, it could be crashing elsewhere and it just looks like glGenTextures is the problem...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
It's almost certainly crashing because you don't yet have an OpenGL context. If you need to confirm that, put another OpenGL call there and see if it still crashes Smile

Remember, if you don't have a current OpenGL context, any OpenGL call may (and on Mac OS X, probably will) crash.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
Am I seeing malloc and free in a class method? new and delete are your friends. I think no real performance issue warrants using malloc and free instead of new and delete. I think it is a bad habit to sporadically use them, you never know when you might alloc an object with malloc by mistake, leaving you in a big mess.

I, too, believe it has to do with the missing gl context, since your function crashes with the first gl function call.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #5
You were right on. Once I put my OpenGL setup code before the GLTexture code it worked fine.

>Am I seeing malloc and free in a class method?

Yes. Grin As I said, this was almost a straight copy from the QTValuePak 2. Right now I just want to get this working and then I will make it look better. Wink
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #6
OK, another question for ya. My texture loading code works now (I think) but I can't get my textures to show. Here's my code:
Code:
void GLSprite::Draw(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
{
    glLoadIdentity();
    glTranslatef(x, y, z);

    glEnable(GL_TEXTURE_2D);

    switch(axis)
    {
        case kRotateXAxis:
            glRotatef(rot, 1.0, 0.0, 0.0);
            break;

        case kRotateYAxis:
            glRotatef(rot, 0.0, 1.0, 0.0);
            break;

        case kRotateZAxis:
            glRotatef(rot, 0.0, 0.0, 1.0);
            break;
    }

    glColor4f(r, g, b, a);

    if(textureID)
        glBindTexture(GL_TEXTURE_2D, textureID);

    glBegin(GL_TRIANGLE_STRIP);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(width, height, depth);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(width, -height, depth);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(-width, -height, depth);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(-width, height, depth);
    glTexCoord2f(0.0, 0.0);
    glVertex3f(width, height, depth);
    glEnd();

    rot += rotIncrement;
}
Sorry for the silly questions. Textures have always given me problems.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #7
you should set texture mode somewhere, and there is one too many calls to glVertex for a quad with tristrips.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #8
What's texture mode? If I leave out the last glVertex call, it have a triangular section missing from the quad.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #9
I think the following is better (you are right, the triangle strip thing works like that, my bad):

[SOURCECODE]
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(width, height, depth);
glTexCoord2f(0.0, 1.0);
glVertex3f(width, -height, depth);
glTexCoord2f(1.0, 1.0);
glVertex3f(-width, -height, depth);
glTexCoord2f(1.0, 0.0);
glVertex3f(-width, height, depth);
glEnd();
[/SOURCECODE]

This gives a nice quad with correct texture coords, depending how you want them oriented.

I cannot tell u about the texture modes right now, but it is something like GL_DECAL, GL_LUMINANCE, etc, which describe how the texture is applied to the triangle. I cant tell more because I am away from my mac, and dont remember the exact syntax for setting them, but the red book explains it.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #10
Blush

Found the problem. It was a mixture of things but the ultimate problem was my texture was not a power of 2. That's what I get for eye-balling it. Thanks to you all for humoring me. Huh
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Draw to texture using FBOs - aspect ratio issues Madrayken 2 3,893 Jul 15, 2010 11:47 AM
Last Post: Madrayken
  glCreateShader crashing Jar445 0 4,296 Jan 19, 2009 02:39 PM
Last Post: Jar445
  ImageIO texture loader crashing - Cocoa mahalis 4 3,016 Aug 16, 2006 07:33 AM
Last Post: djork
  Open GL texture Issues ThorPrime 5 3,672 Jun 13, 2005 11:13 PM
Last Post: ThorPrime
  SDL_mixer crashing on loop - why? sealfin 8 5,569 Jan 26, 2005 09:25 PM
Last Post: aaronsullivan