diff --git a/include/Engine/Rendering/RawModelCustom.h b/include/Engine/Rendering/RawModelCustom.h index bc85e32c..a3c29f62 100644 --- a/include/Engine/Rendering/RawModelCustom.h +++ b/include/Engine/Rendering/RawModelCustom.h @@ -22,7 +22,7 @@ class RawModel : public Resource friend class ResourceManager; protected: - RawModel(std::string& fileName); + RawModel(std::string fileName); public: ~RawModel(); @@ -74,6 +74,13 @@ private: void ReadMaterialFile(std::string filePath); void ReadMaterials(unsigned int &offset, char* fileData, unsigned int& fileByteSize); void ReadMaterialSingle(unsigned int &offset, char* fileData, unsigned int& fileByteSize); + + void ReadAnimationFile(std::string filePath); + void ReadAnimationBindPoses(unsigned int &offset, char* fileData, unsigned int& fileByteSize); + void ReadAnimationJoint(unsigned int &offset, char* fileData, unsigned int& fileByteSize); + void ReadAnimationClips(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int numberOfClips); + void ReadAnimationClipSingle(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int clipIndex); + void ReadAnimationKeyFrame(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int numberOfJoints, Skeleton::Animation& animation); //void CreateSkeleton(std::vector> &boneInfo, std::map &boneNameMapping, aiNode* node, int parentID); }; diff --git a/include/Engine/Rendering/Skeleton.h b/include/Engine/Rendering/Skeleton.h index 73d407dd..4a4d507b 100644 --- a/include/Engine/Rendering/Skeleton.h +++ b/include/Engine/Rendering/Skeleton.h @@ -38,9 +38,9 @@ public: , OffsetMatrix(offsetMatrix) { } - int ID; std::string Name; glm::mat4 OffsetMatrix; + int ID; Bone* Parent; std::vector Children; diff --git a/resources/Schema/Entities/Model.xml b/resources/Schema/Entities/Model.xml index 8962d7e1..6534815c 100644 --- a/resources/Schema/Entities/Model.xml +++ b/resources/Schema/Entities/Model.xml @@ -19,7 +19,7 @@ - models/Baljj + models/Baljj.mesh diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 624c4ad6..547a96e1 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -292,21 +292,21 @@ void EditorSystem::createWidget() m_WidgetPlaneX = m_World->CreateEntity(m_Widget); m_World->AttachComponent(m_WidgetPlaneX, "Transform"); m_World->AttachComponent(m_WidgetPlaneX, "Model"); - m_World->GetComponent(m_WidgetPlaneX, "Model")["Resource"] = "Models/coolCube"; // 360NoScope widgetPlaneX + m_World->GetComponent(m_WidgetPlaneX, "Model")["Resource"] = "Models/coolCube.mesh"; // 360NoScope widgetPlaneX m_WidgetY = m_World->CreateEntity(m_Widget); m_World->AttachComponent(m_WidgetY, "Transform"); m_World->AttachComponent(m_WidgetY, "Model"); m_WidgetPlaneY = m_World->CreateEntity(m_Widget); m_World->AttachComponent(m_WidgetPlaneY, "Transform"); m_World->AttachComponent(m_WidgetPlaneY, "Model"); - m_World->GetComponent(m_WidgetPlaneY, "Model")["Resource"] = "Models/coolCube"; // 360NoScope widgetPlaneY + m_World->GetComponent(m_WidgetPlaneY, "Model")["Resource"] = "Models/coolCube.mesh"; // 360NoScope widgetPlaneY m_WidgetZ = m_World->CreateEntity(m_Widget); m_World->AttachComponent(m_WidgetZ, "Transform"); m_World->AttachComponent(m_WidgetZ, "Model"); m_WidgetPlaneZ = m_World->CreateEntity(m_Widget); m_World->AttachComponent(m_WidgetPlaneZ, "Transform"); m_World->AttachComponent(m_WidgetPlaneZ, "Model"); - m_World->GetComponent(m_WidgetPlaneZ, "Model")["Resource"] = "Models/coolCube"; // 360NoScope widgetPlaneZ + m_World->GetComponent(m_WidgetPlaneZ, "Model")["Resource"] = "Models/coolCube.mesh"; // 360NoScope widgetPlaneZ m_WidgetOrigin = m_World->CreateEntity(m_Widget); m_World->AttachComponent(m_WidgetOrigin, "Transform"); m_World->AttachComponent(m_WidgetOrigin, "Model"); diff --git a/src/Engine/Rendering/RawModelCustom.cpp b/src/Engine/Rendering/RawModelCustom.cpp index b7f2a2fd..f7354d92 100644 --- a/src/Engine/Rendering/RawModelCustom.cpp +++ b/src/Engine/Rendering/RawModelCustom.cpp @@ -1,9 +1,12 @@ #include "Rendering/RawModelCustom.h" -RawModel::RawModel(std::string& fileName) +RawModel::RawModel(std::string fileName) { + fileName = fileName.erase(fileName.find_last_of("."), fileName.find_last_of(".") - fileName.size()); ReadMeshFile(fileName); ReadMaterialFile(fileName); + ReadAnimationFile(fileName); + int k = 0; } void RawModel::ReadMeshFile(std::string filePath) @@ -53,8 +56,7 @@ void RawModel::ReadVertices(unsigned int& offset, char* fileData, unsigned int& if (offset + m_Vertices.size() * sizeof(Vertex) > fileByteSize) { throw Resource::FailedLoadingException("Reading vertices failed"); } - unsigned int i = sizeof(Vertex); - unsigned int ii = sizeof(unsigned int); + memcpy(&m_Vertices[0], fileData + offset, m_Vertices.size() * sizeof(Vertex)); offset += m_Vertices.size() * sizeof(Vertex); #else @@ -187,6 +189,178 @@ void RawModel::ReadMaterialSingle(unsigned int &offset, char* fileData, unsigned MaterialGroups.push_back(newMaterial); } +void RawModel::ReadAnimationFile(std::string filePath) +{ + char* fileData; + filePath += ".anim"; + std::ifstream in(filePath.c_str(), std::ios_base::binary | std::ios_base::ate); + + if (!in.is_open()) { + //throw Resource::FailedLoadingException("Open animation file failed"); + return; // AJABAJA!!!!!!!! + } + + unsigned int fileByteSize = in.tellg(); + in.seekg(0, std::ios_base::beg); + + fileData = new char[fileByteSize]; + in.read(fileData, fileByteSize); + in.close(); + + unsigned int offset = 0; + if (fileByteSize > 0) { + m_Skeleton = new Skeleton(); + +#ifdef BOOST_LITTLE_ENDIAN + unsigned int numBindPoses = *(unsigned int*)(fileData); + offset += sizeof(unsigned int); + unsigned int numAnimations = *(unsigned int*)(fileData); + offset += sizeof(unsigned int); +#else +#endif + + ReadAnimationBindPoses(offset, fileData, fileByteSize); + ReadAnimationClips(offset, fileData, fileByteSize, numAnimations); + } + delete fileData; +} + +void RawModel::ReadAnimationBindPoses(unsigned int &offset, char* fileData, unsigned int& fileByteSize) +{ +#ifdef BOOST_LITTLE_ENDIAN + unsigned int* numBones = (unsigned int*)(fileData + offset); + offset += sizeof(unsigned int); + + for (unsigned int i = 0; i < *numBones; i++) { + ReadAnimationJoint(offset, fileData, fileByteSize); + } +#else +#endif +} + +void RawModel::ReadAnimationJoint(unsigned int &offset, char* fileData, unsigned int& fileByteSize) +{ +#ifdef BOOST_LITTLE_ENDIAN + if (offset + sizeof(unsigned int) > fileByteSize) { + throw Resource::FailedLoadingException("Reading Joint name length failed"); + } + unsigned int jointNameLength = *(unsigned int*)(fileData + offset); + offset += sizeof(unsigned int); + + if (offset + jointNameLength > fileByteSize) { + throw Resource::FailedLoadingException("Reading Joint name failed"); + } + std::string jointName = (fileData + offset); + offset += jointNameLength; + + if (offset + sizeof(float) * 4 * 4 > fileByteSize) { + throw Resource::FailedLoadingException("Reading Joint offset matrix failed"); + } + glm::mat4 offsetMatrix; + memcpy(&offsetMatrix, fileData + offset, sizeof(float) * 4 * 4); + offset += sizeof(float) * 4 * 4; + + if (offset + sizeof(int) > fileByteSize) { + throw Resource::FailedLoadingException("Reading Joint ID failed"); + } + int jointID = *(int*)(fileData + offset); + offset += sizeof(int); + + if (offset + sizeof(int) > fileByteSize) { + throw Resource::FailedLoadingException("Reading Joint Parent ID failed"); + } + int jointParentID = *(int*)(fileData + offset); + offset += sizeof(int); + + // Adding joint to the Skeleton + m_Skeleton->CreateBone(jointID, jointParentID, jointName, offsetMatrix); + + +#else +#endif +} + +void RawModel::ReadAnimationClips(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int numberOfClips) +{ + for (unsigned int i = 0; i < numberOfClips; i++) { + ReadAnimationClipSingle(offset, fileData, fileByteSize, i); + } +} + +void RawModel::ReadAnimationClipSingle(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int clipIndex) +{ +#ifdef BOOST_LITTLE_ENDIAN + Skeleton::Animation newAnimation; + + if (offset + sizeof(unsigned int) > fileByteSize) { + throw Resource::FailedLoadingException("Reading AnimationClip name length failed"); + } + unsigned int clipNameLength = *(unsigned int*)(fileData + offset); + offset += sizeof(unsigned int); + + if (offset + clipNameLength > fileByteSize) { + throw Resource::FailedLoadingException("Reading AnimationClip name failed"); + } + newAnimation.Name = (fileData + offset); + offset += clipNameLength; + + if (offset + sizeof(float) > fileByteSize) { + throw Resource::FailedLoadingException("Reading AnimationClip duration failed"); + } + + newAnimation.Duration = *(float*)(fileData + offset); + offset += sizeof(float); + + if (offset + sizeof(unsigned int) > fileByteSize) { + throw Resource::FailedLoadingException("Reading AnimationClip NrOfKeyframes failed"); + } + unsigned int nrOfKeyframes = *(unsigned int*)(fileData + offset); + offset += sizeof(unsigned int); + + if (offset + sizeof(unsigned int) > fileByteSize) { + throw Resource::FailedLoadingException("Reading AnimationClip NrOfJoints failed"); + } + unsigned int nrOfJoints = *(unsigned int*)(fileData + offset); + offset += sizeof(unsigned int); + + newAnimation.Keyframes.reserve(nrOfKeyframes); + for (unsigned int i = 0; i < nrOfKeyframes; i++) { + ReadAnimationKeyFrame(offset, fileData, fileByteSize, nrOfJoints, newAnimation); + } + m_Skeleton->Animations[newAnimation.Name] = newAnimation; +#else +#endif +} + +void RawModel::ReadAnimationKeyFrame(unsigned int &offset, char* fileData, unsigned int& fileByteSize, unsigned int nrOfJoints, Skeleton::Animation& animation) +{ + Skeleton::Animation::Keyframe newKeyFrame; + + if (offset + sizeof(int) > fileByteSize) { + throw Resource::FailedLoadingException("Reading AnimationKeyFrame index failed"); + } + newKeyFrame.Index = *(int*)(fileData + offset); + offset += sizeof(int); + + if (offset + sizeof(float) > fileByteSize) { + throw Resource::FailedLoadingException("Reading AnimationKeyFrame time failed"); + } + newKeyFrame.Time = *(float*)(fileData + offset); + offset += sizeof(float); + + if (offset + sizeof(Skeleton::Animation::Keyframe::BoneProperty) * nrOfJoints> fileByteSize) { + throw Resource::FailedLoadingException("Reading AnimationKeyFrame joints failed"); + } + + Skeleton::Animation::Keyframe::BoneProperty newBone; + for (unsigned int i = 0; i < nrOfJoints; i++) { + memcpy(&newBone, (fileData + offset), sizeof(Skeleton::Animation::Keyframe::BoneProperty)); + offset += sizeof(Skeleton::Animation::Keyframe::BoneProperty); + newKeyFrame.BoneProperties[i] = newBone; + } + animation.Keyframes.push_back(newKeyFrame); +} + RawModel::~RawModel() { diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 1b016f73..2a6a49c2 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -96,10 +96,10 @@ void RenderSystem::fillModels(std::list>& jobs, World model = ResourceManager::Load<::Model, true>(resource); } catch (const Resource::StillLoadingException&) { //continue; - model = ResourceManager::Load<::Model>("Models/coolCube"); // 360NoScope StillLoading mesh + model = ResourceManager::Load<::Model>("Models/coolCube.mesh"); // 360NoScope StillLoading mesh } catch (const std::exception&) { try { - model = ResourceManager::Load<::Model>("Models/coolCube"); // 360NoScope Error mesh + model = ResourceManager::Load<::Model>("Models/coolCube.mesh"); // 360NoScope Error mesh } catch (const std::exception&) { continue; } diff --git a/tools/MayaExporter/MayaExporter/Export.cpp b/tools/MayaExporter/MayaExporter/Export.cpp index 1db753d7..d25faa4c 100644 --- a/tools/MayaExporter/MayaExporter/Export.cpp +++ b/tools/MayaExporter/MayaExporter/Export.cpp @@ -11,7 +11,9 @@ bool Export::Meshes(std::string pathName, bool selectedOnly) MGlobal::displayError(MString() + "Export::Meshes() got no pathName. Do not know where to write file"); return false; } - + MSelectionList selectedOnStart; + MGlobal::getActiveSelectionList(selectedOnStart); + MObjectArray Objects; if (selectedOnly) { // Retrieving the objects we currently have selected @@ -36,18 +38,14 @@ bool Export::Meshes(std::string pathName, bool selectedOnly) MFnDependencyNode thisNode(node); MPlugArray connections; thisNode.findPlug("inMesh").connectedTo(connections, true, true); - bool next = false; for (unsigned int i = 0; i < connections.length(); i++) { if (connections[i].node().apiType() == MFn::kSkinClusterFilter) { - next = true; - break; + MGlobal::select(node); + MGlobal::executeCommand("gotoBindPose"); } } - if (next) - continue; - Objects.append(node); } } @@ -139,6 +137,7 @@ void Export::WriteAnimData(std::string pathName) int size = allBindPoses.size(); m_AnimFile.writeToFiles(&size); + size = allAnimations.size(); m_AnimFile.writeToFiles(&size); diff --git a/tools/MayaExporter/MayaExporter/Skeleton.cpp b/tools/MayaExporter/MayaExporter/Skeleton.cpp index 0497aa31..9f887561 100644 --- a/tools/MayaExporter/MayaExporter/Skeleton.cpp +++ b/tools/MayaExporter/MayaExporter/Skeleton.cpp @@ -70,6 +70,7 @@ Animation Skeleton::GetAnimData(std::string animationName, int startFrame, int e Animation returnData; double oneDivSixty = 1 / 60.0; returnData.Name = animationName; + returnData.nameLength = animationName.size() + 1; returnData.Duration = (endFrame - startFrame) * oneDivSixty; MItDag jointIt(MItDag::TraversalType::kDepthFirst, MFn::kJoint); @@ -132,7 +133,7 @@ Animation Skeleton::GetAnimData(std::string animationName, int startFrame, int e } int currentFrame = startFrame; - while (currentFrame != endFrame) { + while (currentFrame != endFrame + 1) { // ANDREAS Animation::Keyframe thisKeyFrame; thisKeyFrame.Index = currentFrame - startFrame; thisKeyFrame.Time = thisKeyFrame.Index * oneDivSixty; @@ -188,7 +189,6 @@ std::vector Skeleton::GetBindPoses() MItDag jointIt(MItDag::TraversalType::kDepthFirst, MFn::kJoint); BindPoseSkeletonNode SkeletonStorage; - while (!jointIt.isDone()) { MFnTransform MayaJoint(jointIt.currentItem()); BindPoseSkeletonNode::BindPoseJoint NewJoint; @@ -227,7 +227,8 @@ std::vector Skeleton::GetBindPoses() } NewJoint.Name = MayaJoint.name().asChar(); - + NewJoint.NameLength = MayaJoint.name().length() + 1; + NewJoint.ID = SkeletonStorage.Joints.size(); //double tmp[3]; //((MTransformationMatrix)Matrix).eulerRotation().asVector().get(tmp); //NewJoint.Rotation[0] = tmp[0]; @@ -242,9 +243,9 @@ std::vector Skeleton::GetBindPoses() //NewJoint.Translation[1] = tmp[1]; //NewJoint.Translation[2] = tmp[2]; SkeletonStorage.Joints.push_back(NewJoint); + SkeletonStorage.numBones++; jointIt.next(); } - m_AllSkeletons.push_back(SkeletonStorage); return m_AllSkeletons; diff --git a/tools/MayaExporter/MayaExporter/Skeleton.h b/tools/MayaExporter/MayaExporter/Skeleton.h index dda0cfea..6a288c8a 100644 --- a/tools/MayaExporter/MayaExporter/Skeleton.h +++ b/tools/MayaExporter/MayaExporter/Skeleton.h @@ -13,32 +13,35 @@ public: { struct JointProperty { - int ID; - float Position[3]; - float Rotation[4]; - float Scale[3]; + int ID = 0; + float Position[3]{ 0 }; + float Rotation[4]{ 0 }; + float Scale[3]{ 0 }; }; - int Index; - double Time; + int Index = 0; + float Time = 0; std::vector JointProperties; }; std::string Name; - double Duration; - int NumKeyFrames; - int NumberOfJoints; + int nameLength = 0; + float Duration = 0; + int NumKeyFrames = 0; + int NumberOfJoints = 0; std::vector Keyframes; virtual void WriteBinary(std::ostream& out) { + out.write((char*)&nameLength, sizeof(int)); out.write(Name.c_str(), Name.size() + 1); - out.write((char*)&Duration, sizeof(double)); + out.write((char*)&Duration, sizeof(float)); out.write((char*)&NumKeyFrames, sizeof(int)); out.write((char*)&NumberOfJoints, sizeof(int)); + //Här under loopas alla key frames igenom for (auto aKeyframe : Keyframes) { out.write((char*)&aKeyframe.Index, sizeof(int)); - out.write((char*)&aKeyframe.Time, sizeof(double)); + out.write((char*)&aKeyframe.Time, sizeof(float)); for (auto aJoint : aKeyframe.JointProperties) { out.write((char*)&aJoint.ID, sizeof(int)); out.write((char*)aJoint.Position, sizeof(float) * 3); @@ -72,20 +75,24 @@ class BindPoseSkeletonNode : public OutputData { public: struct BindPoseJoint { - int ParentID; + int NameLength; std::string Name; - float OffsetMatrix[4][4]; + float OffsetMatrix[4][4]{ 0 }; + int ID = 0; + int ParentID = 0; }; - + int numBones = 0; std::string Name; std::vector Joints; virtual void WriteBinary(std::ostream& out) { - out.write(Name.c_str(), Name.size() + 1); + out.write((char*)&numBones, sizeof(int)); for (auto Joint:Joints) { + out.write((char*)&Joint.NameLength, sizeof(int)); out.write(Joint.Name.c_str(), Joint.Name.size() + 1); out.write((char*)&Joint.OffsetMatrix, sizeof(float) * 4 * 4); + out.write((char*)&Joint.ID, sizeof(int)); out.write((char*)&Joint.ParentID, sizeof(int)); } @@ -93,16 +100,19 @@ public: virtual void WriteASCII(std::ostream& out) const { - out << "Bind Pose: " << Name << endl; + out << "Bind Pose: " << Name << " _ not in binary" << endl; + out << "numberOfBones: " << numBones << endl; for (auto Joint : Joints) { - out << Joint.Name << endl; + out << "Joint NameLength " << Joint.NameLength << endl; + out << "Joint name " << Joint.Name << endl; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ out << Joint.OffsetMatrix[i][j] << " "; } out << endl; } + out << Joint.ID << endl; out << Joint.ParentID << endl; } };