Created
October 16, 2021 20:32
-
-
Save Koltonix/a1e61917e2997215a75575f26f5cf7f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ErrorHandler.h" | |
#include "ModelObject.h" | |
#include "Polygon.h" | |
#include "PolygonClipping.h" | |
#include "SceneSystem.h" | |
#include "WallMesh.h" | |
#include <array> | |
#include <iostream> | |
PolygonClipping::~PolygonClipping() | |
{ | |
if (m_clippingAlgorithm) | |
delete(m_clippingAlgorithm); | |
if (m_triangulationAlgorithm) | |
delete(m_triangulationAlgorithm); | |
} | |
WallMesh* PolygonClipping::ClipMesh(WallMesh* mesh, Vector2 position) | |
{ | |
m_vertexColour = mesh->m_wallColour; | |
Polygon offsetCutter = OffsetCutter(m_cutter, position); | |
// Difference Calculation for the Vertices. | |
mesh->m_polygons = m_clippingAlgorithm->ExecuteDifference(mesh->m_polygons, offsetCutter); | |
std::vector<Vertex> vertices = CalculateVertices(mesh->m_polygons); | |
std::vector<GLuint> indices = CalculateTriangles(mesh->m_polygons); | |
if (vertices.size() < 1) | |
{ | |
ErrorHandler::DisplayWarning("CLIPPING FAILED -> RESTORING ORIGINAL MESH"); | |
return mesh; | |
} | |
WallMesh* wall = new WallMesh(vertices, indices, mesh->GetMaterial(), mesh->m_polygons); | |
return wall; | |
} | |
std::vector<GLuint> PolygonClipping::CalculateTriangles(Polygons polygons) | |
{ | |
std::vector<GLuint> indices = m_triangulationAlgorithm->Triangulate(polygons); | |
return indices; | |
} | |
std::vector<Vertex> PolygonClipping::CalculateVertices(Polygons polygons) | |
{ | |
std::vector<Vertex> vertices = std::vector<Vertex>(); | |
for (Polygon polygon : polygons.paths) | |
{ | |
for (Vector2 point : polygon.path) | |
vertices.push_back(Vertex(point.x, point.y, 0.0f, m_vertexColour)); | |
} | |
return vertices; | |
} | |
Polygon PolygonClipping::OffsetCutter(Polygon cutter, Vector2 offset) | |
{ | |
Polygon offsetCutter; | |
for (Vector2 path : cutter.path) | |
offsetCutter.path.push_back(path + offset); | |
return offsetCutter; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ClipperAlgorithm.h" | |
#include "EarcutAlgorithm.h" | |
#include "IClip.h" | |
#include "ITriangulate.h" | |
#include "Polygon.h" | |
#include "Polygons.h" | |
#include "Vertex.h" | |
#include "Vector2.h" | |
class WallMesh; | |
#include <vector> | |
class PolygonClipping | |
{ | |
public: | |
PolygonClipping() {}; | |
~PolygonClipping(); | |
// Regular Meshes are *NOT SUPPORTED* | |
WallMesh* ClipMesh(WallMesh* mesh, Vector2 position); | |
void SetCutter(Polygon cutter) { m_cutter = cutter; } | |
std::vector<GLuint> CalculateTriangles(Polygons polygons); | |
std::vector<Vertex> CalculateVertices(Polygons polygons); | |
Polygon OffsetCutter(Polygon cutter, Vector2 offset); | |
private: | |
// The Cutter *MUST* be a closed path. A point or line is not valid. | |
// Must be from a scale of -1.0f to 1.0f. | |
Polygon m_cutter = Polygon( | |
{ | |
Vector2(-0.1f, 0.0f), | |
Vector2(-0.2f, -0.1f), | |
Vector2(-0.15f, -0.15f), | |
Vector2(0.0f, -0.1f), | |
Vector2(0.1f, -0.12f), | |
Vector2(0.15f, 0.0f), | |
Vector2(0.2f, 0.1f), | |
Vector2(0.15f, 0.2f), | |
Vector2(0.0f, 0.15f), | |
Vector2(-0.1f, 0.2f), | |
Vector2(-0.17f, 0.1f) | |
}); | |
Colour m_vertexColour = Colour(); | |
IClip* m_clippingAlgorithm = new ClipperAlgorithm(); | |
ITriangulate* m_triangulationAlgorithm = new EarcutAlgorithm(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment