diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-03-03 00:08:47 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-03-03 00:08:47 +0000 |
commit | 94311f9c4e82b7475802b1934cc0c5b243e0cd2f (patch) | |
tree | 5ef23f6c1151917eb723b67c262da0168e42343c | |
parent | Our own matrix rotations (diff) | |
download | ilt-94311f9c4e82b7475802b1934cc0c5b243e0cd2f.tar.bz2 ilt-94311f9c4e82b7475802b1934cc0c5b243e0cd2f.tar.xz ilt-94311f9c4e82b7475802b1934cc0c5b243e0cd2f.zip |
Replace Transform with Location
Simpler, unbinds the transformation matrices for location, now done just in the shader.
-rw-r--r-- | game/network/link.h | 4 | ||||
-rw-r--r-- | game/network/rail.cpp | 11 | ||||
-rw-r--r-- | game/network/rail.h | 6 | ||||
-rw-r--r-- | game/terrain.cpp | 9 | ||||
-rw-r--r-- | game/vehicles/railloco.cpp | 16 | ||||
-rw-r--r-- | game/vehicles/railloco.h | 6 | ||||
-rw-r--r-- | game/vehicles/vehicle.h | 4 | ||||
-rw-r--r-- | gfx/followCameraController.cpp | 4 | ||||
-rw-r--r-- | gfx/gl/shader.cpp | 6 | ||||
-rw-r--r-- | gfx/gl/shader.h | 4 | ||||
-rw-r--r-- | gfx/gl/transform.cpp | 16 | ||||
-rw-r--r-- | gfx/gl/transform.h | 53 | ||||
-rw-r--r-- | lib/location.hpp | 14 |
13 files changed, 51 insertions, 102 deletions
diff --git a/game/network/link.h b/game/network/link.h index 4968eba..e541975 100644 --- a/game/network/link.h +++ b/game/network/link.h @@ -2,8 +2,8 @@ #define LINK_H #include <array> -#include <gfx/gl/transform.h> #include <glm/glm.hpp> +#include <location.hpp> #include <memory> #include <special_members.hpp> #include <utility> @@ -39,7 +39,7 @@ public: NO_COPY(Link); NO_MOVE(Link); - [[nodiscard]] virtual Transform positionAt(float dist, unsigned char start) const = 0; + [[nodiscard]] virtual Location positionAt(float dist, unsigned char start) const = 0; std::array<End, 2> ends; float length; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 55d57e4..6834989 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -9,6 +9,7 @@ #include <gfx/models/vertex.hpp> #include <glm/gtx/transform.hpp> #include <initializer_list> +#include <location.hpp> #include <maths.h> #include <stdexcept> #include <type_traits> @@ -97,7 +98,7 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) void RailLinks::render(const Shader & shader) const { - shader.setModel(glm::identity<glm::mat4>()); + shader.setModel(Location {}); texture->Bind(); links.apply(&RailLink::render, shader); } @@ -158,13 +159,13 @@ RailLinkStraight::RailLinkStraight(NodePtr a, NodePtr b, const glm::vec3 & diff) defaultMesh(); } -Transform +Location RailLinkStraight::positionAt(float dist, unsigned char start) const { const auto es {std::make_pair(ends[start].first.get(), ends[1 - start].first.get())}; const auto diff {es.second->pos - es.first->pos}; const auto dir {glm::normalize(diff)}; - return Transform {es.first->pos + RAIL_HEIGHT + dir * dist, {-vector_pitch(dir), vector_yaw(dir), 0}}; + return Location {es.first->pos + RAIL_HEIGHT + dir * dist, {-vector_pitch(dir), vector_yaw(dir), 0}}; } RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec2 c) : @@ -197,7 +198,7 @@ RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec3 c, defaultMesh(); } -Transform +Location RailLinkCurve::positionAt(float dist, unsigned char start) const { static constexpr std::array<float, 2> dirOffset {half_pi, -half_pi}; @@ -210,5 +211,5 @@ RailLinkCurve::positionAt(float dist, unsigned char start) const const auto relClimb {RAIL_HEIGHT + glm::vec3 {0, -centreBase.y + es.first->pos.y + ((es.second->pos.y - es.first->pos.y) * frac), 0}}; const auto pitch {vector_pitch({0, (es.first->pos.y - es.second->pos.y) / length, 0})}; - return Transform {relPos + relClimb + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}}; + return Location {relPos + relClimb + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}}; } diff --git a/game/network/rail.h b/game/network/rail.h index 5837ac1..d25964d 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -3,12 +3,12 @@ #include "collection.hpp" #include "game/worldobject.h" -#include "gfx/gl/transform.h" #include "gfx/models/mesh.h" #include "gfx/models/vertex.hpp" #include "gfx/renderable.h" #include "link.h" #include <glm/glm.hpp> +#include <location.hpp> #include <maths.h> #include <memory> #include <set> @@ -37,7 +37,7 @@ protected: class RailLinkStraight : public RailLink { public: RailLinkStraight(const NodePtr &, const NodePtr &); - [[nodiscard]] Transform positionAt(float dist, unsigned char start) const override; + [[nodiscard]] Location positionAt(float dist, unsigned char start) const override; private: RailLinkStraight(NodePtr, NodePtr, const glm::vec3 & diff); @@ -46,7 +46,7 @@ private: class RailLinkCurve : public RailLink { public: RailLinkCurve(const NodePtr &, const NodePtr &, glm::vec2); - [[nodiscard]] Transform positionAt(float dist, unsigned char start) const override; + [[nodiscard]] Location positionAt(float dist, unsigned char start) const override; private: RailLinkCurve(const NodePtr &, const NodePtr &, glm::vec3, const Arc); diff --git a/game/terrain.cpp b/game/terrain.cpp index 44fd41d..bf16439 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -3,11 +3,11 @@ #include <cache.h> #include <cmath> #include <gfx/gl/shader.h> -#include <gfx/gl/transform.h> #include <gfx/image.h> #include <gfx/models/mesh.h> #include <gfx/models/vertex.hpp> #include <glm/glm.hpp> +#include <location.hpp> #include <random> #include <stb_image.h> @@ -121,9 +121,6 @@ Terrain::finish(unsigned int width, unsigned int height, std::vector<Vertex> & v meshes.create<Mesh>(vertices, indices); } -static const Transform identity {}; -static const auto identityModel {identity.GetModel()}; - void Terrain::tick(TickDuration dur) { @@ -133,11 +130,11 @@ Terrain::tick(TickDuration dur) void Terrain::render(const Shader & shader) const { - shader.setModel(identityModel, Shader::Program::LandMass); + shader.setModel(Location {}, Shader::Program::LandMass); grass->Bind(); meshes.apply(&Mesh::Draw); - shader.setModel(identityModel, Shader::Program::Water); + shader.setModel(Location {}, Shader::Program::Water); shader.setUniform("waves", {waveCycle, 0, 0}); water->Bind(); meshes.apply(&Mesh::Draw); diff --git a/game/vehicles/railloco.cpp b/game/vehicles/railloco.cpp index bc62d43..de47ebb 100644 --- a/game/vehicles/railloco.cpp +++ b/game/vehicles/railloco.cpp @@ -1,6 +1,5 @@ #include "railloco.h" #include "gfx/gl/shader.h" -#include "gfx/gl/transform.h" #include "gfx/models/obj.h" #include "gfx/models/texture.h" #include <algorithm> @@ -10,6 +9,7 @@ #include <glm/glm.hpp> #include <iterator> #include <lib/resource.h> +#include <location.hpp> #include <map> #include <maths.h> #include <memory> @@ -23,10 +23,10 @@ RailVehicle::render(const Shader & shader) const { texture->Bind(); for (const auto & bogie : bogies) { - shader.setModel(bogie.location.GetModel()); + shader.setModel(bogie.location); bogie.mesh->Draw(); } - shader.setModel(location.GetModel()); + shader.setModel(location); bodyMesh->Draw(); } @@ -39,7 +39,7 @@ RailLoco::move(TickDuration dur) while (linkDist > curLink.first->length) { location = curLink.first->positionAt(curLink.first->length, curLink.second); auto nexts {curLink.first->nexts[1 - curLink.second]}; - auto last = std::remove_if(nexts.begin(), nexts.end(), [ang = location.GetRot().y](const Link::Next & n) { + auto last = std::remove_if(nexts.begin(), nexts.end(), [ang = location.rot.y](const Link::Next & n) { return std::abs(normalize(n.first.lock()->ends[n.second].second - ang)) > 0.1F; }); if (last != nexts.begin()) { @@ -54,7 +54,7 @@ RailLoco::move(TickDuration dur) } } -Transform +Location RailLoco::getBogiePosition(float linkDist, float dist) const { float b2linkDist {}; @@ -68,9 +68,9 @@ RailLoco::updateRailVehiclePosition(RailVehicle * w, float trailBy) const const auto overhang {(w->length - w->wheelBase) / 2}; const auto & b1Pos = w->bogies[0].location = getBogiePosition(linkDist, trailBy += overhang); const auto & b2Pos = w->bogies[1].location = getBogiePosition(linkDist, trailBy + wheelBase); - const auto diff = glm::normalize(b2Pos.GetPos() - b1Pos.GetPos()); - w->location.GetPos() = (b1Pos.GetPos() + b2Pos.GetPos()) / 2.F; - w->location.GetRot() = {-vector_pitch(diff), vector_yaw(diff), 0}; + const auto diff = glm::normalize(b2Pos.pos - b1Pos.pos); + w->location.pos = (b1Pos.pos + b2Pos.pos) / 2.F; + w->location.rot = {-vector_pitch(diff), vector_yaw(diff), 0}; } void diff --git a/game/vehicles/railloco.h b/game/vehicles/railloco.h index ddc706f..f08de05 100644 --- a/game/vehicles/railloco.h +++ b/game/vehicles/railloco.h @@ -1,9 +1,9 @@ #include "game/network/link.h" #include "game/vehicles/vehicle.h" #include "game/worldobject.h" -#include "gfx/gl/transform.h" #include "gfx/models/mesh.h" #include <array> +#include <location.hpp> #include <memory> #include <vector> @@ -13,7 +13,7 @@ class Texture; class RailVehicle : public Vehicle { public: struct Bogie { - Transform location; + Location location; MeshPtr mesh; }; @@ -45,7 +45,7 @@ public: private: void move(TickDuration dur); - [[nodiscard]] Transform getBogiePosition(float linkDist, float dist) const; + [[nodiscard]] Location getBogiePosition(float linkDist, float dist) const; void updateRailVehiclePosition(RailVehicle *, float trailBy) const; void updateWagons() const; }; diff --git a/game/vehicles/vehicle.h b/game/vehicles/vehicle.h index 228f15a..a511d3c 100644 --- a/game/vehicles/vehicle.h +++ b/game/vehicles/vehicle.h @@ -1,10 +1,10 @@ #ifndef VEHICLE_H #define VEHICLE_H -#include "gfx/gl/transform.h" #include <game/network/link.h> #include <game/worldobject.h> #include <gfx/renderable.h> +#include <location.hpp> #include <memory> #include <utility> #include <vector> @@ -28,7 +28,7 @@ public: float linkDist; // distance long current link float speed {50}; // speed in m/s (~75 km/h) - Transform location; + Location location; protected: LinkHistory linkHist; diff --git a/gfx/followCameraController.cpp b/gfx/followCameraController.cpp index 25861b6..1ee385a 100644 --- a/gfx/followCameraController.cpp +++ b/gfx/followCameraController.cpp @@ -1,8 +1,8 @@ #include "followCameraController.h" #include "game/vehicles/vehicle.h" -#include "gfx/gl/transform.h" #include <gfx/gl/camera.h> #include <glm/glm.hpp> +#include <location.hpp> #include <maths.h> #include <memory> #include <tuple> @@ -15,7 +15,7 @@ FollowCameraController::updateCamera(Camera * camera) const { const auto [pos, rot] = [this]() { const auto t {target.lock()}; - return std::tie(t->location.GetPos(), t->location.GetRot()); + return std::tie(t->location.pos, t->location.rot); }(); switch (mode) { diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index f30af5c..04d5ac6 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -8,6 +8,9 @@ #include <gfx/gl/shaders/vs-landmassShader.h>
#include <gfx/gl/shaders/vs-waterShader.h>
#include <glm/glm.hpp>
+#include <glm/gtx/transform.hpp>
+#include <location.hpp>
+#include <maths.h>
#include <stdexcept>
#include <string>
@@ -67,10 +70,11 @@ Shader::setUniform(const GLchar * uniform, glm::vec3 v) const }
void
-Shader::setModel(glm::mat4 model, Program pid) const
+Shader::setModel(const Location & loc, Program pid) const
{
auto & prog = programs[(int)pid];
glUseProgram(prog.m_program);
+ const auto model {glm::translate(loc.pos) * rotate_ypr(loc.rot)};
glUniformMatrix4fv(prog.model_uniform, 1, GL_FALSE, &model[0][0]);
}
diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index e4904ed..f1fe705 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -7,6 +7,8 @@ #include <glm/glm.hpp>
#include <string_view>
+class Location;
+
class Shader {
public:
enum class Program { Basic = 0, Water = 1, LandMass = 2 };
@@ -14,7 +16,7 @@ public: Shader();
void setView(glm::mat4 view) const;
- void setModel(glm::mat4 model, Program = Program::Basic) const;
+ void setModel(const Location &, Program = Program::Basic) const;
void setUniform(const GLchar *, glm::vec3 dir) const;
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage);
diff --git a/gfx/gl/transform.cpp b/gfx/gl/transform.cpp deleted file mode 100644 index c7004ba..0000000 --- a/gfx/gl/transform.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "transform.h" -#include <glm/gtx/transform.hpp> -#include <maths.h> - -Transform::Transform(glm::vec3 pos, glm::vec3 rot) : pos {pos}, rot {rot} { } - -glm::mat4 -Transform::GetModel() const -{ - const auto posMat = glm::translate(pos); - const auto rotX = glm::rotate(rot.x, west); - const auto rotY = glm::rotate(rot.y, up); - const auto rotZ = glm::rotate(rot.z, north); - - return posMat * rotY * rotX * rotZ; -} diff --git a/gfx/gl/transform.h b/gfx/gl/transform.h deleted file mode 100644 index 61b571d..0000000 --- a/gfx/gl/transform.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef TRANSFORM_INCLUDED_H
-#define TRANSFORM_INCLUDED_H
-
-#include <glm/glm.hpp>
-
-class Transform {
-public:
- explicit Transform(glm::vec3 pos = {}, glm::vec3 rot = {});
-
- [[nodiscard]] glm::mat4 GetModel() const;
-
- [[nodiscard]] inline glm::vec3 &
- GetPos()
- {
- return pos;
- }
-
- [[nodiscard]] inline const glm::vec3 &
- GetPos() const
- {
- return pos;
- }
-
- [[nodiscard]] inline glm::vec3 &
- GetRot()
- {
- return rot;
- }
-
- [[nodiscard]] inline const glm::vec3 &
- GetRot() const
- {
- return rot;
- }
-
- inline void
- SetPos(glm::vec3 && pos)
- {
- this->pos = pos;
- }
-
- inline void
- SetRot(glm::vec3 && rot)
- {
- this->rot = rot;
- }
-
-private:
- glm::vec3 pos;
- glm::vec3 rot;
-};
-
-#endif
diff --git a/lib/location.hpp b/lib/location.hpp new file mode 100644 index 0000000..b019b7a --- /dev/null +++ b/lib/location.hpp @@ -0,0 +1,14 @@ +#ifndef LOCATION_H +#define LOCATION_H + +#include <glm/glm.hpp> + +class Location { +public: + explicit Location(glm::vec3 pos = {}, glm::vec3 rot = {}) : pos {pos}, rot {rot} { } + + glm::vec3 pos; + glm::vec3 rot; +}; + +#endif |