PDA

View Full Version : Texture crashing issues


Josh
2003.04.09, 04:06 PM
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.

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. :( And yes, this is a mutilated version of the texture loading code in the QTValuePak 2.

Frank C.
2003.04.09, 06:10 PM
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...

OneSadCookie
2003.04.09, 06:20 PM
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 :)

Remember, if you don't have a current OpenGL context, any OpenGL call may (and on Mac OS X, probably will) crash.

DoG
2003.04.09, 08:52 PM
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.

Josh
2003.04.10, 09:29 AM
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. :D 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. ;)

Josh
2003.04.10, 04:11 PM
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:

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.

DoG
2003.04.10, 04:49 PM
you should set texture mode somewhere, and there is one too many calls to glVertex for a quad with tristrips.

Josh
2003.04.10, 07:57 PM
What's texture mode? If I leave out the last glVertex call, it have a triangular section missing from the quad.

DoG
2003.04.11, 06:36 AM
I think the following is better (you are right, the triangle strip thing works like that, my bad):


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();


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.

Josh
2003.04.11, 11:49 PM
: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. :???: