Importing custom mesh without material.

This commit is contained in:
antc13
2016-01-18 19:37:55 +01:00
parent 16c39df439
commit 2a3009212a
21 changed files with 402 additions and 248 deletions
+21 -9
View File
@@ -51,13 +51,15 @@ bool Export::Meshes(std::string pathName, bool selectedOnly)
GetMeshData(node);
}
}
MGlobal::displayInfo(MString() + "nrofmeshes: " + meshes.size());
WriteMeshData(pathName);
return true;
}
bool Export::Materials()
bool Export::Materials(std::string pathName)
{
if (!GetMaterialData())
return false;
WriteMaterialData(pathName);
return true;
}
@@ -73,17 +75,14 @@ bool Export::Animations(std::string pathName, std::vector<AnimationInfo> animInf
MGlobal::displayError(MString() + "Export::Animations() got no pathName. Do not know where to write file");
return false;
}
MGlobal::displayInfo("HALLOOOO");
allBindPoses = m_SkeletonHandler.GetBindPoses();
for (auto clip : animInfo) {
MGlobal::displayInfo("preben");
if (!GetAnimationData(clip)) {
MGlobal::displayError(MString() + "Export::Animations() failed to export " + clip.Name.c_str());
return false;
}
}
MGlobal::displayInfo("ghihgihi");
WriteAnimData(pathName);
return true;
}
@@ -100,11 +99,8 @@ bool Export::GetMeshData(MObject object)
bool Export::GetMaterialData()
{
// Traverse scene and return vector with all materials
std::vector<MaterialNode>* AllMaterials = m_MaterialHandler.DoIt();
AllMaterials = m_MaterialHandler.DoIt();
// Access the colorR component of one material (example)
cout << AllMaterials->at(0).Color[0] << endl;
MGlobal::displayInfo(MString() + AllMaterials->at(0).Color[0]);
return true;
}
@@ -163,6 +159,22 @@ void Export::WriteAnimData(std::string pathName)
MGlobal::displayInfo("Export::WriteAnimData() got called when allBindPoses contained no data, did not write nor created them");
}
void Export::WriteMaterialData(std::string pathName)
{
m_MtrlFile.ASCIIFilePath(pathName +"_mtrl.txt");
m_MtrlFile.binaryFilePath(pathName + ".mtrl");
m_MtrlFile.OpenFiles();
int size = (*AllMaterials).size();
m_MtrlFile.writeToFiles(&size);
for (auto aMaterial : *AllMaterials) {
m_MtrlFile.writeToFiles((OutputData*)&aMaterial);
}
m_MtrlFile.CloseFiles();
}
Export::~Export()
{
/* delete m_MaterialHandler;
+5 -1
View File
@@ -23,7 +23,7 @@ public:
};
bool Meshes(std::string pathName, bool selectedOnly = false);
bool Materials();
bool Materials(std::string pathName);
bool Animations(std::string pathName, std::vector<AnimationInfo> animInfo);
private:
@@ -33,6 +33,7 @@ private:
void WriteMeshData(std::string pathName);
void WriteAnimData(std::string pathName);
void WriteMaterialData(std::string pathName);
Material m_MaterialHandler;
Skeleton m_SkeletonHandler;
@@ -42,6 +43,7 @@ private:
//File export
WriteToFile m_MeshFile;
WriteToFile m_AnimFile;
WriteToFile m_MtrlFile;
//Mesh Data
std::vector<Mesh> meshes;
@@ -50,5 +52,7 @@ private:
std::vector<BindPoseSkeletonNode> allBindPoses;
std::vector<Animation> allAnimations;
//Material Data
std::vector<MaterialNode>* AllMaterials;
};
#endif
+24 -61
View File
@@ -4,57 +4,24 @@ void Material::grabLambertProperties(MaterialNode& material_node, MFnDependencyN
{
material_node.Name = node.name().asChar();
if (findColorTexture(material_node, node)) {
material_node.Color.fill(1.0f);
}
else {
m_Plug = node.findPlug("colorR");
m_Plug.getValue(material_node.Color[0]);
m_Plug = node.findPlug("colorG");
m_Plug.getValue(material_node.Color[1]);
m_Plug = node.findPlug("colorB");
m_Plug.getValue(material_node.Color[2]);
if (!findColorTexture(material_node, node)) {
MGlobal::displayWarning(MString() + "Material " + node.name() + " has no color texture. Please apply a texture insted of using a value");
}
float TempTransp[3];
m_Plug = node.findPlug("transparencyR");
m_Plug.getValue(TempTransp[0]);
m_Plug = node.findPlug("transparencyG");
m_Plug.getValue(TempTransp[1]);
m_Plug = node.findPlug("transparencyB");
m_Plug.getValue(TempTransp[2]);
MColor TranspNode(TempTransp[0], TempTransp[1], TempTransp[2]);
float DummyH, DummyS;
TranspNode.get(MColor::kHSV, DummyH, DummyS, material_node.Color[3]);
}
if (findIncandescenceTexture(material_node, node)) {
material_node.Incandescence.fill(1.0f);
}
else {
m_Plug = node.findPlug("incandescenceR");
m_Plug.getValue(material_node.Incandescence[0]);
m_Plug = node.findPlug("incandescenceG");
m_Plug.getValue(material_node.Incandescence[1]);
m_Plug = node.findPlug("incandescenceB");
m_Plug.getValue(material_node.Incandescence[2]);
MGlobal::displayWarning(MString() + "Material " + node.name() + " has no Incandescence texture. Please apply a texture insted of using value");
}
findNormalTexture(material_node, node);
if (findNormalTexture(material_node, node)) {
MGlobal::displayWarning(MString() + "Material " + node.name() + " has no normal texture. Please apply a texture to it");
}
}
void Material::grabBlinnProperties(MaterialNode& material_node, MFnDependencyNode& node)
{
if (findSpecularTexture(material_node, node)) {
material_node.Specular.fill(1.0f);
}
else {
m_Plug = node.findPlug("specularColorR");
m_Plug.getValue(material_node.Specular[0]);
m_Plug = node.findPlug("specularColorG");
m_Plug.getValue(material_node.Specular[1]);
m_Plug = node.findPlug("specularColorB");
m_Plug.getValue(material_node.Specular[2]);
MGlobal::displayWarning(MString() + "Material " + node.name() + " has no specular texture. Please apply a specular to it");
}
m_Plug = node.findPlug("reflectivity");
@@ -76,17 +43,9 @@ void Material::grabBlinnProperties(MaterialNode& material_node, MFnDependencyNod
void Material::grabPhongProperties(MaterialNode& material_node, MFnDependencyNode& node)
{
if (findSpecularTexture(material_node, node)) {
material_node.Specular.fill(1.0f);
}
else {
m_Plug = node.findPlug("specularColorR");
m_Plug.getValue(material_node.Specular[0]);
m_Plug = node.findPlug("specularColorG");
m_Plug.getValue(material_node.Specular[1]);
m_Plug = node.findPlug("specularColorB");
m_Plug.getValue(material_node.Specular[2]);
}
if (findSpecularTexture(material_node, node)) {
MGlobal::displayWarning(MString() + "Material " + node.name() + " has no specular texture. Please apply a specular to it");
}
m_Plug = node.findPlug("reflectivity");
m_Plug.getValue(material_node.ReflectionFactor);
@@ -109,8 +68,10 @@ bool Material::findColorTexture(MaterialNode& material_node, MFnDependencyNode&
std::string FullPath = TextureNode.findPlug("ftn").asString().asChar();
m_TexturePaths.push_back(FullPath);
material_node.ColorMapFile = FullPath.substr(FullPath.find_last_of("/"));
FullPath = FullPath.substr(FullPath.find_last_of("/") + 1);
material_node.ColorMapFile = FullPath.erase(FullPath.find_last_of("."), FullPath.find_last_of(".") - FullPath.size());
material_node.ColorMapFileLength = material_node.ColorMapFile.length() + 1;
// Test
MGlobal::displayInfo(MString() + "Texture file: " + FullPath.c_str());
return true;
@@ -140,8 +101,9 @@ bool Material::findNormalTexture(MaterialNode& material_node, MFnDependencyNode&
std::string FullPath = TextureNode.findPlug("ftn").asString().asChar();
m_TexturePaths.push_back(FullPath);
material_node.NormalMapFile = FullPath.substr(FullPath.find_last_of("/"));
FullPath = FullPath.substr(FullPath.find_last_of("/") + 1);
material_node.NormalMapFile = FullPath.erase(FullPath.find_last_of("."), FullPath.find_last_of(".") - FullPath.size());
material_node.NormalMapFileLength = material_node.NormalMapFile.length() + 1;
return true;
}
}
@@ -165,8 +127,9 @@ bool Material::findSpecularTexture(MaterialNode& material_node, MFnDependencyNod
std::string FullPath = TextureNode.findPlug("ftn").asString().asChar();
m_TexturePaths.push_back(FullPath);
material_node.SpecularMapFile = FullPath.substr(FullPath.find_last_of("/"));
FullPath = FullPath.substr(FullPath.find_last_of("/") + 1);
material_node.SpecularMapFile = FullPath.erase(FullPath.find_last_of("."), FullPath.find_last_of(".") - FullPath.size());
material_node.SpecularMapFileLength = material_node.SpecularMapFile.length() + 1;
return true;
}
}
@@ -187,8 +150,9 @@ bool Material::findIncandescenceTexture(MaterialNode& material_node, MFnDependen
std::string FullPath = TextureNode.findPlug("ftn").asString().asChar();
m_TexturePaths.push_back(FullPath);
material_node.SpecularMapFile = FullPath.substr(FullPath.find_last_of("/"));
FullPath = FullPath.substr(FullPath.find_last_of("/") + 1);
material_node.IncandescenceMapFile = FullPath.erase(FullPath.find_last_of("."), FullPath.find_last_of(".") - FullPath.size());
material_node.IncandescenceMapFileLength = material_node.IncandescenceMapFile.length() + 1;
return true;
}
}
@@ -206,7 +170,7 @@ std::vector<MaterialNode>* Material::DoIt()
{
// All materials we care about inherit from Lambert
MItDependencyNodes matIt(MFn::kLambert);
m_AllMaterials.clear();
while (!matIt.isDone()) {
MFnDependencyNode MaterialFnDN(matIt.thisNode());
MaterialNode MaterialStorage;
@@ -226,7 +190,6 @@ std::vector<MaterialNode>* Material::DoIt()
else if (matIt.thisNode().hasFn(MFn::kLambert)) {
grabLambertProperties(MaterialStorage, MaterialFnDN);
MaterialStorage.Specular.fill(0.0f);
MaterialStorage.ReflectionFactor = 0.0f;
MaterialStorage.SpecularExponent = 0.0f;
+64 -5
View File
@@ -5,21 +5,80 @@
#include <array>
#include <algorithm>
#include <cmath>
#include <map>
#include "MayaIncludes.h"
#include "OutputData.h"
#include "Mesh.h"
struct MaterialNode
class MaterialNode : public OutputData
{
public:
std::string Name;
std::array<float, 4> Color;
std::array<float, 3> Incandescence;
std::array<float, 3> Specular;
float ReflectionFactor;
float SpecularExponent;
float SpecularExponent;
unsigned int ColorMapFileLength = 0;
std::string ColorMapFile;
unsigned int SpecularMapFileLength = 0;
std::string SpecularMapFile;
unsigned int NormalMapFileLength = 0;
std::string NormalMapFile;
unsigned int IncandescenceMapFileLength = 0;
std::string IncandescenceMapFile;
unsigned int IndexStart;
unsigned int IndexEnd;
virtual void WriteBinary(std::ostream& out)
{
out.write((char*)&ColorMapFileLength, sizeof(unsigned int));
out.write((char*)&NormalMapFileLength, sizeof(unsigned int));
out.write((char*)&SpecularMapFileLength, sizeof(unsigned int));
out.write((char*)&IncandescenceMapFileLength, sizeof(unsigned int));
out.write((char*)&SpecularExponent, sizeof(float));
out.write((char*)&ReflectionFactor, sizeof(float));
out.write((char*)&IndexStart, sizeof(unsigned int));
out.write((char*)&IndexEnd, sizeof(unsigned int));
out.write(ColorMapFile.c_str(), ColorMapFileLength);
out.write(NormalMapFile.c_str(), NormalMapFileLength);
out.write(SpecularMapFile.c_str(), SpecularMapFileLength);
out.write(IncandescenceMapFile.c_str(), IncandescenceMapFileLength);
}
virtual void WriteASCII(std::ostream& out) const
{
out << "New Material _ not in binary" << endl;
out << "number of indices: " << Name << " _ not in binary" << endl;
out << "ColorMapFile length: " << ColorMapFileLength << endl;
out << "NormalMapFile length: " << NormalMapFileLength << endl;
out << "SpecularMapFile length: " << SpecularMapFileLength << endl;
out << "IncandescenceMapFile length: " << IncandescenceMapFileLength << endl;
out << "SpecularExponent: " << SpecularExponent << endl;
out << "ReflectionFactor: " << ReflectionFactor << endl;
out << "IndexStart: " << IndexStart << endl;
out << "IndexEnd: " << IndexEnd << endl;
out << "ColorMapFile length: " << ColorMapFileLength << endl;
if (ColorMapFileLength > 0)
out << "ColorMapFile: " << ColorMapFile << endl;
if (NormalMapFileLength > 0)
out << "NormalMapFile: " << NormalMapFile << endl;
if (SpecularMapFileLength > 0)
out << "SpecularMapFile: " << SpecularMapFile << endl;
if (IncandescenceMapFileLength > 0)
out << "IncandescenceMapFile: " << IncandescenceMapFile << endl;
}
};
class Material
+12 -13
View File
@@ -24,16 +24,15 @@ Menu::Menu(QDialog* dialog)
m_ExportSelectedButton = new QCheckBox(tr("&Export Selected"));
m_ExportAnimationsButton = new QCheckBox(tr("&Export Animations"));
m_CopyTexturesButton = new QCheckBox(tr("&Copy Textures"));
m_Button3 = new QCheckBox(tr("Test Materials"));
m_ExportMaterialButton = new QCheckBox(tr("&Export Material"));;
m_ExportAnimationsButton->setChecked(true);
m_CopyTexturesButton->setChecked(true);
m_ExportMaterialButton->setChecked(true);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(m_ExportSelectedButton);
vbox->addWidget(m_ExportAnimationsButton);
vbox->addWidget(m_CopyTexturesButton);
vbox->addWidget(m_Button3);
vbox->addWidget(m_ExportMaterialButton);
vbox->addStretch(1);
optionsBox->setLayout(vbox);
@@ -46,8 +45,7 @@ Menu::Menu(QDialog* dialog)
connect(m_ExportSelectedButton, SIGNAL(clicked(bool)), this, SLOT(NULL));
connect(m_ExportAnimationsButton, SIGNAL(clicked(bool)), this, SLOT(NULL));
connect(m_CopyTexturesButton, SIGNAL(clicked(bool)), this, SLOT(NULL));
connect(m_Button3, SIGNAL(clicked(bool)), this, SLOT(NULL));
connect(m_ExportMaterialButton, SIGNAL(clicked(bool)), this, SLOT(NULL));
// Creating several layouts, adding widgets & adding them to one layout in the end
QHBoxLayout* topLayout = new QHBoxLayout;
@@ -178,20 +176,14 @@ void Menu::ExportAll(bool)
}
std::vector<Export::AnimationInfo> animations;
MGlobal::displayInfo(MString() + "yeeehaa");
for (unsigned int i = 0; i < m_AnimationClipName.size(); i++) {
Export::AnimationInfo thisClip;
MGlobal::displayInfo(MString() + "qwqw");
thisClip.Name = std::string(m_AnimationClipName[i]->text().toLocal8Bit().constData());
MGlobal::displayInfo(MString() + "shizzzz");
thisClip.Start = m_StartFrameLines[i]->text().toInt();
MGlobal::displayInfo(MString() + "dsdsfg");
thisClip.End = m_EndFrameLines[i]->text().toInt();
MGlobal::displayInfo(MString() + "aaaaaaaaaaaaaaaaaaaa");
animations.push_back(thisClip);
}
MGlobal::displayInfo(MString() + "asdf");
if (m_ExportAnimationsButton->isChecked()) {
//Export Animations
if (!m_Export.Animations(m_ExportPath->text().toLocal8Bit().constData(), animations)) {
@@ -199,6 +191,13 @@ void Menu::ExportAll(bool)
return;
}
}
if (m_ExportMaterialButton->isChecked()) {
if (!m_Export.Materials(m_ExportPath->text().toLocal8Bit().constData())){
MGlobal::displayError(MString() + "Could not export materials");
return;
}
}
}
void Menu::CancelClicked(bool)
+1 -2
View File
@@ -65,8 +65,7 @@ private:
QCheckBox* m_ExportSelectedButton = nullptr;
QCheckBox* m_ExportAnimationsButton = nullptr;
QCheckBox* m_CopyTexturesButton = nullptr;
QCheckBox* m_Button3 = nullptr;
QCheckBox* m_ExportMaterialButton = nullptr;
QLineEdit* m_ExportPath = nullptr;
QFileDialog* m_FileDialog = nullptr;
+140 -66
View File
@@ -76,8 +76,8 @@ std::map<int, MeshClass::WeightInfo> MeshClass::GetWeightData()
Mesh MeshClass::GetMeshData(MObject object)
{
Mesh newMesh;
std::vector<VertexLayout>& vertexList = newMesh.Vertices;
std::vector<int>& indexList = newMesh.Indices;
vector<VertexLayout>& vertexList = newMesh.Vertices;
map<string, vector<int>>& indexLists = newMesh.Indices;
// In here, we retrieve triangulated polygons from the mesh
MFnMesh mesh(object);
@@ -91,11 +91,28 @@ Mesh MeshClass::GetMeshData(MObject object)
float2 UV;
double biTangent[3];
double biNormal[3];
VertexLayout thisVertex;
MFloatVectorArray Tangents;
MFloatVectorArray biNormals;
std::map<int, WeightInfo> vertexWeights = GetWeightData();
MObjectArray shaderList;
MIntArray shaderIndexList;
mesh.getConnectedShaders(0, shaderList, shaderIndexList);
map<string, vector<int>> materialFaceIDs;
MGlobal::displayInfo(MString() + "shaderIndexList: " + shaderIndexList.length());
MGlobal::displayInfo(MString() + "shaderList: " + shaderList.length());
MPlugArray plugArray;
for (int i = 0; i < shaderIndexList.length(); i++)
{
MFnDependencyNode shader(shaderList[shaderIndexList[i]]);
MPlug p_Plug = shader.findPlug("surfaceShader");
if (p_Plug.connectedTo(plugArray, true, false)) {
MFnDependencyNode node = plugArray[0].node();
materialFaceIDs[node.name().asChar()].push_back(i);
}
}
map<int, WeightInfo> vertexWeights = GetWeightData();
mesh.getTangents(Tangents, MSpace::kObject, NULL);
mesh.getBinormals(biNormals, MSpace::kObject, NULL);
@@ -103,82 +120,139 @@ Mesh MeshClass::GetMeshData(MObject object)
MItMeshFaceVertex faceVert(object);
int intDummy = 0;
MItMeshPolygon meshPolyIter(object);
for (MItMeshPolygon meshPolyIter(object); !meshPolyIter.isDone(); meshPolyIter.next()) {
for (auto aMaterial : materialFaceIDs) {
for (auto faceID : aMaterial.second) {
vector<unsigned int> localVertexToGlobalIndex;
unsigned int indexOffset = vertexList.size();
vector<array<unsigned int, 2>> localVertexToGlobalIndex;
meshPolyIter.setIndex(faceID, intDummy);
meshPolyIter.getVertices(vertices);
meshPolyIter.getTriangles(dummy, triangleList);
meshPolyIter.getVertices(vertices);
meshPolyIter.getTriangles(dummy, triangleList);
//MGlobal::displayInfo("Befor Second Loop");
for (unsigned int i = 0; i < vertices.length(); i++) {
VertexLayout thisVertex;
vertexIndex = meshPolyIter.vertexIndex(i);
faceVert.setIndex(meshPolyIter.index(), i, intDummy, intDummy);
//MGlobal::displayInfo("In Second Loop");
pos = faceVert.position();
if (abs(pos.x) > 0.0001)
thisVertex.Pos[0] = pos.x;
if (abs(pos.y) > 0.0001)
thisVertex.Pos[1] = pos.y;
if (abs(pos.z) > 0.0001)
thisVertex.Pos[2] = pos.z;
//MGlobal::displayInfo("Befor Second Loop");
for (unsigned int i = 0; i < vertices.length(); i++) {
vertexIndex = meshPolyIter.vertexIndex(i);
faceVert.setIndex(meshPolyIter.index(), i, intDummy, intDummy);
//MGlobal::displayInfo("In Second Loop");
faceVert.position().get(thisVertex.Pos);
faceVert.getNormal(normal);
thisVertex.Normal[0] = normal[0];
thisVertex.Normal[1] = normal[1];
thisVertex.Normal[2] = normal[2];
faceVert.getNormal(normal);
if (abs(normal[0]) > 0.0001)
thisVertex.Normal[0] = normal[0];
if (abs(normal[1]) > 0.0001)
thisVertex.Normal[1] = normal[1];
if (abs(normal[2]) > 0.0001)
thisVertex.Normal[2] = normal[2];
MFloatVector Tangent = Tangents[faceVert.tangentId()];
//MVector tmp = faceVert.getTangent(MSpace::kObject, NULL);
//tmp.get(biTangent);
thisVertex.Tangent[0] = Tangent[0];
thisVertex.Tangent[1] = Tangent[1];
thisVertex.Tangent[2] = Tangent[2];
MFloatVector Tangent = Tangents[faceVert.tangentId()];
//MVector tmp = faceVert.getTangent(MSpace::kObject, NULL);
//tmp.get(biTangent);
if (abs(Tangent[0]) > 0.0001)
thisVertex.Tangent[0] = Tangent[0];
if (abs(Tangent[1]) > 0.0001)
thisVertex.Tangent[1] = Tangent[1];
if (abs(Tangent[2]) > 0.0001)
thisVertex.Tangent[2] = Tangent[2];
MFloatVector biNormal = biNormals[faceVert.tangentId()];
//faceVert.getBinormal().get(biNormal);
thisVertex.BiNormal[0] = biNormal[0];
thisVertex.BiNormal[1] = biNormal[1];
thisVertex.BiNormal[2] = biNormal[2];
MFloatVector biNormal = biNormals[faceVert.tangentId()];
//faceVert.getBinormal().get(biNormal);
if (abs(biNormal[0]) > 0.0001)
thisVertex.BiNormal[0] = biNormal[0];
if (abs(biNormal[1]) > 0.0001)
thisVertex.BiNormal[1] = biNormal[1];
if (abs(biNormal[2]) > 0.0001)
thisVertex.BiNormal[2] = biNormal[2];
faceVert.getUV(UV);
thisVertex.Uv[0] = UV[0];
thisVertex.Uv[1] = UV[1];
faceVert.getUV(UV);
thisVertex.Uv[0] = UV[0];
thisVertex.Uv[1] = UV[1];
thisVertex.BoneIndices[0] = vertexWeights[faceVert.vertId()].BoneIndices[0];
thisVertex.BoneIndices[1] = vertexWeights[faceVert.vertId()].BoneIndices[1];
thisVertex.BoneIndices[2] = vertexWeights[faceVert.vertId()].BoneIndices[2];
thisVertex.BoneIndices[3] = vertexWeights[faceVert.vertId()].BoneIndices[3];
thisVertex.BoneIndices[0] = vertexWeights[faceVert.vertId()].BoneIndices[0];
thisVertex.BoneIndices[1] = vertexWeights[faceVert.vertId()].BoneIndices[1];
thisVertex.BoneIndices[2] = vertexWeights[faceVert.vertId()].BoneIndices[2];
thisVertex.BoneIndices[3] = vertexWeights[faceVert.vertId()].BoneIndices[3];
thisVertex.BoneWeights[0] = vertexWeights[faceVert.vertId()].BoneWeights[0];
thisVertex.BoneWeights[1] = vertexWeights[faceVert.vertId()].BoneWeights[1];
thisVertex.BoneWeights[2] = vertexWeights[faceVert.vertId()].BoneWeights[2];
thisVertex.BoneWeights[3] = vertexWeights[faceVert.vertId()].BoneWeights[3];
if (abs(vertexWeights[faceVert.vertId()].BoneWeights[0]) > 0.0001)
thisVertex.BoneWeights[0] = vertexWeights[faceVert.vertId()].BoneWeights[0];
if (abs(vertexWeights[faceVert.vertId()].BoneWeights[1]) > 0.0001)
thisVertex.BoneWeights[1] = vertexWeights[faceVert.vertId()].BoneWeights[1];
if (abs(vertexWeights[faceVert.vertId()].BoneWeights[2]) > 0.0001)
thisVertex.BoneWeights[2] = vertexWeights[faceVert.vertId()].BoneWeights[2];
if (abs(vertexWeights[faceVert.vertId()].BoneWeights[3]) > 0.0001)
thisVertex.BoneWeights[3] = vertexWeights[faceVert.vertId()].BoneWeights[3];
std::vector<VertexLayout>::iterator it = std::find(vertexList.begin(), vertexList.end(), thisVertex);
if (it != vertexList.end()) {
localVertexToGlobalIndex.push_back(vertexIndex);
} else {
localVertexToGlobalIndex.push_back(vertexIndex);
vertexList.push_back(thisVertex);
}
//cout << "Pos: " << thisVertex.Pos[0] << "/" << thisVertex.Pos[1] << "/" << thisVertex.Pos[2] << endl;
//cout << "Normals: " << thisVertex.Normal[0] << "/" << thisVertex.Normal[1] << "/" << thisVertex.Normal[2] << endl;
//cout << "Bi-Normals: " << thisVertex.BiNormal[0] << "/" << thisVertex.BiNormal[1] << "/" << thisVertex.BiNormal[2] << endl;
//cout << "Bi-Tangents: " << thisVertex.BiTangent[0] << "/" << thisVertex.BiTangent[1] << "/" << thisVertex.BiTangent[2] << endl;
//cout << "UV: " << thisVertex.Uv[0] << "/" << thisVertex.Uv[1] << endl;
}
for (unsigned int i = 0; i < triangleList.length(); i++) {
unsigned int k = 0;
if (localVertexToGlobalIndex.size() > 0) {
while (localVertexToGlobalIndex[k] != triangleList[i] && k < localVertexToGlobalIndex.size()) {
k++;
float totalWeight = thisVertex.BoneWeights[0] + thisVertex.BoneWeights[1] + thisVertex.BoneWeights[2] + thisVertex.BoneWeights[3];
if (totalWeight < 1.00f && totalWeight > 0.01f) {
thisVertex.BoneWeights[0] /= totalWeight;
thisVertex.BoneWeights[1] /= totalWeight;
thisVertex.BoneWeights[2] /= totalWeight;
thisVertex.BoneWeights[3] /= totalWeight;
}
indexList.push_back(indexOffset + k);
}
std::vector<VertexLayout>::iterator it = std::find(vertexList.begin(), vertexList.end(), thisVertex);
array<unsigned int, 2> tmp;
if (it != vertexList.end()) {
tmp[0] = vertexIndex;
tmp[1] = it - vertexList.begin();
localVertexToGlobalIndex.push_back(tmp);
} else {
tmp[0] = vertexIndex;
tmp[1] = vertexList.size();
localVertexToGlobalIndex.push_back(tmp);
vertexList.push_back(thisVertex);
}
//MGlobal::displayInfo(MString() + "localVertexToGlobalIndex[localVertexToGlobalIndex.size()-1]: " + localVertexToGlobalIndex[localVertexToGlobalIndex.size()-1][0] + " " + localVertexToGlobalIndex[localVertexToGlobalIndex.size()-1][1]);
//cout << "Pos: " << thisVertex.Pos[0] << "/" << thisVertex.Pos[1] << "/" << thisVertex.Pos[2] << endl;
//cout << "Normals: " << thisVertex.Normal[0] << "/" << thisVertex.Normal[1] << "/" << thisVertex.Normal[2] << endl;
//cout << "Bi-Normals: " << thisVertex.BiNormal[0] << "/" << thisVertex.BiNormal[1] << "/" << thisVertex.BiNormal[2] << endl;
//cout << "Bi-Tangents: " << thisVertex.BiTangent[0] << "/" << thisVertex.BiTangent[1] << "/" << thisVertex.BiTangent[2] << endl;
//cout << "UV: " << thisVertex.Uv[0] << "/" << thisVertex.Uv[1] << endl;
}
for (unsigned int i = 0; i < triangleList.length(); i++) {
unsigned int k = 0;
if (localVertexToGlobalIndex.size() > 0) {
//MGlobal::displayInfo(MString() + "triangleList[i] : " + triangleList[i]);
while (localVertexToGlobalIndex[k][0] != triangleList[i] && k < localVertexToGlobalIndex.size()) {
k++;
}
//MGlobal::displayInfo(MString() + "localVertexToGlobalIndex[k] : " + localVertexToGlobalIndex[k][0] + " " + localVertexToGlobalIndex[k][1]);
indexLists[aMaterial.first.c_str()].push_back(localVertexToGlobalIndex[k][1]);
}
}
}
// MGlobal::displayInfo( MString() + "localVertexToGlobalIndex.size(): " + localVertexToGlobalIndex.size());
// if (localVertexToGlobalIndex.size() > 0) {
// MGlobal::displayInfo(MString() + "triangleList.length(): " + triangleList.length());
// for (unsigned int i = triangleList.length() - 1; i >= 0; i--) {
// MGlobal::displayInfo(MString() + "i: " + i);
// unsigned int k = localVertexToGlobalIndex.size() - 1;
// MGlobal::displayInfo(MString() + "triangleList[i] : " + triangleList[i]);
// while (localVertexToGlobalIndex[k] != triangleList[i] && k >= 0) {
// MGlobal::displayInfo(MString() + "k: " + k);
// k--;
// }
// MGlobal::displayInfo(MString() + "localVertexToGlobalIndex[k] : " + localVertexToGlobalIndex[k]);
// indexList.push_back(indexOffset + k);
// }
// }
}
newMesh.NumIndices = newMesh.Indices.size();
int totalIndecies = 0;
for (auto aList : newMesh.Indices) {
totalIndecies += aList.second.size();
}
newMesh.NumIndices = totalIndecies;
newMesh.NumVertices = newMesh.Vertices.size();
return newMesh;
+22 -17
View File
@@ -3,6 +3,7 @@
#include <map>
#include <vector>
#include <array>
#include "OutputData.h"
#include "MayaIncludes.h"
@@ -10,20 +11,20 @@
class VertexLayout : public OutputData
{
public:
float Pos[3];
float Normal[3];
float Tangent[3];
float BiNormal[3];
float Uv[2];
float BoneIndices[4];
float BoneWeights[4];
float Pos[3]{ 0 };
float Normal[3]{ 0 };
float Tangent[3]{ 0 };
float BiNormal[3]{ 0 };
float Uv[2]{ 0 };
float BoneIndices[4]{ 0 };
float BoneWeights[4]{ 0 };
virtual void WriteBinary(std::ostream& out)
{
out.write((char*)&Pos, sizeof(float) * 3);
out.write((char*)&Normal, sizeof(float) * 3);
out.write((char*)&Tangent, sizeof(float) * 3);
out.write((char*)&BiTangent, sizeof(float) * 3);
out.write((char*)&BiNormal, sizeof(float) * 3);
out.write((char*)&Uv, sizeof(float) * 2);
out.write((char*)&BoneIndices, sizeof(float) * 4);
out.write((char*)&BoneWeights, sizeof(float) * 4);
@@ -34,7 +35,7 @@ public:
out << Pos[0] << " " << Pos[1] << " " << Pos[2] << endl;
out << Normal[0] << " " << Normal[1] << " " << Normal[2] << endl;
out << Tangent[0] << " " << Tangent[1] << " " << Tangent[2] << endl;
out << BiTangent[0] << " " << BiTangent[1] << " " << BiTangent[2] << endl;
out << BiNormal[0] << " " << BiNormal[1] << " " << BiNormal[2] << endl;
out << Uv[0] << " " << Uv[1] << endl;
out << BoneIndices[0] << " " << BoneIndices[1] << " " << BoneIndices[2] << " " << BoneIndices[3] << endl;
out << BoneWeights[0] << " " << BoneWeights[1] << " " << BoneWeights[2] << " " << BoneWeights[3] << endl;
@@ -46,7 +47,7 @@ public:
this->Pos[0] == right.Pos[0] && this->Pos[1] == right.Pos[1] && this->Pos[2] == right.Pos[2] &&
this->Normal[0] == right.Normal[0] && this->Normal[1] == right.Normal[1] && this->Normal[2] == right.Normal[2] &&
this->Tangent[0] == right.Tangent[0] && this->Tangent[1] == right.Tangent[1] && this->Tangent[2] == right.Tangent[2] &&
this->BiTangent[0] == right.BiTangent[0] && this->BiTangent[1] == right.BiTangent[1] && this->BiTangent[2] == right.BiTangent[2] &&
this->BiNormal[0] == right.BiNormal[0] && this->BiNormal[1] == right.BiNormal[1] && this->BiNormal[2] == right.BiNormal[2] &&
this->Uv[0] == right.Uv[0] && this->Uv[1] == right.Uv[1] &&
this->BoneIndices[0] == right.BoneIndices[0] && this->BoneIndices[1] == right.BoneIndices[1] && this->BoneIndices[2] == right.BoneIndices[2] && this->BoneIndices[3] == right.BoneIndices[3] &&
this->BoneWeights[0] == right.BoneWeights[0] && this->BoneWeights[1] == right.BoneWeights[1] && this->BoneWeights[2] == right.BoneWeights[2] && this->BoneWeights[3] == right.BoneWeights[3]
@@ -56,10 +57,10 @@ public:
class Mesh : public OutputData {
public:
int NumVertices;
int NumIndices;
unsigned int NumVertices;
unsigned int NumIndices;
std::vector<VertexLayout> Vertices;
std::vector<int> Indices;
std::map<std::string, std::vector<int>> Indices;
virtual void WriteBinary(std::ostream& out)
{
@@ -69,7 +70,7 @@ public:
aVertex.WriteBinary(out);
}
for (auto aIndex : Indices) {
out.write((char*)&aIndex, sizeof(int));
out.write((char*)aIndex.second.data(), sizeof(int) * aIndex.second.size());
}
}
@@ -80,12 +81,16 @@ public:
out << "number of indices: " << NumIndices << endl;
int vertexNumber = 0;
for (auto aVertex : Vertices) {
out << "New vertex number: " << vertexNumber << "_ not in binary" << endl;
out << "New vertex number: " << vertexNumber << " _ not in binary" << endl;
aVertex.WriteASCII(out);
vertexNumber++;
}
for (int i = 0; i < NumIndices; i += 3) {
out << Indices[i] << " " << Indices[i+1] << " " << Indices[i + 2] << endl;
out << "New vertex Triangels: " << NumIndices/3 << " _ not in binary" << endl;
for (auto aIndexList : Indices) {
out << "Using Material: " << aIndexList.first << " _ not in binary" << endl;
for (int i = 0; i < aIndexList.second.size(); i += 3) {
out << aIndexList.second[i] << " " << aIndexList.second[i+1] << " " << aIndexList.second[i + 2] << endl;
}
}
}
};