Original Post
Hey I used to use NeHe's TGA loading function: Recently I switched over to the SDL_image library because it kicks more ass. However I used to do a trick back in my TGA days where I would go like this: So I would execute a bunch of stuff, like display a 2nd layer, but only if an image was loaded up. These days all I have around is an array of GLuints that correspond to the png files that OpenGL loaded into memory. Is there any way to see if there's a loaded image that corresponds to your GLuint? I don't want to have to make a bunch of booleans to store along with my GLuints just to record if they're being used or not. If I set a GLuint to NULL that just makes it zero, doesn't it? For all I know that might actually correspond with an image loaded in memory so that wouldn't work. Does OpenGL have a specific range that it sets the GLuints to? Is there some magic number I can set GLuints to so that I know they aren't being used? I'm looking at glAreTexturesResident and I'm thinking I can send a GLuint to that and it will return false if that GLuint does not correspond to a loaded image. But what if I'm misunderstanding it? What if OpenGL runs out of video memory so it unloads an image that isn't being used much into regular memory? Then would glAreTexturesResident return false for that GLuint even though it's technically loaded, just not in video memory?
struct TextureImage
{
GLubyte *imageData;
GLuint bpp;
GLuint width;
GLuint height;
GLuint texID;
};
bool LoadTGA(TextureImage *texture, char *filename)
{
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");
if(file==NULL||
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare)||
memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0||
fread(header,1,sizeof(header),file)!=sizeof(header))
{
if(file!=NULL)
fclose(file);
return false;
}
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))
{
fclose(file);
return false;
}
texture->bpp=header[4];
bytesPerPixel=texture->bpp/8;
imageSize=texture->width*texture->height*bytesPerPixel;
texture->imageData = (GLubyte *)malloc(imageSize); //Ah, malloc, C's answer to "new". So when is this memory freed?
//you have to free it yourself NeHe has a Deinitialize function
if(texture->imageData==NULL||fread(texture->imageData, 1, imageSize, file)!=imageSize)
{
if(texture->imageData!=NULL)
{
free(texture->imageData); //well, I guess this is how you free it
texture->imageData = NULL;
}
fclose(file);
return false;
}
//for a fun time comment this out
for(GLuint i=0; i<(unsigned int)imageSize; i+=bytesPerPixel)
{
temp=texture->imageData;
texture->imageData = texture->imageData[i + 2];
texture->imageData[i + 2] = temp;
}
fclose (file);
// Build A Texture From The Data
glGenTextures(1, &texture[0].texID); //texture is a pointer to a TextureImage
//why not just texture.texID? why &texture[0]?? wtf
//the the address of a pointer? the first element of it? it has no elements, wtf?
//wtf could [0] possibly mean?
//oh I remember, you use -> for pointers instead of . like above
//so here he wants to use . so maybe ampersand on a pointer "converts" it back to a regular data type?
//I have no idea what's going on
glBindTexture(GL_TEXTURE_2D, texture[0].texID); //And here it needs no ampersand. Why? I don't know.
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(texture[0].bpp==24)
{
type=GL_RGB;
}
//Here using . instead of -> so maybe [0] takes care of it? Then why the ampersand above?
glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height,0, type, GL_UNSIGNED_BYTE, texture[0].imageData);
return true;
}
if(textures[2].imageData!=NULL)
then blah blah blah