Benny ,
dont know if this does what you want ... but I pieced this together from sources I forget now. My idea was to have a minimal 3ds model loader. Anyway you are welcome to code if it helps.
NOTE Beney (you see what I did there?) it was written using GCC so may barf under VC++ for a moment or two ;-)
///////////////////////////////////////////////////////////////////////////////
// Self contained... 3DS CODE
// Its baby code, slow and silly, but it works for now
///////////////////////////////////////////////////////////////////////////////
typedef struct vertex_struct{float x,y,z;} vertex;
typedef struct mapcoord_struct{float u,v;} mapcoord;
typedef struct tri_struct{int a,b,c;} tri;
typedef struct object_struct{
char name[20];
int numVerts,numPolys;
vertex v[20000];
tri p[20000];
mapcoord m[20000];
} object;
static object swirl; //our object
void load3dsObject (object *obj, char *filename)
{
FILE *file; //Our file pointer
char temp; //Temporary char for reading name of object
short chunkID; //Stores ID of current chunk.
int chunkLength;
short useless;
file=fopen (filename, "rb");
int i;
//While current position is lesser than the total length
while (ftell(file) < _filelength (fileno (file)))
{
fread (&chunkID, 2, 1, file);
fread (&chunkLength, 4, 1,file);
switch (chunkID)
{
case 0x4d4d: //Skip these chunks
break;
case 0x3d3d:
break;
case 0x4000: //Chunk containing name
for(i=0;i<20;i++)
{
fread (&temp, 1, 1, file);
obj->name[i]=temp;
if(temp == '\0')break;
}
break;
case 0x3f80: //Skip again
break;
case 0x4100:
break;
case 0x4110: //Chunk with num of vertices
//followed by their coordinates
fread (&obj->numVerts, sizeof (unsigned short), 1, file);
for (i=0; i<obj->numVerts; i++)
{
fread (&obj->v[i].x, sizeof(float), 1, file);
fread (&obj->v[i].y, sizeof(float), 1, file);
fread (&obj->v[i].z, sizeof(float), 1, file);
}
break;
case 0x4120: //Chunk with numPolys and
//the indices
fread (&obj->numPolys, sizeof (unsigned short), 1, file);
for (i=0; i<obj->numPolys; i++)
{
fread (&obj->p[i].a, sizeof (unsigned short), 1, file);
fread (&obj->p[i].b, sizeof (unsigned short), 1, file);
fread (&obj->p[i].c, sizeof (unsigned short), 1, file);
fread (&useless, sizeof (unsigned short), 1, file);
}
break;
case 0x4140: //Chunk with texture coords
fread (&useless, sizeof (unsigned short), 1, file);
for (i=0; i<obj->numVerts; i++)
{
fread (&obj->m[i].u, sizeof (float), 1, file);
fread (&obj->m[i].v, sizeof (float), 1, file);
}
break;
default:
fseek(file,chunkLength-6, SEEK_CUR);
}//switch
}
fclose (file);
}
//OK so draw an object using triangles, dead slow but does allow for some neat
//effects such as explosions and shattering and stuff...
void draw3dsObject(object *obj)
{
int i;
glBegin(GL_TRIANGLES);
for (i=0;i<obj->numPolys;i++)
{
glTexCoord2f( obj->m[ obj->p[i].a ].u, obj->m[ obj->p[i].a ].v);
glVertex3f( obj->v[ obj->p[i].a ].x, obj->v[ obj->p[i].a ].y, obj->v[ obj->p[i].a ].z);
glTexCoord2f( obj->m[ obj->p[i].b ].u, obj->m[ obj->p[i].b ].v);
glVertex3f( obj->v[ obj->p[i].b ].x, obj->v[ obj->p[i].b ].y, obj->v[ obj->p[i].b ].z);
glTexCoord2f( obj->m[ obj->p[i].c ].u, obj->m[ obj->p[i].c ].v);
glVertex3f( obj->v[ obj->p[i].c ].x, obj->v[ obj->p[i].c ].y, obj->v[ obj->p[i].c ].z);
}
glEnd();
}
It works - tested, but no normals. This is a big drawback.
Chris