2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-06-27 14:51:58 +00:00

Export vertex weights for xmodel export

This commit is contained in:
Jan
2021-08-13 17:33:13 +02:00
parent 767daca2ea
commit 0478a88d15
10 changed files with 344 additions and 134 deletions

View File

@ -1,63 +0,0 @@
#include "VertexMerger.h"
#include <cmath>
#include <limits>
bool operator==(const VertexMergerPos& lhs, const VertexMergerPos& rhs)
{
return std::fabs(lhs.x - rhs.x) < std::numeric_limits<float>::epsilon()
&& std::fabs(lhs.y - rhs.y) < std::numeric_limits<float>::epsilon()
&& std::fabs(lhs.z - rhs.z) < std::numeric_limits<float>::epsilon();
}
bool operator!=(const VertexMergerPos& lhs, VertexMergerPos& rhs)
{
return !(lhs == rhs);
}
bool operator<(const VertexMergerPos& lhs, const VertexMergerPos& rhs)
{
return std::tie(lhs.x, lhs.y, lhs.z) < std::tie(rhs.x, rhs.y, rhs.z);
}
/*
VertexMerger::VertexMerger()
: m_current_vertex_index(0)
{
}
VertexMerger::VertexMerger(const size_t totalVertexCount)
: m_current_vertex_index(0)
{
m_position_lookup.reserve(totalVertexCount);
}
void VertexMerger::ProcessVertex(VertexPos pos)
{
const auto mapEntry = m_vertex_index_map.find(pos);
if (mapEntry == m_vertex_index_map.end())
{
m_position_lookup.push_back(m_current_vertex_index);
m_unique_vertex_position_indices.push_back(m_current_vertex_index);
m_vertex_index_map.emplace(std::make_pair(pos, m_current_vertex_index));
}
else
{
m_position_lookup.push_back(mapEntry->second);
}
m_current_vertex_index++;
}
size_t VertexMerger::GetUniqueVertexIndexForVertexIndex(const size_t vertexIndex)
{
if (vertexIndex >= m_position_lookup.size())
return 0;
return m_position_lookup[vertexIndex];
}
const std::vector<size_t>& VertexMerger::GetUniqueVertexIndices() const
{
return m_unique_vertex_position_indices;
}
*/

View File

@ -1,51 +0,0 @@
#pragma once
#include "Utils/DistinctMapper.h"
struct VertexMergerPos
{
float x;
float y;
float z;
friend bool operator==(const VertexMergerPos& lhs, const VertexMergerPos& rhs);
friend bool operator!=(const VertexMergerPos& lhs, const VertexMergerPos& rhs);
friend bool operator<(const VertexMergerPos& lhs, const VertexMergerPos& rhs);
};
typedef DistinctMapper<VertexMergerPos> VertexMerger;
/*
#include <map>
#include <vector>
#include "Utils/ClassUtils.h"
class VertexMerger
{
public:
struct VertexPos
{
float x;
float y;
float z;
friend bool operator==(const VertexPos& lhs, const VertexPos& rhs);
friend bool operator!=(const VertexPos& lhs, const VertexPos& rhs);
friend bool operator<(const VertexPos& lhs, const VertexPos& rhs);
};
VertexMerger();
explicit VertexMerger(size_t totalVertexCount);
void ProcessVertex(VertexPos pos);
size_t GetUniqueVertexIndexForVertexIndex(size_t vertexIndex);
_NODISCARD const std::vector<size_t>& GetUniqueVertexIndices() const;
private:
size_t m_current_vertex_index;
std::vector<size_t> m_unique_vertex_position_indices;
std::vector<size_t> m_position_lookup;
std::map<VertexPos, int> m_vertex_index_map;
};
*/

View File

@ -1,5 +1,8 @@
#include "XModelCommon.h"
#include <cmath>
#include <limits>
void XModelMaterial::ApplyDefaults()
{
// Phong = Color, Bump, Spec, CosinePower
@ -42,3 +45,56 @@ void XModelMaterial::ApplyDefaults()
blinn[1] = -1;
phong = -1;
}
bool operator==(const VertexMergerPos& lhs, const VertexMergerPos& rhs)
{
const auto coordinatesMatch = std::fabs(lhs.x - rhs.x) < std::numeric_limits<float>::epsilon()
&& std::fabs(lhs.y - rhs.y) < std::numeric_limits<float>::epsilon()
&& std::fabs(lhs.z - rhs.z) < std::numeric_limits<float>::epsilon();
if (!coordinatesMatch || lhs.weightCount != rhs.weightCount)
return false;
for (auto weightIndex = 0u; weightIndex < lhs.weightCount; weightIndex++)
{
if (lhs.weights[weightIndex].boneIndex != rhs.weights[weightIndex].boneIndex
|| std::fabs(lhs.weights[weightIndex].weight - rhs.weights[weightIndex].weight) >= std::numeric_limits<float>::epsilon())
{
return false;
}
}
return true;
}
bool operator!=(const VertexMergerPos& lhs, VertexMergerPos& rhs)
{
return !(lhs == rhs);
}
bool operator<(const VertexMergerPos& lhs, const VertexMergerPos& rhs)
{
const auto t0 = std::tie(lhs.x, lhs.y, lhs.z, rhs.weightCount);
const auto t1 = std::tie(rhs.x, rhs.y, rhs.z, rhs.weightCount);
if (t0 < t1)
return true;
if (!(t0 == t1))
return false;
for (auto weightIndex = 0u; weightIndex < lhs.weightCount; weightIndex++)
{
const auto& lhsWeight = lhs.weights[weightIndex];
const auto& rhsWeight = rhs.weights[weightIndex];
const auto t2 = std::tie(lhsWeight.boneIndex, lhsWeight.weight);
const auto t3 = std::tie(rhsWeight.boneIndex, rhsWeight.weight);
if (t2 < t3)
return true;
if (!(t2 == t3))
return false;
}
return false;
}

View File

@ -1,7 +1,9 @@
#pragma once
#include <string>
#include <memory>
#include "Utils/DistinctMapper.h"
#include "Math/Quaternion.h"
struct XModelObject
@ -14,8 +16,28 @@ struct XModelBone
std::string name;
int parentIndex;
float scale[3];
float offset[3];
Quaternion32 rotation;
float globalOffset[3];
float localOffset[3];
Quaternion32 globalRotation;
Quaternion32 localRotation;
};
struct XModelBoneWeight
{
int boneIndex;
float weight;
};
struct XModelVertexBoneWeightCollection
{
std::unique_ptr<XModelBoneWeight[]> weights;
size_t totalWeightCount;
};
struct XModelVertexBoneWeights
{
const XModelBoneWeight* weights;
size_t weightCount;
};
struct XModelVertex
@ -63,4 +85,19 @@ struct XModelMaterial
std::string colorMapName;
void ApplyDefaults();
};
};
struct VertexMergerPos
{
float x;
float y;
float z;
const XModelBoneWeight* weights;
size_t weightCount;
friend bool operator==(const VertexMergerPos& lhs, const VertexMergerPos& rhs);
friend bool operator!=(const VertexMergerPos& lhs, const VertexMergerPos& rhs);
friend bool operator<(const VertexMergerPos& lhs, const VertexMergerPos& rhs);
};
typedef DistinctMapper<VertexMergerPos> VertexMerger;