sealfin
2002.07.15, 12:49 PM
I've been slowly going through the GLUT ports of NeHe's tutorials, recoding/extending them in Carbon to comprehend better the mechanics behind them; but I've hit a wall with tutorial 6 : a recoding of the TGA texture loader originally coded by Anthony Parker kills the app every time the glGenTextures() statement is reached (or on any statements past it if glGenTextures is commented out) - any suggestions would be appreciated! thanks!
p.s. the relevant bit is right at the bottom - sorry!
typedef struct sSkin
{
GLubyte x,
y,
depth,
*skin;
GLuint id;
} tSkin;
...
//
//
signed int ISkinFile_tga(tSkin *outSkin,
char *srcFilePath)
{
GLubyte TGAHeader[12],
TGAComparisonHeader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
TGAFileHeader[6],
bytesPerPixel;
GLuint skinMemorySize,
swap,
depthType = GL_RGBA;
FILE *file;
unsigned int loop;
if((file = fopen(srcFilePath, "rb")) == NULL)
return kErr_FileOpenFailed;
if(fread(TGAHeader, 1, sizeof(TGAHeader), file) != sizeof(TGAHeader))
return kErr_TGAHeaderSizeWrong;
if(memcmp(TGAHeader, TGAComparisonHeader, sizeof(TGAHeader)) != 0)
return kErr_TGAHeaderContentWrong;
if(fread(TGAFileHeader, 1, sizeof(TGAFileHeader), file) != sizeof(TGAFileHeader))
return kErr_TGAFileHeaderSizeWrong;
if((outSkin->x = TGAFileHeader[1]*256+TGAFileHeader[0]) <= 0)
return kErr_SkinXZeroOrNegative;
if((outSkin->y = TGAFileHeader[3]*256+TGAFileHeader[2]) <= 0)
return kErr_SkinYZeroOrNegative;
if(((outSkin->depth = TGAFileHeader[4]) != 24) && (outSkin->depth != 32))
return kErr_SkinWrongDepth;
bytesPerPixel = outSkin->depth/8;
skinMemorySize = outSkin->x*outSkin->y*bytesPerPixel;
outSkin->skin = (GLubyte *)malloc(skinMemorySize);
if(fread(outSkin->skin, 1, skinMemorySize, file) != skinMemorySize)
return kErr_SkinMemorySizeDidNotEqualReservedMemorySize;
for(loop = 0;
loop < (int)skinMemorySize;
loop += bytesPerPixel)
{
swap = outSkin->skin[loop];
outSkin->skin[loop] = outSkin->skin[loop+2];
outSkin->skin[loop+2] = swap;
}
fclose(file);
// dies here (or at any other statement onwards if I comment this out) : I've tried _lots_ of variartions on this statement, with no difference.
glGenTextures(1, &outSkin[0].id);
glBindTextures(GL_TEXTURES_2D, outSkin[0].id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(outSkin[0].depth == 24)
depthType = GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, depthType, outSkin[0].x, outSkin[0].y, 0, depthType, GL_UNSIGNED_BYTE, outSkin[0].skin);
return 0;
}
p.s. the relevant bit is right at the bottom - sorry!
typedef struct sSkin
{
GLubyte x,
y,
depth,
*skin;
GLuint id;
} tSkin;
...
//
//
signed int ISkinFile_tga(tSkin *outSkin,
char *srcFilePath)
{
GLubyte TGAHeader[12],
TGAComparisonHeader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
TGAFileHeader[6],
bytesPerPixel;
GLuint skinMemorySize,
swap,
depthType = GL_RGBA;
FILE *file;
unsigned int loop;
if((file = fopen(srcFilePath, "rb")) == NULL)
return kErr_FileOpenFailed;
if(fread(TGAHeader, 1, sizeof(TGAHeader), file) != sizeof(TGAHeader))
return kErr_TGAHeaderSizeWrong;
if(memcmp(TGAHeader, TGAComparisonHeader, sizeof(TGAHeader)) != 0)
return kErr_TGAHeaderContentWrong;
if(fread(TGAFileHeader, 1, sizeof(TGAFileHeader), file) != sizeof(TGAFileHeader))
return kErr_TGAFileHeaderSizeWrong;
if((outSkin->x = TGAFileHeader[1]*256+TGAFileHeader[0]) <= 0)
return kErr_SkinXZeroOrNegative;
if((outSkin->y = TGAFileHeader[3]*256+TGAFileHeader[2]) <= 0)
return kErr_SkinYZeroOrNegative;
if(((outSkin->depth = TGAFileHeader[4]) != 24) && (outSkin->depth != 32))
return kErr_SkinWrongDepth;
bytesPerPixel = outSkin->depth/8;
skinMemorySize = outSkin->x*outSkin->y*bytesPerPixel;
outSkin->skin = (GLubyte *)malloc(skinMemorySize);
if(fread(outSkin->skin, 1, skinMemorySize, file) != skinMemorySize)
return kErr_SkinMemorySizeDidNotEqualReservedMemorySize;
for(loop = 0;
loop < (int)skinMemorySize;
loop += bytesPerPixel)
{
swap = outSkin->skin[loop];
outSkin->skin[loop] = outSkin->skin[loop+2];
outSkin->skin[loop+2] = swap;
}
fclose(file);
// dies here (or at any other statement onwards if I comment this out) : I've tried _lots_ of variartions on this statement, with no difference.
glGenTextures(1, &outSkin[0].id);
glBindTextures(GL_TEXTURES_2D, outSkin[0].id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(outSkin[0].depth == 24)
depthType = GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, depthType, outSkin[0].x, outSkin[0].y, 0, depthType, GL_UNSIGNED_BYTE, outSkin[0].skin);
return 0;
}