Guest User

Untitled

a guest
Jul 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<fstream>
  4. #include<vector>
  5. #include<string>
  6. using namespace std;
  7.  
  8.  
  9. struct Header {
  10.     //Structure for header on skl files
  11.     char fileType[8];
  12.     int numObjects;
  13.     int skeletonHash;
  14.     int numElements;
  15. };
  16.  
  17. struct Bone {
  18.     //Structure for a bone in skl files
  19.     char name[32];
  20.     int parent;
  21.     float scale;
  22.     float matrix[3][4];
  23. };
  24.  
  25. struct SkinModelHeader {
  26.     //Structure for the Header in skn files
  27.     int magic;
  28.     short numMaterials;
  29.     short numObjects;
  30. };
  31. struct SkinModelMaterial {
  32.     //Structure for a material block in skn files
  33.     int matIndex;
  34.     char name[64];
  35.     int startVertex;
  36.     int numVertices;
  37.     int startIndex;
  38.     int numIndices;
  39. };
  40. struct SkinModelVertex {
  41.     //Vertex block in skn files
  42.     float position[3];
  43.     char  boneIndex[4];
  44.     float weights[4];
  45.     float normal[3];
  46.     float texcoords[2];
  47. };
  48. struct SkinModelData {
  49.     //data block in skn files
  50.     int numIndices;
  51.     int numVertices;
  52.     vector<short> indices;
  53.     vector<SkinModelVertex> verteces;
  54. };
  55.  
  56. struct SkinModel {
  57.     //Skin model data structure
  58.     SkinModelHeader header;
  59.     vector<SkinModelMaterial> materials;
  60.     SkinModelData modelData;
  61.  
  62.     void importSkn(string infile) {
  63.         //import from .skn file
  64.         ifstream fin;
  65.         fin.open(infile.c_str(), ios::binary);
  66.         if (fin.fail()) {
  67.             cout << "ERROR: could not open " << infile << endl;
  68.             exit(1);
  69.         }
  70.         fin.read((char*)(void*)&header,sizeof(SkinModelHeader));
  71.         if (header.magic != 1122867) {
  72.             cout << "ERROR: " << infile << " is not a valid skn file" << endl;
  73.             exit(1);
  74.         }
  75.         //for (int i = 0; i < header.numMaterials; i++) {
  76.         if (header.numMaterials) {
  77.             SkinModelMaterial mat;
  78.             fin.read((char*)(void*)&mat, sizeof(SkinModelMaterial));
  79.             materials.push_back(mat);
  80.         }
  81.        
  82.         fin.read((char*)(void*)&modelData.numIndices,4);
  83.         fin.read((char*)(void*)&modelData.numVertices,4);
  84.        
  85.         for (int i = 0; i < modelData.numIndices; i++) {
  86.             short idx;
  87.             fin.read((char*)(void*)&idx, 2);
  88.             modelData.indices.push_back(idx);
  89.         }
  90.         for (int i = 0; i < modelData.numVertices; i++) {
  91.             SkinModelVertex vtx;
  92.             fin.read((char*)(void*)&vtx,sizeof(SkinModelVertex));
  93.             modelData.verteces.push_back(vtx);
  94.         }
  95.     }
  96. };
  97. struct AnimationHeader {
  98.     char filetype[8];
  99.     int three;
  100.     int magic;
  101.     int numBones;
  102.     int numFrames;
  103.     int fps;
  104. };
  105. struct AnimationFrame {
  106.     float quat[4];
  107.     float position[3];
  108. };
  109. struct AnimationBone {
  110.     char boneName[32];
  111.     int boneID;
  112. };
  113. void quat2mat(const float* quat, float mat[3][4]) {
  114.     float X = quat[0];
  115.     float Y = quat[1];
  116.     float Z = quat[2];
  117.     float W = quat[3];
  118.  
  119.     float xx      = X * X;
  120.     float xy      = X * Y;
  121.     float xz      = X * Z;
  122.     float xw      = X * W;
  123.     float yy      = Y * Y;
  124.     float yz      = Y * Z;
  125.     float yw      = Y * W;
  126.     float zz      = Z * Z;
  127.     float zw      = Z * W;
  128.  
  129.     mat[0][0] = 1 - 2 * ( yy + zz );
  130.     mat[0][1] =     2 * ( xy - zw );
  131.     mat[0][2] =     2 * ( xz + yw );
  132.  
  133.     mat[1][0] =     2 * ( xy + zw );
  134.     mat[1][1] = 1 - 2 * ( xx + zz );
  135.     mat[1][2] =     2 * ( yz - xw );
  136.  
  137.     mat[2][0] =     2 * ( xz - yw );
  138.     mat[2][1] =     2 * ( yz + xw );
  139.     mat[2][2] = 1 - 2 * ( xx + yy );
  140.  
  141.     mat[0][3]  = mat[1][3] = mat[2][3] = 0;
  142. }
  143. struct IniBinHeader {
  144.     int one;
  145.     int numEntries;
  146.     int stringsSize;
  147. };
  148. struct IniBinEntry {
  149.     unsigned int varID;
  150.     int offset;
  151. };
  152.  
  153. int main(int argc, char *argv[])
  154. {
  155.     if(argc<5) exit(5);
  156.     string cmd = string(argv[1]);
  157.     string type = string(argv[2]);
  158.     string inputFile = string(argv[3]);
  159.     string outputFile = string(argv[4]);
  160.  
  161.      if (cmd == "decompile" || cmd == "d") {
  162.         if (type == "skn" || type == "skin") {
  163.             cout << "Decompiling binary .skn file: " << inputFile << " to .obj file: " << outputFile << endl;
  164.             SkinModel model;
  165.             model.importSkn(inputFile);
  166.             model.exportObj(outputFile);
  167.             cout << "Done!" << endl;
  168. }
  169.        
  170.     return 0;
  171. }
Add Comment
Please, Sign In to add comment