From a4c4758a6e5b021187d786717e4734e3f7a1edad Mon Sep 17 00:00:00 2001 From: Neal Kruis Date: Thu, 31 Aug 2023 14:15:57 -0600 Subject: [PATCH] Move implementation source into separate files. Minor clang-tidy changes. --- examples/awning.cpp | 3 +- src/gl/context.cpp | 6 +-- src/penumbra-implementation.cpp | 34 +++++++++++++++++ src/penumbra.cpp | 16 -------- src/surface-implementation.cpp | 65 ++++++++++++++++++++++++++++++++ src/surface.cpp | 67 --------------------------------- test/penumbra_test.cpp | 2 +- 7 files changed, 105 insertions(+), 88 deletions(-) create mode 100644 src/penumbra-implementation.cpp create mode 100644 src/surface-implementation.cpp diff --git a/examples/awning.cpp b/examples/awning.cpp index dfabc00..db57ede 100644 --- a/examples/awning.cpp +++ b/examples/awning.cpp @@ -8,6 +8,7 @@ #include class AwningLogger : public Penumbra::PenumbraLogger { +public: void info(const std::string_view message) override { write_message("Info", message); } @@ -27,7 +28,7 @@ int main() { Penumbra::Penumbra::is_valid_context(); - std::shared_ptr logger = std::make_shared(); + std::shared_ptr logger = std::make_shared(); Penumbra::Penumbra penumbra(512u, logger); unsigned wall_id = penumbra.add_surface(wall); diff --git a/src/gl/context.cpp b/src/gl/context.cpp index cae7704..d617aae 100644 --- a/src/gl/context.cpp +++ b/src/gl/context.cpp @@ -142,8 +142,8 @@ Context::Context(GLint size_in, Courierr::Courierr *logger_in) : size(size_in), }; auto mouse_callback = [](GLFWwindow *w, int button, int action, int /*mods*/) { - // Set a booleon to tell if the left button is down. And at the actual press, initialize the - // curosor position. Note that you only come in here the moments the button is pressed and + // Set a boolean to tell if the left button is down. And at the actual press, initialize the + // cursor position. Note that you only come in here the moments the button is pressed and // released, not between. if (button == GLFW_MOUSE_BUTTON_LEFT) { @@ -305,7 +305,7 @@ float Context::set_scene(mat4x4 sun_view, const SurfaceBuffer *surface_buffer, b mat4x4_dup(view, sun_view); - // calculate clipping planes in rendered coorinates + // calculate clipping planes in rendered coordinates left = MAX_FLOAT; right = -MAX_FLOAT; bottom = MAX_FLOAT; diff --git a/src/penumbra-implementation.cpp b/src/penumbra-implementation.cpp new file mode 100644 index 0000000..21dbe27 --- /dev/null +++ b/src/penumbra-implementation.cpp @@ -0,0 +1,34 @@ +/* Copyright (c) 2017 Big Ladder Software LLC. All rights reserved. + * See the LICENSE file for additional terms and conditions. */ + +// Standard +#include + +#ifndef NDEBUG +#ifdef __unix__ +#include +#endif +#endif + +// Penumbra +#include "penumbra-implementation.h" + +namespace Penumbra { + +PenumbraImplementation::PenumbraImplementation(int size, + const std::shared_ptr &logger_in) + : context(size, logger_in.get()), logger(logger_in) {} + +void PenumbraImplementation::add_surface(const Surface &surface) { + surface.surface->logger = logger; + if (surface.surface->name.empty()) { + surface.surface->name = fmt::format("Surface {}", surfaces.size()); + } + surfaces.push_back(*surface.surface); +} + +bool PenumbraImplementation::check_surface(const unsigned index) const { + return index < surfaces.size(); +} + +} // namespace Penumbra diff --git a/src/penumbra.cpp b/src/penumbra.cpp index eaefde8..fa984e1 100644 --- a/src/penumbra.cpp +++ b/src/penumbra.cpp @@ -278,20 +278,4 @@ std::shared_ptr Penumbra::get_logger() { return penumbra->logger; } -PenumbraImplementation::PenumbraImplementation(int size, - const std::shared_ptr &logger_in) - : context(size, logger_in.get()), logger(logger_in) {} - -void PenumbraImplementation::add_surface(const Surface &surface) { - surface.surface->logger = logger; - if (surface.surface->name.empty()) { - surface.surface->name = fmt::format("Surface {}", surfaces.size()); - } - surfaces.push_back(*surface.surface); -} - -bool PenumbraImplementation::check_surface(const unsigned index) const { - return index < surfaces.size(); -} - } // namespace Penumbra diff --git a/src/surface-implementation.cpp b/src/surface-implementation.cpp new file mode 100644 index 0000000..df92525 --- /dev/null +++ b/src/surface-implementation.cpp @@ -0,0 +1,65 @@ +/* Copyright (c) 2017 Big Ladder Software LLC. All rights reserved. + * See the LICENSE file for additional terms and conditions. */ + +// Standard +#include +#include + +// Penumbra +#include +#include "surface-implementation.h" +#include + +namespace Penumbra { + +TessData::TessData(const float *array, unsigned number_of_vertices) + : number_of_vertices(number_of_vertices) { + vertices.insert(vertices.end(), (const float *)array, (const float *)array + number_of_vertices); +} + +SurfaceImplementation::SurfaceImplementation(Polygon polygon) : polygon(std::move(polygon)) {} + +TessData SurfaceImplementation::tessellate() { + TESStesselator *tess = tessNewTess(nullptr); + + if (!tess) { + throw PenumbraException(fmt::format("Unable to create tessellator for surface, \"{}\".", name), + *logger); + } + + // Add primary polygon + tessAddContour(tess, TessData::polygon_size, &polygon[0], sizeof(float) * TessData::vertex_size, + (int)polygon.size() / TessData::vertex_size); + + // Add holes + for (auto &hole : holes) { + tessAddContour(tess, TessData::polygon_size, &hole[0], sizeof(float) * TessData::vertex_size, + (int)hole.size() / TessData::vertex_size); + } + + if (!tessTesselate(tess, TESS_WINDING_ODD, TESS_POLYGONS, TessData::polygon_size, + TessData::vertex_size, nullptr)) { + throw PenumbraException(fmt::format("Unable to tessellate surface, \"{}\".", name), *logger); + } + + // For now convert to glDrawArrays() style of vertices, sometime may change to glDrawElements + // (with element buffers) + std::vector vertex_array; + const TESSreal *vertices = tessGetVertices(tess); + const int number_of_elements = tessGetElementCount(tess); + const TESSindex *elements = tessGetElements(tess); + for (int i = 0; i < number_of_elements * TessData::polygon_size; ++i) { + const int vertex = *(elements + i); + for (int j = 0; j < TessData::vertex_size; ++j) { + vertex_array.push_back(vertices[vertex * TessData::vertex_size + j]); + } + } + + TessData data(&vertex_array[0], static_cast(vertex_array.size())); + + tessDeleteTess(tess); + + return data; +} + +} // namespace Penumbra diff --git a/src/surface.cpp b/src/surface.cpp index 088419e..15a65dd 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -8,31 +8,9 @@ // Penumbra #include #include -#include namespace Penumbra { -/* -void* stdAlloc(void* userData, unsigned size) -{ - int* allocated = ( int*)userData; - TESS_NOTUSED(userData); - *allocated += (int)size; - return malloc(size); -} - -void stdFree(void* userData, void* ptr) -{ - TESS_NOTUSED(userData); - free(ptr); -} -*/ - -TessData::TessData(const float *array, unsigned number_of_vertices) - : number_of_vertices(number_of_vertices) { - vertices.insert(vertices.end(), (const float *)array, (const float *)array + number_of_vertices); -} - Surface::Surface() { surface = std::make_shared(); } @@ -52,49 +30,4 @@ void Surface::add_hole(const Polygon &hole) { surface->holes.push_back(hole); } -SurfaceImplementation::SurfaceImplementation(Polygon polygon) : polygon(std::move(polygon)) {} - -TessData SurfaceImplementation::tessellate() { - TESStesselator *tess = tessNewTess(nullptr); - - if (!tess) { - throw PenumbraException(fmt::format("Unable to create tessellator for surface, \"{}\".", name), - *logger); - } - - // Add primary polygon - tessAddContour(tess, TessData::polygon_size, &polygon[0], sizeof(float) * TessData::vertex_size, - (int)polygon.size() / TessData::vertex_size); - - // Add holes - for (auto &hole : holes) { - tessAddContour(tess, TessData::polygon_size, &hole[0], sizeof(float) * TessData::vertex_size, - (int)hole.size() / TessData::vertex_size); - } - - if (!tessTesselate(tess, TESS_WINDING_ODD, TESS_POLYGONS, TessData::polygon_size, - TessData::vertex_size, nullptr)) { - throw PenumbraException(fmt::format("Unable to tessellate surface, \"{}\".", name), *logger); - } - - // For now convert to glDrawArrays() style of vertices, sometime may change to glDrawElements - // (with element buffers) - std::vector vertex_array; - const TESSreal *verticies = tessGetVertices(tess); - const int number_of_elements = tessGetElementCount(tess); - const TESSindex *elements = tessGetElements(tess); - for (int i = 0; i < number_of_elements * TessData::polygon_size; ++i) { - const int vertex = *(elements + i); - for (int j = 0; j < TessData::vertex_size; ++j) { - vertex_array.push_back(verticies[vertex * TessData::vertex_size + j]); - } - } - - TessData data(&vertex_array[0], static_cast(vertex_array.size())); - - tessDeleteTess(tess); - - return data; -} - } // namespace Penumbra diff --git a/test/penumbra_test.cpp b/test/penumbra_test.cpp index a52eaf1..76f2a7f 100644 --- a/test/penumbra_test.cpp +++ b/test/penumbra_test.cpp @@ -15,7 +15,7 @@ constexpr float m_pi_4_f = static_cast(M_PI_4); float calculate_surface_exposure(float azimuth, float altitude) { - // Due to the one sided nature of shaded surfaces, we will revert + // Due to the one-sided nature of shaded surfaces, we will revert // negative values to zero, as this indicates that the opposite side // of the surface is shaded. This shading should be disregarded. auto incident_azimuth = cos(azimuth);