2019-07-28 01:31:16 +02:00
|
|
|
#include "lc_global.h"
|
|
|
|
#include "lc_meshloader.h"
|
|
|
|
#include "lc_file.h"
|
|
|
|
#include "lc_colors.h"
|
|
|
|
#include "lc_library.h"
|
|
|
|
#include "lc_application.h"
|
|
|
|
#include "lc_texture.h"
|
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
static void lcCheckTexCoordsWrap(const lcVector4& Plane2, const lcVector3 (&Positions)[3], lcVector2 (&TexCoords)[3])
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
lcVector2& TexCoords1 = TexCoords[0];
|
|
|
|
lcVector2& TexCoords2 = TexCoords[1];
|
|
|
|
lcVector2& TexCoords3 = TexCoords[2];
|
2021-06-13 04:29:13 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const float u12 = fabsf(TexCoords1.x - TexCoords2.x);
|
|
|
|
const float u13 = fabsf(TexCoords1.x - TexCoords3.x);
|
|
|
|
const float u23 = fabsf(TexCoords2.x - TexCoords3.x);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (u12 < 0.5f && u13 < 0.5f && u23 < 0.5f)
|
|
|
|
return;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const float Dot1 = fabsf(lcDot(Plane2, lcVector4(Positions[0], 1.0f)));
|
|
|
|
const float Dot2 = fabsf(lcDot(Plane2, lcVector4(Positions[1], 1.0f)));
|
|
|
|
const float Dot3 = fabsf(lcDot(Plane2, lcVector4(Positions[2], 1.0f)));
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (Dot1 > Dot2)
|
|
|
|
{
|
|
|
|
if (Dot1 > Dot3)
|
|
|
|
{
|
|
|
|
if (u12 > 0.5f)
|
|
|
|
TexCoords2.x += TexCoords2.x < 0.5f ? 1.0f : -1.0f;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (u13 > 0.5f)
|
|
|
|
TexCoords3.x += TexCoords3.x < 0.5f ? 1.0f : -1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (u13 > 0.5f)
|
|
|
|
TexCoords1.x += TexCoords1.x < 0.5f ? 1.0f : -1.0f;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (u23 > 0.5f)
|
|
|
|
TexCoords2.x += TexCoords2.x < 0.5f ? 1.0f : -1.0f;
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-06-14 00:35:40 +02:00
|
|
|
else
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
if (Dot2 > Dot3)
|
|
|
|
{
|
|
|
|
if (u12 > 0.5f)
|
|
|
|
TexCoords1.x += TexCoords1.x < 0.5f ? 1.0f : -1.0f;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (u23 > 0.5f)
|
|
|
|
TexCoords3.x += TexCoords3.x < 0.5f ? 1.0f : -1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (u13 > 0.5f)
|
|
|
|
TexCoords1.x += TexCoords1.x < 0.5f ? 1.0f : -1.0f;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (u23 > 0.5f)
|
|
|
|
TexCoords2.x += TexCoords2.x < 0.5f ? 1.0f : -1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
static void lcCheckTexCoordsPole(const lcVector4& FrontPlane, const lcVector4& Plane2, const lcVector3(&Positions)[3], lcVector2(&TexCoords)[3])
|
|
|
|
{
|
|
|
|
int PoleIndex;
|
|
|
|
int EdgeIndex1, EdgeIndex2;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (fabsf(lcDot(lcVector4(Positions[0], 1.0f), FrontPlane)) < 0.01f && fabsf(lcDot(lcVector4(Positions[0], 1.0f), Plane2)) < 0.01f)
|
|
|
|
{
|
|
|
|
PoleIndex = 0;
|
|
|
|
EdgeIndex1 = 1;
|
|
|
|
EdgeIndex2 = 2;
|
|
|
|
}
|
|
|
|
else if (fabsf(lcDot(lcVector4(Positions[1], 1.0f), FrontPlane)) < 0.01f && fabsf(lcDot(lcVector4(Positions[1], 1.0f), Plane2)) < 0.01f)
|
|
|
|
{
|
|
|
|
PoleIndex = 1;
|
|
|
|
EdgeIndex1 = 0;
|
|
|
|
EdgeIndex2 = 2;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-06-14 00:35:40 +02:00
|
|
|
else if (fabsf(lcDot(lcVector4(Positions[2], 1.0f), FrontPlane)) < 0.01f && fabsf(lcDot(lcVector4(Positions[2], 1.0f), Plane2)) < 0.01f)
|
|
|
|
{
|
|
|
|
PoleIndex = 2;
|
|
|
|
EdgeIndex1 = 0;
|
|
|
|
EdgeIndex2 = 1;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-06-14 00:35:40 +02:00
|
|
|
else
|
|
|
|
return;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcVector3 OppositeEdge = Positions[EdgeIndex2] - Positions[EdgeIndex1];
|
|
|
|
const lcVector3 SideEdge = Positions[PoleIndex] - Positions[EdgeIndex1];
|
2021-06-14 00:35:40 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const float OppositeLength = lcLength(OppositeEdge);
|
|
|
|
const float Projection = lcDot(OppositeEdge, SideEdge) / (OppositeLength * OppositeLength);
|
2021-06-14 00:35:40 +02:00
|
|
|
|
|
|
|
TexCoords[PoleIndex].x = TexCoords[EdgeIndex1].x + (TexCoords[EdgeIndex2].x - TexCoords[EdgeIndex1].x) * Projection;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
static void lcResequenceQuad(int* Indices, int a, int b, int c, int d)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
|
|
|
Indices[0] = a;
|
|
|
|
Indices[1] = b;
|
|
|
|
Indices[2] = c;
|
|
|
|
Indices[3] = d;
|
|
|
|
}
|
|
|
|
|
2021-12-23 21:15:33 +01:00
|
|
|
static void lcTestQuad(int (&TriangleIndices)[2][3], const lcVector3 (&Vertices)[4])
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcVector3 v01 = Vertices[1] - Vertices[0];
|
|
|
|
const lcVector3 v02 = Vertices[2] - Vertices[0];
|
|
|
|
const lcVector3 v03 = Vertices[3] - Vertices[0];
|
|
|
|
const lcVector3 cp1 = lcCross(v01, v02);
|
|
|
|
const lcVector3 cp2 = lcCross(v02, v03);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (lcDot(cp1, cp2) > 0.0f)
|
|
|
|
return;
|
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcVector3 v12 = Vertices[2] - Vertices[1];
|
|
|
|
const lcVector3 v13 = Vertices[3] - Vertices[1];
|
|
|
|
const lcVector3 v23 = Vertices[3] - Vertices[2];
|
2021-12-23 21:15:33 +01:00
|
|
|
int QuadIndices[4] = { 0, 1, 2, 3 };
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (lcDot(lcCross(v12, v01), lcCross(v01, v13)) > 0.0f)
|
|
|
|
{
|
|
|
|
if (-lcDot(lcCross(v02, v12), lcCross(v12, v23)) > 0.0f)
|
2021-03-07 03:54:24 +01:00
|
|
|
lcResequenceQuad(QuadIndices, 1, 2, 3, 0);
|
2019-07-28 01:31:16 +02:00
|
|
|
else
|
2021-03-07 03:54:24 +01:00
|
|
|
lcResequenceQuad(QuadIndices, 0, 3, 1, 2);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (-lcDot(lcCross(v02, v12), lcCross(v12, v23)) > 0.0f)
|
2021-03-07 03:54:24 +01:00
|
|
|
lcResequenceQuad(QuadIndices, 0, 1, 3, 2);
|
2019-07-28 01:31:16 +02:00
|
|
|
else
|
2021-03-07 03:54:24 +01:00
|
|
|
lcResequenceQuad(QuadIndices, 1, 2, 3, 0);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-12-23 21:15:33 +01:00
|
|
|
|
|
|
|
TriangleIndices[0][0] = QuadIndices[0];
|
|
|
|
TriangleIndices[0][1] = QuadIndices[1];
|
|
|
|
TriangleIndices[0][2] = QuadIndices[2];
|
|
|
|
|
|
|
|
TriangleIndices[1][0] = QuadIndices[2];
|
|
|
|
TriangleIndices[1][1] = QuadIndices[3];
|
|
|
|
TriangleIndices[1][2] = QuadIndices[0];
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
constexpr float lcDistanceEpsilon = 0.01f; // Maximum value for 50591.dat
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
static bool lcCompareVertices(const lcVector3& Position1, const lcVector3& Position2)
|
|
|
|
{
|
|
|
|
return fabsf(Position1.x - Position2.x) < lcDistanceEpsilon && fabsf(Position1.y - Position2.y) < lcDistanceEpsilon && fabsf(Position1.z - Position2.z) < lcDistanceEpsilon;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
lcMeshLoaderSection* lcMeshLoaderTypeData::AddSection(lcMeshPrimitiveType PrimitiveType, lcMeshLoaderMaterial* Material)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderSection>& Section : mSections)
|
2021-06-13 04:29:13 +02:00
|
|
|
if (Section->mMaterial == Material && Section->mPrimitiveType == PrimitiveType)
|
2021-03-07 03:54:24 +01:00
|
|
|
return Section.get();
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
mSections.emplace_back(new lcMeshLoaderSection(PrimitiveType, Material));
|
2021-03-07 03:54:24 +01:00
|
|
|
|
|
|
|
return mSections.back().get();
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
quint32 lcMeshLoaderTypeData::AddVertex(const lcVector3& Position, bool Optimize)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
|
|
|
if (Optimize)
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int VertexIdx = mVertices.size() - 1; VertexIdx >= 0; VertexIdx--)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
const lcMeshLoaderVertex& Vertex = mVertices[VertexIdx];
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (lcCompareVertices(Position, Vertex.Position))
|
|
|
|
return VertexIdx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
lcMeshLoaderVertex& Vertex = mVertices.emplace_back();
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2019-07-28 01:31:16 +02:00
|
|
|
Vertex.Position = Position;
|
|
|
|
Vertex.Normal = lcVector3(0.0f, 0.0f, 0.0f);
|
|
|
|
Vertex.NormalWeight = 0.0f;
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
return mVertices.size() - 1;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-12-23 21:15:33 +01:00
|
|
|
quint32 lcMeshLoaderTypeData::AddVertex(const lcVector3& Position, const lcVector3& Normal, float NormalWeight, bool Optimize)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
|
|
|
if (Optimize)
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int VertexIdx = mVertices.size() - 1; VertexIdx >= 0; VertexIdx--)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-03-07 03:54:24 +01:00
|
|
|
lcMeshLoaderVertex& Vertex = mVertices[VertexIdx];
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (lcCompareVertices(Position, Vertex.Position))
|
|
|
|
{
|
|
|
|
if (Vertex.NormalWeight == 0.0f)
|
|
|
|
{
|
|
|
|
Vertex.Normal = Normal;
|
2021-12-23 21:15:33 +01:00
|
|
|
Vertex.NormalWeight = NormalWeight;
|
2019-07-28 01:31:16 +02:00
|
|
|
return VertexIdx;
|
|
|
|
}
|
2021-12-23 21:15:33 +01:00
|
|
|
else if (lcDot(Normal, Vertex.Normal) > 0.71f)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-12-23 21:15:33 +01:00
|
|
|
Vertex.Normal = lcNormalize(Vertex.Normal * Vertex.NormalWeight + Normal * NormalWeight);
|
|
|
|
Vertex.NormalWeight += NormalWeight;
|
2019-07-28 01:31:16 +02:00
|
|
|
return VertexIdx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
lcMeshLoaderVertex& Vertex = mVertices.emplace_back();
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2019-07-28 01:31:16 +02:00
|
|
|
Vertex.Position = Position;
|
|
|
|
Vertex.Normal = Normal;
|
|
|
|
Vertex.NormalWeight = 1.0f;
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
return mVertices.size() - 1;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 18:57:17 +01:00
|
|
|
quint32 lcMeshLoaderTypeData::AddConditionalVertex(const lcVector3(&Position)[4])
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
lcMeshLoaderConditionalVertex& Vertex = mConditionalVertices.emplace_back();
|
2021-03-07 03:54:24 +01:00
|
|
|
|
|
|
|
Vertex.Position[0] = Position[0];
|
|
|
|
Vertex.Position[1] = Position[1];
|
|
|
|
Vertex.Position[2] = Position[2];
|
|
|
|
Vertex.Position[3] = Position[3];
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
return mConditionalVertices.size() - 1;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
void lcMeshLoaderTypeData::ProcessLine(int LineType, lcMeshLoaderMaterial* Material, bool WindingCCW, lcVector3 (&Vertices)[4], bool Optimize)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
constexpr lcMeshPrimitiveType PrimitiveTypes[4] = { LC_MESH_LINES, LC_MESH_TRIANGLES, LC_MESH_TRIANGLES, LC_MESH_CONDITIONAL_LINES };
|
2019-07-28 01:31:16 +02:00
|
|
|
lcMeshPrimitiveType PrimitiveType = PrimitiveTypes[LineType - 2];
|
2021-06-14 00:35:40 +02:00
|
|
|
|
|
|
|
if (Material->Type != lcMeshLoaderMaterialType::Solid && PrimitiveType == LC_MESH_TRIANGLES)
|
|
|
|
PrimitiveType = LC_MESH_TEXTURED_TRIANGLES;
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
lcMeshLoaderSection* Section = AddSection(PrimitiveType, Material);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (LineType == 3 || LineType == 4)
|
|
|
|
{
|
2021-12-23 21:15:33 +01:00
|
|
|
int TriangleIndices[2][3] = { { 0, 1, 2 }, { 2, 3, 0 } };
|
|
|
|
|
2019-07-28 01:31:16 +02:00
|
|
|
if (LineType == 4)
|
2021-12-23 21:15:33 +01:00
|
|
|
lcTestQuad(TriangleIndices, Vertices);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
lcVector3 Normal = lcNormalize(lcCross(Vertices[1] - Vertices[0], Vertices[2] - Vertices[0]));
|
|
|
|
|
|
|
|
if (!WindingCCW)
|
|
|
|
Normal = -Normal;
|
|
|
|
|
2021-12-23 21:15:33 +01:00
|
|
|
for (int TriangleIndex = 0; TriangleIndex < LineType - 2; TriangleIndex++)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-12-23 21:15:33 +01:00
|
|
|
const lcVector3& Vertex1 = Vertices[TriangleIndices[TriangleIndex][0]];
|
|
|
|
const lcVector3& Vertex2 = Vertices[TriangleIndices[TriangleIndex][1]];
|
|
|
|
const lcVector3& Vertex3 = Vertices[TriangleIndices[TriangleIndex][2]];
|
|
|
|
const lcVector3 Edge1 = lcNormalize(Vertex2 - Vertex1);
|
|
|
|
const lcVector3 Edge2 = lcNormalize(Vertex3 - Vertex1);
|
|
|
|
const lcVector3 Edge3 = lcNormalize(Vertex3 - Vertex2);
|
|
|
|
const float Angle1 = acosf(lcDot(Edge1, Edge2));
|
|
|
|
const float Angle2 = acosf(lcDot(-Edge1, Edge3));
|
|
|
|
const float Angle3 = LC_PI - Angle1 - Angle2;
|
|
|
|
|
|
|
|
int Indices[3];
|
|
|
|
|
|
|
|
Indices[0] = AddVertex(Vertex1, Normal, Angle1, Optimize);
|
|
|
|
Indices[1] = AddVertex(Vertex2, Normal, Angle2, Optimize);
|
|
|
|
Indices[2] = AddVertex(Vertex3, Normal, Angle3, Optimize);
|
|
|
|
|
|
|
|
if (Indices[0] != Indices[1] && Indices[0] != Indices[2] && Indices[1] != Indices[2])
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-03-07 03:54:24 +01:00
|
|
|
if (WindingCCW)
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
Section->mIndices.emplace_back(Indices[0]);
|
|
|
|
Section->mIndices.emplace_back(Indices[1]);
|
|
|
|
Section->mIndices.emplace_back(Indices[2]);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
Section->mIndices.emplace_back(Indices[2]);
|
|
|
|
Section->mIndices.emplace_back(Indices[1]);
|
|
|
|
Section->mIndices.emplace_back(Indices[0]);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-06-14 01:24:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (LineType == 2)
|
|
|
|
{
|
2021-12-23 21:15:33 +01:00
|
|
|
int Indices[2];
|
|
|
|
|
|
|
|
Indices[0] = AddVertex(Vertices[0], Optimize);
|
|
|
|
Indices[1] = AddVertex(Vertices[1], Optimize);
|
2021-06-14 01:24:12 +02:00
|
|
|
|
|
|
|
if (Indices[0] != Indices[1])
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
Section->mIndices.emplace_back(Indices[0]);
|
|
|
|
Section->mIndices.emplace_back(Indices[1]);
|
2021-06-14 01:24:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (LineType == 5)
|
|
|
|
{
|
2021-12-23 21:15:33 +01:00
|
|
|
int Indices[2];
|
|
|
|
|
2021-06-14 01:24:12 +02:00
|
|
|
Indices[0] = AddConditionalVertex(Vertices);
|
2024-05-26 22:01:34 +02:00
|
|
|
Section->mIndices.emplace_back(Indices[0]);
|
2021-06-14 01:24:12 +02:00
|
|
|
|
|
|
|
std::swap(Vertices[0], Vertices[1]);
|
|
|
|
|
|
|
|
Indices[1] = AddConditionalVertex(Vertices);
|
2024-05-26 22:01:34 +02:00
|
|
|
Section->mIndices.emplace_back(Indices[1]);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
void lcMeshLoaderTypeData::AddMeshData(const lcMeshLoaderTypeData& Data, const lcMatrix44& Transform, quint32 CurrentColorCode, bool InvertWinding, bool InvertNormals, lcMeshLoaderTextureMap* TextureMap)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-03-07 03:54:24 +01:00
|
|
|
const lcArray<lcMeshLoaderVertex>& DataVertices = Data.mVertices;
|
2024-05-26 22:01:34 +02:00
|
|
|
lcArray<quint32> IndexRemap(DataVertices.size());
|
2021-03-18 05:43:02 +01:00
|
|
|
const lcMatrix33 NormalTransform = lcMatrix33Transpose(lcMatrix33(lcMatrix44Inverse(Transform)));
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
mVertices.AllocGrow(DataVertices.size());
|
2021-06-14 00:35:40 +02:00
|
|
|
|
|
|
|
for (const lcMeshLoaderVertex& DataVertex : DataVertices)
|
2021-03-07 03:54:24 +01:00
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcVector3 Position = lcMul31(DataVertex.Position, Transform);
|
2021-06-14 00:35:40 +02:00
|
|
|
int Index;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (DataVertex.NormalWeight == 0.0f)
|
|
|
|
Index = AddVertex(Position, true);
|
|
|
|
else
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
lcVector3 Normal = lcNormalize(lcMul(DataVertex.Normal, NormalTransform));
|
|
|
|
if (InvertNormals)
|
|
|
|
Normal = -Normal;
|
2021-12-23 21:15:33 +01:00
|
|
|
Index = AddVertex(Position, Normal, DataVertex.NormalWeight, true);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
IndexRemap.emplace_back(Index);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
mConditionalVertices.AllocGrow(Data.mConditionalVertices.size());
|
|
|
|
lcArray<quint32> ConditionalRemap(Data.mConditionalVertices.size());
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
for (const lcMeshLoaderConditionalVertex& DataVertex : Data.mConditionalVertices)
|
|
|
|
{
|
|
|
|
lcVector3 Position[4];
|
|
|
|
|
|
|
|
Position[0] = lcMul31(DataVertex.Position[0], Transform);
|
|
|
|
Position[1] = lcMul31(DataVertex.Position[1], Transform);
|
|
|
|
Position[2] = lcMul31(DataVertex.Position[2], Transform);
|
|
|
|
Position[3] = lcMul31(DataVertex.Position[3], Transform);
|
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const int Index = AddConditionalVertex(Position);
|
2024-05-26 22:01:34 +02:00
|
|
|
ConditionalRemap.emplace_back(Index);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-04-11 01:18:15 +02:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderSection>& SrcSection : Data.mSections)
|
2021-03-07 03:54:24 +01:00
|
|
|
{
|
2021-06-13 04:29:13 +02:00
|
|
|
const quint32 ColorCode = SrcSection->mMaterial->Color == 16 ? CurrentColorCode : SrcSection->mMaterial->Color;
|
2021-03-07 03:54:24 +01:00
|
|
|
lcMeshPrimitiveType PrimitiveType = SrcSection->mPrimitiveType;
|
2021-06-13 04:29:13 +02:00
|
|
|
lcMeshLoaderSection* DstSection;
|
|
|
|
|
|
|
|
if (SrcSection->mMaterial->Type != lcMeshLoaderMaterialType::Solid)
|
|
|
|
{
|
|
|
|
lcMeshLoaderTextureMap DstTextureMap = *TextureMap;
|
|
|
|
|
|
|
|
for (lcVector3& Point : DstTextureMap.Points)
|
|
|
|
Point = lcMul31(Point, Transform);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
DstSection = AddSection(PrimitiveType, mMeshData->GetTexturedMaterial(ColorCode, DstTextureMap));
|
|
|
|
}
|
2021-03-07 03:54:24 +01:00
|
|
|
else if (TextureMap && SrcSection->mPrimitiveType == LC_MESH_TRIANGLES)
|
|
|
|
{
|
|
|
|
PrimitiveType = LC_MESH_TEXTURED_TRIANGLES;
|
2021-06-13 04:29:13 +02:00
|
|
|
|
|
|
|
DstSection = AddSection(PrimitiveType, mMeshData->GetTexturedMaterial(ColorCode, *TextureMap));
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
|
|
|
else
|
2021-06-13 04:29:13 +02:00
|
|
|
{
|
|
|
|
DstSection = AddSection(PrimitiveType, mMeshData->GetMaterial(ColorCode));
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection->mIndices.AllocGrow(SrcSection->mIndices.size());
|
2021-03-07 03:54:24 +01:00
|
|
|
|
|
|
|
if (PrimitiveType == LC_MESH_CONDITIONAL_LINES)
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const quint32 Index : SrcSection->mIndices)
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection->mIndices.emplace_back(ConditionalRemap[Index]);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
|
|
|
else if (!InvertWinding || (PrimitiveType == LC_MESH_LINES))
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const quint32 Index : SrcSection->mIndices)
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection->mIndices.emplace_back(IndexRemap[Index]);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.size(); IndexIdx += 3)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection->mIndices.emplace_back(IndexRemap[SrcSection->mIndices[IndexIdx + 2]]);
|
|
|
|
DstSection->mIndices.emplace_back(IndexRemap[SrcSection->mIndices[IndexIdx + 1]]);
|
|
|
|
DstSection->mIndices.emplace_back(IndexRemap[SrcSection->mIndices[IndexIdx + 0]]);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
void lcMeshLoaderTypeData::AddMeshDataNoDuplicateCheck(const lcMeshLoaderTypeData& Data, const lcMatrix44& Transform, quint32 CurrentColorCode, bool InvertWinding, bool InvertNormals, lcMeshLoaderTextureMap* TextureMap)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-03-07 03:54:24 +01:00
|
|
|
const lcArray<lcMeshLoaderVertex>& DataVertices = Data.mVertices;
|
|
|
|
quint32 BaseIndex;
|
2021-03-18 05:43:02 +01:00
|
|
|
const lcMatrix33 NormalTransform = lcMatrix33Transpose(lcMatrix33(lcMatrix44Inverse(Transform)));
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
BaseIndex = mVertices.size();
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
mVertices.SetGrow(lcMin(mVertices.size(), 8 * 1024 * 1024));
|
|
|
|
mVertices.AllocGrow(DataVertices.size());
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int SrcVertexIdx = 0; SrcVertexIdx < DataVertices.size(); SrcVertexIdx++)
|
2021-03-07 03:54:24 +01:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
const lcMeshLoaderVertex& SrcVertex = DataVertices[SrcVertexIdx];
|
2024-05-26 22:01:34 +02:00
|
|
|
lcMeshLoaderVertex& DstVertex = mVertices.emplace_back();
|
2021-06-14 00:35:40 +02:00
|
|
|
DstVertex.Position = lcMul31(SrcVertex.Position, Transform);
|
|
|
|
DstVertex.Normal = lcNormalize(lcMul(SrcVertex.Normal, NormalTransform));
|
|
|
|
if (InvertNormals)
|
|
|
|
DstVertex.Normal = -DstVertex.Normal;
|
|
|
|
DstVertex.NormalWeight = SrcVertex.NormalWeight;
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
mConditionalVertices.AllocGrow(Data.mConditionalVertices.size());
|
|
|
|
const quint32 BaseConditional = mConditionalVertices.size();
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
for (const lcMeshLoaderConditionalVertex& DataVertex : Data.mConditionalVertices)
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
lcMeshLoaderConditionalVertex& Vertex = mConditionalVertices.emplace_back();
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
Vertex.Position[0] = lcMul31(DataVertex.Position[0], Transform);
|
|
|
|
Vertex.Position[1] = lcMul31(DataVertex.Position[1], Transform);
|
|
|
|
Vertex.Position[2] = lcMul31(DataVertex.Position[2], Transform);
|
|
|
|
Vertex.Position[3] = lcMul31(DataVertex.Position[3], Transform);
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-04-11 01:18:15 +02:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderSection>& SrcSection : Data.mSections)
|
2021-03-07 03:54:24 +01:00
|
|
|
{
|
2021-06-13 04:29:13 +02:00
|
|
|
const quint32 ColorCode = SrcSection->mMaterial->Color == 16 ? CurrentColorCode : SrcSection->mMaterial->Color;
|
2021-03-07 03:54:24 +01:00
|
|
|
lcMeshPrimitiveType PrimitiveType = SrcSection->mPrimitiveType;
|
2021-06-13 04:29:13 +02:00
|
|
|
lcMeshLoaderSection* DstSection;
|
|
|
|
|
|
|
|
if (SrcSection->mMaterial->Type != lcMeshLoaderMaterialType::Solid)
|
|
|
|
{
|
|
|
|
lcMeshLoaderTextureMap DstTextureMap = *TextureMap;
|
|
|
|
|
|
|
|
for (lcVector3& Point : DstTextureMap.Points)
|
|
|
|
Point = lcMul31(Point, Transform);
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
DstSection = AddSection(PrimitiveType, mMeshData->GetTexturedMaterial(ColorCode, DstTextureMap));
|
|
|
|
}
|
2021-03-07 03:54:24 +01:00
|
|
|
else if (TextureMap && SrcSection->mPrimitiveType == LC_MESH_TRIANGLES)
|
|
|
|
{
|
|
|
|
PrimitiveType = LC_MESH_TEXTURED_TRIANGLES;
|
2021-06-13 04:29:13 +02:00
|
|
|
|
|
|
|
DstSection = AddSection(PrimitiveType, mMeshData->GetTexturedMaterial(ColorCode, *TextureMap));
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-03-07 03:54:24 +01:00
|
|
|
else
|
2021-06-13 04:29:13 +02:00
|
|
|
{
|
|
|
|
DstSection = AddSection(PrimitiveType, mMeshData->GetMaterial(ColorCode));
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection->mIndices.SetGrow(lcMin(DstSection->mIndices.size(), 8 * 1024 * 1024));
|
|
|
|
DstSection->mIndices.AllocGrow(SrcSection->mIndices.size());
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
if (PrimitiveType == LC_MESH_CONDITIONAL_LINES)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const quint32 Index : SrcSection->mIndices)
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection->mIndices.emplace_back(BaseConditional + Index);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
|
|
|
else if (!InvertWinding || (PrimitiveType == LC_MESH_LINES))
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const quint32 Index : SrcSection->mIndices)
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection->mIndices.emplace_back(BaseIndex + Index);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.size(); IndexIdx += 3)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection->mIndices.emplace_back(BaseIndex + SrcSection->mIndices[IndexIdx + 2]);
|
|
|
|
DstSection->mIndices.emplace_back(BaseIndex + SrcSection->mIndices[IndexIdx + 1]);
|
|
|
|
DstSection->mIndices.emplace_back(BaseIndex + SrcSection->mIndices[IndexIdx + 0]);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
void lcLibraryMeshData::AddVertices(lcMeshDataType MeshDataType, int VertexCount, int* BaseVertex, lcMeshLoaderVertex** VertexBuffer)
|
|
|
|
{
|
|
|
|
lcArray<lcMeshLoaderVertex>& Vertices = mData[MeshDataType].mVertices;
|
2024-05-26 22:01:34 +02:00
|
|
|
int CurrentSize = Vertices.size();
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
Vertices.resize(CurrentSize + VertexCount);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
*BaseVertex = CurrentSize;
|
|
|
|
*VertexBuffer = &Vertices[CurrentSize];
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
void lcLibraryMeshData::AddIndices(lcMeshDataType MeshDataType, lcMeshPrimitiveType PrimitiveType, quint32 ColorCode, int IndexCount, quint32** IndexBuffer)
|
|
|
|
{
|
2021-06-13 04:29:13 +02:00
|
|
|
lcMeshLoaderSection* Section = mData[MeshDataType].AddSection(PrimitiveType, GetMaterial(ColorCode));
|
2021-03-07 03:54:24 +01:00
|
|
|
lcArray<quint32>& Indices = Section->mIndices;
|
2024-05-26 22:01:34 +02:00
|
|
|
const int CurrentSize = Indices.size();
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
Indices.resize(CurrentSize + IndexCount);
|
2021-03-07 03:54:24 +01:00
|
|
|
|
|
|
|
*IndexBuffer = &Indices[CurrentSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
void lcLibraryMeshData::AddMeshData(const lcLibraryMeshData& Data, const lcMatrix44& Transform, quint32 CurrentColorCode, bool InvertWinding, bool InvertNormals, lcMeshLoaderTextureMap* TextureMap, lcMeshDataType OverrideDestIndex)
|
|
|
|
{
|
|
|
|
for (int MeshDataIdx = 0; MeshDataIdx < LC_NUM_MESHDATA_TYPES; MeshDataIdx++)
|
|
|
|
{
|
|
|
|
const int DestIndex = OverrideDestIndex == LC_MESHDATA_SHARED ? MeshDataIdx : OverrideDestIndex;
|
|
|
|
mData[DestIndex].AddMeshData(Data.mData[MeshDataIdx], Transform, CurrentColorCode, InvertWinding, InvertNormals, TextureMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
mHasTextures |= (Data.mHasTextures || TextureMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lcLibraryMeshData::AddMeshDataNoDuplicateCheck(const lcLibraryMeshData& Data, const lcMatrix44& Transform, quint32 CurrentColorCode, bool InvertWinding, bool InvertNormals, lcMeshLoaderTextureMap* TextureMap, lcMeshDataType OverrideDestIndex)
|
|
|
|
{
|
|
|
|
for (int MeshDataIdx = 0; MeshDataIdx < LC_NUM_MESHDATA_TYPES; MeshDataIdx++)
|
|
|
|
{
|
|
|
|
const int DestIndex = OverrideDestIndex == LC_MESHDATA_SHARED ? MeshDataIdx : OverrideDestIndex;
|
|
|
|
mData[DestIndex].AddMeshDataNoDuplicateCheck(Data.mData[MeshDataIdx], Transform, CurrentColorCode, InvertWinding, InvertNormals, TextureMap);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-03-07 03:54:24 +01:00
|
|
|
|
|
|
|
mHasTextures |= (Data.mHasTextures || TextureMap);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
lcMeshLoaderMaterial* lcLibraryMeshData::GetMaterial(quint32 ColorCode)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderMaterial>& Material : mMaterials)
|
2021-06-13 04:29:13 +02:00
|
|
|
if (Material->Type == lcMeshLoaderMaterialType::Solid && Material->Color == ColorCode)
|
|
|
|
return Material.get();
|
|
|
|
|
|
|
|
lcMeshLoaderMaterial* Material = new lcMeshLoaderMaterial();
|
|
|
|
mMaterials.emplace_back(Material);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
Material->Type = lcMeshLoaderMaterialType::Solid;
|
|
|
|
Material->Color = ColorCode;
|
|
|
|
|
|
|
|
return Material;
|
|
|
|
}
|
|
|
|
|
|
|
|
lcMeshLoaderMaterial* lcLibraryMeshData::GetTexturedMaterial(quint32 ColorCode, const lcMeshLoaderTextureMap& TextureMap)
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderMaterial>& Material : mMaterials)
|
2021-06-13 04:29:13 +02:00
|
|
|
{
|
|
|
|
if (Material->Type != TextureMap.Type || Material->Color != ColorCode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(Material->Name, TextureMap.Name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Material->Points[0] != TextureMap.Points[0] || Material->Points[1] != TextureMap.Points[1] || Material->Points[2] != TextureMap.Points[2])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Material->Type == lcMeshLoaderMaterialType::Cylindrical)
|
|
|
|
{
|
|
|
|
if (Material->Angles[0] != TextureMap.Angles[0])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (Material->Type == lcMeshLoaderMaterialType::Spherical)
|
|
|
|
{
|
|
|
|
if (Material->Angles[0] != TextureMap.Angles[0] || Material->Angles[1] != TextureMap.Angles[1])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Material.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
lcMeshLoaderMaterial* Material = new lcMeshLoaderMaterial();
|
|
|
|
mMaterials.emplace_back(Material);
|
|
|
|
|
|
|
|
Material->Type = TextureMap.Type;
|
|
|
|
Material->Color = ColorCode;
|
|
|
|
Material->Points[0] = TextureMap.Points[0];
|
|
|
|
Material->Points[1] = TextureMap.Points[1];
|
|
|
|
Material->Points[2] = TextureMap.Points[2];
|
|
|
|
Material->Angles[0] = TextureMap.Angles[0];
|
|
|
|
Material->Angles[1] = TextureMap.Angles[1];
|
|
|
|
strcpy(Material->Name, TextureMap.Name);
|
|
|
|
|
|
|
|
return Material;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool lcMeshLoaderFinalSectionCompare(const lcMeshLoaderFinalSection& a, const lcMeshLoaderFinalSection& b)
|
|
|
|
{
|
|
|
|
if (a.PrimitiveType != b.PrimitiveType)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 01:24:12 +02:00
|
|
|
const lcMeshPrimitiveType PrimitiveOrder[LC_MESH_NUM_PRIMITIVE_TYPES] =
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
|
|
|
LC_MESH_TRIANGLES,
|
|
|
|
LC_MESH_TEXTURED_TRIANGLES,
|
|
|
|
LC_MESH_LINES,
|
|
|
|
LC_MESH_CONDITIONAL_LINES
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int PrimitiveType = 0; PrimitiveType < LC_MESH_NUM_PRIMITIVE_TYPES; PrimitiveType++)
|
|
|
|
{
|
2021-06-14 01:24:12 +02:00
|
|
|
const lcMeshPrimitiveType Primitive = PrimitiveOrder[PrimitiveType];
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
if (a.PrimitiveType == Primitive)
|
2019-11-23 19:46:18 +01:00
|
|
|
return true;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
if (b.PrimitiveType == Primitive)
|
2019-11-23 19:46:18 +01:00
|
|
|
return false;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const bool TranslucentA = lcIsColorTranslucent(a.Color);
|
|
|
|
const bool TranslucentB = lcIsColorTranslucent(b.Color);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (TranslucentA != TranslucentB)
|
2019-11-23 19:46:18 +01:00
|
|
|
return !TranslucentA;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
return a.Color > b.Color;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
quint32 lcLibraryMeshData::AddTexturedVertex(const lcVector3& Position, const lcVector3& Normal, const lcVector2& TexCoords)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int VertexIndex = mTexturedVertices.size() - 1; VertexIndex >= 0; VertexIndex--)
|
2021-06-14 00:35:40 +02:00
|
|
|
{
|
|
|
|
const lcMeshLoaderTexturedVertex& Vertex = mTexturedVertices[VertexIndex];
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (Vertex.Position == Position && Vertex.Normal == Normal && Vertex.TexCoords == TexCoords)
|
|
|
|
return VertexIndex;
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
lcMeshLoaderTexturedVertex& Vertex = mTexturedVertices.emplace_back();
|
2021-06-14 00:35:40 +02:00
|
|
|
|
|
|
|
Vertex.Position = Position;
|
|
|
|
Vertex.Normal = Normal;
|
|
|
|
Vertex.TexCoords = TexCoords;
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
return mTexturedVertices.size() - 1;
|
2021-06-14 00:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void lcLibraryMeshData::GeneratePlanarTexcoords(lcMeshLoaderSection* Section, const lcMeshLoaderTypeData& Data)
|
|
|
|
{
|
|
|
|
const lcMeshLoaderMaterial* Material = Section->mMaterial;
|
|
|
|
lcVector4 Planes[2];
|
|
|
|
|
|
|
|
for (int EdgeIdx = 0; EdgeIdx < 2; EdgeIdx++)
|
|
|
|
{
|
|
|
|
lcVector3 Normal = Material->Points[EdgeIdx + 1] - Material->Points[0];
|
2021-11-15 03:34:24 +01:00
|
|
|
const float Length = lcLength(Normal);
|
2021-06-14 00:35:40 +02:00
|
|
|
Normal /= Length;
|
|
|
|
|
|
|
|
Planes[EdgeIdx].x = Normal.x / Length;
|
|
|
|
Planes[EdgeIdx].y = Normal.y / Length;
|
|
|
|
Planes[EdgeIdx].z = Normal.z / Length;
|
|
|
|
Planes[EdgeIdx].w = -lcDot(Normal, Material->Points[0]) / Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (quint32& Index : Section->mIndices)
|
|
|
|
{
|
|
|
|
const lcMeshLoaderVertex& SrcVertex = Data.mVertices[Index];
|
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcVector2 TexCoords(lcDot3(SrcVertex.Position, Planes[0]) + Planes[0].w, lcDot3(SrcVertex.Position, Planes[1]) + Planes[1].w);
|
2021-06-14 00:35:40 +02:00
|
|
|
|
|
|
|
Index = AddTexturedVertex(SrcVertex.Position, SrcVertex.Normal, TexCoords);
|
|
|
|
}
|
|
|
|
}
|
2021-06-13 04:29:13 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
void lcLibraryMeshData::GenerateCylindricalTexcoords(lcMeshLoaderSection* Section, const lcMeshLoaderTypeData& Data)
|
|
|
|
{
|
|
|
|
const lcMeshLoaderMaterial* Material = Section->mMaterial;
|
|
|
|
const lcVector3 Up = Material->Points[0] - Material->Points[1];
|
|
|
|
const float UpLength = lcLength(Up);
|
|
|
|
const lcVector3 Front = lcNormalize(Material->Points[2] - Material->Points[1]);
|
|
|
|
const lcVector3 Plane1Normal = Up / UpLength;
|
|
|
|
const lcVector3 Plane2Normal = lcNormalize(lcCross(Front, Up));
|
|
|
|
const lcVector4 FrontPlane = lcVector4(Front, -lcDot(Front, Material->Points[1]));
|
|
|
|
const lcVector4 Plane1 = lcVector4(Plane1Normal, -lcDot(Plane1Normal, Material->Points[1]));
|
|
|
|
const lcVector4 Plane2 = lcVector4(Plane2Normal, -lcDot(Plane2Normal, Material->Points[1]));
|
|
|
|
const float Angle = 360.0f / Material->Angles[0];
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int TriangleIndex = 0; TriangleIndex < Section->mIndices.size(); TriangleIndex += 3)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
const lcVector3 Positions[3] =
|
|
|
|
{
|
|
|
|
Data.mVertices[Section->mIndices[TriangleIndex + 0]].Position,
|
|
|
|
Data.mVertices[Section->mIndices[TriangleIndex + 1]].Position,
|
|
|
|
Data.mVertices[Section->mIndices[TriangleIndex + 2]].Position
|
|
|
|
};
|
|
|
|
|
|
|
|
lcVector2 TexCoords[3];
|
|
|
|
|
|
|
|
for (int CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
const float DotPlane1 = lcDot(lcVector4(Positions[CornerIndex], 1.0f), Plane1);
|
|
|
|
const lcVector3 PointInPlane1 = Positions[CornerIndex] - lcVector3(Plane1) * DotPlane1;
|
|
|
|
const float DotFrontPlane = lcDot(lcVector4(PointInPlane1, 1.0f), FrontPlane);
|
|
|
|
const float DotPlane2 = lcDot(lcVector4(PointInPlane1, 1.0f), Plane2);
|
|
|
|
const float Angle1 = atan2f(DotPlane2, DotFrontPlane) / LC_PI * Angle;
|
|
|
|
|
|
|
|
TexCoords[CornerIndex].x = lcClamp(0.5f + 0.5f * Angle1, 0.0f, 1.0f);
|
|
|
|
TexCoords[CornerIndex].y = DotPlane1 / UpLength;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-06-14 00:35:40 +02:00
|
|
|
|
|
|
|
lcCheckTexCoordsWrap(Plane2, Positions, TexCoords);
|
|
|
|
|
|
|
|
for (int CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
|
|
Section->mIndices[TriangleIndex + CornerIndex] = AddTexturedVertex(Positions[CornerIndex], Data.mVertices[Section->mIndices[TriangleIndex + CornerIndex]].Normal, TexCoords[CornerIndex]);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-06-14 00:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void lcLibraryMeshData::GenerateSphericalTexcoords(lcMeshLoaderSection* Section, const lcMeshLoaderTypeData& Data)
|
|
|
|
{
|
|
|
|
const lcMeshLoaderMaterial* Material = Section->mMaterial;
|
|
|
|
const lcVector3 Front = lcNormalize(Material->Points[1] - Material->Points[0]);
|
|
|
|
const lcVector3 Plane1Normal = lcNormalize(lcCross(Front, Material->Points[2] - Material->Points[0]));
|
|
|
|
const lcVector3 Plane2Normal = lcNormalize(lcCross(Plane1Normal, Front));
|
|
|
|
const lcVector4 FrontPlane = lcVector4(Front, -lcDot(Front, Material->Points[0]));
|
|
|
|
const lcVector3 Center = Material->Points[0];
|
|
|
|
const lcVector4 Plane1 = lcVector4(Plane1Normal, -lcDot(Plane1Normal, Material->Points[0]));
|
|
|
|
const lcVector4 Plane2 = lcVector4(Plane2Normal, -lcDot(Plane2Normal, Material->Points[0]));
|
|
|
|
const float Angle1 = 360.0f / Material->Angles[0];
|
|
|
|
const float Angle2 = 180.0f / Material->Angles[1];
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int TriangleIndex = 0; TriangleIndex < Section->mIndices.size(); TriangleIndex += 3)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
const lcVector3 Positions[3] =
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
Data.mVertices[Section->mIndices[TriangleIndex + 0]].Position,
|
|
|
|
Data.mVertices[Section->mIndices[TriangleIndex + 1]].Position,
|
|
|
|
Data.mVertices[Section->mIndices[TriangleIndex + 2]].Position
|
|
|
|
};
|
|
|
|
|
|
|
|
lcVector2 TexCoords[3];
|
|
|
|
|
|
|
|
for (int CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
const lcVector3 VertexDir = Positions[CornerIndex] - Center;
|
2021-06-14 00:35:40 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const float DotPlane1 = lcDot(lcVector4(Positions[CornerIndex], 1.0f), Plane1);
|
|
|
|
const lcVector3 PointInPlane1 = Positions[CornerIndex] - lcVector3(Plane1) * DotPlane1;
|
|
|
|
const float DotFrontPlane = lcDot(lcVector4(PointInPlane1, 1.0f), FrontPlane);
|
|
|
|
const float DotPlane2 = lcDot(lcVector4(PointInPlane1, 1.0f), Plane2);
|
2021-06-14 00:35:40 +02:00
|
|
|
|
|
|
|
const float AngleX = atan2f(DotPlane2, DotFrontPlane) / LC_PI * Angle1;
|
|
|
|
TexCoords[CornerIndex].x = 0.5f + 0.5f * AngleX;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
const float AngleY = asinf(DotPlane1 / lcLength(VertexDir)) / LC_PI * Angle2;
|
|
|
|
TexCoords[CornerIndex].y = 0.5f - AngleY;
|
|
|
|
}
|
|
|
|
|
|
|
|
lcCheckTexCoordsWrap(Plane2, Positions, TexCoords);
|
|
|
|
lcCheckTexCoordsPole(FrontPlane, Plane2, Positions, TexCoords);
|
|
|
|
|
|
|
|
for (int CornerIndex = 0; CornerIndex < 3; CornerIndex++)
|
|
|
|
Section->mIndices[TriangleIndex + CornerIndex] = AddTexturedVertex(Positions[CornerIndex], Data.mVertices[Section->mIndices[TriangleIndex + CornerIndex]].Normal, TexCoords[CornerIndex]);
|
|
|
|
}
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
void lcLibraryMeshData::GenerateTexturedVertices()
|
|
|
|
{
|
|
|
|
for (lcMeshLoaderTypeData& Data : mData)
|
|
|
|
{
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderSection>& Section : Data.mSections)
|
2021-06-14 00:35:40 +02:00
|
|
|
{
|
|
|
|
switch (Section->mMaterial->Type)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
case lcMeshLoaderMaterialType::Solid:
|
|
|
|
break;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
case lcMeshLoaderMaterialType::Planar:
|
|
|
|
GeneratePlanarTexcoords(Section.get(), Data);
|
|
|
|
break;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
case lcMeshLoaderMaterialType::Cylindrical:
|
|
|
|
GenerateCylindricalTexcoords(Section.get(), Data);
|
|
|
|
break;
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
case lcMeshLoaderMaterialType::Spherical:
|
|
|
|
GenerateSphericalTexcoords(Section.get(), Data);
|
|
|
|
break;
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-14 00:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lcMesh* lcLibraryMeshData::CreateMesh()
|
|
|
|
{
|
|
|
|
lcMesh* Mesh = new lcMesh();
|
|
|
|
|
|
|
|
int BaseVertices[LC_NUM_MESHDATA_TYPES];
|
|
|
|
int BaseConditionalVertices[LC_NUM_MESHDATA_TYPES];
|
|
|
|
int NumVertices = 0;
|
|
|
|
int ConditionalVertexCount = 0;
|
|
|
|
|
|
|
|
for (const std::unique_ptr<lcMeshLoaderMaterial>& Material : mMaterials)
|
|
|
|
Material->Color = lcGetColorIndex(Material->Color);
|
|
|
|
|
|
|
|
for (int MeshDataIdx = 0; MeshDataIdx < LC_NUM_MESHDATA_TYPES; MeshDataIdx++)
|
|
|
|
{
|
|
|
|
BaseVertices[MeshDataIdx] = NumVertices;
|
2024-05-26 22:01:34 +02:00
|
|
|
NumVertices += mData[MeshDataIdx].mVertices.size();
|
2021-06-14 00:35:40 +02:00
|
|
|
BaseConditionalVertices[MeshDataIdx] = ConditionalVertexCount;
|
2024-05-26 22:01:34 +02:00
|
|
|
ConditionalVertexCount += mData[MeshDataIdx].mConditionalVertices.size();
|
2021-06-14 00:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mHasTextures)
|
|
|
|
GenerateTexturedVertices();
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
quint16 NumSections[LC_NUM_MESH_LODS];
|
|
|
|
int NumIndices = 0;
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
lcArray<lcMeshLoaderFinalSection> FinalSections[LC_NUM_MESH_LODS];
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
for (int LodIdx = 0; LodIdx < LC_NUM_MESH_LODS; LodIdx++)
|
|
|
|
{
|
2021-06-13 04:29:13 +02:00
|
|
|
auto AddFinalSection = [](lcMeshLoaderSection* Section, lcArray<lcMeshLoaderFinalSection>& FinalSections)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-13 04:29:13 +02:00
|
|
|
for (const lcMeshLoaderFinalSection& FinalSection : FinalSections)
|
|
|
|
if (FinalSection.PrimitiveType == Section->mPrimitiveType && FinalSection.Color == Section->mMaterial->Color && !strcmp(FinalSection.Name, Section->mMaterial->Name))
|
|
|
|
return;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
lcMeshLoaderFinalSection& FinalSection = FinalSections.emplace_back();
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
FinalSection.PrimitiveType = Section->mPrimitiveType;
|
|
|
|
FinalSection.Color = Section->mMaterial->Color;
|
|
|
|
strcpy(FinalSection.Name, Section->mMaterial->Name);
|
|
|
|
};
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderSection>& Section : mData[LC_MESHDATA_SHARED].mSections)
|
2021-06-13 04:29:13 +02:00
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
NumIndices += Section->mIndices.size();
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
AddFinalSection(Section.get(), FinalSections[LodIdx]);
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderSection>& Section : mData[LodIdx].mSections)
|
2021-06-13 04:29:13 +02:00
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
NumIndices += Section->mIndices.size();
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
AddFinalSection(Section.get(), FinalSections[LodIdx]);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
NumSections[LodIdx] = FinalSections[LodIdx].size();
|
2021-06-13 04:29:13 +02:00
|
|
|
std::sort(FinalSections[LodIdx].begin(), FinalSections[LodIdx].end(), lcMeshLoaderFinalSectionCompare);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
Mesh->Create(NumSections, NumVertices, mTexturedVertices.size(), ConditionalVertexCount, NumIndices);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
lcVertex* DstVerts = (lcVertex*)Mesh->mVertexData;
|
|
|
|
|
2021-06-14 01:24:12 +02:00
|
|
|
for (const lcMeshLoaderTypeData& Data : mData)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
for (const lcMeshLoaderVertex& SrcVertex : Data.mVertices)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
lcVertex& DstVertex = *DstVerts++;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
DstVertex.Position = lcVector3LDrawToLeoCAD(SrcVertex.Position);
|
|
|
|
DstVertex.Normal = lcPackNormal(lcVector3LDrawToLeoCAD(SrcVertex.Normal));
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
lcVertexTextured* DstTexturedVerts = (lcVertexTextured*)DstVerts;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
if (mHasTextures)
|
|
|
|
{
|
|
|
|
for (const lcMeshLoaderTexturedVertex& SrcVertex : mTexturedVertices)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
lcVertexTextured& DstVertex = *DstTexturedVerts++;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
DstVertex.Position = lcVector3LDrawToLeoCAD(SrcVertex.Position);
|
|
|
|
DstVertex.Normal = lcPackNormal(lcVector3LDrawToLeoCAD(SrcVertex.Normal));
|
|
|
|
DstVertex.TexCoord = SrcVertex.TexCoords;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-06-14 00:35:40 +02:00
|
|
|
}
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
lcVertexConditional* DstConditionalVerts = (lcVertexConditional*)DstTexturedVerts;
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2021-06-14 01:24:12 +02:00
|
|
|
for (const lcMeshLoaderTypeData& Data : mData)
|
2021-06-14 00:35:40 +02:00
|
|
|
{
|
|
|
|
for (const lcMeshLoaderConditionalVertex& SrcVertex : Data.mConditionalVertices)
|
2021-03-07 03:54:24 +01:00
|
|
|
{
|
2021-06-14 00:35:40 +02:00
|
|
|
lcVertexConditional& DstVertex = *DstConditionalVerts++;
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
DstVertex.Position1 = lcVector3LDrawToLeoCAD(SrcVertex.Position[0]);
|
|
|
|
DstVertex.Position2 = lcVector3LDrawToLeoCAD(SrcVertex.Position[1]);
|
|
|
|
DstVertex.Position3 = lcVector3LDrawToLeoCAD(SrcVertex.Position[2]);
|
|
|
|
DstVertex.Position4 = lcVector3LDrawToLeoCAD(SrcVertex.Position[3]);
|
2021-03-07 03:54:24 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 19:51:34 +02:00
|
|
|
if (Mesh->mIndexType == GL_UNSIGNED_SHORT)
|
2021-06-26 23:01:02 +02:00
|
|
|
WriteSections<quint16>(Mesh, FinalSections, BaseVertices, BaseConditionalVertices);
|
2021-06-06 19:51:34 +02:00
|
|
|
else
|
2021-06-26 23:01:02 +02:00
|
|
|
WriteSections<quint32>(Mesh, FinalSections, BaseVertices, BaseConditionalVertices);
|
2021-06-06 19:51:34 +02:00
|
|
|
|
|
|
|
if (mHasStyleStud)
|
|
|
|
Mesh->mFlags |= lcMeshFlag::HasStyleStud;
|
|
|
|
|
|
|
|
UpdateMeshBoundingBox(Mesh);
|
|
|
|
|
|
|
|
return Mesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename IndexType>
|
2021-06-26 23:01:02 +02:00
|
|
|
void lcLibraryMeshData::WriteSections(lcMesh* Mesh, const lcArray<lcMeshLoaderFinalSection> (&FinalSections)[LC_NUM_MESH_LODS], int(&BaseVertices)[LC_NUM_MESHDATA_TYPES], int(&BaseConditionalVertices)[LC_NUM_MESHDATA_TYPES])
|
2021-06-06 19:51:34 +02:00
|
|
|
{
|
|
|
|
int NumIndices = 0;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
for (int LodIdx = 0; LodIdx < LC_NUM_MESH_LODS; LodIdx++)
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int SectionIdx = 0; SectionIdx < FinalSections[LodIdx].size(); SectionIdx++)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-13 04:29:13 +02:00
|
|
|
const lcMeshLoaderFinalSection& FinalSection = FinalSections[LodIdx][SectionIdx];
|
2019-07-28 01:31:16 +02:00
|
|
|
lcMeshSection& DstSection = Mesh->mLods[LodIdx].Sections[SectionIdx];
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
DstSection.ColorIndex = FinalSection.Color;
|
|
|
|
DstSection.PrimitiveType = FinalSection.PrimitiveType;
|
2019-07-28 01:31:16 +02:00
|
|
|
DstSection.NumIndices = 0;
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
if (!FinalSection.Name[0])
|
|
|
|
DstSection.Texture = nullptr;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mMeshLoader)
|
|
|
|
DstSection.Texture = lcGetPiecesLibrary()->FindTexture(FinalSection.Name, mMeshLoader->mCurrentProject, mMeshLoader->mSearchProjectFolder);
|
|
|
|
else
|
|
|
|
DstSection.Texture = lcGetPiecesLibrary()->FindTexture(FinalSection.Name, nullptr, false);
|
|
|
|
|
|
|
|
if (DstSection.Texture)
|
|
|
|
DstSection.Texture->AddRef();
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-06 19:51:34 +02:00
|
|
|
DstSection.IndexOffset = NumIndices * sizeof(IndexType);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-06 19:51:34 +02:00
|
|
|
IndexType* Index = (IndexType*)Mesh->mIndexData + NumIndices;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-11-21 04:56:52 +01:00
|
|
|
const auto AddSection = [&DstSection, &Index, &BaseVertices, &BaseConditionalVertices](lcMeshLoaderSection* SrcSection, lcMeshDataType SrcDataType)
|
2021-06-06 19:51:34 +02:00
|
|
|
{
|
2021-06-27 21:13:54 +02:00
|
|
|
switch (DstSection.PrimitiveType)
|
2021-06-06 19:51:34 +02:00
|
|
|
{
|
2021-06-27 21:13:54 +02:00
|
|
|
case LC_MESH_LINES:
|
|
|
|
case LC_MESH_TRIANGLES:
|
2021-03-07 03:54:24 +01:00
|
|
|
{
|
2021-06-26 23:01:02 +02:00
|
|
|
const IndexType BaseVertex = BaseVertices[SrcDataType];
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.size(); IndexIdx++)
|
2021-03-07 03:54:24 +01:00
|
|
|
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
|
|
|
}
|
2021-06-27 21:13:54 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LC_MESH_CONDITIONAL_LINES:
|
2021-06-26 23:01:02 +02:00
|
|
|
{
|
2021-06-27 21:13:54 +02:00
|
|
|
const IndexType BaseVertex = BaseConditionalVertices[SrcDataType];
|
2021-06-26 23:01:02 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.size(); IndexIdx++)
|
2021-06-26 23:01:02 +02:00
|
|
|
*Index++ = BaseVertex + SrcSection->mIndices[IndexIdx];
|
|
|
|
}
|
2021-06-27 21:13:54 +02:00
|
|
|
break;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-27 21:13:54 +02:00
|
|
|
case LC_MESH_TEXTURED_TRIANGLES:
|
|
|
|
{
|
2024-05-26 22:01:34 +02:00
|
|
|
for (int IndexIdx = 0; IndexIdx < SrcSection->mIndices.size(); IndexIdx++)
|
2021-06-27 21:13:54 +02:00
|
|
|
*Index++ = SrcSection->mIndices[IndexIdx];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LC_MESH_NUM_PRIMITIVE_TYPES:
|
|
|
|
break;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2021-06-06 19:51:34 +02:00
|
|
|
|
2024-05-26 22:01:34 +02:00
|
|
|
DstSection.NumIndices += SrcSection->mIndices.size();
|
2021-06-13 04:29:13 +02:00
|
|
|
};
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderSection>& Section : mData[LC_MESHDATA_SHARED].mSections)
|
2021-06-13 04:29:13 +02:00
|
|
|
if (FinalSection.PrimitiveType == Section->mPrimitiveType && FinalSection.Color == Section->mMaterial->Color && !strcmp(FinalSection.Name, Section->mMaterial->Name))
|
|
|
|
AddSection(Section.get(), LC_MESHDATA_SHARED);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
const lcMeshDataType MeshDataType = (LodIdx == LC_MESH_LOD_LOW) ? LC_MESHDATA_LOW : LC_MESHDATA_HIGH;
|
2021-03-07 03:54:24 +01:00
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
for (const std::unique_ptr<lcMeshLoaderSection>& Section : mData[MeshDataType].mSections)
|
2021-06-13 04:29:13 +02:00
|
|
|
if (FinalSection.PrimitiveType == Section->mPrimitiveType && FinalSection.Color == Section->mMaterial->Color && !strcmp(FinalSection.Name, Section->mMaterial->Name))
|
|
|
|
AddSection(Section.get(), MeshDataType);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (DstSection.PrimitiveType == LC_MESH_TRIANGLES || DstSection.PrimitiveType == LC_MESH_TEXTURED_TRIANGLES)
|
|
|
|
{
|
|
|
|
if (DstSection.ColorIndex == gDefaultColor)
|
|
|
|
Mesh->mFlags |= lcMeshFlag::HasDefault;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lcIsColorTranslucent(DstSection.ColorIndex))
|
|
|
|
Mesh->mFlags |= lcMeshFlag::HasTranslucent;
|
|
|
|
else
|
|
|
|
Mesh->mFlags |= lcMeshFlag::HasSolid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Mesh->mFlags |= lcMeshFlag::HasLines;
|
|
|
|
|
2019-11-02 19:38:24 +01:00
|
|
|
if (DstSection.PrimitiveType == LC_MESH_TEXTURED_TRIANGLES)
|
2019-07-28 01:31:16 +02:00
|
|
|
Mesh->mFlags |= lcMeshFlag::HasTexture;
|
|
|
|
|
|
|
|
NumIndices += DstSection.NumIndices;
|
|
|
|
}
|
|
|
|
}
|
2021-03-07 19:14:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void lcLibraryMeshData::UpdateMeshBoundingBox(lcMesh* Mesh)
|
|
|
|
{
|
2019-11-22 21:48:40 +01:00
|
|
|
lcVector3 MeshMin(FLT_MAX, FLT_MAX, FLT_MAX), MeshMax(-FLT_MAX, -FLT_MAX, -FLT_MAX);
|
|
|
|
bool UpdatedBoundingBox = false;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2019-11-22 21:48:40 +01:00
|
|
|
for (int LodIdx = 0; LodIdx < LC_NUM_MESH_LODS; LodIdx++)
|
|
|
|
{
|
|
|
|
lcMeshLod& Lod = Mesh->mLods[LodIdx];
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2019-11-22 21:48:40 +01:00
|
|
|
for (int SectionIdx = 0; SectionIdx < Lod.NumSections; SectionIdx++)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2019-11-22 21:48:40 +01:00
|
|
|
lcMeshSection& Section = Lod.Sections[SectionIdx];
|
|
|
|
lcVector3 SectionMin(FLT_MAX, FLT_MAX, FLT_MAX), SectionMax(-FLT_MAX, -FLT_MAX, -FLT_MAX);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-13 11:59:57 +01:00
|
|
|
if (Mesh->mIndexType == GL_UNSIGNED_SHORT)
|
2021-03-07 19:14:48 +01:00
|
|
|
UpdateMeshSectionBoundingBox<quint16>(Mesh, Section, SectionMin, SectionMax);
|
2019-07-28 01:31:16 +02:00
|
|
|
else
|
2021-03-07 19:14:48 +01:00
|
|
|
UpdateMeshSectionBoundingBox<quint32>(Mesh, Section, SectionMin, SectionMax);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2019-11-22 21:48:40 +01:00
|
|
|
Section.BoundingBox.Max = SectionMax;
|
|
|
|
Section.BoundingBox.Min = SectionMin;
|
|
|
|
Section.Radius = lcLength((SectionMax - SectionMin) / 2.0f);
|
|
|
|
|
|
|
|
if (Section.PrimitiveType == LC_MESH_TRIANGLES || Section.PrimitiveType == LC_MESH_TEXTURED_TRIANGLES)
|
|
|
|
{
|
|
|
|
UpdatedBoundingBox = true;
|
|
|
|
MeshMin = lcMin(SectionMin, MeshMin);
|
|
|
|
MeshMax = lcMax(SectionMax, MeshMax);
|
|
|
|
}
|
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
2019-11-22 21:48:40 +01:00
|
|
|
|
|
|
|
if (!UpdatedBoundingBox)
|
|
|
|
MeshMin = MeshMax = lcVector3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
|
|
|
Mesh->mBoundingBox.Max = MeshMax;
|
|
|
|
Mesh->mBoundingBox.Min = MeshMin;
|
|
|
|
Mesh->mRadius = lcLength((MeshMax - MeshMin) / 2.0f);
|
2021-03-07 19:14:48 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-03-07 19:14:48 +01:00
|
|
|
template<typename IndexType>
|
2021-11-15 03:34:24 +01:00
|
|
|
void lcLibraryMeshData::UpdateMeshSectionBoundingBox(const lcMesh* Mesh, const lcMeshSection& Section, lcVector3& SectionMin, lcVector3& SectionMax)
|
2021-03-07 19:14:48 +01:00
|
|
|
{
|
|
|
|
const IndexType* IndexBuffer = reinterpret_cast<IndexType*>(static_cast<char*>(Mesh->mIndexData) + Section.IndexOffset);
|
|
|
|
|
2021-06-27 21:13:54 +02:00
|
|
|
switch (Section.PrimitiveType)
|
2021-03-07 19:14:48 +01:00
|
|
|
{
|
2021-06-27 21:13:54 +02:00
|
|
|
case LC_MESH_LINES:
|
|
|
|
case LC_MESH_TRIANGLES:
|
2021-03-07 19:14:48 +01:00
|
|
|
{
|
|
|
|
const lcVertex* VertexBuffer = Mesh->GetVertexData();
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Section.NumIndices; Index++)
|
|
|
|
{
|
|
|
|
const lcVector3& Position = VertexBuffer[IndexBuffer[Index]].Position;
|
|
|
|
SectionMin = lcMin(SectionMin, Position);
|
|
|
|
SectionMax = lcMax(SectionMax, Position);
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 21:13:54 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LC_MESH_CONDITIONAL_LINES:
|
2021-03-07 19:14:48 +01:00
|
|
|
{
|
|
|
|
const lcVertexConditional* VertexBuffer = Mesh->GetConditionalVertexData();
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Section.NumIndices; Index++)
|
|
|
|
{
|
|
|
|
const lcVector3& Position = VertexBuffer[IndexBuffer[Index]].Position1;
|
|
|
|
SectionMin = lcMin(SectionMin, Position);
|
|
|
|
SectionMax = lcMax(SectionMax, Position);
|
|
|
|
}
|
|
|
|
}
|
2021-06-27 21:13:54 +02:00
|
|
|
break;
|
2021-03-07 19:14:48 +01:00
|
|
|
|
2021-06-27 21:13:54 +02:00
|
|
|
case LC_MESH_TEXTURED_TRIANGLES:
|
2021-03-07 19:14:48 +01:00
|
|
|
{
|
2021-06-27 21:13:54 +02:00
|
|
|
const lcVertexTextured* VertexBuffer = Mesh->GetTexturedVertexData();
|
|
|
|
|
|
|
|
for (int Index = 0; Index < Section.NumIndices; Index++)
|
|
|
|
{
|
|
|
|
const lcVector3& Position = VertexBuffer[IndexBuffer[Index]].Position;
|
|
|
|
SectionMin = lcMin(SectionMin, Position);
|
|
|
|
SectionMax = lcMax(SectionMax, Position);
|
|
|
|
}
|
2021-03-07 19:14:48 +01:00
|
|
|
}
|
2021-06-27 21:13:54 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LC_MESH_NUM_PRIMITIVE_TYPES:
|
|
|
|
break;
|
2021-03-07 19:14:48 +01:00
|
|
|
}
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lcMeshLoader::lcMeshLoader(lcLibraryMeshData& MeshData, bool Optimize, Project* CurrentProject, bool SearchProjectFolder)
|
2021-06-26 22:27:17 +02:00
|
|
|
: mCurrentProject(CurrentProject), mSearchProjectFolder(SearchProjectFolder), mMeshData(MeshData), mOptimize(Optimize)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-13 04:29:13 +02:00
|
|
|
MeshData.SetMeshLoader(this);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool lcMeshLoader::LoadMesh(lcFile& File, lcMeshDataType MeshDataType)
|
|
|
|
{
|
2021-06-06 02:44:20 +02:00
|
|
|
return ReadMeshData(File, lcMatrix44Identity(), 16, false, MeshDataType);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 02:44:20 +02:00
|
|
|
bool lcMeshLoader::ReadMeshData(lcFile& File, const lcMatrix44& CurrentTransform, quint32 CurrentColorCode, bool InvertWinding, lcMeshDataType MeshDataType)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
|
|
|
char Buffer[1024];
|
|
|
|
char* Line;
|
|
|
|
bool InvertNext = false;
|
|
|
|
bool WindingCCW = !InvertWinding;
|
|
|
|
lcPiecesLibrary* Library = lcGetPiecesLibrary();
|
|
|
|
|
|
|
|
while (File.ReadLine(Buffer, sizeof(Buffer)))
|
|
|
|
{
|
|
|
|
if (Library->ShouldCancelLoading())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
quint32 ColorCode, ColorCodeHex;
|
|
|
|
bool LastToken = false;
|
|
|
|
int LineType;
|
|
|
|
|
|
|
|
Line = Buffer;
|
|
|
|
|
|
|
|
if (sscanf(Line, "%d", &LineType) != 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (LineType == 0)
|
|
|
|
{
|
|
|
|
char* Token = Line;
|
|
|
|
|
|
|
|
while (*Token && *Token <= 32)
|
|
|
|
Token++;
|
|
|
|
|
|
|
|
Token++;
|
|
|
|
|
|
|
|
while (*Token && *Token <= 32)
|
|
|
|
Token++;
|
|
|
|
|
|
|
|
char* End = Token;
|
|
|
|
while (*End && *End > 32)
|
|
|
|
End++;
|
|
|
|
|
|
|
|
LastToken = (*End == 0);
|
|
|
|
*End = 0;
|
|
|
|
|
|
|
|
if (!strcmp(Token, "!TEXMAP"))
|
|
|
|
{
|
|
|
|
Token += 8;
|
|
|
|
|
|
|
|
while (*Token && *Token <= 32)
|
|
|
|
Token++;
|
|
|
|
|
|
|
|
End = Token;
|
|
|
|
while (*End && *End > 32)
|
|
|
|
End++;
|
|
|
|
*End = 0;
|
|
|
|
|
|
|
|
bool Start = false;
|
|
|
|
bool Next = false;
|
|
|
|
|
|
|
|
if (!strcmp(Token, "START"))
|
|
|
|
{
|
|
|
|
Token += 6;
|
|
|
|
Start = true;
|
|
|
|
}
|
|
|
|
else if (!strcmp(Token, "NEXT"))
|
|
|
|
{
|
|
|
|
Token += 5;
|
|
|
|
Next = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Start || Next)
|
|
|
|
{
|
|
|
|
while (*Token && *Token <= 32)
|
|
|
|
Token++;
|
|
|
|
|
|
|
|
End = Token;
|
|
|
|
while (*End && *End > 32)
|
|
|
|
End++;
|
|
|
|
*End = 0;
|
|
|
|
|
|
|
|
auto CleanTextureName = [](char* FileName)
|
|
|
|
{
|
|
|
|
char* Ch;
|
|
|
|
for (Ch = FileName; *Ch; Ch++)
|
|
|
|
{
|
|
|
|
if (*Ch >= 'a' && *Ch <= 'z')
|
|
|
|
*Ch = *Ch + 'A' - 'a';
|
|
|
|
else if (*Ch == '\\')
|
|
|
|
*Ch = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ch - FileName > 4)
|
|
|
|
{
|
|
|
|
Ch -= 4;
|
|
|
|
if (!memcmp(Ch, ".PNG", 4))
|
|
|
|
*Ch = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!strcmp(Token, "PLANAR"))
|
|
|
|
{
|
|
|
|
Token += 7;
|
|
|
|
|
2021-06-14 00:41:57 +02:00
|
|
|
mTextureStack.emplace_back();
|
|
|
|
lcMeshLoaderTextureMap& Map = mTextureStack.back();
|
2021-06-14 00:35:40 +02:00
|
|
|
Map.Type = lcMeshLoaderMaterialType::Planar;
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
lcVector3 (&Points)[3] = Map.Points;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
sscanf(Token, "%f %f %f %f %f %f %f %f %f %s", &Points[0].x, &Points[0].y, &Points[0].z, &Points[1].x, &Points[1].y, &Points[1].z, &Points[2].x, &Points[2].y, &Points[2].z, Map.Name);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
Points[0] = lcMul31(Points[0], CurrentTransform);
|
|
|
|
Points[1] = lcMul31(Points[1], CurrentTransform);
|
|
|
|
Points[2] = lcMul31(Points[2], CurrentTransform);
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
CleanTextureName(Map.Name);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
else if (!strcmp(Token, "CYLINDRICAL"))
|
|
|
|
{
|
|
|
|
Token += 12;
|
|
|
|
|
2021-06-14 00:41:57 +02:00
|
|
|
mTextureStack.emplace_back();
|
|
|
|
lcMeshLoaderTextureMap& Map = mTextureStack.back();
|
2021-06-14 00:35:40 +02:00
|
|
|
Map.Type = lcMeshLoaderMaterialType::Cylindrical;
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
lcVector3 (&Points)[3] = Map.Points;
|
|
|
|
float& Angle = Map.Angles[0];
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
sscanf(Token, "%f %f %f %f %f %f %f %f %f %f %s", &Points[0].x, &Points[0].y, &Points[0].z, &Points[1].x, &Points[1].y, &Points[1].z, &Points[2].x, &Points[2].y, &Points[2].z, &Angle, Map.Name);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
Points[0] = lcMul31(Points[0], CurrentTransform);
|
|
|
|
Points[1] = lcMul31(Points[1], CurrentTransform);
|
|
|
|
Points[2] = lcMul31(Points[2], CurrentTransform);
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
CleanTextureName(Map.Name);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
else if (!strcmp(Token, "SPHERICAL"))
|
|
|
|
{
|
|
|
|
Token += 10;
|
|
|
|
|
2021-06-14 00:41:57 +02:00
|
|
|
mTextureStack.emplace_back();
|
|
|
|
lcMeshLoaderTextureMap& Map = mTextureStack.back();
|
2021-06-14 00:35:40 +02:00
|
|
|
Map.Type = lcMeshLoaderMaterialType::Spherical;
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
lcVector3(&Points)[3] = Map.Points;
|
|
|
|
float& Angle1 = Map.Angles[0];
|
|
|
|
float& Angle2 = Map.Angles[1];
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
sscanf(Token, "%f %f %f %f %f %f %f %f %f %f %f %s", &Points[0].x, &Points[0].y, &Points[0].z, &Points[1].x, &Points[1].y, &Points[1].z, &Points[2].x, &Points[2].y, &Points[2].z, &Angle1, &Angle2, Map.Name);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
Points[0] = lcMul31(Points[0], CurrentTransform);
|
|
|
|
Points[1] = lcMul31(Points[1], CurrentTransform);
|
|
|
|
Points[2] = lcMul31(Points[2], CurrentTransform);
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
CleanTextureName(Map.Name);
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp(Token, "FALLBACK"))
|
|
|
|
{
|
2021-06-14 00:41:57 +02:00
|
|
|
if (!mTextureStack.empty())
|
|
|
|
mTextureStack.back().Fallback = true;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
else if (!strcmp(Token, "END"))
|
|
|
|
{
|
2021-06-14 00:41:57 +02:00
|
|
|
if (!mTextureStack.empty())
|
|
|
|
mTextureStack.pop_back();
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(Token, "BFC"))
|
|
|
|
{
|
|
|
|
while (!LastToken)
|
|
|
|
{
|
|
|
|
Token = End + 1;
|
|
|
|
|
|
|
|
while (*Token && *Token <= 32)
|
|
|
|
Token++;
|
|
|
|
|
|
|
|
End = Token;
|
|
|
|
while (*End && *End > 32)
|
|
|
|
End++;
|
|
|
|
|
|
|
|
LastToken = (*End == 0);
|
|
|
|
*End = 0;
|
|
|
|
|
|
|
|
if (!strcmp(Token, "INVERTNEXT"))
|
|
|
|
InvertNext = true;
|
|
|
|
else if (!strcmp(Token, "CCW"))
|
|
|
|
WindingCCW = !InvertWinding;
|
|
|
|
else if (!strcmp(Token, "CW"))
|
|
|
|
WindingCCW = InvertWinding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp(Token, "!:"))
|
|
|
|
{
|
|
|
|
Token += 3;
|
|
|
|
|
|
|
|
Line = Token;
|
|
|
|
|
2021-06-14 00:41:57 +02:00
|
|
|
if (mTextureStack.empty())
|
2019-07-28 01:31:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sscanf(Line, "%d %d", &LineType, &ColorCode) != 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (LineType < 1 || LineType > 5)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ColorCode == 0)
|
|
|
|
{
|
|
|
|
sscanf(Line, "%d %i", &LineType, &ColorCodeHex);
|
|
|
|
|
|
|
|
if (ColorCode != ColorCodeHex)
|
|
|
|
ColorCode = ColorCodeHex | LC_COLOR_DIRECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ColorCode == 16)
|
|
|
|
ColorCode = CurrentColorCode;
|
|
|
|
|
2021-03-07 03:54:24 +01:00
|
|
|
lcMeshLoaderTextureMap* TextureMap = nullptr;
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:41:57 +02:00
|
|
|
if (!mTextureStack.empty())
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-14 00:41:57 +02:00
|
|
|
TextureMap = &mTextureStack.back();
|
2019-07-28 01:31:16 +02:00
|
|
|
|
2021-06-14 00:35:40 +02:00
|
|
|
// TODO: think about a way to handle the texture fallback
|
2021-06-13 04:29:13 +02:00
|
|
|
// if (TextureMap->Texture)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
|
|
|
if (TextureMap->Fallback)
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-13 04:29:13 +02:00
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// if (!TextureMap->Fallback)
|
|
|
|
// continue;
|
|
|
|
//
|
|
|
|
// TextureMap = nullptr;
|
|
|
|
// }
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Dummy;
|
|
|
|
lcVector3 Points[4];
|
|
|
|
|
|
|
|
switch (LineType)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
char OriginalFileName[LC_MAXPATH];
|
|
|
|
float fm[12];
|
|
|
|
|
|
|
|
sscanf(Line, "%d %i %f %f %f %f %f %f %f %f %f %f %f %f %s", &LineType, &Dummy, &fm[0], &fm[1], &fm[2], &fm[3], &fm[4], &fm[5], &fm[6], &fm[7], &fm[8], &fm[9], &fm[10], &fm[11], OriginalFileName);
|
|
|
|
|
|
|
|
char FileName[LC_MAXPATH];
|
|
|
|
strcpy(FileName, OriginalFileName);
|
|
|
|
|
|
|
|
char* Ch;
|
|
|
|
for (Ch = FileName; *Ch; Ch++)
|
|
|
|
{
|
|
|
|
if (*Ch >= 'a' && *Ch <= 'z')
|
|
|
|
*Ch = *Ch + 'A' - 'a';
|
|
|
|
else if (*Ch == '\\')
|
|
|
|
*Ch = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
lcLibraryPrimitive* Primitive = !TextureMap ? Library->FindPrimitive(FileName) : nullptr;
|
|
|
|
lcMatrix44 IncludeTransform(lcVector4(fm[3], fm[6], fm[9], 0.0f), lcVector4(fm[4], fm[7], fm[10], 0.0f), lcVector4(fm[5], fm[8], fm[11], 0.0f), lcVector4(fm[0], fm[1], fm[2], 1.0f));
|
|
|
|
IncludeTransform = lcMul(IncludeTransform, CurrentTransform);
|
|
|
|
bool Mirror = IncludeTransform.Determinant() < 0.0f;
|
|
|
|
|
2021-11-15 03:34:24 +01:00
|
|
|
const auto FileCallback = [this, &IncludeTransform, &ColorCode, &Mirror, &InvertNext, &MeshDataType](lcFile& File)
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-06-06 02:44:20 +02:00
|
|
|
ReadMeshData(File, IncludeTransform, ColorCode, Mirror ^ InvertNext, MeshDataType);
|
2019-07-28 01:31:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Primitive)
|
|
|
|
{
|
2021-01-18 22:38:17 +01:00
|
|
|
if (Primitive->mState != lcPrimitiveState::Loaded && !Library->LoadPrimitive(Primitive))
|
2019-07-28 01:31:16 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (Primitive->mStud)
|
|
|
|
mMeshData.AddMeshDataNoDuplicateCheck(Primitive->mMeshData, IncludeTransform, ColorCode, Mirror ^ InvertNext, InvertNext, TextureMap, MeshDataType);
|
|
|
|
else if (!Primitive->mSubFile)
|
|
|
|
{
|
|
|
|
if (mOptimize)
|
|
|
|
mMeshData.AddMeshData(Primitive->mMeshData, IncludeTransform, ColorCode, Mirror ^ InvertNext, InvertNext, TextureMap, MeshDataType);
|
|
|
|
else
|
|
|
|
mMeshData.AddMeshDataNoDuplicateCheck(Primitive->mMeshData, IncludeTransform, ColorCode, Mirror ^ InvertNext, InvertNext, TextureMap, MeshDataType);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Library->GetPrimitiveFile(Primitive, FileCallback);
|
2019-09-27 23:52:50 +02:00
|
|
|
|
2021-01-23 23:55:00 +01:00
|
|
|
mMeshData.mHasStyleStud |= Primitive->mStudStyle | Primitive->mMeshData.mHasStyleStud;
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Library->GetPieceFile(FileName, FileCallback);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
sscanf(Line, "%d %i %f %f %f %f %f %f", &LineType, &Dummy, &Points[0].x, &Points[0].y, &Points[0].z, &Points[1].x, &Points[1].y, &Points[1].z);
|
|
|
|
|
|
|
|
Points[0] = lcMul31(Points[0], CurrentTransform);
|
|
|
|
Points[1] = lcMul31(Points[1], CurrentTransform);
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
mMeshData.mData[MeshDataType].ProcessLine(LineType, mMeshData.GetMaterial(ColorCode), WindingCCW, Points, mOptimize);
|
2019-07-28 01:31:16 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
sscanf(Line, "%d %i %f %f %f %f %f %f %f %f %f", &LineType, &Dummy, &Points[0].x, &Points[0].y, &Points[0].z,
|
|
|
|
&Points[1].x, &Points[1].y, &Points[1].z, &Points[2].x, &Points[2].y, &Points[2].z);
|
|
|
|
|
|
|
|
Points[0] = lcMul31(Points[0], CurrentTransform);
|
|
|
|
Points[1] = lcMul31(Points[1], CurrentTransform);
|
|
|
|
Points[2] = lcMul31(Points[2], CurrentTransform);
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
if (!TextureMap)
|
|
|
|
mMeshData.mData[MeshDataType].ProcessLine(LineType, mMeshData.GetMaterial(ColorCode), WindingCCW, Points, mOptimize);
|
|
|
|
else
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-03-07 03:54:24 +01:00
|
|
|
mMeshData.mHasTextures = true;
|
2021-06-14 00:35:40 +02:00
|
|
|
mMeshData.mData[MeshDataType].ProcessLine(LineType, mMeshData.GetTexturedMaterial(ColorCode, *TextureMap), WindingCCW, Points, mOptimize);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (TextureMap->Next)
|
2021-06-14 00:41:57 +02:00
|
|
|
mTextureStack.pop_back();
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
sscanf(Line, "%d %i %f %f %f %f %f %f %f %f %f %f %f %f", &LineType, &Dummy, &Points[0].x, &Points[0].y, &Points[0].z,
|
|
|
|
&Points[1].x, &Points[1].y, &Points[1].z, &Points[2].x, &Points[2].y, &Points[2].z, &Points[3].x, &Points[3].y, &Points[3].z);
|
|
|
|
|
|
|
|
Points[0] = lcMul31(Points[0], CurrentTransform);
|
|
|
|
Points[1] = lcMul31(Points[1], CurrentTransform);
|
|
|
|
Points[2] = lcMul31(Points[2], CurrentTransform);
|
|
|
|
Points[3] = lcMul31(Points[3], CurrentTransform);
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
if (!TextureMap)
|
|
|
|
mMeshData.mData[MeshDataType].ProcessLine(LineType, mMeshData.GetMaterial(ColorCode), WindingCCW, Points, mOptimize);
|
|
|
|
else
|
2019-07-28 01:31:16 +02:00
|
|
|
{
|
2021-03-07 03:54:24 +01:00
|
|
|
mMeshData.mHasTextures = true;
|
2021-06-14 00:35:40 +02:00
|
|
|
mMeshData.mData[MeshDataType].ProcessLine(LineType, mMeshData.GetTexturedMaterial(ColorCode, *TextureMap), WindingCCW, Points, mOptimize);
|
2019-07-28 01:31:16 +02:00
|
|
|
|
|
|
|
if (TextureMap->Next)
|
2021-06-14 00:41:57 +02:00
|
|
|
mTextureStack.pop_back();
|
2019-07-28 01:31:16 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
sscanf(Line, "%d %i %f %f %f %f %f %f %f %f %f %f %f %f", &LineType, &Dummy, &Points[0].x, &Points[0].y, &Points[0].z,
|
|
|
|
&Points[1].x, &Points[1].y, &Points[1].z, &Points[2].x, &Points[2].y, &Points[2].z, &Points[3].x, &Points[3].y, &Points[3].z);
|
|
|
|
|
|
|
|
Points[0] = lcMul31(Points[0], CurrentTransform);
|
|
|
|
Points[1] = lcMul31(Points[1], CurrentTransform);
|
|
|
|
Points[2] = lcMul31(Points[2], CurrentTransform);
|
|
|
|
Points[3] = lcMul31(Points[3], CurrentTransform);
|
|
|
|
|
2021-06-13 04:29:13 +02:00
|
|
|
mMeshData.mData[MeshDataType].ProcessLine(LineType, mMeshData.GetMaterial(ColorCode), WindingCCW, Points, mOptimize);
|
2019-07-28 01:31:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
InvertNext = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|