Welcome! Log In Create A New Profile

Advanced

MD2 Model Binary File Reading

Posted by michela 
MD2 Model Binary File Reading
March 18, 2010 04:36PM
I would like to render 3D models by using an md2 file loader then code my own rendering functions.
The loader is given by the really good web site : [tfc.duke.free.fr]

The tutorial is easy to understand and, under cygwin, i can compile the loading part and test it with some models (cars, helicopters, ogre !!). The C code works without any errors and i can even use printf commands to show the results for frame 0. So by inserting printf("GX_..") commands and including the text in GRRlib examples after the cube, i was able to render my car or helicopter model with its texture on the wii.

Now I would like to use the loader code directly in my wii source code to be able to load different models and animate them.
I thought the loader porting on the wii to be direct by using libfat but i encounter some difficulties. Maybe you can help me to pass this step.

The code compile and starts but i encounter an error at the very beginning of the MD2loader when it starts reading the header of the file. I wonder if i need to change the syntax of the fread command to adapt to wii libfat or if i made a mistake.

a) sizeof (struct md2_header_t) = 68 OK !!
b) mdl->header.ident = 1229213746 WRONG !!
c) mdl->header.version = 134217728 WRONG !!

Thank you in advance for your help.


#include 
#include 

#include 
#include 
#include 

#include 
#include 

...

int
TestReadMD2ModelHeader (const char *filename, struct md2_model_t *mdl)
{
FILE *fp;
printf ("Open File TestReadMD2ModelA [%s]\n", filename);
  fp = fopen (filename, "rb");
  if (!fp)
    {
      printf ("Error: couldn't open \"%s\"!\n", filename);
      return 0;
    }
	
   /* Read header */ 
   printf ("Read Header\n");	
   printf ("Header Size = %d \n" ,sizeof (struct md2_header_t)  );

   fread (&mdl->header, 1, sizeof (struct md2_header_t), fp);

   if ((mdl->header.ident != 844121161) ||
      (mdl->header.version != 8))
    {
      /* Error! */	  
      printf ("Error: bad version or identifier\n");
      printf ("mdl->header.ident  = %d  [Should Be 844121161 ] \n", mdl->header.ident);
      printf ("mdl->header.version  =%d [Should Be 8         ] \n", mdl->header.version);
      fclose (fp);
      return 0;
    }

   return 1;   
}

Re: MD2 Model Binary File Reading
March 18, 2010 05:14PM
The ident integer 844121161 corresponds to the string "IDP2".
If i try to fread a char[4], i only see [ IDP].
If i use a 5*size(char) in fread, i see the whole word but the second integer 8 is not good.
I don't think it is the good solution but it can help to find a solution.

I continue to search ...
Re: MD2 Model Binary File Reading
March 18, 2010 05:46PM
Endianess is probably the issue.

I already wrote a MD2 model reader some time back. If you want it I've put it here :- [scanff.com]
Re: MD2 Model Binary File Reading
March 18, 2010 05:54PM
Thank you very much.

I was looking for such a code on the web but it was not able to find it.
I think many people will be happy to use your code or adapt it for their use.

About my question :
I have tried to read the file one char after one char and the result is OK so i think i have to come back to the basics of binary file format and understand my mistakes.

Thank you again.
Re: MD2 Model Binary File Reading
March 18, 2010 06:00PM
Just look at

unsigned int int4_swap (unsigned int x)
{
   return (((x&0x000000FF)<<24)+((x&0x0000FF00)<<8)+((x&0x00FF0000)>>8)+((x&0xFF000000)>>24));
}

MD2's are stored in little-endian, Wii is big-endian. So for every value bigger than 1 byte you must swap the bytes.
Re: MD2 Model Binary File Reading
March 18, 2010 06:16PM
Ok! I understand.
I was not a specialist of endian computing so i could search a long time !

For the newbees like me :

844121161 = int4_swap(1229213746)
8 = int4_swap(134217728)

The problem is fully solved.
Thank you.
Sorry, only registered users may post in this forum.

Click here to login