MS3D MilkShape Format - Pieces come together
Author bkenwright@xbdev.net
Now its a pretty well
known fact that the .ms3d file format is documented, and you can do a google
search and find the specs for it, which is great. The only problem,
which I usually find, is going from theory to practice. As you'll see,
the little things like, data alignment and things are never what you expect at
time. But of course I'll show you what to look out for and how to get to
the goal!...which is our 3d data.
First thing, is to get
the ms3d.h file, which contains the basic data layout of the file. Its
relatively simple to follow through and gives you most of the information you
need. The biggest thing I hit when I first started reading in the
information, is the data alignment! As the structure below for the
header, it was telling me this was of size 16!!!!
typedef
struct
{
char
id[10]; //
always "MS3D000000"
int
version; // 3
}
ms3d_header_t; |
But of course this is wrong!...and leads to some messy problems as we
progress along and read in the rest of the data!...I found this out, as I was
getting 1 for my number of vertices!...which made me thing, something was
terribly wrong. So of course I did some extra checking, which If found the
problem....the compiler is padding the structure to 16byte alignment!!...... the
fix is to force our compiler to pack the data to 1 byte alignment.
# pragma
pack (1)
typedef
struct
{
char
id[10]; //
always "MS3D000000"
int
version; // 3
}
ms3d_header_t;
# pragma
pack () |
So now that I had that sorted....we can continue with the code...
This next step of the code isn't a big step, but we have included the ms3d
header for the structures, and are starting to read in all the data in the
correct order and put it into structures which we can use.
Source Code - Download |
/**********************************************************************************/
/* */
/* File: main.cpp */
/* Author: bkenwright@xbdev.net */
/* Web: www.xbdev.net */
/* Date: 28-08-2006 */
/* */
/**********************************************************************************/
/*
Introduction to the milkshape ms3d file format
*/
/**********************************************************************************/
#include <stdio.h> // fopen(..), sprintf(..)
#include <stdarg.h> // va_start, va_end
#include "ms3d.h" // The definition of the layout of the ms3d file format, and
// some structures to show how its layed out!
//Saving debug information to a log file
void dprintf(const char *fmt, ...)
{
va_list parms;
char buf[256];
// Try to print in the allocated space.
va_start(parms, fmt);
vsprintf (buf, fmt, parms);
va_end(parms);
// Write the information out to a txt file
FILE *fp = fopen("output.txt", "a+");
fprintf(fp, "%s", buf);
fclose(fp);
}// End dprintf(..)
/**********************************************************************************/
/* */
/* main() - Program Entry Point! Where it all starts... */
/* */
/**********************************************************************************/
void main()
{
dprintf("Welcome to the ms3d tutorial...\n");
// Lets open the test ms3d file we are using, called box.ms3d
FILE * fp = fopen("box.ms3d", "rb");
// Temp buffer to store our data in, as we read it in
char buf[256];
// Lets read in the head information
ms3d_header_t head;
int r = // The total number of items readed is returned.
fread ( &head, // void * buffer,
sizeof(head), // size
1, // count,
fp ); // FILE * stream
// Tricky bug!...check that our data isn't being padded!!
dprintf("sizeof(ms3d_header_t) == %d\n", sizeof(ms3d_header_t));
// Log them to our debug txt file
dprintf("ID: %c%c%c%c\n",
head.id[0], // 'M'
head.id[1], // 'S'
head.id[2], // '3'
head.id[3]);// 'D'
dprintf("Version: 0x%2X\n", head.version);
// Read in the number of verts
unsigned short nNumVertices;
fread ( &nNumVertices,
sizeof(unsigned short),
1,
fp );
// Lets print out the number of vertices in this demo.
dprintf("Num Vertices: %d\n", nNumVertices);
// Close our File!
fclose(fp);
// Exit
dprintf("Goodbye\n");
}
/* output.txt:
Welcome to the ms3d tutorial...
sizeof(ms3d_header_t) == 14
ID: MS3D
Version: 0x 4
Num Vertices: 8
Goodbye
*/
|
|