bool C3DModel::Load(LPCSTR Filename)
{
int i;
FILE * l_file;
unsigned short l_chunk_id;
unsigned int l_chunk_length;
unsigned short l_qty;
unsigned short l_face_flags;
ModelMesh = new CMesh;
if ((l_file=fopen (Filename, "rb"))== NULL)
return false;
while (ftell (l_file) < filelength (fileno (l_file))) //Loop to scan the whole file
{
fread (&l_chunk_id, 2, 1, l_file); //Read the chunk header
fread (&l_chunk_length, 4, 1, l_file); //Read the length of the chunk
switch (l_chunk_id)
{
case 0x4d4d:
break;
case 0x3d3d:
break;
case 0x4000:
break;
case 0x4100:
break;
case 0x4110:
fread (&l_qty, sizeof (unsigned short), 1, l_file);
ModelMesh->i_VertexCount = l_qty;
ModelMesh->Vertices = new Vertex[l_qty];
for (i = 0; i < l_qty; i++)
{
fread (&ModelMesh->Vertices.x, sizeof(float), 1, l_file);
fread (&ModelMesh->Vertices.y, sizeof(float), 1, l_file);
fread (&ModelMesh->Vertices.z, sizeof(float), 1, l_file);
}
break;
case 0x4120:
fread (&l_qty, sizeof (unsigned short), 1, l_file);
ModelMesh->i_PolygonCount = l_qty;
ModelMesh->Polygons = new Vector4[l_qty];
for (i = 0; i < l_qty; i++)
{
fread (&ModelMesh->Polygons.a, sizeof (unsigned short), 1, l_file);
fread (&ModelMesh->Polygons.b, sizeof (unsigned short), 1, l_file);
fread (&ModelMesh->Polygons.c, sizeof (unsigned short), 1, l_file);
fread (&l_face_flags, sizeof (unsigned short), 1, l_file);
}
break;
case 0x4140:
fread (&l_qty, sizeof (unsigned short), 1, l_file);
for (i = 0; i < l_qty; i++)
{
fread (&ModelMesh->Vertices.u, sizeof (float), 1, l_file);
fread (&ModelMesh->Vertices.v, sizeof (float), 1, l_file);
}
break;
default:
fseek(l_file, l_chunk_length-6, SEEK_CUR);
}
}
fclose (l_file);
return true;
}
3DS trouble
Hi all,
I am having some difficulties reading in a 3ds file (see code below). Though the program goes through the entire while loop about 8 times (should be right) the vertex count and polygon count stay at 0. So it's just not reading em. I'm sure the 3ds file is valid (tried 2, 1 made by me, 1 from the internet).
I found that the while loop is run OK, but the code never goes into the case-statements handling the vertices and polygons...
What am I doing wrong here?
Or could someone point me to a free class / article on how to write a loader thats better than this one?
Currently I am limited to 1 object in a 3ds file. Dont even know how to load the material data yet. And the Vertex en Polygon data isnt loading as it should (hence the first post)
I am trying to create a class to load the data object orientated.
In the end I just want to say
C3DModel test = new C3DModel("somemodel.3ds");
...
test->Render(); or something. Just not sure how to go about this..
Currently I am limited to 1 object in a 3ds file. Dont even know how to load the material data yet. And the Vertex en Polygon data isnt loading as it should (hence the first post)
I am trying to create a class to load the data object orientated.
In the end I just want to say
C3DModel test = new C3DModel("somemodel.3ds");
...
test->Render(); or something. Just not sure how to go about this..
You could try putting a breakpoint at the switch() line and checking that chunk_id is being read correctly. If it wasnt that would explain the problems you are having. I dont know how to load a 3DS file so sorry I can't be more help.
Luke.
Luke.
Member of the NeHe team.
I got it working. Though by using a different tutorial (with more options in there, see 2nd post)
Only still dont have colors working, but bitmap textures work. Maybe the 3d editor stores the material colors wrong or something (Cinema 4D).
Tnx anyway :D
Only still dont have colors working, but bitmap textures work. Maybe the 3d editor stores the material colors wrong or something (Cinema 4D).
Tnx anyway :D
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement