   Loading .TGA files
  I've set up the code according to Nehe's tut but I think I'm missing something...the texture doesn't load..   typedef struct{GLubyte *imageData;GLuint bpp;GLuint width;GLuint height;GLuint texID;} TextureImage;TextureImage textures[1];//////bool LoadTGA(TextureImage *texture, char *filename)// Loads A TGA File Into Memory{    GLubyteTGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};// Uncompressed TGA HeaderGLubyteTGAcompare[12];// Used To Compare TGA HeaderGLubyteheader[6];// First 6 Useful Bytes From The HeaderGLuintbytesPerPixel;// Holds Number Of Bytes Per Pixel Used In The TGA FileGLuintimageSize;// Used To Store The Image Size When Setting Aside RamGLuinttemp;// Temporary VariableGLuinttype=GL_RGBA;// Set The Default GL Mode To RBGA (32 BPP)FILE *file = fopen(filename, "rb");// Open The TGA Fileif(file==NULL ||// Does File Even Exist?fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||// Are There 12 Bytes To Read?memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0||// Does The Header Match What We Want?fread(header,1,sizeof(header),file)!=sizeof(header))// If So Read Next 6 Header Bytes{if (file == NULL)// Did The File Even Exist? *Added Jim Strong*return false;// Return Falseelse{fclose(file);// If Anything Failed, Close The Filereturn false;// Return False}}texture->width  = header[1] * 256 + header[0];// Determine The TGA Width(highbyte*256+lowbyte)texture->height = header[3] * 256 + header[2];// Determine The TGA Height(highbyte*256+lowbyte)if(texture->width<=0||// Is The Width Less Than Or Equal To Zerotexture->height<=0||// Is The Height Less Than Or Equal To Zero(header[4]!=24 && header[4]!=32))// Is The TGA 24 or 32 Bit?{fclose(file);// If Anything Failed, Close The Filereturn false;// Return False}texture->bpp= header[4];// Grab The TGA's Bits Per Pixel (24 or 32)bytesPerPixel= texture->bpp/8;// Divide By 8 To Get The Bytes Per PixelimageSize= texture->width*texture->height*bytesPerPixel;// Calculate The Memory Required For The TGA Datatexture->imageData=(GLubyte *)malloc(imageSize);// Reserve Memory To Hold The TGA Dataif(texture->imageData==NULL ||// Does The Storage Memory Exist?fread(texture->imageData, 1, imageSize, file)!=imageSize)// Does The Image Size Match The Memory Reserved?{if(texture->imageData!=NULL)// Was Image Data Loadedfree(texture->imageData);// If So, Release The Image Datafclose(file);// Close The Filereturn false;// Return False} for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)// Loop Through The Image Data{// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)temp=texture->imageData[i];// Temporarily Store The Value At Image Data 'i'texture->imageData[i] = texture->imageData[i + 2];// Set The 1st Byte To The Value Of The 3rd Bytetexture->imageData[i + 2] = temp;// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)}fclose(file);// Close The FileglGenTextures(1, &texture[0].texID);// Generate OpenGL texture IDsglBindTexture(GL_TEXTURE_2D, texture[0].texID);// Bind Our TextureglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);// Linear FilteredglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);// Linear Filteredif (texture[0].bpp==24)// Was The TGA 24 Bits{type=GL_RGB;// If So Set The 'type' To GL_RGB}glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);return true;// Texture Building Went Ok, Return True}/*** stuff ****///////int InitGL(GLvoid)// OpenGL Setup{if (!LoadTGA(&textures[0],"land.TGA"))// Load The Texture{return false;// If Loading Failed, Return False}glEnable(GL_TEXTURE_2D);// Enable Texture Mapping ( NEW )// more stuff here//////int DrawGLScene(GLvoid){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear Screen and Depth BufferglLoadIdentity();// Reset the current Modelview MatrixglTranslatef(0.0f,0.0f,-3.0f);glRasterPos3f(-1.6f,1.1f,0.01f);FrameRate();glDisable(GL_TEXTURE_2D);glPrint("%2.0f", FPSCounter);glPrint(" FPS");glRotatef(xrot,1.0f,0.0f,0.0f);// Rotate On The X AxisglRotatef(yrot,0.0f,1.0f,0.0f);// Rotate On The Y AxisglEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texture[0]);// Select A Texture Based On filterglBegin(GL_QUADS);// Draws squares/quads// Front FaceglNormal3f( 0.0f, 0.0f, 1.0f);// Normal Pointing Towards ViewerglTexCoord2f(0.0f, 0.0f); glVertex3f(-1.7f, -1.2f, 0.0f);// Bottom Left Of The Texture and QuadglTexCoord2f(1.0f, 0.0f); glVertex3f(1.7f, -1.2f, 0.0f);// Bottom Right Of The Texture and QuadglTexCoord2f(1.0f, 1.0f); glVertex3f(1.7f, 1.2f, 0.0f);// Top Right Of The Texture and QuadglTexCoord2f(0.0f, 1.0f); glVertex3f(-1.7f, 1.2f, 0.0f);// Top Left Of The Texture and QuadglEnd();// more drawing herecan anyone spot something I've missed? I've tried changing 'rb' to 'tree.tga' then it says 'initialisation failed'...[edited by - Squalion on August 1, 2003 3:15:32 PM]
  rb means read binary...   "C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"  ;  right after you open the file and after the first two fread, you are checking to see if the file is null, but basically what you''re doing is that if the file ISN''T null, you STILL EXIT the function in the else statement. get rid of that else. ;  edited..to no avail bool LoadTGA(TextureImage *texture, char *filename)// Loads A TGA File Into Memory{    GLubyteTGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};// Uncompressed TGA HeaderGLubyteTGAcompare[12];// Used To Compare TGA HeaderGLubyteheader[6];// First 6 Useful Bytes From The HeaderGLuintbytesPerPixel;// Holds Number Of Bytes Per Pixel Used In The TGA FileGLuintimageSize;// Used To Store The Image Size When Setting Aside RamGLuinttemp;// Temporary VariableGLuinttype=GL_RGBA;// Set The Default GL Mode To RBGA (32 BPP)FILE *file = fopen(filename, "rb");// Open The TGA Fileif(file==NULL ||// Does File Even Exist?fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||// Are There 12 Bytes To Read?memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0||// Does The Header Match What We Want?fread(header,1,sizeof(header),file)!=sizeof(header))// If So Read Next 6 Header Bytes{if (file == NULL)// Did The File Even Exist? *Added Jim Strong*return false;// Return False}texture->width  = header[1] * 256 + header[0];// Determine The TGA Width(highbyte*256+lowbyte)texture->height = header[3] * 256 + header[2];// Determine The TGA Height(highbyte*256+lowbyte)if(texture->width<=0||// Is The Width Less Than Or Equal To Zerotexture->height<=0||// Is The Height Less Than Or Equal To Zero(header[4]!=24 && header[4]!=32))// Is The TGA 24 or 32 Bit?{fclose(file);// If Anything Failed, Close The Filereturn false;// Return False}texture->bpp= header[4];// Grab The TGA's Bits Per Pixel (24 or 32)bytesPerPixel= texture->bpp/8;// Divide By 8 To Get The Bytes Per PixelimageSize= texture->width*texture->height*bytesPerPixel;// Calculate The Memory Required For The TGA Datatexture->imageData=(GLubyte *)malloc(imageSize);// Reserve Memory To Hold The TGA Dataif(texture->imageData==NULL ||// Does The Storage Memory Exist?fread(texture->imageData, 1, imageSize, file)!=imageSize)// Does The Image Size Match The Memory Reserved?{if(texture->imageData!=NULL)// Was Image Data Loadedfree(texture->imageData);// If So, Release The Image Datafclose(file);// Close The Filereturn false;// Return False} for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)// Loop Through The Image Data{// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)temp=texture->imageData[i];// Temporarily Store The Value At Image Data 'i'texture->imageData[i] = texture->imageData[i + 2];// Set The 1st Byte To The Value Of The 3rd Bytetexture->imageData[i + 2] = temp;// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)}fclose(file);// Close The FileglGenTextures(1, &texture[0].texID);// Generate OpenGL texture IDsglBindTexture(GL_TEXTURE_2D, texture[0].texID);// Bind Our TextureglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);// Linear FilteredglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);// Linear Filteredif (texture[0].bpp==24)// Was The TGA 24 Bits{type=GL_RGB;// If So Set The 'type' To GL_RGB}glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);return true;// Texture Building Went Ok, Return True}edit: I added a msgbox to just before 'return true;', so it's obvious the loading code is working...yet the texture won't bind..   [edited by - Squalion on August 1, 2003 3:45:53 PM]