Archived

This topic is now archived and is closed to further replies.

playmesumch00ns

TGA image loading "Debug Assertion Failed!"

Recommended Posts

Sorry to start a new thread on this but i must know now what is going on. After reading similar posts on this kind of error I''ve taken a look at my pointers, memory allocation and use of free and really can''t see what''s doing it. Here''s the error MSDEV gives me: Debug Assertion Failed! Program: C:\c\OpenGL RTG Loader\rtgload.exe File: fopen.c Line: 53 Expression: file != NULL ...blah blah see documentation blah And here is as much of the code as I can see is relevant. Incidentaly, the loadTGA function is the actual one from lesson 25, just copied across. Lesson 25 compiles and runs fine. #include #include #include //#include #include //#include //#include #include "model.h" Model Mdl; GLuint mdlPtr; float xrot, yrot, zpos; typedef struct { //pointer to image data (up to 32 bit) GLubyte *imageData; //image colour depth in bits per pixel GLuint bpp; GLuint width; GLuint height; GLuint texID; } TextureImage; //storage for one texture TextureImage textures[1]; bool loadTGA(TextureImage *texture, char *filename) // Loads A TGA File Into Memory { GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header GLubyte TGAcompare[12]; // Used To Compare TGA Header GLubyte header[6]; // First 6 Useful Bytes From The Header GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram GLuint temp; // Temporary Variable GLuint type=GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP) FILE *file = fopen(filename, "rb"); // Open The TGA File if( 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 else { fclose(file); // If Anything Failed, Close The File 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 Zero texture->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 File return 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 Pixel imageSize = texture->width*texture->height*bytesPerPixel; // Calculate The Memory Required For The TGA Data texture->imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data if( 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 Loaded free(texture->imageData); // If So, Release The Image Data fclose(file); // Close The File return false; // Return False } for(GLuint i=0; iimageData; // 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 Byte texture->imageData[i + 2] = temp; // Set The 3rd Byte To The Value In ''temp'' (1st Byte Value) } fclose (file); // Close The File // Build A Texture From The Data glGenTextures(1, &texture[0].texID); // Generate OpenGL texture IDs glBindTexture(GL_TEXTURE_2D, texture[0].texID); // Bind Our Texture glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtered if (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 } bool init(void) { if (!loadTGA(&textures[0], "crate.tga")) return false; GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0 }; GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0}; glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glBindTexture(GL_TEXTURE_2D, textures[0].texID); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); return true; } Please somebody help this is driving me up the wall and I really need to get on with the project (GRR!!) Thanks ch00ns

Share this post


Link to post
Share on other sites
Hi,

That assertation will raise if filename points to NULL, as far as I can see. You might want to install the CRT sources, because they are often helpful. You''ll see that _tfopen does not modify the first argument (the filename) before passing it to _tfsopen, which ASSERTs that filename != NULL.

Try to write
  
const char *szFileName = "crate.tga"

//...


bool bReturnValue = false;
bReturn Value = LoadTga(&textures[0], szFileName);
if(!bReturnValue)

//....



This ensures that the compiler really generates the string. I think the compiler does not store your string in the program''s data segment. Try to search the binary (the .exe or .dll) for the string "crate.tga". If it is not in there, it was probably optimized away. Also try to set warning level to 4, which will probably give you a warning such as ''function call in a conditional expression'' or such.

Hope this helps,
syn

Share this post


Link to post
Share on other sites