Bon, voilà le code que j'ai écrit pour charger un objet se trouvant dans un fichier 3DS.
#include <windows.h>
#include <stdio.h>
///////////////////////////////////////////////////////////////////
//Mesh
char objectname [256];
WORD nvertices;
WORD nfaces;
double listvertices;
double listfaces;
double listuvcoord;
//Material
char materialname [256];
double ambient;
double diffuse;
double specular;
//Mapping
char texturename [256];
float uscale; float vscale;
double listuvscale [2] = {uscale, vscale};
float uoffset; float voffset;
double listuvoffset [2] = {uoffset, voffset};
float rotationangle;
///////////////////////////////////////////////////////////////////
FILE *bin3ds;
///////////////////////////////////////////////////////////////////
unsigned char ReadChar ()
{
return (fgetc (bin3ds));
}
unsigned int ReadInt ()
{
unsigned int temp = ReadChar ();
return (temp | (ReadChar () << 8));
}
///////////////////////////////////////////////////////////////////
int read_material (void)
{
bin3ds = fopen ("box.3ds", "r" );
WORD id;
int done = 0;
while (done != 1)
{
if (feof (bin3ds))
{
done = 1;
}
id = ReadInt ();
switch (id)
{
case 0xA000: //Material Name
{
int i = 0;
char ch;
while ((ch = fgetc (bin3ds)) != 0)
{
materialname [i] = ch;
i++;
}
materialname [i] = '\0';
}
break;
case 0xA010: //Ambient Color
{
short red;
short green;
short blue;
double redambient;
double greenambient;
double blueambient;
fread (&red, sizeof (byte), 1, bin3ds);
fread (&green, sizeof (byte), 1, bin3ds);
fread (&blue, sizeof (byte), 1, bin3ds);
redambient = (1/255)*red;
greenambient = (1/255)*green;
blueambient = (1/255)*blue;
ambient = (redambient, greenambient, blueambient);
}
break;
case 0xA020: //Diffuse Color
{
short red;
short green;
short blue;
double reddiffuse;
double greendiffuse;
double bluediffuse;
fread (&red, sizeof (byte), 1, bin3ds);
fread (&green, sizeof (byte), 1, bin3ds);
fread (&blue, sizeof (byte), 1, bin3ds);
diffuse = (reddiffuse, greendiffuse, bluediffuse);
}
break;
case 0xA030: //Specular Color
{
short red;
short green;
short blue;
double redspecular;
double greenspecular;
double bluespecular;
fread (&red, sizeof (byte), 1, bin3ds);
fread (&green, sizeof (byte), 1, bin3ds);
fread (&blue, sizeof (byte), 1, bin3ds);
specular = (redspecular, greenspecular, bluespecular);
}
break;
}
}
fclose (bin3ds);
return 0;
}
int read_mapping (void)
{
bin3ds = fopen ("box.3ds", "r" );
WORD id;
int done = 0;
while (done != 1)
{
if (feof (bin3ds))
{
done = 1;
}
id = ReadInt ();
switch (id)
{
case 0xA300: //Mapping Filename
{
int i = 0;
char ch;
while ((ch = fgetc (bin3ds)) != 0)
{
texturename [i] = ch;
i++;
}
texturename [i] = '\0';
}
break;
case 0xA354: //V Scale
{
fread (&vscale, sizeof (float), 1, bin3ds);
}
case 0xA356: //U Scale
{
fread (&uscale, sizeof (float), 1, bin3ds);
}
case 0xA358: //U Offset
{
fread (&uoffset, sizeof (float), 1, bin3ds);
}
case 0xA35A: //V Offset
{
fread (&voffset, sizeof (float), 1, bin3ds);
}
case 0xA35C: //Rotation Angle
{
fread (&rotationangle, sizeof (float), 1, bin3ds);
}
}
}
fclose (bin3ds);
return 0;
}
int read_mesh (void)
{
bin3ds = fopen ("box.3ds", "r" );
WORD id;
int done = 0;
while (done != 1)
{
if (feof (bin3ds))
{
done = 1;
}
id = ReadInt ();
switch (id)
{
case 0x4000: //Object Name
{
int i = 0;
char ch;
while ((ch = fgetc (bin3ds)) != 0)
{
objectname [i] = ch;
i++;
}
objectname [i] = '\0';
}
case 0x4110: //Vertices List
{
fread (&nvertices, sizeof (WORD), 1, bin3ds);
fread (&listvertices, sizeof (float), nvertices, bin3ds);
}
case 0x4140: //U And V Coordinates
{
fread (&listuvcoord, sizeof (float), (nvertices*2), bin3ds);
}
}
}
fclose (bin3ds);
return 0;
}
///////////////////////////////////////////////////////////////////
int main (void)
{
printf ("%s", listvertices);
while (1)
{
}
return 0;
}
La fonction MAIN est juste là pour tester mon programme, par exemple dans ce cas le programme devrait afficher les coordonnées de tout les sommets de l'objet. Mais en réalité il affiche: <null>.
Je pense que le programme ne cherche pas correctement dans le fichier, mais je ne sais pas comment corriger l'erreur. Si vous pouviez m'aider, ce serait super.
Thanks.