Advertisement
Ember

nif0

Mar 6th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. // Load from NIF
  2. bool Mesh::CreateFromNIF(String filename)
  3. {
  4.     struct NIF
  5.     {
  6.         struct ShortString
  7.         {
  8.             UByte Length;
  9.             String Text;
  10.         };
  11.  
  12.         struct SizedString
  13.         {
  14.             UInt Length;
  15.             String Text;
  16.         };
  17.  
  18.         struct Header
  19.         {
  20.             // Header
  21.             String Name;
  22.             UByte4 Version;
  23.             Byte Endianess;
  24.             UInt UserVersion;
  25.             UInt NumBlocks;
  26.             UInt UserVersion2;
  27.             // Export information
  28.             ShortString Creator;
  29.             ShortString Info1;
  30.             ShortString Info2;
  31.             // Header again
  32.             UShort NumBlockTypes;
  33.             // Types of Blocks
  34.             Array<SizedString> Types; // [NumBlockTypes];
  35.             // Block Type ID linking thingy, idk
  36.             Array<UShort> BlockTypeIndex; // [NumBlocks];
  37.             // omg why
  38.             Array<UInt> BlockSize; // [NumBlocks];
  39.             // finally
  40.             UInt NumStrings;
  41.             UInt MaxStringLength;
  42.             // or not, you're killing me
  43.             Array<SizedString> Strings; // [NumStrings];
  44.             //  I give up
  45.             UInt Unknown;
  46.         };
  47.  
  48.         struct Node
  49.         {
  50.             Int Name; // String Reference
  51.             UInt NumExtraData;
  52.             //UInt ExtraData[NumExtraData];
  53.             Int Controller; // Object Reference
  54.             UShort Flags;
  55.             UShort Unknown;
  56.             Float3 Translation;
  57.             Float3x3 Rotation;
  58.             Float Scale;
  59.             Int CollisionObject; // Object Reference
  60.             UInt NumChildren;
  61.             //Int Children[NumChildren];
  62.             UInt NumEffects;
  63.             //Int Effects[NumEffects];
  64.         };
  65.  
  66.         struct DecalPlacement
  67.         {
  68.             Int Name; // String Reference
  69.             UShort NumArrays;
  70.             // Arrays
  71.             UShort NumVectors;
  72.             // Array
  73.             //Float3 Points[NumVectors];
  74.             //Float3 Normals[NumVectors];
  75.         };
  76.     };
  77.  
  78.     // Temporary stuffs
  79.     File meshFile;
  80.  
  81.     // Open the mesh file
  82.     ZeroMemory(&meshFile, sizeof(File));
  83.     meshFile.Open(filename, File::Mode::Read);
  84.  
  85.     Log::Singleton->Write("Importing 3DS mesh: " + filename);
  86.  
  87.     // Begin importing the mesh
  88.  
  89.     NIF::Header header;
  90.     // Load the header
  91.     // Begin reading the string located at the beginning of the file
  92.     Byte letter;
  93.     header.Name = "";
  94.     while((letter = meshFile.Read<Byte>()) != 0x0A) { header.Name += letter; };
  95.     // Now read each data element individually because I can't be arsed to make this more efficient
  96.     header.Version = meshFile.Read<UByte4>();
  97.     header.Endianess = meshFile.Read<Byte>();
  98.     header.UserVersion = meshFile.Read<UInt>();
  99.     header.NumBlocks = meshFile.Read<UInt>();
  100.     header.UserVersion2 = meshFile.Read<UInt>();
  101.     // Export information
  102.     header.Creator.Length = meshFile.Read<UByte>();
  103.     header.Creator.Text += meshFile.Read<char>(header.Creator.Length);
  104.     header.Info1.Length = meshFile.Read<UByte>();
  105.     header.Info1.Text += meshFile.Read<char>(header.Info1.Length);
  106.     header.Info2.Length = meshFile.Read<UByte>();
  107.     header.Info2.Text += meshFile.Read<char>(header.Info2.Length);
  108.     // Header again
  109.     header.NumBlockTypes = meshFile.Read<UShort>();
  110.     // Types of Blocks
  111.     header.Types.resize(header.NumBlockTypes);
  112.     for(Byte i = 0; i < header.NumBlockTypes; ++i)
  113.     {
  114.         header.Types[i].Length = meshFile.Read<UInt>();
  115.         header.Types[i].Text += meshFile.Read<char>(header.Types[i].Length);
  116.     }
  117.     // Block Type ID linking thingy, idk
  118.     header.BlockTypeIndex.resize(header.NumBlocks);
  119.     meshFile.Read<UShort>(header.BlockTypeIndex.data(), header.NumBlocks);
  120.     // omg why
  121.     header.BlockSize.resize(header.NumBlocks);
  122.     meshFile.Read<UInt>(header.BlockSize.data(), header.NumBlocks);
  123.     // finally
  124.     header.NumStrings = meshFile.Read<UInt>();
  125.     header.MaxStringLength = meshFile.Read<UInt>();
  126.     // or not, you're killing me
  127.     header.Strings.resize(header.NumStrings);
  128.     for(Byte i = 0; i < header.NumStrings; ++i)
  129.     {
  130.         header.Strings[i].Length = meshFile.Read<UInt>();
  131.         header.Strings[i].Text += meshFile.Read<char>(header.Strings[i].Length);
  132.     }
  133.     //  I give up
  134.     header.Unknown = meshFile.Read<UInt>();
  135.  
  136.     meshFile.Close();
  137.  
  138.     return false;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement