Skip to content

Commit

Permalink
#5115: Move implementation to .cpp file, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 8, 2020
1 parent 54fde58 commit 3c68f76
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 120 deletions.
1 change: 1 addition & 0 deletions radiantcore/Makefile.am
Expand Up @@ -172,6 +172,7 @@ libradiantcore_la_SOURCES = Radiant.cpp \
model/export/Lwo2Exporter.cpp \
model/export/ModelExporter.cpp \
model/export/ModelScalePreserver.cpp \
model/export/PatchSurface.cpp \
model/export/ScaledModelExporter.cpp \
model/export/WavefrontExporter.cpp \
model/picomodel/PicoModelLoader.cpp \
Expand Down
39 changes: 0 additions & 39 deletions radiantcore/model/export/ModelExporter.cpp
Expand Up @@ -36,19 +36,6 @@ ArbitraryMeshVertex convertWindingVertex(const WindingVertex& in)
return out;
}

// Adapter methods to convert patch vertices to ArbitraryMeshVertex type
ArbitraryMeshVertex convertPatchVertex(const VertexNT& in)
{
ArbitraryMeshVertex out;

out.vertex = in.vertex;
out.normal = in.normal;
out.texcoord = in.texcoord;
out.colour.set(1.0, 1.0, 1.0);

return out;
}

// Create a polygon out of 3 vertices defined in counter-clockwise winding
// Only the normal will be calculated, texcoord, tangent and bitangents will be zero
model::ModelPolygon createPolyCCW(const Vertex3f& a, const Vertex3f& b, const Vertex3f& c)
Expand Down Expand Up @@ -200,35 +187,9 @@ void ModelExporter::processPatch(const scene::INodePtr& node)
PatchMesh mesh = patch->getTesselatedPatchMesh();
Matrix4 exportTransform = node->localToWorld().getPremultipliedBy(_centerTransform);

#if 0
std::vector<model::ModelPolygon> polys;

for (std::size_t h = 0; h < mesh.height - 1; ++h)
{
for (std::size_t w = 0; w < mesh.width - 1; ++w)
{
model::ModelPolygon poly;

poly.a = convertPatchVertex(mesh.vertices[w + (h*mesh.width)]);
poly.b = convertPatchVertex(mesh.vertices[w + 1 + (h*mesh.width)]);
poly.c = convertPatchVertex(mesh.vertices[w + mesh.width + (h*mesh.width)]);

polys.push_back(poly);

poly.a = convertPatchVertex(mesh.vertices[w + 1 + (h*mesh.width)]);
poly.b = convertPatchVertex(mesh.vertices[w + 1 + mesh.width + (h*mesh.width)]);
poly.c = convertPatchVertex(mesh.vertices[w + mesh.width + (h*mesh.width)]);

polys.push_back(poly);
}
}

_exporter->addPolygons(materialName, polys, exportTransform);
#else
// Convert the patch mesh to an indexed surface
PatchSurface surface(materialName, mesh);
_exporter->addSurface(surface, exportTransform);
#endif
}

void ModelExporter::processBrush(const scene::INodePtr& node)
Expand Down
97 changes: 97 additions & 0 deletions radiantcore/model/export/PatchSurface.cpp
@@ -0,0 +1,97 @@
#include "PatchSurface.h"

#include <algorithm>

namespace model
{

namespace
{
// Adapter method to convert patch vertices to ArbitraryMeshVertex type
inline ArbitraryMeshVertex convertPatchVertex(const VertexNT& in)
{
// Colour will be set to 1,1,1 by the constructor
return ArbitraryMeshVertex(in.vertex, in.normal, in.texcoord);
}
}

PatchSurface::PatchSurface(const std::string& materialName, PatchMesh& mesh) :
_materialName(materialName)
{
assert(mesh.width >= 2 && mesh.height >= 2);

_vertices.reserve(mesh.vertices.size());
_indices.reserve((mesh.height - 1) * (mesh.width - 1) * 6); // 6 => 2 triangles per quad

// Load all the vertices into the target vector
std::transform(mesh.vertices.begin(), mesh.vertices.end(),
std::back_inserter(_vertices), convertPatchVertex);

// Generate the indices to define the triangles in clockwise order
for (std::size_t h = 0; h < mesh.height - 1; ++h)
{
auto rowOffset = h * mesh.width;

for (std::size_t w = 0; w < mesh.width - 1; ++w)
{
_indices.push_back(static_cast<unsigned int>(rowOffset + w + mesh.width));
_indices.push_back(static_cast<unsigned int>(rowOffset + w + 1));
_indices.push_back(static_cast<unsigned int>(rowOffset + w));

_indices.push_back(static_cast<unsigned int>(rowOffset + w + mesh.width));
_indices.push_back(static_cast<unsigned int>(rowOffset + w + mesh.width + 1));
_indices.push_back(static_cast<unsigned int>(rowOffset + w + 1));
}
}
}

// Inherited via IIndexedModelSurface
int PatchSurface::getNumVertices() const
{
return static_cast<int>(_vertices.size());
}

int PatchSurface::getNumTriangles() const
{
return static_cast<int>(_indices.size() / 3); // 3 indices per triangle
}

const ArbitraryMeshVertex& PatchSurface::getVertex(int vertexNum) const
{
return _vertices[vertexNum];
}

ModelPolygon PatchSurface::getPolygon(int polygonIndex) const
{
assert(polygonIndex >= 0 && polygonIndex * 3 < static_cast<int>(_indices.size()));

ModelPolygon poly;

poly.a = _vertices[_indices[polygonIndex * 3]];
poly.b = _vertices[_indices[polygonIndex * 3 + 1]];
poly.c = _vertices[_indices[polygonIndex * 3 + 2]];

return poly;
}

const std::string& PatchSurface::getDefaultMaterial() const
{
return _materialName;
}

const std::string& PatchSurface::getActiveMaterial() const
{
return _materialName;
}

const std::vector<ArbitraryMeshVertex>& PatchSurface::getVertexArray() const
{
return _vertices;
}

const std::vector<unsigned int>& PatchSurface::getIndexArray() const
{
return _indices;
}

}
90 changes: 9 additions & 81 deletions radiantcore/model/export/PatchSurface.h
Expand Up @@ -6,13 +6,6 @@
namespace model
{

// Adapter methods to convert patch vertices to ArbitraryMeshVertex type
inline ArbitraryMeshVertex convertPatchVertex(const VertexNT& in)
{
// Colour will be set to 1,1,1 by the constructor
return ArbitraryMeshVertex(in.vertex, in.normal, in.texcoord);
}

/**
* Adapter class converting a PatchTesselation object into
* a surface suitable for exporting it to a model file.
Expand All @@ -28,84 +21,19 @@ class PatchSurface :
std::string _materialName;

public:
PatchSurface(const std::string& materialName, PatchMesh& mesh) :
_materialName(materialName)
{
assert(mesh.width >= 2 && mesh.height >= 2);

_vertices.reserve(mesh.vertices.size());
_indices.reserve((mesh.height - 1) * (mesh.width- 1) * 6); // 6 => 2 triangles per quad

// Load all the vertices into the target vector
std::transform(mesh.vertices.begin(), mesh.vertices.end(),
std::back_inserter(_vertices), convertPatchVertex);

// Generate the indices to define the triangles in clockwise order
for (std::size_t h = 0; h < mesh.height - 1; ++h)
{
auto rowOffset = h * mesh.width;

for (std::size_t w = 0; w < mesh.width - 1; ++w)
{
_indices.push_back(rowOffset + w + mesh.width);
_indices.push_back(rowOffset + w + 1);
_indices.push_back(rowOffset + w);

_indices.push_back(rowOffset + w + mesh.width);
_indices.push_back(rowOffset + w + mesh.width + 1);
_indices.push_back(rowOffset + w + 1);
}
}
}

// Inherited via IIndexedModelSurface
int getNumVertices() const override
{
return static_cast<int>(_vertices.size());
}

int getNumTriangles() const override
{
return static_cast<int>(_indices.size() / 3); // 3 indices per triangle
}

const ArbitraryMeshVertex& getVertex(int vertexNum) const override
{
return _vertices[vertexNum];
}

ModelPolygon getPolygon(int polygonIndex) const override
{
assert(polygonIndex >= 0 && polygonIndex * 3 < static_cast<int>(_indices.size()));

ModelPolygon poly;

poly.a = _vertices[_indices[polygonIndex * 3]];
poly.b = _vertices[_indices[polygonIndex * 3 + 1]];
poly.c = _vertices[_indices[polygonIndex * 3 + 2]];

return poly;
}
PatchSurface(const std::string& materialName, PatchMesh& mesh);

const std::string& getDefaultMaterial() const override
{
return _materialName;
}
int getNumVertices() const override;
int getNumTriangles() const override;

const std::string& getActiveMaterial() const override
{
return _materialName;
}
const ArbitraryMeshVertex& getVertex(int vertexNum) const override;
ModelPolygon getPolygon(int polygonIndex) const override;

const std::vector<ArbitraryMeshVertex>& getVertexArray() const override
{
return _vertices;
}
const std::string& getDefaultMaterial() const override;
const std::string& getActiveMaterial() const override;

const std::vector<unsigned int>& getIndexArray() const override
{
return _indices;
}
const std::vector<ArbitraryMeshVertex>& getVertexArray() const override;
const std::vector<unsigned int>& getIndexArray() const override;
};

}
1 change: 1 addition & 0 deletions tools/msvc/DarkRadiantCore.vcxproj
Expand Up @@ -145,6 +145,7 @@
<ClCompile Include="..\..\radiantcore\model\export\Lwo2Exporter.cpp" />
<ClCompile Include="..\..\radiantcore\model\export\ModelExporter.cpp" />
<ClCompile Include="..\..\radiantcore\model\export\ModelScalePreserver.cpp" />
<ClCompile Include="..\..\radiantcore\model\export\PatchSurface.cpp" />
<ClCompile Include="..\..\radiantcore\model\export\ScaledModelExporter.cpp" />
<ClCompile Include="..\..\radiantcore\model\export\WavefrontExporter.cpp" />
<ClCompile Include="..\..\radiantcore\model\md5\MD5Anim.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/DarkRadiantCore.vcxproj.filters
Expand Up @@ -997,6 +997,9 @@
<ClCompile Include="..\..\radiantcore\camera\CameraManager.cpp">
<Filter>src\camera</Filter>
</ClCompile>
<ClCompile Include="..\..\radiantcore\model\export\PatchSurface.cpp">
<Filter>src\model\export</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\radiantcore\modulesystem\ModuleLoader.h">
Expand Down

0 comments on commit 3c68f76

Please sign in to comment.