From 5e022ee1ea94812b3446cac76e4f9ff9f3856d8c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 23 Apr 2023 21:26:32 +0100 Subject: Add BufferedLocation Wraps Location, storing a pre-computed mat4 transformation of the position/rotation in a VBO handled by InstanceVertices. --- gfx/gl/bufferedLocation.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++ gfx/gl/bufferedLocation.h | 30 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 gfx/gl/bufferedLocation.cpp create mode 100644 gfx/gl/bufferedLocation.h (limited to 'gfx') diff --git a/gfx/gl/bufferedLocation.cpp b/gfx/gl/bufferedLocation.cpp new file mode 100644 index 0000000..4484053 --- /dev/null +++ b/gfx/gl/bufferedLocation.cpp @@ -0,0 +1,77 @@ +#include "bufferedLocation.h" +#include "location.hpp" +#include "maths.h" +#include + +BufferedLocation::BufferedLocation(InstanceVertices & i, glm::vec3 p, glm::vec3 r) : + BufferedLocation {i, Location {p, r}} +{ +} + +BufferedLocation::BufferedLocation(InstanceVertices & i, const Location & l) : + loc {l}, buffer {i.acquire(getTransform())} +{ +} + +BufferedLocation::operator const Location &() const +{ + return loc; +} + +BufferedLocation & +BufferedLocation::operator=(const Location & l) +{ + loc = l; + updateBuffer(); + return *this; +} + +glm::vec3 +BufferedLocation::position() const +{ + return loc.pos; +} + +glm::vec3 +BufferedLocation::rotation() const +{ + return loc.rot; +} + +void +BufferedLocation::setPosition(glm::vec3 p, bool update) +{ + loc.pos = p; + if (update) { + updateBuffer(); + } +} + +void +BufferedLocation::setRotation(glm::vec3 r, bool update) +{ + loc.rot = r; + if (update) { + updateBuffer(); + } +} + +void +BufferedLocation::setLocation(glm::vec3 p, glm::vec3 r) +{ + loc.pos = p; + loc.rot = r; + updateBuffer(); +} + +void +BufferedLocation::updateBuffer() +{ + buffer = getTransform(); +} + +glm::mat4 +BufferedLocation::getTransform() const +{ + return loc.getTransform(); +} diff --git a/gfx/gl/bufferedLocation.h b/gfx/gl/bufferedLocation.h new file mode 100644 index 0000000..cf7afed --- /dev/null +++ b/gfx/gl/bufferedLocation.h @@ -0,0 +1,30 @@ +#pragma once + +#include "instanceVertices.h" +#include "location.hpp" +#include +#include + +class BufferedLocation { +public: + BufferedLocation(InstanceVertices &, glm::vec3 = {}, glm::vec3 = {}); + BufferedLocation(InstanceVertices &, const Location &); + + BufferedLocation & operator=(const Location &); + + operator const Location &() const; + + glm::vec3 position() const; + glm::vec3 rotation() const; + void setPosition(glm::vec3, bool update = true); + void setRotation(glm::vec3, bool update = true); + void setLocation(glm::vec3, glm::vec3); + + glm::mat4 getTransform() const; + +private: + void updateBuffer(); + + Location loc; + InstanceVertices::InstanceProxy buffer; +}; -- cgit v1.2.3