Importing & Reading Custom animation data. TODO: Shaders.

This commit is contained in:
antc13
2016-01-20 18:36:03 +01:00
parent 84b7cf165c
commit c984f76b2f
9 changed files with 230 additions and 39 deletions
+8 -1
View File
@@ -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<std::tuple<std::string, glm::mat4>> &boneInfo, std::map<std::string, int> &boneNameMapping, aiNode* node, int parentID);
};
+1 -1
View File
@@ -38,9 +38,9 @@ public:
, OffsetMatrix(offsetMatrix)
{ }
int ID;
std::string Name;
glm::mat4 OffsetMatrix;
int ID;
Bone* Parent;
std::vector<Bone*> Children;
+1 -1
View File
@@ -19,7 +19,7 @@
<Entity>
<Components>
<c:Model>
<Resource>models/Baljj</Resource>
<Resource>models/Baljj.mesh</Resource>
</c:Model>
<c:Transform/>
</Components>
+3 -3
View File
@@ -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");
+177 -3
View File
@@ -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()
{
+2 -2
View File
@@ -96,10 +96,10 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& 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;
}
+6 -7
View File
@@ -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);
+5 -4
View File
@@ -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<BindPoseSkeletonNode> 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<BindPoseSkeletonNode> 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<BindPoseSkeletonNode> 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;
+27 -17
View File
@@ -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<JointProperty> JointProperties;
};
std::string Name;
double Duration;
int NumKeyFrames;
int NumberOfJoints;
int nameLength = 0;
float Duration = 0;
int NumKeyFrames = 0;
int NumberOfJoints = 0;
std::vector<Keyframe> 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<BindPoseJoint> 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;
}
};