/ source / tools / src / kmd2tool.cpp
kmd2tool.cpp
 1  
 2  #include "compat.h"
 3  
 4  typedef struct { float x, y, z; } point3d;
 5  
 6  typedef struct
 7  {  int id, vers, skinxsiz, skinysiz, framebytes; // id:"IPD2", vers:8
 8      int numskins, numverts, numuv, numtris, numglcmds, numframes;
 9      int ofsskins, ofsuv, ofstris, ofsframes, ofsglcmds, ofseof; // ofsskins: skin names (64 bytes each)
10  } md2typ;
11  
12  typedef struct { point3d mul, add; } frametyp;
13  
14  int main (const int argc, const char **argv)
15  {
16      BFILE *fil;
17      int i, leng;
18      char *fbuf;
19      float zoffset = 0.0f;
20      md2typ *head;
21      frametyp *fptr;
22  
23      if (argc != 4)
24      {
25          Bputs("KMD2TOOL <MD2 in file> <MD2 out file> <z offset>              by Ken Silverman");
26          return(1);
27      }
28      if (!Bstrcasecmp(argv[1],argv[2]))
29      {
30          Bputs("Error: input and output filenames cannot be the same");
31          return(2);
32      }
33  
34      zoffset = Batof(argv[3]);
35      if (0.0f == zoffset)
36      {
37          Bputs("Error: offset of zero");
38          return(3);
39      }
40  
41      fil = Bfopen(argv[1],"rb");
42      if (!fil)
43      {
44          Bputs("Error: could not open input MD2");
45          return(4);
46      }
47  
48      Bfseek(fil, 0, SEEK_END);
49      leng = Bftell(fil);
50      Bfseek(fil, 0, SEEK_SET);
51  
52      fbuf = (char *)Bmalloc(leng * sizeof(char));
53      if (!fbuf)
54      {
55          Bputs("Error: Could not allocate buffer");
56          return(5);
57      }
58  
59      Bfread(fbuf,leng,1,fil);
60      Bfclose(fil);
61  
62      head = (md2typ *)fbuf;
63      if ((head->id != 0x32504449) && (head->vers != 8)) // "IDP2"
64      {
65          Bfree(fbuf);
66          Bputs("Error: input is not an MD2 file");
67          return(6);
68      }
69  
70      for(i=0; i<head->numframes; ++i)
71      {
72          fptr = (frametyp *)&fbuf[head->ofsframes+head->framebytes*i];
73          Bprintf("frame %2d scale:%f,%f,%f offs:%f,%f,%f\n",i,fptr->mul.x,fptr->mul.y,fptr->mul.z,fptr->add.x,fptr->add.y,fptr->add.z);
74          fptr->add.z += zoffset;
75      }
76  
77      fil = Bfopen(argv[2],"wb");
78      if (!fil)
79      {
80          Bputs("Error: could not open output file for writing");
81          return(7);
82      }
83      Bfwrite(fbuf,leng,1,fil);
84      Bfclose(fil);
85  
86      Bfree(fbuf);
87  
88      return(0);
89  }