Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Model.h"
- //Outputs a Matrix to the console.
- void Model::DebugMatrix(C3D_Mtx matrix, int debugIndex)
- {
- if(!outputtedMatrix[debugIndex]){
- printf("Matrix\n");
- printf("[%.3f, %.3f, %.3f, %.3f]\n", matrix.r[0].x, matrix.r[0].y, matrix.r[0].z, matrix.r[0].w);
- printf("[%.3f, %.3f, %.3f, %.3f]\n", matrix.r[1].x, matrix.r[1].y, matrix.r[1].z, matrix.r[1].w);
- printf("[%.3f, %.3f, %.3f, %.3f]\n", matrix.r[2].x, matrix.r[2].y, matrix.r[2].z, matrix.r[2].w);
- printf("[%.3f, %.3f, %.3f, %.3f]\n", matrix.r[3].x, matrix.r[3].y, matrix.r[3].z, matrix.r[3].w);
- outputtedMatrix[debugIndex] = true;
- }
- }
- //Extracts the path from the file location.
- void Model::ExtractPath(){
- //Create a copy of the file location.
- std::string fileLocationCopy = _fileLocation;
- std::string tempString = "";
- std::vector<std::string> filePathSections;
- //Remove all of the slashes.
- StringModifier::replaceCharacterWithSpace(&fileLocationCopy, '/');
- //Create a temporary string stream and read from the string.
- std::stringstream stringStream;
- stringStream.str(fileLocationCopy);
- while (stringStream >> tempString){
- filePathSections.push_back(tempString);
- }
- //Append all of the sections to the path string.
- for (unsigned int i = 0; i < filePathSections.size(); i++){
- if (i != filePathSections.size() - 1){
- _filePath += filePathSections.at(i) + "/";
- }
- }
- //Output the file path.
- std::cout << "FilePath: " << _filePath << std::endl;
- //Clear everything.
- filePathSections.clear();
- stringStream.clear();
- }
- //Extracts the base file name from the file location.
- void Model::ExtractBaseFileName(){
- //Create a copy of the file location.
- std::string fileLocationCopy = _fileLocation;
- std::string tempString = "";
- std::vector<std::string> filePathSections;
- //Remove all of the slashes and periods.
- StringModifier::replaceCharacterWithSpace(&fileLocationCopy, '/');
- StringModifier::replaceCharacterWithSpace(&fileLocationCopy, '.');
- //Create a temporary string stream and read from the string.
- std::stringstream stringStream;
- stringStream.str(fileLocationCopy);
- while (stringStream >> tempString){
- filePathSections.push_back(tempString);
- }
- //Extract the base file name.
- _baseFileName = filePathSections.at(filePathSections.size() - 2);
- //Output the file path.
- std::cout << "BaseFilename: " << _baseFileName << std::endl;
- //Clear everything.
- filePathSections.clear();
- stringStream.clear();
- }
- //Locates the index of a bone in the vector.
- int Model::getBoneIndex(std::string boneName){
- int index = -1;
- for (unsigned int i = 0; i < _bones.size(); i++){
- if (_bones.at(i)._boneName == boneName){
- index = i;
- }
- }
- return index;
- }
- //Debugs data about a vertex.
- void Model::DebugVertex(Vertex* vertex){
- std::cout << "Position: (" << vertex->position.x << ", " << vertex->position.y << ", " << vertex->position.z << ", " << vertex->position.w << ")" << std::endl;
- std::cout << "Normal: (" << vertex->normals.x << ", " << vertex->normals.y << ", " << vertex->normals.z << ")" << std::endl;
- std::cout << "UV Coordinate: (" << vertex->uvs.x << ", " << vertex->uvs.y << ")" << std::endl;
- std::cout << "Joint IDs: (" << vertex->boneIDs.x << ", " << vertex->boneIDs.y << ", " << vertex->boneIDs.z << ")" << std::endl;
- std::cout << "Joint Weights: (" << vertex->boneWeights.x << ", " << vertex->boneWeights.y << ", " << vertex->boneWeights.z << ")" << std::endl;
- }
- //Recursively generate the Model-Space transforms of all of the bones.
- void Model::CalculateModelSpaceTransforms(ArmatureBone* targetBone, C3D_Mtx parentMatrix)
- {
- //std::cout << "Calculating Bone Matrix: " << targetBone->_boneName << std::endl;
- //Calculate the Model-Space transform for the current bone.
- if (_animationManager._enabled){
- //Generate the Model-Space Bind Matrix of the bone.
- Mtx_Multiply(&targetBone->_boneMatrix, &parentMatrix, &targetBone->_localAnimatedBoneMatrix);
- }
- else{
- //Generate the model-space matrix of the bone.
- targetBone->_boneMatrix = targetBone->_globalBindTransform;
- }
- //Calculate the Model-Space transforms for all of the children.
- for (unsigned int i = 0; i < targetBone->_childCount; i++){
- CalculateModelSpaceTransforms(&_bones.at(targetBone->_childrenBoneIndices[i]), targetBone->_boneMatrix);
- }
- }
- //Sends the matrices to the GPU.
- void Model::WriteMatrices()
- {
- //Used for writing to the GPU.
- C3D_FVec* boneMatrixPointer = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, _boneMatrixArrayLocation, 64);
- //Set the matrix within the matrix array.
- for (unsigned int i = 0; i < 16; i++){
- //Set the bone's matrix.
- Mtx_Multiply(&_boneMatrices[i], &_bones.at(i)._boneMatrix, &_bones.at(i)._inverseBindTransform);
- DebugMatrix(_bones.at(i)._boneMatrix, 0);
- DebugMatrix(_bones.at(i)._inverseBindTransform, 1);
- //Push the matrix to the GPU.
- if(boneMatrixPointer){
- boneMatrixPointer[(i * 4)] = _boneMatrices[i].r[0];
- boneMatrixPointer[(i * 4) + 1] = _boneMatrices[i].r[1];
- boneMatrixPointer[(i * 4) + 2] = _boneMatrices[i].r[2];
- boneMatrixPointer[(i * 4) + 3] = _boneMatrices[i].r[3];
- }
- }
- }
- //Create the shape to represent the bones.
- void Model::CreateBoneModel(std::string boneType)
- {
- if(boneType == "QUAD"){
- //Set the size of the index array.
- indexArraySize = 6;
- //Allocate memory for the geometry data.
- boneModelVertexArray = (Vertex*)linearAlloc(sizeof(Vertex) * 4);
- boneModelIndexArray = (unsigned short*)linearAlloc(sizeof(unsigned short) * indexArraySize);
- float _width = 0.2f;
- float _height = 0.2f;
- //Vertex 1
- //Position
- boneModelVertexArray[0].position.x = 0.0f;
- boneModelVertexArray[0].position.y = _height;
- boneModelVertexArray[0].position.z = 0.0f;
- boneModelVertexArray[0].position.w = 1.0f;
- //Color
- boneModelVertexArray[0].color.r = 0.0f;
- boneModelVertexArray[0].color.g = 0.0f;
- boneModelVertexArray[0].color.b = 0.0f;
- boneModelVertexArray[0].color.a = 1.0f;
- //Vertex 2
- //Position
- boneModelVertexArray[1].position.x = 0.0f;
- boneModelVertexArray[1].position.y = 0.0f;
- boneModelVertexArray[1].position.z = 0.0f;
- boneModelVertexArray[1].position.w = 1.0f;
- //Color
- boneModelVertexArray[1].color.r = 0.0f;
- boneModelVertexArray[1].color.g = 0.0f;
- boneModelVertexArray[1].color.b = 0.0f;
- boneModelVertexArray[1].color.a = 1.0f;
- //Vertex 3
- //Position
- boneModelVertexArray[2].position.x = _width;
- boneModelVertexArray[2].position.y = _height;
- boneModelVertexArray[2].position.z = 0.0f;
- boneModelVertexArray[2].position.w = 1.0f;
- //Color
- boneModelVertexArray[2].color.r = 0.0f;
- boneModelVertexArray[2].color.g = 0.0f;
- boneModelVertexArray[2].color.b = 0.0f;
- boneModelVertexArray[2].color.a = 1.0f;
- //Vertex 4
- //Position
- boneModelVertexArray[3].position.x = _width;
- boneModelVertexArray[3].position.y = 0.0f;
- boneModelVertexArray[3].position.z = 0.0f;
- boneModelVertexArray[3].position.w = 1.0f;
- //Color
- boneModelVertexArray[3].color.r = 0.0f;
- boneModelVertexArray[3].color.g = 0.0f;
- boneModelVertexArray[3].color.b = 0.0f;
- boneModelVertexArray[3].color.a = 1.0f;
- //Generate the Index data.
- boneModelIndexArray[0] = 0;
- boneModelIndexArray[1] = 1;
- boneModelIndexArray[2] = 2;
- boneModelIndexArray[3] = 2;
- boneModelIndexArray[4] = 1;
- boneModelIndexArray[5] = 3;
- } else if(boneType == "CUBE"){
- //Set the size of the index array.
- indexArraySize = 36;
- boneModelVertexArray = (Vertex*)linearAlloc(sizeof(Vertex) * 8);
- boneModelIndexArray = (unsigned short*)linearAlloc(sizeof(unsigned short) * indexArraySize);
- float size = 10.0f;
- //Positions
- boneModelVertexArray[0].position.x = -size;
- boneModelVertexArray[0].position.y = 0.0f;
- boneModelVertexArray[0].position.z = -size;
- boneModelVertexArray[1].position.x = -size;
- boneModelVertexArray[1].position.y = 0.0f;
- boneModelVertexArray[1].position.z = size;
- boneModelVertexArray[2].position.x = size;
- boneModelVertexArray[2].position.y = 0.0f;
- boneModelVertexArray[2].position.z = -size;
- boneModelVertexArray[3].position.x = size;
- boneModelVertexArray[3].position.y = 0.0f;
- boneModelVertexArray[3].position.z = size;
- boneModelVertexArray[4].position.x = -size;
- boneModelVertexArray[4].position.y = size;
- boneModelVertexArray[4].position.z = -size;
- boneModelVertexArray[5].position.x = -size;
- boneModelVertexArray[5].position.y = size;
- boneModelVertexArray[5].position.z = size;
- boneModelVertexArray[6].position.x = size;
- boneModelVertexArray[6].position.y = size;
- boneModelVertexArray[6].position.z = -size;
- boneModelVertexArray[7].position.x = size;
- boneModelVertexArray[7].position.y = size;
- boneModelVertexArray[7].position.z = size;
- //Colors
- boneModelVertexArray[0].color.r = 1.0f;
- boneModelVertexArray[0].color.g = 1.0f;
- boneModelVertexArray[0].color.b = 1.0f;
- boneModelVertexArray[1].color.r = 1.0f;
- boneModelVertexArray[1].color.g = 1.0f;
- boneModelVertexArray[1].color.b = 1.0f;
- boneModelVertexArray[2].color.r = 1.0f;
- boneModelVertexArray[2].color.g = 1.0f;
- boneModelVertexArray[2].color.b = 1.0f;
- boneModelVertexArray[3].color.r = 1.0f;
- boneModelVertexArray[3].color.g = 1.0f;
- boneModelVertexArray[3].color.b = 1.0f;
- boneModelVertexArray[4].color.r = 1.0f;
- boneModelVertexArray[4].color.g = 1.0f;
- boneModelVertexArray[4].color.b = 1.0f;
- boneModelVertexArray[5].color.r = 1.0f;
- boneModelVertexArray[5].color.g = 1.0f;
- boneModelVertexArray[5].color.b = 1.0f;
- boneModelVertexArray[6].color.r = 1.0f;
- boneModelVertexArray[6].color.g = 1.0f;
- boneModelVertexArray[6].color.b = 1.0f;
- boneModelVertexArray[7].color.r = 1.0f;
- boneModelVertexArray[7].color.g = 1.0f;
- boneModelVertexArray[7].color.b = 1.0f;
- //Base Indices
- boneModelIndexArray[0] = 0;
- boneModelIndexArray[1] = 1;
- boneModelIndexArray[2] = 2;
- boneModelIndexArray[3] = 2;
- boneModelIndexArray[4] = 1;
- boneModelIndexArray[5] = 3;
- boneModelIndexArray[6] = 4;
- boneModelIndexArray[7] = 0;
- boneModelIndexArray[8] = 2;
- boneModelIndexArray[9] = 6;
- boneModelIndexArray[10] = 0;
- boneModelIndexArray[11] = 2;
- boneModelIndexArray[12] = 5;
- boneModelIndexArray[13] = 1;
- boneModelIndexArray[14] = 0;
- boneModelIndexArray[15] = 4;
- boneModelIndexArray[16] = 1;
- boneModelIndexArray[17] = 0;
- boneModelIndexArray[18] = 5;
- boneModelIndexArray[19] = 1;
- boneModelIndexArray[20] = 7;
- boneModelIndexArray[21] = 7;
- boneModelIndexArray[22] = 1;
- boneModelIndexArray[23] = 3;
- boneModelIndexArray[24] = 7;
- boneModelIndexArray[25] = 3;
- boneModelIndexArray[26] = 6;
- boneModelIndexArray[27] = 6;
- boneModelIndexArray[28] = 3;
- boneModelIndexArray[29] = 2;
- boneModelIndexArray[30] = 4;
- boneModelIndexArray[31] = 5;
- boneModelIndexArray[32] = 7;
- boneModelIndexArray[33] = 6;
- boneModelIndexArray[34] = 5;
- boneModelIndexArray[35] = 7;
- }
- }
- //Loads the model from an .sffmf file.
- void Model::LoadSFFMF(std::string fileLocation)
- {
- /*DEBUGGING INFO*/
- std::cout << "Loading SFFMF File: " << fileLocation << std::endl;
- std::cout << "SFFMF Loader v0.1" << std::endl;
- /*PATH EXTRACTION*/
- //Set a local path to the file.
- _fileLocation = fileLocation;
- //Extract the path from the location of the file.
- ExtractPath();
- //Set the modelCode for debugging.
- _modelCode = (unsigned int*)linearAlloc(4);
- *_modelCode = 0;
- /*Binary Parsing*/
- //Create an empty file pointer.
- FILE* inputFile = NULL;
- //Open the file for binary reading.
- inputFile = fopen(fileLocation.c_str(), "rb");
- //Obtain the size of the file.
- long int fileSize = 0;
- fseek(inputFile, 0, SEEK_END);
- fileSize = ftell(inputFile);
- fseek(inputFile, 0, SEEK_SET);
- //Assuming the file exists, begin parsing.
- if (inputFile){
- //Load the modelCode, which indicates the type of model this is.
- fread(_modelCode, sizeof(unsigned int), 1, inputFile);
- //*_modelCode = 1;
- //Load the amount of modelParts.
- unsigned int* modelPartCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(modelPartCount, sizeof(int), 1, inputFile);
- //Load the offsets for all of the modelParts.
- unsigned int* modelPartOffsets = (unsigned int*)linearAlloc(sizeof(unsigned int) * (*modelPartCount));
- fread(modelPartOffsets, sizeof(unsigned int), (*modelPartCount), inputFile);
- //Create and load the data about each modelPart.
- for (unsigned int i = 0; i < (*modelPartCount); i++){
- //Create the modelPart.
- modelParts.emplace_back();
- //Load the vertex and index count.
- unsigned int* vertexCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- unsigned int* indexCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(vertexCount, sizeof(unsigned int), 1, inputFile);
- fread(indexCount, sizeof(unsigned int), 1, inputFile);
- //Allocate memory for the vertex and index data.
- modelParts.back()._vertexArray = (Vertex*)linearAlloc(sizeof(Vertex) * (*vertexCount));
- modelParts.back()._indexArray = (unsigned short*)linearAlloc(sizeof(unsigned short) * (*indexCount));
- //Read all of the vertex and index data.
- fread(modelParts.back()._vertexArray, sizeof(Vertex), (*vertexCount), inputFile);
- fread(modelParts.back()._indexArray, sizeof(unsigned short), (*indexCount), inputFile);
- //Load the number of characters in the material name.
- unsigned int* materialNameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(materialNameCount, sizeof(unsigned int), 1, inputFile);
- //Load the material name.
- char* materialName = (char*)linearAlloc((*materialNameCount));
- fread(materialName, 1, (*materialNameCount), inputFile);
- //Load the diffuse of the material.
- float* diffuse = (float*)linearAlloc(sizeof(float) * 3);
- fread(diffuse, sizeof(float), 3, inputFile);
- //Load the number of characters in the texture name.
- unsigned int* textureNameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(textureNameCount, sizeof(unsigned int), 1, inputFile);
- //Load the texture name.
- char* textureName = (char*)linearAlloc((*textureNameCount));
- fread(textureName, 1, (*textureNameCount), inputFile);
- //Set the internal counts of the modelPart.
- modelParts.back().vertexCount = (*vertexCount);
- modelParts.back().indexCount = (*indexCount);
- //Set the properties of the material.
- modelParts.back().material.materialName = materialName;
- modelParts.back().material.r = diffuse[0];
- modelParts.back().material.g = diffuse[1];
- modelParts.back().material.b = diffuse[2];
- modelParts.back().material.textureFileName = textureName;
- //Load the texture, if applicable.
- if ((*_modelCode > 0)){
- //Set the texture of the material.
- modelParts.back().material.texture = TextureCollection::getTexture(modelParts.back().material.textureFileName);
- }
- //Free up memory.
- linearFree(vertexCount);
- linearFree(indexCount);
- linearFree(materialNameCount);
- linearFree(materialName);
- linearFree(textureNameCount);
- linearFree(textureName);
- }
- //Load the armature, if applicable.
- if (ftell(inputFile) < fileSize){
- //Load the amount of bones to read.
- unsigned int* boneCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(boneCount, sizeof(unsigned int), 1, inputFile);
- //Read all of the bone data.
- for (unsigned int i = 0; i < (*boneCount); i++){
- //Add a bone to the vector.
- _bones.emplace_back();
- //Load the amount of characters in the name of the bone.
- unsigned int* boneNameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(boneNameCount, sizeof(unsigned int), 1, inputFile);
- //Load the name of the bone.
- char* boneName = (char*)linearAlloc(*boneNameCount);
- fread(boneName, 1, (*boneNameCount), inputFile);
- //Load the amount of children bones.
- unsigned int* boneChildrenCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(boneChildrenCount, sizeof(unsigned int), 1, inputFile);
- //Allocate memory for the children indices and load them.
- if ((*boneChildrenCount) > 0){
- _bones.back()._childrenBoneIndices = (unsigned int*)linearAlloc(sizeof(unsigned int) * (*boneChildrenCount));
- fread(_bones.back()._childrenBoneIndices, sizeof(unsigned int), (*boneChildrenCount), inputFile);
- }
- //Set the ID of the bone.
- _bones.back()._boneID = _bones.size() - 1;
- //Set the name of the bone.
- _bones.back()._boneName = boneName;
- //Set the childrenCount of the bone.
- _bones.back()._childCount = (*boneChildrenCount);
- //Allocate memory for a matrix.
- float* floatArray = (float*)linearAlloc(sizeof(float) * 16);
- //Load the matrices of the bone.
- fread(floatArray, sizeof(float), 16, inputFile);
- _bones.back()._globalBindTransform.r[0].x = floatArray[0];
- _bones.back()._globalBindTransform.r[0].y = floatArray[4];
- _bones.back()._globalBindTransform.r[0].z = floatArray[8];
- _bones.back()._globalBindTransform.r[0].w = floatArray[12];
- _bones.back()._globalBindTransform.r[1].x = floatArray[1];
- _bones.back()._globalBindTransform.r[1].y = floatArray[5];
- _bones.back()._globalBindTransform.r[1].z = floatArray[9];
- _bones.back()._globalBindTransform.r[1].w = floatArray[13];
- _bones.back()._globalBindTransform.r[2].x = floatArray[2];
- _bones.back()._globalBindTransform.r[2].y = floatArray[6];
- _bones.back()._globalBindTransform.r[2].z = floatArray[10];
- _bones.back()._globalBindTransform.r[2].w = floatArray[14];
- _bones.back()._globalBindTransform.r[3].x = floatArray[3];
- _bones.back()._globalBindTransform.r[3].y = floatArray[7];
- _bones.back()._globalBindTransform.r[3].z = floatArray[11];
- _bones.back()._globalBindTransform.r[3].w = floatArray[15];
- fread(floatArray, sizeof(float), 16, inputFile);
- _bones.back()._inverseBindTransform.r[0].x = floatArray[0];
- _bones.back()._inverseBindTransform.r[0].y = floatArray[4];
- _bones.back()._inverseBindTransform.r[0].z = floatArray[8];
- _bones.back()._inverseBindTransform.r[0].w = floatArray[12];
- _bones.back()._inverseBindTransform.r[1].x = floatArray[1];
- _bones.back()._inverseBindTransform.r[1].y = floatArray[5];
- _bones.back()._inverseBindTransform.r[1].z = floatArray[9];
- _bones.back()._inverseBindTransform.r[1].w = floatArray[13];
- _bones.back()._inverseBindTransform.r[2].x = floatArray[2];
- _bones.back()._inverseBindTransform.r[2].y = floatArray[6];
- _bones.back()._inverseBindTransform.r[2].z = floatArray[10];
- _bones.back()._inverseBindTransform.r[2].w = floatArray[14];
- _bones.back()._inverseBindTransform.r[3].x = floatArray[3];
- _bones.back()._inverseBindTransform.r[3].y = floatArray[7];
- _bones.back()._inverseBindTransform.r[3].z = floatArray[11];
- _bones.back()._inverseBindTransform.r[3].w = floatArray[15];
- //Free up memory.
- linearFree(floatArray);
- linearFree(boneNameCount);
- linearFree(boneName);
- linearFree(boneChildrenCount);
- }
- //Free up memory.
- linearFree(boneCount);
- }
- //Load the animations, if applicable.
- if (ftell(inputFile) < fileSize){
- //Load the amount of animations to read.
- unsigned int* animationCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(animationCount, sizeof(unsigned int), 1, inputFile);
- //Read in all of the animation data.
- for (unsigned int i = 0; i < (*animationCount); i++){
- //Add an animation object to the vector.
- _animationManager._animations.emplace_back();
- //Load the amount of characters in the animationName.
- unsigned int* animationNameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(animationNameCount, sizeof(unsigned int), 1, inputFile);
- //Load the name of the animation.
- char* animationName = (char*)linearAlloc(*animationNameCount);
- fread(animationName, 1, (*animationNameCount), inputFile);
- //Set the name of the animation.
- _animationManager._animations.back().animationName = animationName;
- //Load the amount of frames contained within the animation.
- fread(&_animationManager._animations.back().frameCount, sizeof(unsigned int), 1, inputFile);
- //Load the amount of bones contained in the animation.
- unsigned int* animatedBoneCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(animatedBoneCount, sizeof(unsigned int), 1, inputFile);
- //Read in the data for every bone.
- for (unsigned int j = 0; j < (*animatedBoneCount); j++){
- //Add a BoneAnimationData object to the vector.
- _animationManager._animations.back().boneAnimationData.emplace_back();
- //Load the index of the bone.
- fread(&_animationManager._animations.back().boneAnimationData.back().boneIndex, sizeof(unsigned int), 1, inputFile);
- //Load the amount of frames/matrices in the animation.
- unsigned int* frameCount = (unsigned int*)linearAlloc(sizeof(unsigned int));
- fread(frameCount, sizeof(unsigned int), 1, inputFile);
- //Set the frame count of the boneAnimationData.
- _animationManager._animations.back().boneAnimationData.back().frameCount = (*frameCount);
- //Allocate memory for all of the matrices.
- _animationManager._animations.back().boneAnimationData.back().boneMatrices = (C3D_Mtx*)linearAlloc(sizeof(C3D_Mtx) * (*frameCount));
- //Allocate memory for a matrix.
- float* floatArray = (float*)linearAlloc(sizeof(float) * 16);
- //Load all of the matrices.
- for (unsigned int k = 0; k < (*frameCount); k++){
- fread(floatArray, sizeof(float), 16, inputFile);
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[0].x = floatArray[0];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[0].y = floatArray[4];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[0].z = floatArray[8];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[0].w = floatArray[12];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[1].x = floatArray[1];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[1].y = floatArray[5];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[1].z = floatArray[9];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[1].w = floatArray[13];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[2].x = floatArray[2];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[2].y = floatArray[6];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[2].z = floatArray[10];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[2].w = floatArray[14];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[3].x = floatArray[3];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[3].y = floatArray[7];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[3].z = floatArray[11];
- _animationManager._animations.back().boneAnimationData.back().boneMatrices[k].r[3].w = floatArray[15];
- }
- //Free up memory.
- linearFree(floatArray);
- linearFree(frameCount);
- }
- //Free up memory.
- linearFree(animationNameCount);
- linearFree(animationName);
- linearFree(animatedBoneCount);
- }
- //Free up memory.
- linearFree(animationCount);
- }
- //Free up memory.
- linearFree(modelPartCount);
- linearFree(modelPartOffsets);
- //Close the file.
- fclose(inputFile);
- }
- /*ATTRIBUTE FETCHING*/
- //Obtain the shaders from the collection.
- if ((*_modelCode) < 2){
- _modelShader = ShaderCollection::getShader("ModelShader");
- }
- else{
- _modelShader = ShaderCollection::getShader("SkinnedModelShader");
- }
- _boneShader = ShaderCollection::getShader("BoneShader");
- //Obtain the location of uniforms in the shader.
- _transformationMatrixLocation = shaderInstanceGetUniformLocation(_modelShader->vertexShader, "modelMatrix");
- _projectionMatrixLocation = shaderInstanceGetUniformLocation(_modelShader->vertexShader, "projectionMatrix");
- _viewMatrixLocation = shaderInstanceGetUniformLocation(_modelShader->vertexShader, "viewMatrix");
- //Load certain data if this object has an armature.
- if ((*_modelCode) > 1){
- //Load the uniforms.
- _boneMatrixArrayLocation = shaderInstanceGetUniformLocation(_modelShader->vertexShader, "boneMatrices");
- _boneMatrixLocation = shaderInstanceGetUniformLocation(_boneShader->vertexShader, "boneMatrix");
- _boneTransformationMatrixLocation = shaderInstanceGetUniformLocation(_boneShader->vertexShader, "modelMatrix");
- _boneViewMatrixLocation = shaderInstanceGetUniformLocation(_boneShader->vertexShader, "viewMatrix");
- _boneProjectionMatrixLocation = shaderInstanceGetUniformLocation(_boneShader->vertexShader, "projectionMatrix");
- printf("_boneMatrixLocation: %d\n", _boneMatrixLocation);
- printf("_boneTransformationMatrixLocation: %d\n", _boneTransformationMatrixLocation);
- printf("_boneViewMatrixLocation: %d\n", _boneViewMatrixLocation);
- printf("_boneProjectionMatrixLocation: %d\n", _boneProjectionMatrixLocation);
- //Allocate memory for the matrices.
- _boneMatrices = (C3D_Mtx*)linearAlloc(sizeof(C3D_Mtx) * _bones.size());
- //Enable the animation manager, but only if an animation exists.
- _animationManager.SetAnimation(0, &_bones);
- _animationManager._enabled = true;
- }
- /*MATRIX GENERATION*/
- //Set the Up Direction.
- upDirection.y = 1.0f;
- //Setup the matrices.
- Mtx_Scale(&_modelMatrix, 1.0f, 1.0f, 1.0f);
- Mtx_Rotate(&_modelMatrix, upDirection, C3D_AngleFromDegrees(_yRotation), false);
- Mtx_Translate(&_modelMatrix, 0.0f, 0.0f, 0.0f, false);
- /**MODEL GENERATION**/
- //Put all of the data in a VBO. Also calculate total vertex and index count.
- for (unsigned int i = 0; i < modelParts.size(); i++){
- //Add on to the totals.
- _vertexCount += modelParts.at(i).vertexCount;
- _indexCount += modelParts.at(i).indexCount;
- }
- //Create the bone model.
- CreateBoneModel("QUAD");
- }
- //Renders the model.
- void Model::Render(float delta, PerspectiveCamera* perspectiveCamera){
- //Bind the shader.
- C3D_BindProgram(_modelShader);
- //Send the matrices to the GPU.
- C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _transformationMatrixLocation, &_modelMatrix);
- C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _viewMatrixLocation, &perspectiveCamera->_viewMatrix);
- C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _projectionMatrixLocation, &perspectiveCamera->_projectionMatrix);
- //Wrap the Y Rotation.
- if (_yRotation > 360.0f){
- _yRotation -= 360.0f;
- }
- if (_yRotation < 0.0f){
- _yRotation += 360.0f;
- }
- //Update the animation and matrix data of the model.
- if ((*_modelCode) > 1){
- //Update the current animation.
- _animationManager.Update(&_bones);
- //Create an identity matrix.
- C3D_Mtx identityMatrix;
- Mtx_Identity(&identityMatrix);
- //Calculate the model-space transforms for every bone.
- CalculateModelSpaceTransforms(&_bones.at(0), identityMatrix);
- //Push all of the Matrices to the GPU.
- //WriteMatrices();
- }
- //Matrix calculation.
- Mtx_Identity(&_modelMatrix);
- Mtx_Scale(&_modelMatrix, 1.0f, 1.0f, 1.0f);
- Mtx_Rotate(&_modelMatrix, upDirection, C3D_AngleFromDegrees(_yRotation), false);
- Mtx_Translate(&_modelMatrix, 0.0f, 0.0f, 0.0f, false);
- //Render all of the model parts.
- for (unsigned int i = 0; i < modelParts.size(); i++){
- //Bind the buffer.
- modelParts.at(i).bufferInfo = C3D_GetBufInfo();
- BufInfo_Init(modelParts.at(i).bufferInfo);
- BufInfo_Add(modelParts.at(i).bufferInfo, modelParts.at(i)._vertexArray, sizeof(Vertex), 6, 0x543210);
- //Bind the texture, if applicable.
- if ((*_modelCode) == 1 || (*_modelCode) == 3){
- if (modelParts.at(i).material.texture){
- C3D_TexBind(0, &modelParts.at(i).material.texture->_texture);
- }
- }
- //Draw the modelPart.
- C3D_DrawElements(GPU_TRIANGLES, modelParts.at(i).indexCount, C3D_UNSIGNED_SHORT, modelParts.at(i)._indexArray);
- }
- }
- //Renders the armature.
- void Model::RenderArmature(PerspectiveCamera* perspectiveCamera)
- {
- if ((*_modelCode) > 1){
- //Bind the shader.
- C3D_BindProgram(_boneShader);
- //Send the matrices to the GPU.
- C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _boneTransformationMatrixLocation, &_modelMatrix);
- C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _boneViewMatrixLocation, &perspectiveCamera->_viewMatrix);
- C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _boneProjectionMatrixLocation, &perspectiveCamera->_projectionMatrix);
- //Bind the buffer.
- pyramidBufferInfo = C3D_GetBufInfo();
- BufInfo_Init(pyramidBufferInfo);
- BufInfo_Add(pyramidBufferInfo, boneModelVertexArray, sizeof(Vertex), 6, 0x543210);
- //Disable depth testing.
- C3D_DepthTest(false, GPU_GEQUAL, GPU_WRITE_ALL);
- //Loop through all of the bones.
- for (unsigned int i = 0; i < _bones.size(); i++){
- //Make the root node point large, with the other bones being slightly smaller.
- if (_animationManager._enabled){
- //_bones.at(i)._boneMatrix = _bones.at(i)._boneMatrix * _bones.at(i)._inverseBindTransform;
- }
- //Generate the position of the vertex.
- //glm::vec4 originPoint = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
- //glm::mat4 boneWorldMatrix = _modelMatrix * _bones.at(i)._boneMatrix;
- //originPoint = boneWorldMatrix * originPoint;
- //Output the point.
- //std::cout << _bones.at(i)->_boneName << " Position: (" << originPoint.x << ", " << originPoint.y << ", " << originPoint.z << ")" << std::endl;
- //For every bone, set the matrix within the shader.
- C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, _boneMatrixLocation, &_bones.at(i)._boneMatrix);
- //Draw a point using the matrix of the bone.
- C3D_DrawElements(GPU_TRIANGLES, indexArraySize, C3D_UNSIGNED_SHORT, boneModelIndexArray);
- }
- }
- }
- //Constructor
- Model::Model()
- {
- }
- //Destructor
- Model::~Model()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment