Mdlcpp.doc
// This is a example routine
for reading mdl-file format
// Henry Väppling 1998
//The ReadMdl function open a file for reading and send
//buf[bytes] to CheckBuffer() who make a check if i is
//anything useful in the first bytes of the buf[]. When trace
//of object is found, the mdl.. function extract information,
//advance the filepointer fpoffset and return.
//These function check if objects are visible and jump if
//they are marked invisible in the function that call ReadMdl.
//If this sort of function is intended to read and store mdl
//data for later use, then the visibility check should be done
//in a later function, when drawing or something like
//that.
void __far ReadMdl(CString mdlfile);
void CheckBuffer();
void __far mdl2a00(); // Polygon colored
void __far mdl1a000100(); // PointArray
void mdl22001a(); // New part - always visible
void mdl220000(); // Detecting EndOfFile
void mdl220022(); // Detecting EOF
void __far mdl220024(); // New part - spec view effects
void __far mdl220039(); // SetViewEffects
void __far mdl22002900(); // Sttructure + pointarray
void __far mdl2900(); // Pointarray
void __far mdl38003e(); // Polygon - part
void __far mdl4800ff(); // Picture
void mdl5000xxf0(); // Color - strukture 2a
void mdl5100xxf0(); // Color - line
void mdl5200xxf0(); // Color - part 38
void __far mdl7a00xx00(); // Polygon texturemapped
float kx[100], ky[100], kz[100];
int po[100];
int newpart;
unsigned char buf[2048];
FILE *outf;
long fpoffset;
int objnr;
CString mdlfile;
int Facelist[100];
int EndOF = 0;
int mFaces;
int mPoints;
int Flapvis, Gearvis, Propvis;
int Visible;
int Color50, Color51, Color52;
void __far ReadMdl(CString mdlfile)
{
FILE *fp;
int readsize;
fpoffset=5000; //start reading at byte 5000
objnr=0;
if((fp=fopen( mdlfile, "rb"))==NULL) { //mdlfile
AfxMessageBox("No input file selected",MB_OK | MB_ICONSTOP,
0);
}
outf=fopen("test.raw","w");
do {
if(fseek(fp,fpoffset, SEEK_SET)) {
}
readsize=fread(buf,1, 2048,fp);
CheckBuffer();
fpoffset++ ;
}while(readsize>0 && EndOF == 0 && fpoffset<72000
);
//files in FS98 can be bigger than 72000
fclose(fp);
fclose(outf);
}
void CheckBuffer()
{
if( buf[0]== 0x1a && buf[1]== 0x0 && buf[2]==
0x1 && buf[3]== 0x0)
mdl1a000100();
else if( buf[0]== 0x22 && buf[1]== 0x0 && buf[2]==
0x0 && buf[3]==0x0)
mdl220000();
else if( buf[0]== 0x22 && buf[1]== 0x0 && buf[2]==
0x1a)
mdl22001a();
else if( buf[0]== 0x22 && buf[1]== 0x0 && buf[2]==
0x22)
mdl220022();
else if( buf[0]== 0x22 && buf[1]== 0x0 && buf[2]==
0x24)
mdl220024();
else if( buf[0]== 0x22 && buf[1]== 0x0 && buf[2]==
0x29)
mdl220029();
else if( buf[0]== 0x22 && buf[1]== 0x0 && buf[2]
== 0x39)
mdl220039();
else if( buf[0]== 0x29 && buf[1]== 0x0 && buf[2]==0x1)
mdl2900();
else if( buf[0]== 0x2a && buf[1]== 0x0 && buf[3]==0x0)
mdl2a00();
else if( buf[0]== 0x38 && buf[1]== 0x0 && buf[2]==
0x3e)
mdl38003e(); //
else if( buf[0]== 0x48 && buf[1]== 0x0 && buf[2]==
0xff)
mdl4800ff();
else if( buf[0]== 0x50 && buf[1]== 0x0 && buf[3]==
0xf0)
mdl5000xxf0();
else if( buf[0]== 0x51 && buf[1]== 0x0 && buf[3]==
0xf0)
mdl5100xxf0();
else if( buf[0]== 0x52 && buf[1]== 0x0 && buf[3]==
0xf0)
mdl5200xxf0();
else if( buf[0]== 0x7a && buf[1]== 0x0)
mdl7a00xx00();
}
void __far mdl22001a()
// New part
{
newpart=1;
objnr++;
fpoffset++;
}
void mdl220000()
{
//This is the end of the file
EndOF = 1;
}
void __far mdl220022()
{
//This is the end of file
EndOF = 1;
}
void __far mdl220024()
{
// New Part - with some special view effect
newpart=1;
objnr++;
if(buf[6]==0x5a && Propvis==0) Visible=0;
else if(buf[6]==0x54 && Gearvis==0) Visible=0;
else if(buf[6]==0x52 && Flapvis==0) Visible=0;
else Visible=1;
fpoffset+=10;
}
void __far mdl22002900()
{
newpart=1;
objnr++;
}
void __far mdl220039()
//New structure with special view effects
{
if(buf[6]==0x5a && Propvis==0) Visible=0;
else if(buf[6]==0x54 && Gearvis==0) Visible=0;
else if(buf[6]==0x52 && Flapvis==0) Visible=0;
else Visible=1;
}
void __far mdl2a00()
{
int n;
n=14+buf[2]*2;
if(Visible==1)
{
if(newpart==1){
newpart=0;
}
if(buf[2]==3)
{
mFaces++;
for( int c=14,pntn=0;c<n;c+=2,pntn++)
{
int pn=buf[c];
}
}
if (buf[2]>3 ){
int buf2=buf[2];
mFaces++;
for( int c=14,pntn=0;c<n;c+=2,pntn++)
{
int pn=buf[c];
po[pntn]=buf[c];
}
//example using pointcord when printing triangles. This is
//good for the raw or pov-format, change it if using fileformat
//that accept polygons with 4 or more corner.
//the first triangle
//fprintf(outf,"%f %f %f ", kx[po[0]], ky[po[0]], kz[po[0]]);
//fprintf(outf,"%f %f %f ", kx[po[1]], ky[po[1]], kz[po[1]]);
//fprintf(outf,"%f %f %f\n ", kx[po[2]], ky[po[2]],
kz[po[2]]);
//second triangle
for(int i=2 ;i<buf2; i++ )
{
//fprintf(outf,"%f %f %f ", kx[po[0]], ky[po[0]], kz[po[0]]);
//fprintf(outf,"%f %f %f ", kx[po[i-1]], ky[po[i-1]],
kz[po[i-1]]);
//fprintf(outf,"%f %f %f\n ", kx[po[i]], ky[po[i]],
kz[po[i]]);
}
}
}
//check for end of object
if( buf[n] == 0x22 && buf[n+1] == 0x0) {
Visible=1;
// do something with the object
//clean up
mPoints=mFaces=0;
minx=miny=minz=maxx=maxy=maxz=0;
}
fpoffset+=n-1;
}
void __far mdl38003e()
{
int n;
n=16+buf[4]*2;
if(Visible==1)
{
if(newpart==1){
newpart=0;
}
if(buf[4]==3)
{
mFaces++;
for( int c=16,pntn=0;c<n;c+=2,pntn++)
{
int pn=buf[c]
}
}
if(buf[4]>3){
mFaces++;
int buf2=buf[4];
for( int c=16,pntn=0;c<n;c+=2,pntn++)
{
int pn=buf[c];
po[pntn]=buf[c];
}
//first triangle
//fprintf(outf,"%f %f %f ", kx[po[0]], ky[po[0]], kz[po[0]]);
//fprintf(outf,"%f %f %f ", kx[po[1]], ky[po[1]], kz[po[1]]);
//fprintf(outf,"%f %f %f\n ", kx[po[2]], ky[po[2]],
kz[po[2]]);
for(int i=3;i<buf2;i++)
{
//fprintf(outf,"%f %f %f ", kx[po[0]], ky[po[0]], kz[po[0]]);
//fprintf(outf,"%f %f %f ", kx[po[i-1]], ky[po[i-1]],
kz[po[i-1]]);
//fprintf(outf,"%f %f %f\n ", kx[po[i]], ky[po[i]],
kz[po[i]]);
}
}
}
if( buf[n] == 0x22 && buf[n+1] == 0x0)
//check for end of object
{
Visible=1;
// Use the collected data
mPoints=mFaces=0;
minx=miny=minz=maxx=maxy=maxz=0;
}
fpoffset+=n-1;
}
void __far mdl4800ff()
{
Cstring bildfil;
for(int c=14; c<=25 ;c++){
bildfil+=buf[c];
}
}
void __far mdl2900()
{
int n =buf[4];
mPoints=n;
for(int c=6,i=0; i<n ;i++, c+=12)
{
float p1,p2,p3,p4,p5,p6;
p1=buf[c];
p2=buf[c+1];
p3=buf[c+2];
p4=buf[c+3];
p5=buf[c+4];
p6=buf[c+5];
if(p2>128) p2= p2-256;
if(p4>128) p4= p4-256;
if(p6>128) p6= p6-256;
kx[i+1]= (p1 + p2*256);
ky[i+1]= (p3 + p4*256);
kz[i+1]= (p5 + p6*256);
//vectors, not used in this code
p1=buf[c+6];
p2=buf[c+7];
p3=buf[c+8];
p4=buf[c+9];
p5=buf[c+10];
p6=buf[c+11];
if(p2>128) p2= p2-256;
if(p4>128) p4= p4-256;
if(p6>128) p6= p6-256;
}
fpoffset+= (5+ n*12);
}
void __far mdl1a000100()
{
int n =buf[4];
mPoints=n;
for(int c=6,i=0; i<n ;i++, c+=6)
{
float p1,p2,p3,p4,p5,p6;
p1=buf[c];
p2=buf[c+1];
p3=buf[c+2];
p4=buf[c+3];
p5=buf[c+4];
p6=buf[c+5];
if(p2>128) p2= p2-256;
if(p4>128) p4= p4-256;
if(p6>128) p6= p6-256;
kx[i+1]= (p1 + p2*256);
ky[i+1]= (p3 + p4*256);
kz[i+1]= (p5 + p6*256);
}
fpoffset+=6+n*6-1;
}
void mdl5000xxf0()
{
Color50= buf[2];
fpoffset+=3;
}
void mdl5100xxf0()
{
Color51= buf[2];
fpoffset+=3;
}
void mdl5200xxf0()
{
Color52= buf[2];
fpoffset+=3;
}
void __far mdl7a00xx00()
{
//This function is identical to the mdl2a()
int n;
n=13+buf[2]*6;
fpoffset+=n-1;
}