PDA

View Full Version : Loading a 3DS file



Alload
03-26-2001, 07:51 AM
I coded a program to load 3ds file and to convert the object inside it into OpenGL code.

But it doesn't work, it doesn't load the data correctly.

Here's the code (the name of the file isn't good, I change it when I try to load a real file):


#include <windows.h>
#include <stdio.h>

///////////////////////////////////////////////////////////////////

//Mesh
char objectname [256];
WORD nvertices;
WORD nfaces;
float listvertices;
float listfaces;
float 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 ("file", "r");
WORD id;
int done = 0;

while (done != 1)
{
if (feof (bin3ds))
{
done = 1;
break;
}

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 ("file", "r");
WORD id;
int done = 0;

while (done != 1)
{
if (feof (bin3ds))
{
done = 1;
break;
}

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 ("file", "r");
WORD id;
int done = 0;

while (done != 1)
{
if (feof (bin3ds))
{
done = 1;
break;
}

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;
}

///////////////////////////////////////////////////////////////////