mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
refactor: extract TangentData from JsonXModelLoader
This commit is contained in:
parent
b282e611fc
commit
543a4f06e4
@ -16,6 +16,7 @@
|
|||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
|
|
||||||
#include "XModel/PartClassificationState.h"
|
#include "XModel/PartClassificationState.h"
|
||||||
|
#include "XModel/TangentData.h"
|
||||||
#include "XModel/Tangentspace.h"
|
#include "XModel/Tangentspace.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -60,57 +61,6 @@ namespace
|
|||||||
};
|
};
|
||||||
static_assert(std::extent_v<decltype(HITLOC_NAMES)> == HITLOC_COUNT);
|
static_assert(std::extent_v<decltype(HITLOC_NAMES)> == HITLOC_COUNT);
|
||||||
|
|
||||||
class TangentData
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void CreateTangentData(const XModelCommon& common)
|
|
||||||
{
|
|
||||||
if (common.m_vertices.empty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
const auto vertexCount = common.m_vertices.size();
|
|
||||||
m_tangents.resize(vertexCount);
|
|
||||||
m_binormals.resize(vertexCount);
|
|
||||||
|
|
||||||
auto triCount = 0u;
|
|
||||||
for (const auto& object : common.m_objects)
|
|
||||||
triCount += object.m_faces.size();
|
|
||||||
|
|
||||||
std::vector<uint16_t> indices(triCount * 3u);
|
|
||||||
auto triOffset = 0u;
|
|
||||||
for (const auto& object : common.m_objects)
|
|
||||||
{
|
|
||||||
for (const auto& face : object.m_faces)
|
|
||||||
{
|
|
||||||
indices[triOffset++] = static_cast<uint16_t>(face.vertexIndex[0]);
|
|
||||||
indices[triOffset++] = static_cast<uint16_t>(face.vertexIndex[1]);
|
|
||||||
indices[triOffset++] = static_cast<uint16_t>(face.vertexIndex[2]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto& firstVertex = common.m_vertices[0];
|
|
||||||
|
|
||||||
tangent_space::VertexData vertexData{
|
|
||||||
firstVertex.coordinates,
|
|
||||||
sizeof(XModelVertex),
|
|
||||||
firstVertex.normal,
|
|
||||||
sizeof(XModelVertex),
|
|
||||||
firstVertex.uv,
|
|
||||||
sizeof(XModelVertex),
|
|
||||||
m_tangents.data(),
|
|
||||||
sizeof(float) * 3,
|
|
||||||
m_binormals.data(),
|
|
||||||
sizeof(float) * 3,
|
|
||||||
indices.data(),
|
|
||||||
};
|
|
||||||
|
|
||||||
tangent_space::CalculateTangentSpace(vertexData, triCount, vertexCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::array<float, 3>> m_tangents;
|
|
||||||
std::vector<std::array<float, 3>> m_binormals;
|
|
||||||
};
|
|
||||||
|
|
||||||
class JsonLoader
|
class JsonLoader
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
47
src/ObjLoading/XModel/TangentData.cpp
Normal file
47
src/ObjLoading/XModel/TangentData.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include "TangentData.h"
|
||||||
|
|
||||||
|
#include "Tangentspace.h"
|
||||||
|
|
||||||
|
void TangentData::CreateTangentData(const XModelCommon& common)
|
||||||
|
{
|
||||||
|
if (common.m_vertices.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
const auto vertexCount = common.m_vertices.size();
|
||||||
|
m_tangents.resize(vertexCount);
|
||||||
|
m_binormals.resize(vertexCount);
|
||||||
|
|
||||||
|
auto triCount = 0u;
|
||||||
|
for (const auto& object : common.m_objects)
|
||||||
|
triCount += object.m_faces.size();
|
||||||
|
|
||||||
|
std::vector<uint16_t> indices(triCount * 3u);
|
||||||
|
auto triOffset = 0u;
|
||||||
|
for (const auto& object : common.m_objects)
|
||||||
|
{
|
||||||
|
for (const auto& face : object.m_faces)
|
||||||
|
{
|
||||||
|
indices[triOffset++] = static_cast<uint16_t>(face.vertexIndex[0]);
|
||||||
|
indices[triOffset++] = static_cast<uint16_t>(face.vertexIndex[1]);
|
||||||
|
indices[triOffset++] = static_cast<uint16_t>(face.vertexIndex[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& firstVertex = common.m_vertices[0];
|
||||||
|
|
||||||
|
tangent_space::VertexData vertexData{
|
||||||
|
firstVertex.coordinates,
|
||||||
|
sizeof(XModelVertex),
|
||||||
|
firstVertex.normal,
|
||||||
|
sizeof(XModelVertex),
|
||||||
|
firstVertex.uv,
|
||||||
|
sizeof(XModelVertex),
|
||||||
|
m_tangents.data(),
|
||||||
|
sizeof(float) * 3,
|
||||||
|
m_binormals.data(),
|
||||||
|
sizeof(float) * 3,
|
||||||
|
indices.data(),
|
||||||
|
};
|
||||||
|
|
||||||
|
tangent_space::CalculateTangentSpace(vertexData, triCount, vertexCount);
|
||||||
|
}
|
15
src/ObjLoading/XModel/TangentData.h
Normal file
15
src/ObjLoading/XModel/TangentData.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "XModel/XModelCommon.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class TangentData
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void CreateTangentData(const XModelCommon& common);
|
||||||
|
|
||||||
|
std::vector<std::array<float, 3>> m_tangents;
|
||||||
|
std::vector<std::array<float, 3>> m_binormals;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user