From b5b1140f5c3a29b3fc91969df603507c7195c86d Mon Sep 17 00:00:00 2001 From: antc13 Date: Sun, 24 Jan 2016 13:28:40 +0100 Subject: [PATCH] Custom Animations should now work. We also save color values if there's no texture. --- include/Engine/Rendering/RawModelCustom.h | 3 + resources/Schema/Entities/AnimatedArmy | 273 ++++++++++++++++++ resources/Shaders/ForwardPlus.vert.glsl | 2 +- src/Engine/Rendering/DrawFinalPass.cpp | 6 +- src/Engine/Rendering/RawModelCustom.cpp | 15 +- tools/MayaExporter/MayaExporter/Material.cpp | 30 +- tools/MayaExporter/MayaExporter/Material.h | 9 + tools/MayaExporter/MayaExporter/Menu.cpp | 7 +- tools/MayaExporter/MayaExporter/Mesh.cpp | 14 +- .../MayaExporter/MayaExporter/WriteToFile.cpp | 9 +- tools/MayaExporter/MayaExporter/WriteToFile.h | 1 + 11 files changed, 341 insertions(+), 28 deletions(-) create mode 100644 resources/Schema/Entities/AnimatedArmy diff --git a/include/Engine/Rendering/RawModelCustom.h b/include/Engine/Rendering/RawModelCustom.h index 2dea51ff..a1a2f1f6 100644 --- a/include/Engine/Rendering/RawModelCustom.h +++ b/include/Engine/Rendering/RawModelCustom.h @@ -48,6 +48,9 @@ public: { float SpecularExponent; float ReflectionFactor; + float DiffuseColor[3]; + float SpecularColor[3]; + float IncandescenceColor[3]; unsigned int StartIndex; unsigned int EndIndex; //float Transparency; diff --git a/resources/Schema/Entities/AnimatedArmy b/resources/Schema/Entities/AnimatedArmy new file mode 100644 index 00000000..b711a0fe --- /dev/null +++ b/resources/Schema/Entities/AnimatedArmy @@ -0,0 +1,273 @@ + + + + + + + + + + + + + + + + + + + + + + + models/dummyscene.mesh + + + + + + + + + + + models/animtest. + + + + + + + + + + + models/animTest.mesh + + + + + + + + + + + models/animTest.mesh + + + + + + + + + + + models/animTest.mesh + + + + + + + + + + + 0.13000047206878662 + + + + + + + + + + + + 0.30000001192092896 + + + + + + + + + + + + 5 + + + + + + + + + + + + 5 + 0.80000001192092896 + + + + + + + + + + + + 5 + + + + + + + + + + + + 5 + + + + + + + + + + + + + 0.50999981164932251 + + + + + + + + + + + + + 5 + 5 + + + + + + + + + + + + 5 + + + + + + + + + + + + 5 + + + + + + + + + + + + 5 + + + + + + + + + + + + 5 + + + + + + + + + + + + 5 + + + + + + + + + + + + 5 + + + + + + + + + + + 5 + + + + + + + + + + + + 5 + + + + + + + + + + + + diff --git a/resources/Shaders/ForwardPlus.vert.glsl b/resources/Shaders/ForwardPlus.vert.glsl index cb5f41b3..c51216cb 100644 --- a/resources/Shaders/ForwardPlus.vert.glsl +++ b/resources/Shaders/ForwardPlus.vert.glsl @@ -24,7 +24,7 @@ void main() mat4 boneTransform = mat4(1); - if(BoneWeights[0] > 0.f){ + if(BoneWeights[0] > 0.0f){ boneTransform = BoneWeights[0] * Bones[int(BoneIndices[0])] + BoneWeights[1] * Bones[int(BoneIndices[1])] + BoneWeights[2] * Bones[int(BoneIndices[2])] diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index afb27d69..7be1223a 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -21,7 +21,7 @@ void DrawFinalPass::InitializeShaderPrograms() m_ForwardPlusProgram->Compile(); m_ForwardPlusProgram->Link(); } - +static double tempTime = 0.0; void DrawFinalPass::Draw(RenderScene& scene) { GLERROR("DrawFinalPass::Draw: Pre"); @@ -53,14 +53,14 @@ void DrawFinalPass::Draw(RenderScene& scene) glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); } - + tempTime += 0.01f; //TODO: Fixa så att modelsJobs kan spela upp olika animationer och så att den kan få in en tid istället för 1.0f - Hälsningar Johan och Andreas :) if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { auto animation = modelJob->Model->m_RawModel->m_Skeleton->GetAnimation("running"); if (animation != nullptr) { std::vector frameBones = modelJob->Model->m_RawModel->m_Skeleton->GetFrameBones( *animation, - 1.0f + tempTime ); glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); } diff --git a/src/Engine/Rendering/RawModelCustom.cpp b/src/Engine/Rendering/RawModelCustom.cpp index 3bad2ae5..de87d5fc 100644 --- a/src/Engine/Rendering/RawModelCustom.cpp +++ b/src/Engine/Rendering/RawModelCustom.cpp @@ -129,8 +129,8 @@ void RawModelCustom::ReadMaterialSingle(unsigned int &offset, char* fileData, un unsigned int* nameLengths = (unsigned int*)(fileData + offset); offset += sizeof(unsigned int) * 4; - if (offset + sizeof(float) * 2 > fileByteSize) { - throw Resource::FailedLoadingException("Reading Material specular and reflection values failed"); + if (offset + sizeof(float) * 11 + sizeof(unsigned int) * 2 > fileByteSize) { + throw Resource::FailedLoadingException("Reading Material specular, reflection, color and start and end index values failed"); } newMaterial.SpecularExponent = *(float*)(fileData + offset); @@ -138,9 +138,12 @@ void RawModelCustom::ReadMaterialSingle(unsigned int &offset, char* fileData, un newMaterial.ReflectionFactor = *(float*)(fileData + offset); offset += sizeof(float); - if (offset + sizeof(unsigned int) * 2 > fileByteSize) { - throw Resource::FailedLoadingException("Reading Material start and end index values failed"); - } + memcpy(newMaterial.DiffuseColor, fileData + offset, sizeof(float) * 3); + offset += sizeof(float) * 3; + memcpy(newMaterial.SpecularColor, fileData + offset, sizeof(float) * 3); + offset += sizeof(float) * 3; + memcpy(newMaterial.IncandescenceColor, fileData + offset, sizeof(float) * 3); + offset += sizeof(float) * 3; newMaterial.StartIndex = *(unsigned int*)(fileData + offset); offset += sizeof(unsigned int); @@ -202,7 +205,7 @@ void RawModelCustom::ReadAnimationFile(std::string filePath) if (!in.is_open()) { //throw Resource::FailedLoadingException("Open animation file failed"); - return; // AJABAJA!!!!!!!! + return; } unsigned int fileByteSize = in.tellg(); diff --git a/tools/MayaExporter/MayaExporter/Material.cpp b/tools/MayaExporter/MayaExporter/Material.cpp index 0a897e6e..03521231 100644 --- a/tools/MayaExporter/MayaExporter/Material.cpp +++ b/tools/MayaExporter/MayaExporter/Material.cpp @@ -5,23 +5,30 @@ void Material::grabLambertProperties(MaterialNode& material_node, MFnDependencyN material_node.Name = node.name().asChar(); if (!findColorTexture(material_node, node)) { - MGlobal::displayWarning(MString() + "Material " + node.name() + " has no color texture. Please apply a texture insted of using a value"); + node.findPlug("colorR").getValue(material_node.DiffuseColor[0]); + node.findPlug("colorG").getValue(material_node.DiffuseColor[1]); + node.findPlug("colorB").getValue(material_node.DiffuseColor[2]); + } - if (findIncandescenceTexture(material_node, node)) { - MGlobal::displayWarning(MString() + "Material " + node.name() + " has no Incandescence texture. Please apply a texture insted of using value"); + if (!findIncandescenceTexture(material_node, node)) { + node.findPlug("incandescenceR").getValue(material_node.IncandescenceColor[0]); + node.findPlug("incandescenceG").getValue(material_node.IncandescenceColor[1]); + node.findPlug("incandescenceB").getValue(material_node.IncandescenceColor[2]); } if (findNormalTexture(material_node, node)) { - MGlobal::displayWarning(MString() + "Material " + node.name() + " has no normal texture. Please apply a texture to it"); + MGlobal::displayWarning(MString() + "Material " + node.name() + " has no normal texture."); } } void Material::grabBlinnProperties(MaterialNode& material_node, MFnDependencyNode& node) { - if (findSpecularTexture(material_node, node)) { - MGlobal::displayWarning(MString() + "Material " + node.name() + " has no specular texture. Please apply a specular to it"); + if (!findSpecularTexture(material_node, node)) { + node.findPlug("specularColorR").getValue(material_node.SpecularColor[0]); + node.findPlug("specularColorG").getValue(material_node.SpecularColor[1]); + node.findPlug("specularColorB").getValue(material_node.SpecularColor[2]); } m_Plug = node.findPlug("reflectivity"); @@ -43,8 +50,10 @@ void Material::grabBlinnProperties(MaterialNode& material_node, MFnDependencyNod void Material::grabPhongProperties(MaterialNode& material_node, MFnDependencyNode& node) { - if (findSpecularTexture(material_node, node)) { - MGlobal::displayWarning(MString() + "Material " + node.name() + " has no specular texture. Please apply a specular to it"); + if (!findSpecularTexture(material_node, node)) { + node.findPlug("specularColorR").getValue(material_node.SpecularColor[0]); + node.findPlug("specularColorG").getValue(material_node.SpecularColor[1]); + node.findPlug("specularColorB").getValue(material_node.SpecularColor[2]); } m_Plug = node.findPlug("reflectivity"); @@ -188,20 +197,21 @@ std::vector* Material::DoIt(Mesh mesh) // All materials we care about inherit from Lambert MItDependencyNodes matIt(MFn::kLambert); m_AllMaterials.clear(); - int totalIndices = 0; while (!matIt.isDone()) { MFnDependencyNode MaterialFnDN(matIt.thisNode()); MaterialNode MaterialStorage; bool meshHasMaterial = false; //Mesh Indices is a map with : indices> + int totalIndices = 0; for (auto aMeshMaterial : mesh.Indices) { + MGlobal::displayInfo(MString() + "Material: " + aMeshMaterial.first.c_str() + " " + MaterialFnDN.name().asChar()); if (aMeshMaterial.first.compare(MaterialFnDN.name().asChar()) == 0) { meshHasMaterial = true; MaterialStorage.IndexStart = totalIndices; MaterialStorage.IndexEnd = totalIndices + aMeshMaterial.second.size() - 1; - totalIndices += aMeshMaterial.second.size(); break; } + totalIndices += aMeshMaterial.second.size(); } if (meshHasMaterial) { grabLambertProperties(MaterialStorage, MaterialFnDN); diff --git a/tools/MayaExporter/MayaExporter/Material.h b/tools/MayaExporter/MayaExporter/Material.h index 4b567d8b..341843e8 100644 --- a/tools/MayaExporter/MayaExporter/Material.h +++ b/tools/MayaExporter/MayaExporter/Material.h @@ -19,15 +19,18 @@ public: float ReflectionFactor; float SpecularExponent; + float DiffuseColor[3]{ 1.0f }; unsigned int ColorMapFileLength = 0; std::string ColorMapFile; + float SpecularColor[3]{ 1.0f }; unsigned int SpecularMapFileLength = 0; std::string SpecularMapFile; unsigned int NormalMapFileLength = 0; std::string NormalMapFile; + float IncandescenceColor[3]{ 1.0f }; unsigned int IncandescenceMapFileLength = 0; std::string IncandescenceMapFile; @@ -43,6 +46,9 @@ public: out.write((char*)&SpecularExponent, sizeof(float)); out.write((char*)&ReflectionFactor, sizeof(float)); + out.write((char*)&DiffuseColor, sizeof(float) * 3); + out.write((char*)&SpecularColor, sizeof(float) * 3); + out.write((char*)&IncandescenceColor, sizeof(float) * 3); out.write((char*)&IndexStart, sizeof(unsigned int)); out.write((char*)&IndexEnd, sizeof(unsigned int)); @@ -64,6 +70,9 @@ public: out << "SpecularExponent: " << SpecularExponent << endl; out << "ReflectionFactor: " << ReflectionFactor << endl; + out << "DiffuseColor: " << DiffuseColor[0] << " " << DiffuseColor[1] << " " << DiffuseColor[2] << endl; + out << "SpecularColor: " << SpecularColor[0] << " " << SpecularColor[1] << " " << SpecularColor[2] << endl; + out << "IncandescenceColor: " << IncandescenceColor[0] << " " << IncandescenceColor[1] << " " << IncandescenceColor[2] << endl; out << "IndexStart: " << IndexStart << endl; out << "IndexEnd: " << IndexEnd << endl; diff --git a/tools/MayaExporter/MayaExporter/Menu.cpp b/tools/MayaExporter/MayaExporter/Menu.cpp index 4f32af84..9cc6de91 100644 --- a/tools/MayaExporter/MayaExporter/Menu.cpp +++ b/tools/MayaExporter/MayaExporter/Menu.cpp @@ -104,9 +104,10 @@ Menu::Menu(QDialog* dialog) // Set the layout for our window dialog->setLayout(baseLayout); - for (unsigned int i = 0; i < 3; i++) { - this->AddClipClicked(true); - } + this->AddClipClicked(true); + //for (unsigned int i = 0; i < 3; i++) { + // this->AddClipClicked(true); + //} } diff --git a/tools/MayaExporter/MayaExporter/Mesh.cpp b/tools/MayaExporter/MayaExporter/Mesh.cpp index 26d5c7b2..6af8a557 100644 --- a/tools/MayaExporter/MayaExporter/Mesh.cpp +++ b/tools/MayaExporter/MayaExporter/Mesh.cpp @@ -309,12 +309,22 @@ Mesh MeshClass::GetMeshData(MObjectArray object) if (hasSkin) { + float totalWeight = 0.0f; + unsigned int totalBones = 0; MIntArray jointIDs /* ??? */; weights.selectAncestorLogicalIndex(vertexIndex, weightListObject); weights.getExistingArrayAttributeIndices(jointIDs); for (unsigned int i = 0; i < jointIDs.length() && i < 4; i++) { - thisVertex.BoneIndices[i] = jointIDs[i]; - thisVertex.BoneWeights[i] = weights[i].asFloat(); + if (weights[i].asFloat() > 0.001f) { + thisVertex.BoneIndices[totalBones] = jointIDs[i]; + thisVertex.BoneWeights[totalBones] = weights[i].asFloat(); + totalWeight = totalWeight + weights[i].asFloat(); + totalBones++; + } + } + + for (unsigned int i = 0; i < 4; i++) { + thisVertex.BoneWeights[i] = thisVertex.BoneWeights[i] / totalWeight; } } diff --git a/tools/MayaExporter/MayaExporter/WriteToFile.cpp b/tools/MayaExporter/MayaExporter/WriteToFile.cpp index 10883416..a0368c4f 100644 --- a/tools/MayaExporter/MayaExporter/WriteToFile.cpp +++ b/tools/MayaExporter/MayaExporter/WriteToFile.cpp @@ -25,10 +25,13 @@ bool WriteToFile::ASCIIFilePath(string filePathAndFileName) void WriteToFile::OpenFiles() { - if (binFile) - binFile.open(binFileName, ofstream::binary); - if (ASCIIFile) + if (binFile) { + binFile.open(binFileName, ofstream::binary); + } + if (ASCIIFile){ ASCIIFile.open(ASCIIFileName); + ASCIIFile << std::fixed << std::setprecision(3); + } } void WriteToFile::CloseFiles() diff --git a/tools/MayaExporter/MayaExporter/WriteToFile.h b/tools/MayaExporter/MayaExporter/WriteToFile.h index 91ca7103..216fcfc9 100644 --- a/tools/MayaExporter/MayaExporter/WriteToFile.h +++ b/tools/MayaExporter/MayaExporter/WriteToFile.h @@ -5,6 +5,7 @@ #include "OutputData.h" #include #include +#include using namespace std;