Archived

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

iNsAn1tY

MD3 Trouble...

Recommended Posts

Evening all. I've got a problem with opening MD3 files, and it's driving me nuts. I'm using the tutorial off www.gametutorials.com for reference, and, having read through it, have started to write my own MD3 loader. The code for it is listed below. The problem is, the file opens OK, and the header is read in correctly, but when I go to read in the "Bones" (as the tutorial refers to them; they're actually frames), the program simply quits. I traced the problem to an "Access Violation" error during the fread function. I compiled the tutorial, with the same compiler (MSVC++6), and it worked fine. There is little difference between the code in mine and the tutorial, and I've exhausted myself looking for the error. I'm using the same structures, and the same method. In fact, the only thing that's different is the method of dynamic memory allocation; he uses new and I use malloc(), and I've looked for an error there, and not found one. Maybe I'm missing something. Here's the loader function. It takes a model struct (which is the basically just all the required md3 structs in one place) and the filename of the model:
  // Opens the specified *.md3 file, and

// loads in it's contents...

int LoadMD3(WAR_MODEL warModel, char *sFilename)
{
	
	char *ID; FILE *fpModel;
	WAR_MD3BONE *md3Bones;
	WAR_MD3HEADER md3Header;
	
	// Open the file...

	if((fpModel = fopen(sFilename, "rb")) == NULL)
	{
		
		// The file probably couldn't be found.

		// Return an error, quit the program...

		sprintf(sBuffer, "The *.md3 file specified couldn't be opened...");
		MessageBox(hWnd, sBuffer, "Error...", MB_OK | MB_ICONEXCLAMATION);
		return(1);
		
	}
	
	// Read in the file header...

	if(fread(&md3Header, 1, sizeof(WAR_MD3HEADER), fpModel) < 1)
	{
		
		// ...and quit if it failed...

		sprintf(sBuffer, "An md3 header couldn't be read...");
		MessageBox(hWnd, sBuffer, "Error...", MB_OK | MB_ICONEXCLAMATION);
		fclose(fpModel);
		return(1);
		
	}
	
	// Get this file's ID...

	ID = md3Header.fileID;
	
	// Check if this md3 file is valid...

	if((ID[0] != 'I' || ID[1] != 'D' || ID[2] != 'P' || ID[3] != '3') && md3Header.version != 15)
	{
		
		// ...and quit if it isn't...

		sprintf(sBuffer, "This isn't a valid md3 file...");
		MessageBox(hWnd, sBuffer, "Error...", MB_OK | MB_ICONEXCLAMATION);
		fclose(fpModel);
		return(1);
		
	}
	
	// Allocate some space for the bones,

	// then read them all in. They're not

	// required, however, so discard them...

	
	// HERE'S WHERE THE PROBLEM IS.

	// This works fine...

	md3Bones = (WAR_MD3BONE *)malloc(md3Header.numFrames);
	// This screws up...

	fread(&md3Bones, sizeof(WAR_MD3BONE), md3Header.numFrames, fpModel);
	// This works fine...

	free(md3Bones);
	
	// Next, allocate some memory for the

	// tags, and read them in...

	
	
	// Everything went fine, so close

	// the model, and return a success...

	fclose(fpModel);
	return(0);
	
}    
I'm running WinXP, if that's any help. Thanks in advance for any assistance...

Coding Stuff ->  [ iNsAn1tY Games | DarkVertex | How To Do CSG | Direct3D Vs. OpenGL | Google ]
Fun Stuff    ->  [ Evil T-Shirts | Stick-Based Comedy | You're Already Here | The Best Film Reviews ]
[edited by - iNsAn1tY on December 26, 2002 11:15:27 PM]

Share this post


Link to post
Share on other sites
Hey, thanks, it worked. I was also sending the address of the pointer (the &md3Bones bit in the fread statement) rather than the pointer itself. I knew it was something stupid...



Coding Stuff ->  [ iNsAn1tY Games | DarkVertex | How To Do CSG | Direct3D Vs. OpenGL | Google ]
Fun Stuff    ->  [ Evil T-Shirts | Stick-Based Comedy | You''re Already Here | The Best Film Reviews ]

Share this post


Link to post
Share on other sites