Skip to content

Commit

Permalink
Move implementation source into separate files. Minor clang-tidy chan…
Browse files Browse the repository at this point in the history
…ges.
  • Loading branch information
nealkruis committed Aug 31, 2023
1 parent f2084e8 commit a4c4758
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 88 deletions.
3 changes: 2 additions & 1 deletion examples/awning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <penumbra/penumbra.h>

class AwningLogger : public Penumbra::PenumbraLogger {
public:
void info(const std::string_view message) override {
write_message("Info", message);
}
Expand All @@ -27,7 +28,7 @@ int main() {

Penumbra::Penumbra::is_valid_context();

std::shared_ptr<Penumbra::PenumbraLogger> logger = std::make_shared<Penumbra::PenumbraLogger>();
std::shared_ptr<AwningLogger> logger = std::make_shared<AwningLogger>();
Penumbra::Penumbra penumbra(512u, logger);

unsigned wall_id = penumbra.add_surface(wall);
Expand Down
6 changes: 3 additions & 3 deletions src/gl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions src/penumbra-implementation.cpp
Original file line number Diff line number Diff line change
@@ -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 <memory>

#ifndef NDEBUG
#ifdef __unix__
#include <cfenv>
#endif
#endif

// Penumbra
#include "penumbra-implementation.h"

namespace Penumbra {

PenumbraImplementation::PenumbraImplementation(int size,
const std::shared_ptr<Courierr::Courierr> &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
16 changes: 0 additions & 16 deletions src/penumbra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,4 @@ std::shared_ptr<Courierr::Courierr> Penumbra::get_logger() {
return penumbra->logger;
}

PenumbraImplementation::PenumbraImplementation(int size,
const std::shared_ptr<Courierr::Courierr> &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
65 changes: 65 additions & 0 deletions src/surface-implementation.cpp
Original file line number Diff line number Diff line change
@@ -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 <vector>
#include <array>

// Penumbra
#include <penumbra/surface.h>
#include "surface-implementation.h"
#include <penumbra/logging.h>

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<float> 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<unsigned int>(vertex_array.size()));

tessDeleteTess(tess);

return data;
}

} // namespace Penumbra
67 changes: 0 additions & 67 deletions src/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,9 @@
// Penumbra
#include <penumbra/surface.h>
#include <surface-implementation.h>
#include <penumbra/logging.h>

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<SurfaceImplementation>();
}
Expand All @@ -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<float> 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<unsigned int>(vertex_array.size()));

tessDeleteTess(tess);

return data;
}

} // namespace Penumbra
2 changes: 1 addition & 1 deletion test/penumbra_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ constexpr float m_pi_4_f = static_cast<float>(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);
Expand Down

0 comments on commit a4c4758

Please sign in to comment.