iMackshun

Model.cpp

Jan 16th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.75 KB | None | 0 0
  1. #include "Model.h"
  2.  
  3. //Outputs a Matrix to the console.
  4. void Model::DebugMatrix(C3D_Mtx matrix, int debugIndex)
  5. {
  6.     if(!outputtedMatrix[debugIndex]){
  7.         printf("Matrix\n");
  8.         printf("[%.3f, %.3f, %.3f, %.3f]\n", matrix.r[0].x, matrix.r[0].y, matrix.r[0].z, matrix.r[0].w);
  9.         printf("[%.3f, %.3f, %.3f, %.3f]\n", matrix.r[1].x, matrix.r[1].y, matrix.r[1].z, matrix.r[1].w);
  10.         printf("[%.3f, %.3f, %.3f, %.3f]\n", matrix.r[2].x, matrix.r[2].y, matrix.r[2].z, matrix.r[2].w);
  11.         printf("[%.3f, %.3f, %.3f, %.3f]\n", matrix.r[3].x, matrix.r[3].y, matrix.r[3].z, matrix.r[3].w);
  12.         outputtedMatrix[debugIndex] = true;
  13.     }
  14. }
  15.  
  16. //Extracts the path from the file location.
  17. void Model::ExtractPath(){
  18.     //Create a copy of the file location.
  19.     std::string fileLocationCopy = _fileLocation;
  20.     std::string tempString = "";
  21.     std::vector<std::string> filePathSections;
  22.     //Remove all of the slashes.
  23.     StringModifier::replaceCharacterWithSpace(&fileLocationCopy, '/');
  24.     //Create a temporary string stream and read from the string.
  25.     std::stringstream stringStream;
  26.     stringStream.str(fileLocationCopy);
  27.     while (stringStream >> tempString){
  28.         filePathSections.push_back(tempString);
  29.     }
  30.     //Append all of the sections to the path string.
  31.     for (unsigned int i = 0; i < filePathSections.size(); i++){
  32.         if (i != filePathSections.size() - 1){
  33.             _filePath += filePathSections.at(i) + "/";
  34.         }
  35.     }
  36.     //Output the file path.
  37.     std::cout << "FilePath: " << _filePath << std::endl;
  38.     //Clear everything.
  39.     filePathSections.clear();
  40.     stringStream.clear();
  41. }
  42.  
  43. //Extracts the base file name from the file location.
  44. void Model::ExtractBaseFileName(){
  45.     //Create a copy of the file location.
  46.     std::string fileLocationCopy = _fileLocation;
  47.     std::string tempString = "";
  48.     std::vector<std::string> filePathSections;
  49.     //Remove all of the slashes and periods.
  50.     StringModifier::replaceCharacterWithSpace(&fileLocationCopy, '/');
  51.     StringModifier::replaceCharacterWithSpace(&fileLocationCopy, '.');
  52.     //Create a temporary string stream and read from the string.
  53.     std::stringstream stringStream;
  54.     stringStream.str(fileLocationCopy);
  55.     while (stringStream >> tempString){
  56.         filePathSections.push_back(tempString);
  57.     }
  58.     //Extract the base file name.
  59.     _baseFileName = filePathSections.at(filePathSections.size() - 2);
  60.     //Output the file path.
  61.     std::cout << "BaseFilename: " << _baseFileName << std::endl;
  62.     //Clear everything.
  63.     filePathSections.clear();
  64.     stringStream.clear();
  65. }
  66.  
  67. //Locates the index of a bone in the vector.
  68. int Model::getBoneIndex(std::string boneName){
  69.     int index = -1;
  70.     for (unsigned int i = 0; i < _bones.size(); i++){
  71.         if (_bones.at(i)._boneName == boneName){
  72.             index = i;
  73.         }
  74.     }
  75.     return index;
  76. }
  77.  
  78. //Debugs data about a vertex.
  79. void Model::DebugVertex(Vertex* vertex){
  80.     std::cout << "Position: (" << vertex->position.x << ", " << vertex->position.y << ", " << vertex->position.z << ", " << vertex->position.w << ")" <<  std::endl;
  81.     std::cout << "Normal: (" << vertex->normals.x << ", " << vertex->normals.y << ", " << vertex->normals.z << ")" << std::endl;
  82.     std::cout << "UV Coordinate: (" << vertex->uvs.x << ", " << vertex->uvs.y << ")" << std::endl;
  83.     std::cout << "Joint IDs: (" << vertex->boneIDs.x << ", " << vertex->boneIDs.y << ", " << vertex->boneIDs.z << ")" << std::endl;
  84.     std::cout << "Joint Weights: (" << vertex->boneWeights.x << ", " << vertex->boneWeights.y << ", " << vertex->boneWeights.z << ")" << std::endl;
  85. }
  86.  
  87. //Recursively generate the Model-Space transforms of all of the bones.
  88. void Model::CalculateModelSpaceTransforms(ArmatureBone* targetBone, C3D_Mtx parentMatrix)
  89. {
  90.     //std::cout << "Calculating Bone Matrix: " << targetBone->_boneName << std::endl;
  91.     //Calculate the Model-Space transform for the current bone.
  92.     if (_animationManager._enabled){
  93.         //Generate the Model-Space Bind Matrix of the bone.
  94.         Mtx_Multiply(&targetBone->_boneMatrix, &parentMatrix, &targetBone->_localAnimatedBoneMatrix);
  95.     }
  96.     else{
  97.         //Generate the model-space matrix of the bone.
  98.         targetBone->_boneMatrix = targetBone->_globalBindTransform;
  99.     }
  100.     //Calculate the Model-Space transforms for all of the children.
  101.     for (unsigned int i = 0; i < targetBone->_childCount; i++){
  102.         CalculateModelSpaceTransforms(&_bones.at(targetBone->_childrenBoneIndices[i]), targetBone->_boneMatrix);
  103.     }
  104. }
  105.  
  106. //Sends the matrices to the GPU.
  107. void Model::WriteMatrices()
  108. {
  109.     //Used for writing to the GPU.
  110.     C3D_FVec* boneMatrixPointer = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, _boneMatrixArrayLocation, 64);
  111.     //Set the matrix within the matrix array.
  112.     for (unsigned int i = 0; i < 16; i++){
  113.         //Set the bone's matrix.
  114.         Mtx_Multiply(&_boneMatrices[i], &_bones.at(i)._boneMatrix, &_bones.at(i)._inverseBindTransform);
  115.         DebugMatrix(_bones.at(i)._boneMatrix, 0);
  116.         DebugMatrix(_bones.at(i)._inverseBindTransform, 1);
  117.         //Push the matrix to the GPU.
  118.         if(boneMatrixPointer){
  119.             boneMatrixPointer[(i * 4)] = _boneMatrices[i].r[0];
  120.             boneMatrixPointer[(i * 4) + 1] = _boneMatrices[i].r[1];
  121.             boneMatrixPointer[(i * 4) + 2] = _boneMatrices[i].r[2];
  122.             boneMatrixPointer[(i * 4) + 3] = _boneMatrices[i].r[3];
  123.         }
  124.     }
  125. }
  126.  
  127. //Create the shape to represent the bones.
  128. void Model::CreateBoneModel(std::string boneType)
  129. {
  130.     if(boneType == "QUAD"){
  131.         //Set the size of the index array.
  132.         indexArraySize = 6;
  133.        
  134.         //Allocate memory for the geometry data.
  135.         boneModelVertexArray = (Vertex*)linearAlloc(sizeof(Vertex) * 4);
  136.         boneModelIndexArray = (unsigned short*)linearAlloc(sizeof(unsigned short) * indexArraySize);
  137.        
  138.         float _width = 0.2f;
  139.         float _height = 0.2f;
  140.        
  141.         //Vertex 1
  142.         //Position
  143.         boneModelVertexArray[0].position.x = 0.0f;
  144.         boneModelVertexArray[0].position.y = _height;
  145.         boneModelVertexArray[0].position.z = 0.0f;
  146.         boneModelVertexArray[0].position.w = 1.0f;
  147.         //Color
  148.         boneModelVertexArray[0].color.r = 0.0f;
  149.         boneModelVertexArray[0].color.g = 0.0f;
  150.         boneModelVertexArray[0].color.b = 0.0f;
  151.         boneModelVertexArray[0].color.a = 1.0f;
  152.  
  153.         //Vertex 2
  154.         //Position
  155.         boneModelVertexArray[1].position.x = 0.0f;
  156.         boneModelVertexArray[1].position.y = 0.0f;
  157.         boneModelVertexArray[1].position.z = 0.0f;
  158.         boneModelVertexArray[1].position.w = 1.0f;
  159.         //Color
  160.         boneModelVertexArray[1].color.r = 0.0f;
  161.         boneModelVertexArray[1].color.g = 0.0f;
  162.         boneModelVertexArray[1].color.b = 0.0f;
  163.         boneModelVertexArray[1].color.a = 1.0f;
  164.  
  165.         //Vertex 3
  166.         //Position
  167.         boneModelVertexArray[2].position.x = _width;
  168.         boneModelVertexArray[2].position.y = _height;
  169.         boneModelVertexArray[2].position.z = 0.0f;
  170.         boneModelVertexArray[2].position.w = 1.0f;
  171.         //Color
  172.         boneModelVertexArray[2].color.r = 0.0f;
  173.         boneModelVertexArray[2].color.g = 0.0f;
  174.         boneModelVertexArray[2].color.b = 0.0f;
  175.         boneModelVertexArray[2].color.a = 1.0f;
  176.  
  177.         //Vertex 4
  178.         //Position
  179.         boneModelVertexArray[3].position.x = _width;
  180.         boneModelVertexArray[3].position.y = 0.0f;
  181.         boneModelVertexArray[3].position.z = 0.0f;
  182.         boneModelVertexArray[3].position.w = 1.0f;
  183.         //Color
  184.         boneModelVertexArray[3].color.r = 0.0f;
  185.         boneModelVertexArray[3].color.g = 0.0f;
  186.         boneModelVertexArray[3].color.b = 0.0f;
  187.         boneModelVertexArray[3].color.a = 1.0f;
  188.        
  189.         //Generate the Index data.
  190.         boneModelIndexArray[0] = 0;
  191.         boneModelIndexArray[1] = 1;
  192.         boneModelIndexArray[2] = 2;
  193.         boneModelIndexArray[3] = 2;
  194.         boneModelIndexArray[4] = 1;
  195.         boneModelIndexArray[5] = 3;
  196.     } else if(boneType == "CUBE"){
  197.         //Set the size of the index array.
  198.         indexArraySize = 36;
  199.        
  200.         boneModelVertexArray = (Vertex*)linearAlloc(sizeof(Vertex) * 8);
  201.         boneModelIndexArray = (unsigned short*)linearAlloc(sizeof(unsigned short) * indexArraySize);
  202.        
  203.         float size = 10.0f;
  204.        
  205.         //Positions
  206.         boneModelVertexArray[0].position.x = -size;
  207.         boneModelVertexArray[0].position.y = 0.0f;
  208.         boneModelVertexArray[0].position.z = -size;
  209.        
  210.         boneModelVertexArray[1].position.x = -size;
  211.         boneModelVertexArray[1].position.y = 0.0f;
  212.         boneModelVertexArray[1].position.z = size;
  213.        
  214.         boneModelVertexArray[2].position.x = size;
  215.         boneModelVertexArray[2].position.y = 0.0f;
  216.         boneModelVertexArray[2].position.z = -size;
  217.        
  218.         boneModelVertexArray[3].position.x = size;
  219.         boneModelVertexArray[3].position.y = 0.0f;
  220.         boneModelVertexArray[3].position.z = size;
  221.        
  222.         boneModelVertexArray[4].position.x = -size;
  223.         boneModelVertexArray[4].position.y = size;
  224.         boneModelVertexArray[4].position.z = -size;
  225.        
  226.         boneModelVertexArray[5].position.x = -size;
  227.         boneModelVertexArray[5].position.y = size;
  228.         boneModelVertexArray[5].position.z = size;
  229.        
  230.         boneModelVertexArray[6].position.x = size;
  231.         boneModelVertexArray[6].position.y = size;
  232.         boneModelVertexArray[6].position.z = -size;
  233.        
  234.         boneModelVertexArray[7].position.x = size;
  235.         boneModelVertexArray[7].position.y = size;
  236.         boneModelVertexArray[7].position.z = size;
  237.        
  238.         //Colors
  239.         boneModelVertexArray[0].color.r = 1.0f;
  240.         boneModelVertexArray[0].color.g = 1.0f;
  241.         boneModelVertexArray[0].color.b = 1.0f;
  242.        
  243.         boneModelVertexArray[1].color.r = 1.0f;
  244.         boneModelVertexArray[1].color.g = 1.0f;
  245.         boneModelVertexArray[1].color.b = 1.0f;
  246.        
  247.         boneModelVertexArray[2].color.r = 1.0f;
  248.         boneModelVertexArray[2].color.g = 1.0f;
  249.         boneModelVertexArray[2].color.b = 1.0f;
  250.        
  251.         boneModelVertexArray[3].color.r = 1.0f;
  252.         boneModelVertexArray[3].color.g = 1.0f;
  253.         boneModelVertexArray[3].color.b = 1.0f;
  254.        
  255.         boneModelVertexArray[4].color.r = 1.0f;
  256.         boneModelVertexArray[4].color.g = 1.0f;
  257.         boneModelVertexArray[4].color.b = 1.0f;
  258.        
  259.         boneModelVertexArray[5].color.r = 1.0f;
  260.         boneModelVertexArray[5].color.g = 1.0f;
  261.         boneModelVertexArray[5].color.b = 1.0f;
  262.        
  263.         boneModelVertexArray[6].color.r = 1.0f;
  264.         boneModelVertexArray[6].color.g = 1.0f;
  265.         boneModelVertexArray[6].color.b = 1.0f;
  266.        
  267.         boneModelVertexArray[7].color.r = 1.0f;
  268.         boneModelVertexArray[7].color.g = 1.0f;
  269.         boneModelVertexArray[7].color.b = 1.0f;
  270.        
  271.         //Base Indices
  272.         boneModelIndexArray[0] = 0;
  273.         boneModelIndexArray[1] = 1;
  274.         boneModelIndexArray[2] = 2;
  275.         boneModelIndexArray[3] = 2;
  276.         boneModelIndexArray[4] = 1;
  277.         boneModelIndexArray[5] = 3;
  278.         boneModelIndexArray[6] = 4;
  279.         boneModelIndexArray[7] = 0;
  280.         boneModelIndexArray[8] = 2;
  281.         boneModelIndexArray[9] = 6;
  282.         boneModelIndexArray[10] = 0;
  283.         boneModelIndexArray[11] = 2;
  284.         boneModelIndexArray[12] = 5;
  285.         boneModelIndexArray[13] = 1;
  286.         boneModelIndexArray[14] = 0;
  287.         boneModelIndexArray[15] = 4;
  288.         boneModelIndexArray[16] = 1;
  289.         boneModelIndexArray[17] = 0;
  290.         boneModelIndexArray[18] = 5;
  291.         boneModelIndexArray[19] = 1;
  292.         boneModelIndexArray[20] = 7;
  293.         boneModelIndexArray[21] = 7;
  294.         boneModelIndexArray[22] = 1;
  295.         boneModelIndexArray[23] = 3;
  296.         boneModelIndexArray[24] = 7;
  297.         boneModelIndexArray[25] = 3;
  298.         boneModelIndexArray[26] = 6;
  299.         boneModelIndexArray[27] = 6;
  300.         boneModelIndexArray[28] = 3;
  301.         boneModelIndexArray[29] = 2;
  302.         boneModelIndexArray[30] = 4;
  303.         boneModelIndexArray[31] = 5;
  304.         boneModelIndexArray[32] = 7;
  305.         boneModelIndexArray[33] = 6;
  306.         boneModelIndexArray[34] = 5;
  307.         boneModelIndexArray[35] = 7;
  308.     }
  309. }
  310.  
  311. //Loads the model from an .sffmf file.
  312. void Model::LoadSFFMF(std::string fileLocation)
  313. {
  314.     /*DEBUGGING INFO*/
  315.     std::cout << "Loading SFFMF File: " << fileLocation << std::endl;
  316.     std::cout << "SFFMF Loader v0.1" << std::endl;
  317.  
  318.     /*PATH EXTRACTION*/
  319.     //Set a local path to the file.
  320.     _fileLocation = fileLocation;
  321.     //Extract the path from the location of the file.
  322.     ExtractPath();
  323.  
  324.     //Set the modelCode for debugging.
  325.     _modelCode = (unsigned int*)linearAlloc(4);
  326.     *_modelCode = 0;
  327.  
  328.     /*Binary Parsing*/
  329.     //Create an empty file pointer.
  330.     FILE* inputFile = NULL;
  331.     //Open the file for binary reading.
  332.     inputFile = fopen(fileLocation.c_str(), "rb");
  333.     //Obtain the size of the file.
  334.     long int fileSize = 0;
  335.     fseek(inputFile, 0, SEEK_END);
  336.     fileSize = ftell(inputFile);
  337.     fseek(inputFile, 0, SEEK_SET);
  338.     //Assuming the file exists, begin parsing.
  339.     if (inputFile){
  340.         //Load the modelCode, which indicates the type of model this is.
  341.         fread(_modelCode, sizeof(unsigned int), 1, inputFile);
  342.         //*_modelCode = 1;
  343.         //Load the amount of modelParts.
  344.         unsigned int* modelPartCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  345.         fread(modelPartCount, sizeof(int), 1, inputFile);
  346.         //Load the offsets for all of the modelParts.
  347.         unsigned int* modelPartOffsets = (unsigned int*)linearAlloc(sizeof(unsigned int) * (*modelPartCount));
  348.         fread(modelPartOffsets, sizeof(unsigned int), (*modelPartCount), inputFile);
  349.         //Create and load the data about each modelPart.
  350.         for (unsigned int i = 0; i < (*modelPartCount); i++){
  351.             //Create the modelPart.
  352.             modelParts.emplace_back();
  353.             //Load the vertex and index count.
  354.             unsigned int* vertexCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  355.             unsigned int* indexCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  356.             fread(vertexCount, sizeof(unsigned int), 1, inputFile);
  357.             fread(indexCount, sizeof(unsigned int), 1, inputFile);
  358.             //Allocate memory for the vertex and index data.
  359.             modelParts.back()._vertexArray = (Vertex*)linearAlloc(sizeof(Vertex) * (*vertexCount));
  360.             modelParts.back()._indexArray = (unsigned short*)linearAlloc(sizeof(unsigned short) * (*indexCount));
  361.             //Read all of the vertex and index data.
  362.             fread(modelParts.back()._vertexArray, sizeof(Vertex), (*vertexCount), inputFile);
  363.             fread(modelParts.back()._indexArray, sizeof(unsigned short), (*indexCount), inputFile);
  364.             //Load the number of characters in the material name.
  365.             unsigned int* materialNameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  366.             fread(materialNameCount, sizeof(unsigned int), 1, inputFile);
  367.             //Load the material name.
  368.             char* materialName = (char*)linearAlloc((*materialNameCount));
  369.             fread(materialName, 1, (*materialNameCount), inputFile);
  370.             //Load the diffuse of the material.
  371.             float* diffuse = (float*)linearAlloc(sizeof(float) * 3);
  372.             fread(diffuse, sizeof(float), 3, inputFile);
  373.             //Load the number of characters in the texture name.
  374.             unsigned int* textureNameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  375.             fread(textureNameCount, sizeof(unsigned int), 1, inputFile);
  376.             //Load the texture name.
  377.             char* textureName = (char*)linearAlloc((*textureNameCount));
  378.             fread(textureName, 1, (*textureNameCount), inputFile);
  379.             //Set the internal counts of the modelPart.
  380.             modelParts.back().vertexCount = (*vertexCount);
  381.             modelParts.back().indexCount = (*indexCount);
  382.             //Set the properties of the material.
  383.             modelParts.back().material.materialName = materialName;
  384.             modelParts.back().material.r = diffuse[0];
  385.             modelParts.back().material.g = diffuse[1];
  386.             modelParts.back().material.b = diffuse[2];
  387.             modelParts.back().material.textureFileName = textureName;
  388.             //Load the texture, if applicable.
  389.             if ((*_modelCode > 0)){
  390.                 //Set the texture of the material.
  391.                 modelParts.back().material.texture = TextureCollection::getTexture(modelParts.back().material.textureFileName);
  392.             }
  393.             //Free up memory.
  394.             linearFree(vertexCount);
  395.             linearFree(indexCount);
  396.             linearFree(materialNameCount);
  397.             linearFree(materialName);
  398.             linearFree(textureNameCount);
  399.             linearFree(textureName);
  400.         }
  401.  
  402.         //Load the armature, if applicable.
  403.         if (ftell(inputFile) < fileSize){
  404.             //Load the amount of bones to read.
  405.             unsigned int* boneCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  406.             fread(boneCount, sizeof(unsigned int), 1, inputFile);
  407.             //Read all of the bone data.
  408.             for (unsigned int i = 0; i < (*boneCount); i++){
  409.                 //Add a bone to the vector.
  410.                 _bones.emplace_back();
  411.                 //Load the amount of characters in the name of the bone.
  412.                 unsigned int* boneNameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  413.                 fread(boneNameCount, sizeof(unsigned int), 1, inputFile);
  414.                 //Load the name of the bone.
  415.                 char* boneName = (char*)linearAlloc(*boneNameCount);
  416.                 fread(boneName, 1, (*boneNameCount), inputFile);
  417.                 //Load the amount of children bones.
  418.                 unsigned int* boneChildrenCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  419.                 fread(boneChildrenCount, sizeof(unsigned int), 1, inputFile);
  420.                 //Allocate memory for the children indices and load them.
  421.                 if ((*boneChildrenCount) > 0){
  422.                     _bones.back()._childrenBoneIndices = (unsigned int*)linearAlloc(sizeof(unsigned int) * (*boneChildrenCount));
  423.                     fread(_bones.back()._childrenBoneIndices, sizeof(unsigned int), (*boneChildrenCount), inputFile);
  424.                 }
  425.                 //Set the ID of the bone.
  426.                 _bones.back()._boneID = _bones.size() - 1;
  427.                 //Set the name of the bone.
  428.                 _bones.back()._boneName = boneName;
  429.                 //Set the childrenCount of the bone.
  430.                 _bones.back()._childCount = (*boneChildrenCount);
  431.                 //Allocate memory for a matrix.
  432.                 float* floatArray = (float*)linearAlloc(sizeof(float) * 16);
  433.                 //Load the matrices of the bone.
  434.                 fread(floatArray, sizeof(float), 16, inputFile);
  435.                 _bones.back()._globalBindTransform.r[0].x = floatArray[0];
  436.                 _bones.back()._globalBindTransform.r[0].y = floatArray[4];
  437.                 _bones.back()._globalBindTransform.r[0].z = floatArray[8];
  438.                 _bones.back()._globalBindTransform.r[0].w = floatArray[12];
  439.                 _bones.back()._globalBindTransform.r[1].x = floatArray[1];
  440.                 _bones.back()._globalBindTransform.r[1].y = floatArray[5];
  441.                 _bones.back()._globalBindTransform.r[1].z = floatArray[9];
  442.                 _bones.back()._globalBindTransform.r[1].w = floatArray[13];
  443.                 _bones.back()._globalBindTransform.r[2].x = floatArray[2];
  444.                 _bones.back()._globalBindTransform.r[2].y = floatArray[6];
  445.                 _bones.back()._globalBindTransform.r[2].z = floatArray[10];
  446.                 _bones.back()._globalBindTransform.r[2].w = floatArray[14];
  447.                 _bones.back()._globalBindTransform.r[3].x = floatArray[3];
  448.                 _bones.back()._globalBindTransform.r[3].y = floatArray[7];
  449.                 _bones.back()._globalBindTransform.r[3].z = floatArray[11];
  450.                 _bones.back()._globalBindTransform.r[3].w = floatArray[15];
  451.  
  452.                 fread(floatArray, sizeof(float), 16, inputFile);
  453.                 _bones.back()._inverseBindTransform.r[0].x = floatArray[0];
  454.                 _bones.back()._inverseBindTransform.r[0].y = floatArray[4];
  455.                 _bones.back()._inverseBindTransform.r[0].z = floatArray[8];
  456.                 _bones.back()._inverseBindTransform.r[0].w = floatArray[12];
  457.                 _bones.back()._inverseBindTransform.r[1].x = floatArray[1];
  458.                 _bones.back()._inverseBindTransform.r[1].y = floatArray[5];
  459.                 _bones.back()._inverseBindTransform.r[1].z = floatArray[9];
  460.                 _bones.back()._inverseBindTransform.r[1].w = floatArray[13];
  461.                 _bones.back()._inverseBindTransform.r[2].x = floatArray[2];
  462.                 _bones.back()._inverseBindTransform.r[2].y = floatArray[6];
  463.                 _bones.back()._inverseBindTransform.r[2].z = floatArray[10];
  464.                 _bones.back()._inverseBindTransform.r[2].w = floatArray[14];
  465.                 _bones.back()._inverseBindTransform.r[3].x = floatArray[3];
  466.                 _bones.back()._inverseBindTransform.r[3].y = floatArray[7];
  467.                 _bones.back()._inverseBindTransform.r[3].z = floatArray[11];
  468.                 _bones.back()._inverseBindTransform.r[3].w = floatArray[15];
  469.                 //Free up memory.
  470.                 linearFree(floatArray);
  471.                 linearFree(boneNameCount);
  472.                 linearFree(boneName);
  473.                 linearFree(boneChildrenCount);
  474.             }
  475.             //Free up memory.
  476.             linearFree(boneCount);
  477.         }
  478.  
  479.         //Load the animations, if applicable.
  480.         if (ftell(inputFile) < fileSize){
  481.             //Load the amount of animations to read.
  482.             unsigned int* animationCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  483.             fread(animationCount, sizeof(unsigned int), 1, inputFile);
  484.             //Read in all of the animation data.
  485.             for (unsigned int i = 0; i < (*animationCount); i++){
  486.                 //Add an animation object to the vector.
  487.                 _animationManager._animations.emplace_back();
  488.                 //Load the amount of characters in the animationName.
  489.                 unsigned int* animationNameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  490.                 fread(animationNameCount, sizeof(unsigned int), 1, inputFile);
  491.                 //Load the name of the animation.
  492.                 char* animationName = (char*)linearAlloc(*animationNameCount);
  493.                 fread(animationName, 1, (*animationNameCount), inputFile);
  494.                 //Set the name of the animation.
  495.                 _animationManager._animations.back().animationName = animationName;
  496.                 //Load the amount of frames contained within the animation.
  497.                 fread(&_animationManager._animations.back().frameCount, sizeof(unsigned int), 1, inputFile);
  498.                 //Load the amount of bones contained in the animation.
  499.                 unsigned int* animatedBoneCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  500.                 fread(animatedBoneCount, sizeof(unsigned int), 1, inputFile);
  501.                 //Read in the data for every bone.
  502.                 for (unsigned int j = 0; j < (*animatedBoneCount); j++){
  503.                     //Add a BoneAnimationData object to the vector.
  504.                     _animationManager._animations.back().boneAnimationData.emplace_back();
  505.                     //Load the index of the bone.
  506.                     fread(&_animationManager._animations.back().boneAnimationData.back().boneIndex, sizeof(unsigned int), 1, inputFile);
  507.                     //Load the amount of frames/matrices in the animation.
  508.                     unsigned int* frameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
  509.                     fread(frameCount, sizeof(unsigned int), 1, inputFile);
  510.                     //Set the frame count of the boneAnimationData.
  511.                     _animationManager._animations.back().boneAnimationData.back().frameCount = (*frameCount);
  512.                     //Allocate memory for all of the matrices.
  513.                     _animationManager._animations.back().boneAnimationData.back().boneMatrices = (C3D_Mtx*)linearAlloc(sizeof(C3D_Mtx) * (*frameCount));
  514.                     //Allocate memory for a matrix.
  515.                     float* floatArray = (float*)linearAlloc(sizeof(float) * 16);
  516.                     //Load all of the matrices.
  517.                     for (unsigned int k = 0; k < (*frameCount); k++){
  518.                         fread(floatArray, sizeof(float), 16, inputFile);
  519.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[0].x = floatArray[0];
  520.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[0].y = floatArray[4];
  521.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[0].z = floatArray[8];
  522.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[0].w = floatArray[12];
  523.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[1].x = floatArray[1];
  524.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[1].y = floatArray[5];
  525.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[1].z = floatArray[9];
  526.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[1].w = floatArray[13];
  527.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[2].x = floatArray[2];
  528.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[2].y = floatArray[6];
  529.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[2].z = floatArray[10];
  530.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[2].w = floatArray[14];
  531.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[3].x = floatArray[3];
  532.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[3].y = floatArray[7];
  533.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[3].z = floatArray[11];
  534.                         _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[3].w = floatArray[15];
  535.                     }
  536.                     //Free up memory.
  537.                     linearFree(floatArray);
  538.                     linearFree(frameCount);
  539.                 }
  540.                 //Free up memory.
  541.                 linearFree(animationNameCount);
  542.                 linearFree(animationName);
  543.                 linearFree(animatedBoneCount);
  544.             }
  545.             //Free up memory.
  546.             linearFree(animationCount);
  547.         }
  548.  
  549.         //Free up memory.
  550.         linearFree(modelPartCount);
  551.         linearFree(modelPartOffsets);
  552.  
  553.         //Close the file.
  554.         fclose(inputFile);
  555.     }
  556.    
  557.     /*ATTRIBUTE FETCHING*/
  558.     //Obtain the shaders from the collection.
  559.     if ((*_modelCode) < 2){
  560.         _modelShader = ShaderCollection::getShader("ModelShader");
  561.     }
  562.     else{
  563.         _modelShader = ShaderCollection::getShader("SkinnedModelShader");
  564.     }
  565.     _boneShader = ShaderCollection::getShader("BoneShader");
  566.     //Obtain the location of uniforms in the shader.
  567.     _transformationMatrixLocation = shaderInstanceGetUniformLocation(_modelShader->vertexShader, "modelMatrix");
  568.     _projectionMatrixLocation = shaderInstanceGetUniformLocation(_modelShader->vertexShader, "projectionMatrix");
  569.     _viewMatrixLocation = shaderInstanceGetUniformLocation(_modelShader->vertexShader, "viewMatrix");
  570.  
  571.     //Load certain data if this object has an armature.
  572.     if ((*_modelCode) > 1){
  573.         //Load the uniforms.
  574.         _boneMatrixArrayLocation = shaderInstanceGetUniformLocation(_modelShader->vertexShader, "boneMatrices");
  575.         _boneMatrixLocation = shaderInstanceGetUniformLocation(_boneShader->vertexShader, "boneMatrix");
  576.         _boneTransformationMatrixLocation = shaderInstanceGetUniformLocation(_boneShader->vertexShader, "modelMatrix");
  577.         _boneViewMatrixLocation = shaderInstanceGetUniformLocation(_boneShader->vertexShader, "viewMatrix");
  578.         _boneProjectionMatrixLocation = shaderInstanceGetUniformLocation(_boneShader->vertexShader, "projectionMatrix");
  579.         printf("_boneMatrixLocation: %d\n", _boneMatrixLocation);
  580.         printf("_boneTransformationMatrixLocation: %d\n", _boneTransformationMatrixLocation);
  581.         printf("_boneViewMatrixLocation: %d\n", _boneViewMatrixLocation);
  582.         printf("_boneProjectionMatrixLocation: %d\n", _boneProjectionMatrixLocation);
  583.         //Allocate memory for the matrices.
  584.         _boneMatrices = (C3D_Mtx*)linearAlloc(sizeof(C3D_Mtx) * _bones.size());
  585.         //Enable the animation manager, but only if an animation exists.
  586.         _animationManager.SetAnimation(0, &_bones);
  587.         _animationManager._enabled = true;
  588.     }
  589.  
  590.     /*MATRIX GENERATION*/
  591.     //Set the Up Direction.
  592.     upDirection.y = 1.0f;
  593.     //Setup the matrices.
  594.     Mtx_Scale(&_modelMatrix, 1.0f, 1.0f, 1.0f);
  595.     Mtx_Rotate(&_modelMatrix, upDirection, C3D_AngleFromDegrees(_yRotation), false);
  596.     Mtx_Translate(&_modelMatrix, 0.0f, 0.0f, 0.0f, false);
  597.  
  598.     /**MODEL GENERATION**/
  599.     //Put all of the data in a VBO. Also calculate total vertex and index count.
  600.     for (unsigned int i = 0; i < modelParts.size(); i++){
  601.         //Add on to the totals.
  602.         _vertexCount += modelParts.at(i).vertexCount;
  603.         _indexCount += modelParts.at(i).indexCount;
  604.     }
  605.    
  606.     //Create the bone model.
  607.     CreateBoneModel("QUAD");
  608. }
  609.  
  610. //Renders the model.
  611. void Model::Render(float delta, PerspectiveCamera* perspectiveCamera){
  612.     //Bind the shader.
  613.     C3D_BindProgram(_modelShader);
  614.     //Send the matrices to the GPU.
  615.     C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _transformationMatrixLocation, &_modelMatrix); 
  616.     C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _viewMatrixLocation, &perspectiveCamera->_viewMatrix);
  617.     C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _projectionMatrixLocation, &perspectiveCamera->_projectionMatrix);
  618.     //Wrap the Y Rotation.
  619.     if (_yRotation > 360.0f){
  620.         _yRotation -= 360.0f;
  621.     }
  622.     if (_yRotation < 0.0f){
  623.         _yRotation += 360.0f;
  624.     }
  625.     //Update the animation and matrix data of the model.
  626.     if ((*_modelCode) > 1){
  627.         //Update the current animation.
  628.         _animationManager.Update(&_bones);
  629.         //Create an identity matrix.
  630.         C3D_Mtx identityMatrix;
  631.         Mtx_Identity(&identityMatrix);
  632.         //Calculate the model-space transforms for every bone.
  633.         CalculateModelSpaceTransforms(&_bones.at(0), identityMatrix);
  634.         //Push all of the Matrices to the GPU.
  635.         //WriteMatrices();
  636.     }
  637.     //Matrix calculation.
  638.     Mtx_Identity(&_modelMatrix);
  639.     Mtx_Scale(&_modelMatrix, 1.0f, 1.0f, 1.0f);
  640.     Mtx_Rotate(&_modelMatrix, upDirection, C3D_AngleFromDegrees(_yRotation), false);
  641.     Mtx_Translate(&_modelMatrix, 0.0f, 0.0f, 0.0f, false);
  642.     //Render all of the model parts.
  643.     for (unsigned int i = 0; i < modelParts.size(); i++){          
  644.         //Bind the buffer.
  645.         modelParts.at(i).bufferInfo = C3D_GetBufInfo();
  646.         BufInfo_Init(modelParts.at(i).bufferInfo);
  647.         BufInfo_Add(modelParts.at(i).bufferInfo, modelParts.at(i)._vertexArray, sizeof(Vertex), 6, 0x543210);
  648.         //Bind the texture, if applicable.
  649.         if ((*_modelCode) == 1 || (*_modelCode) == 3){
  650.             if (modelParts.at(i).material.texture){
  651.                 C3D_TexBind(0, &modelParts.at(i).material.texture->_texture);
  652.             }
  653.         }
  654.         //Draw the modelPart.
  655.         C3D_DrawElements(GPU_TRIANGLES, modelParts.at(i).indexCount, C3D_UNSIGNED_SHORT, modelParts.at(i)._indexArray);
  656.     }
  657. }
  658.  
  659. //Renders the armature.
  660. void Model::RenderArmature(PerspectiveCamera* perspectiveCamera)
  661. {
  662.     if ((*_modelCode) > 1){
  663.         //Bind the shader.
  664.         C3D_BindProgram(_boneShader);
  665.         //Send the matrices to the GPU.
  666.         C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _boneTransformationMatrixLocation, &_modelMatrix); 
  667.         C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _boneViewMatrixLocation, &perspectiveCamera->_viewMatrix);
  668.         C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _boneProjectionMatrixLocation, &perspectiveCamera->_projectionMatrix);
  669.         //Bind the buffer.
  670.         pyramidBufferInfo = C3D_GetBufInfo();
  671.         BufInfo_Init(pyramidBufferInfo);
  672.         BufInfo_Add(pyramidBufferInfo, boneModelVertexArray, sizeof(Vertex), 6, 0x543210);
  673.         //Disable depth testing.
  674.         C3D_DepthTest(false, GPU_GEQUAL, GPU_WRITE_ALL);
  675.         //Loop through all of the bones.
  676.         for (unsigned int i = 0; i < _bones.size(); i++){
  677.             //Make the root node point large, with the other bones being slightly smaller.
  678.             if (_animationManager._enabled){
  679.                 //_bones.at(i)._boneMatrix = _bones.at(i)._boneMatrix * _bones.at(i)._inverseBindTransform;
  680.             }
  681.             //Generate the position of the vertex.
  682.             //glm::vec4 originPoint = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
  683.             //glm::mat4 boneWorldMatrix = _modelMatrix * _bones.at(i)._boneMatrix;
  684.             //originPoint = boneWorldMatrix * originPoint;
  685.             //Output the point.
  686.             //std::cout << _bones.at(i)->_boneName << " Position: (" << originPoint.x << ", " << originPoint.y << ", " << originPoint.z << ")" << std::endl;
  687.             //For every bone, set the matrix within the shader.
  688.             C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _boneMatrixLocation, &_bones.at(i)._boneMatrix);
  689.             //Draw a point using the matrix of the bone.
  690.             C3D_DrawElements(GPU_TRIANGLES, indexArraySize, C3D_UNSIGNED_SHORT, boneModelIndexArray);
  691.         }
  692.     }
  693. }
  694.  
  695. //Constructor
  696. Model::Model()
  697. {
  698. }
  699.  
  700. //Destructor
  701. Model::~Model()
  702. {
  703. }
Advertisement
Add Comment
Please, Sign In to add comment