diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-01-01 17:56:26 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-01-01 17:56:26 +0000 |
commit | d5cdbbf38380239524e351cb69aec94090884ca5 (patch) | |
tree | 5d7dff2f2775701069806eceb4eaef23b22eba3f /gfx | |
parent | Reformat with new clang-format (diff) | |
parent | Remove more use of legacy types (diff) | |
download | ilt-d5cdbbf38380239524e351cb69aec94090884ca5.tar.bz2 ilt-d5cdbbf38380239524e351cb69aec94090884ca5.tar.xz ilt-d5cdbbf38380239524e351cb69aec94090884ca5.zip |
Merge remote-tracking branch 'origin/terrain'
Diffstat (limited to 'gfx')
37 files changed, 302 insertions, 248 deletions
diff --git a/gfx/followCameraController.cpp b/gfx/followCameraController.cpp index 5b08483..5114840 100644 --- a/gfx/followCameraController.cpp +++ b/gfx/followCameraController.cpp @@ -24,11 +24,11 @@ FollowCameraController::updateCamera(Camera * camera) const break; case Mode::Ride: - camera->setView(pos + (up * 4.8F), !-sincosf(rot.y)); + camera->setView(pos + GlobalPosition3D(up * 4.8F), -sincosf(rot.y) || 0.F); break; case Mode::ISO: - camera->setView(pos + ((up + north + east) * 40.F), glm::normalize(down + south + west), + camera->setView(pos + GlobalPosition3D((up + north + east) * 40.F), glm::normalize(down + south + west), glm::normalize(up - north - east)); break; } diff --git a/gfx/gl/bufferedLocation.cpp b/gfx/gl/bufferedLocation.cpp index eb3dac3..f1bedfe 100644 --- a/gfx/gl/bufferedLocation.cpp +++ b/gfx/gl/bufferedLocation.cpp @@ -1,9 +1,8 @@ #include "bufferedLocation.h" #include "location.h" -#include "maths.h" #include <glm/gtx/transform.hpp> -BufferedLocation::BufferedLocation(glm::vec3 p, glm::vec3 r) : BufferedLocation {Location {p, r}} { } +BufferedLocation::BufferedLocation(GlobalPosition3D p, Rotation3D r) : BufferedLocation {Location {p, r}} { } BufferedLocation::BufferedLocation(const Location & l) : loc {l} { } @@ -20,20 +19,20 @@ BufferedLocation::operator=(const Location & l) return *this; } -glm::vec3 +GlobalPosition3D BufferedLocation::position() const { return loc.pos; } -glm::vec3 +Rotation3D BufferedLocation::rotation() const { return loc.rot; } void -BufferedLocation::setPosition(glm::vec3 p, bool update) +BufferedLocation::setPosition(GlobalPosition3D p, bool update) { loc.pos = p; if (update) { @@ -42,7 +41,7 @@ BufferedLocation::setPosition(glm::vec3 p, bool update) } void -BufferedLocation::setRotation(glm::vec3 r, bool update) +BufferedLocation::setRotation(Rotation3D r, bool update) { loc.rot = r; if (update) { @@ -51,7 +50,7 @@ BufferedLocation::setRotation(glm::vec3 r, bool update) } void -BufferedLocation::setLocation(glm::vec3 p, glm::vec3 r) +BufferedLocation::setLocation(GlobalPosition3D p, Rotation3D r) { loc.pos = p; loc.rot = r; @@ -59,7 +58,13 @@ BufferedLocation::setLocation(glm::vec3 p, glm::vec3 r) } glm::mat4 -BufferedLocation::getTransform() const +BufferedLocation::getRotationTransform() const { - return loc.getTransform(); + return loc.getRotationTransform(); +} + +void +BufferedLocationUpdater::updateBuffer() const +{ + onUpdate(this); } diff --git a/gfx/gl/bufferedLocation.h b/gfx/gl/bufferedLocation.h index 8096489..87b957f 100644 --- a/gfx/gl/bufferedLocation.h +++ b/gfx/gl/bufferedLocation.h @@ -4,11 +4,11 @@ #include <functional> #include <glm/mat4x4.hpp> #include <glm/vec3.hpp> -#include <tuple> +#include <utility> class BufferedLocation { public: - BufferedLocation(glm::vec3 = {}, glm::vec3 = {}); + BufferedLocation(GlobalPosition3D = {}, Rotation3D = {}); BufferedLocation(const Location &); virtual ~BufferedLocation() = default; @@ -16,25 +16,25 @@ public: 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); + [[nodiscard]] GlobalPosition3D position() const; + [[nodiscard]] Rotation3D rotation() const; + void setPosition(GlobalPosition3D, bool update = true); + void setRotation(Rotation3D, bool update = true); + void setLocation(GlobalPosition3D, Rotation3D); - glm::mat4 getTransform() const; + [[nodiscard]] glm::mat4 getRotationTransform() const; private: - virtual void updateBuffer() = 0; + virtual void updateBuffer() const = 0; Location loc; }; -template<typename... Target> class BufferedLocationT : public BufferedLocation { +class BufferedLocationUpdater : public BufferedLocation { public: template<typename... LocationArgs> - BufferedLocationT(Target &&... target, LocationArgs &&... t) : - BufferedLocation {std::forward<LocationArgs>(t)...}, target {std::forward<Target>(target)...} + BufferedLocationUpdater(std::function<void(const BufferedLocation *)> onUpdate, LocationArgs &&... t) : + BufferedLocation {std::forward<LocationArgs>(t)...}, onUpdate {std::move(onUpdate)} { updateBuffer(); } @@ -42,11 +42,7 @@ public: using BufferedLocation::operator=; private: - void - updateBuffer() override - { - std::apply(std::invoke<const Target &...>, target) = getTransform(); - } + void updateBuffer() const override; - std::tuple<Target...> target; + std::function<void(const BufferedLocation *)> onUpdate; }; diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp index c4c9544..d362b94 100644 --- a/gfx/gl/camera.cpp +++ b/gfx/gl/camera.cpp @@ -1,55 +1,55 @@ #include "camera.h" #include <collections.h> -#include <glm/gtx/intersect.hpp> // IWYU pragma: keep -#include <glm/gtx/transform.hpp> // IWYU pragma: keep +#include <glm/gtx/transform.hpp> #include <maths.h> #include <ray.h> -Camera::Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar) : +Camera::Camera(GlobalPosition3D pos, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar) : position {pos}, forward {::north}, up {::up}, near {zNear}, far {zFar}, - projection {glm::perspective(fov, aspect, zNear, zFar)}, - viewProjection {projection * glm::lookAt(position, position + forward, up)}, - inverseViewProjection {glm::inverse(viewProjection)} + projection { + glm::perspective(fov, aspect, static_cast<RelativeDistance>(zNear), static_cast<RelativeDistance>(zFar))}, + viewProjection {}, inverseViewProjection {} { + updateView(); } Ray -Camera::unProject(const glm::vec2 & mouse) const +Camera::unProject(const ScreenRelCoord & mouse) const { static constexpr const glm::vec4 screen {0, 0, 1, 1}; - const auto mouseProjection = glm::lookAt(::origin, forward, up); - return {position, glm::normalize(glm::unProject(mouse ^ 1, mouseProjection, projection, screen))}; + const auto mouseProjection = glm::lookAt({}, forward, up); + return {position, glm::normalize(glm::unProject(mouse || 1.F, mouseProjection, projection, screen))}; } void Camera::updateView() { - viewProjection = projection * glm::lookAt(position, position + forward, up); + viewProjection = projection * glm::lookAt({}, forward, up); inverseViewProjection = glm::inverse(viewProjection); } -glm::vec3 -Camera::upFromForward(const glm::vec3 & forward) +Direction3D +Camera::upFromForward(const Direction3D & forward) { const auto right = glm::cross(forward, ::down); return glm::cross(forward, right); } -std::array<glm::vec4, 4> -Camera::extentsAtDist(const float dist) const +std::array<GlobalPosition4D, 4> +Camera::extentsAtDist(const GlobalDistance dist) const { - const auto clampToSeaFloor = [this, dist](const glm::vec3 & target) { - if (target.z < -1.5F) { - const auto vec = glm::normalize(target - position); - constexpr glm::vec3 seafloor {0, 0, -1.5F}; - float outdist; - if (glm::intersectRayPlane(position, vec, seafloor, ::up, outdist)) { - return (vec * outdist + position) ^ outdist; - } + const auto clampToSeaFloor = [this, dist](GlobalPosition3D target) -> GlobalPosition4D { + target += position; + if (target.z < -1500) { + const CalcPosition3D diff = target - position; + const CalcDistance limit = -1500 - position.z; + return {position + GlobalPosition3D((limit * diff) / diff.z), (limit * dist) / diff.z}; } - return target ^ dist; + return {target, dist}; }; - const auto depth = -(2.F * (dist - near) * far) / (dist * (near - far)) - 1.F; + const auto depth = -(2.F * (static_cast<float>(dist - near)) * static_cast<float>(far)) + / (static_cast<float>(dist) * (static_cast<float>(near - far))) + - 1.F; static constexpr const std::array extents {-1.F, 1.F}; static constexpr const auto cartesianExtents = extents * extents; return cartesianExtents * [&depth, this, &clampToSeaFloor](const auto & extent) { diff --git a/gfx/gl/camera.h b/gfx/gl/camera.h index 7956ec3..eca7b8f 100644 --- a/gfx/gl/camera.h +++ b/gfx/gl/camera.h @@ -1,12 +1,13 @@ #pragma once +#include "config/types.h" #include <glm/glm.hpp> #include <maths.h> #include <ray.h> class Camera { public: - Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar); + Camera(GlobalPosition3D, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar); [[nodiscard]] glm::mat4 getViewProjection() const @@ -14,23 +15,23 @@ public: return viewProjection; } - [[nodiscard]] Ray unProject(const glm::vec2 &) const; + [[nodiscard]] Ray unProject(const ScreenRelCoord &) const; void - setPosition(const glm::vec3 & p) + setPosition(const GlobalPosition3D & p) { position = p; updateView(); } void - setForward(const glm::vec3 & f) + setForward(const Direction3D & f) { setForward(f, upFromForward(f)); } void - setForward(const glm::vec3 & f, const glm::vec3 & u) + setForward(const Direction3D & f, const Direction3D & u) { forward = f; up = u; @@ -38,23 +39,23 @@ public: } void - setView(const glm::vec3 & p, const glm::vec3 & f) + setView(const GlobalPosition3D & p, const Direction3D & f) { position = p; setForward(f); } void - setView(const glm::vec3 & p, const glm::vec3 & f, const glm::vec3 & u) + setView(const GlobalPosition3D & p, const Direction3D & f, const Direction3D & u) { position = p; setView(f, u); } void - lookAt(const glm::vec3 & target) + lookAt(const GlobalPosition3D & target) { - setForward(glm::normalize(target - position)); + setForward(glm::normalize(RelativePosition3D(target - position))); } [[nodiscard]] auto @@ -69,18 +70,18 @@ public: return position; } - [[nodiscard]] std::array<glm::vec4, 4> extentsAtDist(float) const; + [[nodiscard]] std::array<GlobalPosition4D, 4> extentsAtDist(GlobalDistance) const; - [[nodiscard]] static glm::vec3 upFromForward(const glm::vec3 & forward); + [[nodiscard]] static Direction3D upFromForward(const Direction3D & forward); private: void updateView(); - glm::vec3 position; - glm::vec3 forward; - glm::vec3 up; + GlobalPosition3D position; + Direction3D forward; + Direction3D up; - float near, far; + GlobalDistance near, far; glm::mat4 projection; glm::mat4 viewProjection, inverseViewProjection; }; diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index 5799daf..c856279 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -14,52 +14,56 @@ static constexpr const std::array<const glm::i8vec4, 4> displayVAOdata {{ {1, -1, 1, 0}, }}; -SceneRenderer::SceneRenderer(glm::ivec2 s, GLuint o) : - camera {{-1250.0F, -1250.0F, 35.0F}, quarter_pi, ratio(s), 0.1F, 10000.0F}, size {s}, output {o}, +SceneRenderer::SceneRenderer(ScreenAbsCoord s, GLuint o) : + camera {{-1250000, -1250000, 35.0F}, quarter_pi, ratio(s), 100, 10000000}, size {s}, output {o}, lighting {lighting_vs, lighting_fs}, shadowMapper {{2048, 2048}} { shader.setViewPort({0, 0, size.x, size.y}); VertexArrayObject {displayVAO}.addAttribs<glm::i8vec4>(displayVBO, displayVAOdata); + const auto configuregdata = [this](const GLuint data, const std::initializer_list<GLint> iformats, + const GLenum format, const GLenum attachment) { + glBindTexture(GL_TEXTURE_2D, data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + for (const auto iformat : iformats) { + glTexImage2D(GL_TEXTURE_2D, 0, iformat, size.x, size.y, 0, format, GL_BYTE, nullptr); + + glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, data, 0); + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) { + return iformat; + } + } + throw std::runtime_error("Framebuffer could not be completed!"); + }; + glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); - const auto configuregdata - = [this](const GLuint data, const std::initializer_list<GLint> formats, const GLenum attachment) { - glBindTexture(GL_TEXTURE_2D, data); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - for (const auto format : formats) { - glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, GL_RGB, GL_BYTE, nullptr); - - glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, data, 0); - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) { - return format; - } - } - throw std::runtime_error("Framebuffer could not be completed!"); - }; - configuregdata(gPosition, {GL_RGB32F}, GL_COLOR_ATTACHMENT0); - configuregdata(gNormal, {GL_RGB8_SNORM, GL_RGB16F}, GL_COLOR_ATTACHMENT1); - configuregdata(gAlbedoSpec, {GL_RGB8}, GL_COLOR_ATTACHMENT2); - configuregdata(gIllumination, {GL_RGB8}, GL_COLOR_ATTACHMENT3); + configuregdata(gPosition, {GL_RGB32I}, GL_RGB_INTEGER, GL_COLOR_ATTACHMENT0); + configuregdata(gNormal, {GL_RGB8_SNORM, GL_RGB16F}, GL_RGB, GL_COLOR_ATTACHMENT1); + configuregdata(gAlbedoSpec, {GL_RGB8}, GL_RGB, GL_COLOR_ATTACHMENT2); + constexpr std::array<unsigned int, 3> attachments { + GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2}; + glDrawBuffers(attachments.size(), attachments.data()); glBindRenderbuffer(GL_RENDERBUFFER, depth); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size.x, size.y); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth); + glBindFramebuffer(GL_FRAMEBUFFER, gBufferIll); + configuregdata(gIllumination, {GL_RGB8}, GL_RGB, GL_COLOR_ATTACHMENT0); + glDrawBuffer(GL_COLOR_ATTACHMENT0); + glBindFramebuffer(GL_FRAMEBUFFER, output); } void SceneRenderer::render(const SceneProvider & scene) const { - shader.setViewProjection(camera.getViewProjection()); + shader.setViewProjection(camera.getPosition(), camera.getViewProjection()); glViewport(0, 0, size.x, size.y); // Geometry pass glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); - static constexpr std::array<unsigned int, 3> attachments { - GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2}; - glDrawBuffers(attachments.size(), attachments.data()); glEnable(GL_BLEND); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); @@ -70,7 +74,7 @@ SceneRenderer::render(const SceneProvider & scene) const scene.content(shader); // Illumination pass - glDrawBuffer(GL_COLOR_ATTACHMENT3); + glBindFramebuffer(GL_FRAMEBUFFER, gBufferIll); glBlendFunc(GL_ONE, GL_ONE); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, gPosition); @@ -97,23 +101,22 @@ SceneRenderer::render(const SceneProvider & scene) const } void -SceneRenderer::setAmbientLight(const glm::vec3 & colour) const +SceneRenderer::setAmbientLight(const RGB & colour) const { - glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); + glBindFramebuffer(GL_FRAMEBUFFER, gBufferIll); glClearColor(colour.r, colour.g, colour.b, 1.0F); glClear(GL_COLOR_BUFFER_BIT); } void -SceneRenderer::setDirectionalLight( - const glm::vec3 & colour, const glm::vec3 & direction, const SceneProvider & scene) const +SceneRenderer::setDirectionalLight(const RGB & colour, const Direction3D & direction, const SceneProvider & scene) const { if (colour.r > 0 || colour.g > 0 || colour.b > 0) { const auto lvp = shadowMapper.update(scene, direction, camera); - glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); + glBindFramebuffer(GL_FRAMEBUFFER, gBufferIll); glViewport(0, 0, size.x, size.y); dirLight.use(); - dirLight.setDirectionalLight(colour, direction, lvp.projections, lvp.regions, lvp.maps); + dirLight.setDirectionalLight(colour, direction, camera.getPosition(), lvp.projections, lvp.regions, lvp.maps); renderQuad(); } } @@ -128,20 +131,22 @@ SceneRenderer::renderQuad() const SceneRenderer::DirectionalLightProgram::DirectionalLightProgram() : Program {lighting_vs, directionalLight_fs}, directionLoc {*this, "lightDirection"}, - colourLoc {*this, "lightColour"}, lightViewProjectionLoc {*this, "lightViewProjection"}, + colourLoc {*this, "lightColour"}, lightPointLoc {*this, "lightPoint"}, + lightViewProjectionLoc {*this, "lightViewProjection"}, lightViewProjectionCountLoc {*this, "lightViewProjectionCount"}, lightViewShadowMapRegionLoc {*this, "shadowMapRegion"} { } void -SceneRenderer::DirectionalLightProgram::setDirectionalLight(const glm::vec3 & c, const glm::vec3 & d, - const std::span<const glm::mat4x4> lvp, const std::span<const glm::vec4> shadowMapRegions, - std::size_t maps) const +SceneRenderer::DirectionalLightProgram::setDirectionalLight(const RGB & c, const Direction3D & d, + const GlobalPosition3D & p, const std::span<const glm::mat4x4> lvp, + const std::span<const TextureRelRegion> shadowMapRegions, std::size_t maps) const { glUniform3fv(colourLoc, 1, glm::value_ptr(c)); const auto nd = glm::normalize(d); glUniform3fv(directionLoc, 1, glm::value_ptr(nd)); + glUniform3iv(lightPointLoc, 1, glm::value_ptr(p)); glUniform1ui(lightViewProjectionCountLoc, static_cast<GLuint>(maps)); glUniformMatrix4fv(lightViewProjectionLoc, static_cast<GLsizei>(maps), GL_FALSE, glm::value_ptr(lvp.front())); glUniform4fv(lightViewShadowMapRegionLoc, static_cast<GLsizei>(maps), glm::value_ptr(shadowMapRegions.front())); diff --git a/gfx/gl/sceneRenderer.h b/gfx/gl/sceneRenderer.h index 55df84d..797ecf1 100644 --- a/gfx/gl/sceneRenderer.h +++ b/gfx/gl/sceneRenderer.h @@ -11,20 +11,20 @@ class SceneRenderer { public: - explicit SceneRenderer(glm::ivec2 size, GLuint output); + explicit SceneRenderer(ScreenAbsCoord size, GLuint output); void render(const SceneProvider &) const; - void setAmbientLight(const glm::vec3 & colour) const; - void setDirectionalLight(const glm::vec3 & colour, const glm::vec3 & direction, const SceneProvider &) const; + void setAmbientLight(const RGB & colour) const; + void setDirectionalLight(const RGB & colour, const Direction3D & direction, const SceneProvider &) const; Camera camera; -private: +protected: void renderQuad() const; - glm::ivec2 size; + ScreenAbsCoord size; GLuint output; - glFrameBuffer gBuffer; + glFrameBuffer gBuffer, gBufferIll; glTexture gPosition, gNormal, gAlbedoSpec, gIllumination; glRenderBuffer depth; @@ -39,12 +39,12 @@ private: DirectionalLightProgram(); using Program::use; - void setDirectionalLight(const glm::vec3 &, const glm::vec3 &, const std::span<const glm::mat4x4>, - const std::span<const glm::vec4>, std::size_t maps) const; + void setDirectionalLight(const RGB &, const Direction3D &, const GlobalPosition3D &, + const std::span<const glm::mat4x4>, const std::span<const TextureRelRegion>, std::size_t maps) const; private: - RequiredUniformLocation directionLoc, colourLoc, lightViewProjectionLoc, lightViewProjectionCountLoc, - lightViewShadowMapRegionLoc; + RequiredUniformLocation directionLoc, colourLoc, lightPointLoc, lightViewProjectionLoc, + lightViewProjectionCountLoc, lightViewShadowMapRegionLoc; }; DeferredLightProgram lighting; diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index 1354611..ccc1a1d 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -26,16 +26,16 @@ SceneShader::SceneShader() : } void -SceneShader::setViewProjection(const glm::mat4 & viewProjection) const +SceneShader::setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const { for (const auto & prog : std::array<const SceneProgram *, 7> { &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLight}) { - prog->setViewProjection(viewProjection); + prog->setViewProjection(viewPoint, viewProjection); } } void -SceneShader::setViewPort(const glm::ivec4 & viewPort) const +SceneShader::setViewPort(const ViewPort & viewPort) const { for (const auto & prog : std::array<const SceneProgram *, 7> { &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLight}) { @@ -44,14 +44,15 @@ SceneShader::setViewPort(const glm::ivec4 & viewPort) const } void -SceneShader::SceneProgram::setViewProjection(const glm::mat4 & viewProjection) const +SceneShader::SceneProgram::setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const { glUseProgram(*this); glUniformMatrix4fv(viewProjectionLoc, 1, GL_FALSE, glm::value_ptr(viewProjection)); + glUniform3iv(viewPointLoc, 1, glm::value_ptr(viewPoint)); } void -SceneShader::SceneProgram::setViewPort(const glm::ivec4 & viewPort) const +SceneShader::SceneProgram::setViewPort(const ViewPort & viewPort) const { if (viewPortLoc >= 0) { glUseProgram(*this); @@ -59,13 +60,16 @@ SceneShader::SceneProgram::setViewPort(const glm::ivec4 & viewPort) const } } -SceneShader::BasicProgram::BasicProgram() : SceneProgram {dynamicPoint_vs, material_fs}, modelLoc {*this, "model"} { } +SceneShader::BasicProgram::BasicProgram() : + SceneProgram {dynamicPoint_vs, material_fs}, modelLoc {*this, "model"}, modelPosLoc {*this, "modelPos"} +{ +} void SceneShader::BasicProgram::setModel(Location const & location) const { - const auto model {glm::translate(location.pos) * rotate_ypr(location.rot)}; - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(rotate_ypr(location.rot))); + glUniform3iv(modelPosLoc, 1, glm::value_ptr(location.pos)); } void @@ -81,38 +85,39 @@ void SceneShader::WaterProgram::use(float waveCycle) const { Program::use(); - glm::vec3 waves {waveCycle, 0.F, 0.F}; - glUniform3fv(waveLoc, 1, glm::value_ptr(waves)); + glUniform1f(waveLoc, waveCycle); } SceneShader::PointLightShader::PointLightShader() : - SceneProgram {pointLight_vs, pointLight_gs, pointLight_fs}, colourLoc {*this, "colour"}, kqLoc {*this, "kq"} + SceneProgram {pointLight_vs, pointLight_gs, pointLight_fs}, colourLoc {*this, "colour"}, kqLoc {*this, "kq"}, + viewPointLoc {*this, "viewPoint"} { - VertexArrayObject {va}.addAttribs<glm::vec3>(b); + VertexArrayObject {va}.addAttribs<Position3D>(b); } void -SceneShader::PointLightShader::add(const glm::vec3 & position, const glm::vec3 & colour, const float kq) const +SceneShader::PointLightShader::add(const Position3D & position, const RGB & colour, const float kq) const { Program::use(); glBindVertexArray(va); glBindBuffer(GL_ARRAY_BUFFER, b); glUniform3fv(colourLoc, 1, glm::value_ptr(colour)); glUniform1f(kqLoc, kq); - glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3), glm::value_ptr(position), GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(Position3D), glm::value_ptr(position), GL_DYNAMIC_DRAW); glDrawArrays(GL_POINTS, 0, 1); } SceneShader::SpotLightShader::SpotLightShader() : SceneProgram {spotLight_vs, spotLight_gs, spotLight_fs}, directionLoc {*this, "v_direction"}, - colourLoc {*this, "colour"}, kqLoc {*this, "kq"}, arcLoc {*this, "arc"} + colourLoc {*this, "colour"}, kqLoc {*this, "kq"}, arcLoc {*this, "arc"}, viewPointLoc {*this, "viewPoint"} + { - using v3pair = std::pair<glm::vec3, glm::vec3>; + using v3pair = std::pair<Position3D, Direction3D>; VertexArrayObject {va}.addAttribs<v3pair, &v3pair::first, &v3pair::second>(b); } void -SceneShader::SpotLightShader::add(const glm::vec3 & position, const glm::vec3 & direction, const glm::vec3 & colour, +SceneShader::SpotLightShader::add(const Position3D & position, const Direction3D & direction, const RGB & colour, const float kq, const float arc) const { Program::use(); @@ -122,6 +127,6 @@ SceneShader::SpotLightShader::add(const glm::vec3 & position, const glm::vec3 & glUniform3fv(directionLoc, 1, glm::value_ptr(direction)); glUniform1f(kqLoc, kq); glUniform1f(arcLoc, arc); - glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3), glm::value_ptr(position), GL_DYNAMIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(Position3D), glm::value_ptr(position), GL_DYNAMIC_DRAW); glDrawArrays(GL_POINTS, 0, 1); } diff --git a/gfx/gl/sceneShader.h b/gfx/gl/sceneShader.h index ead184e..7ffaacd 100644 --- a/gfx/gl/sceneShader.h +++ b/gfx/gl/sceneShader.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include "program.h" #include <glArrays.h> @@ -10,15 +11,16 @@ class SceneShader { public: template<typename... S> inline explicit SceneProgram(const S &... srcs) : - Program {srcs...}, viewProjectionLoc {*this, "viewProjection"}, viewPortLoc {*this, "viewPort"} + Program {srcs...}, viewProjectionLoc {*this, "viewProjection"}, viewPointLoc {*this, "viewPoint"}, + viewPortLoc {*this, "viewPort"} { } - void setViewProjection(const glm::mat4 &) const; - void setViewPort(const glm::ivec4 &) const; + void setViewProjection(const GlobalPosition3D &, const glm::mat4 &) const; + void setViewPort(const ViewPort &) const; private: - RequiredUniformLocation viewProjectionLoc; + RequiredUniformLocation viewProjectionLoc, viewPointLoc; UniformLocation viewPortLoc; }; @@ -30,6 +32,7 @@ class SceneShader { private: RequiredUniformLocation modelLoc; + RequiredUniformLocation modelPosLoc; }; class AbsolutePosProgram : public SceneProgram { @@ -52,11 +55,12 @@ class SceneShader { public: PointLightShader(); - void add(const glm::vec3 & position, const glm::vec3 & colour, const float kq) const; + void add(const Position3D & position, const RGB & colour, const float kq) const; private: UniformLocation colourLoc; UniformLocation kqLoc; + UniformLocation viewPointLoc; glVertexArray va; glBuffer b; }; @@ -65,7 +69,7 @@ class SceneShader { public: SpotLightShader(); - void add(const glm::vec3 & position, const glm::vec3 & direction, const glm::vec3 & colour, const float kq, + void add(const Position3D & position, const Direction3D & direction, const RGB & colour, const float kq, const float arc) const; private: @@ -73,6 +77,7 @@ class SceneShader { UniformLocation colourLoc; UniformLocation kqLoc; UniformLocation arcLoc; + UniformLocation viewPointLoc; glVertexArray va; glBuffer b; }; @@ -86,6 +91,6 @@ public: PointLightShader pointLight; SpotLightShader spotLight; - void setViewProjection(const glm::mat4 & viewProjection) const; - void setViewPort(const glm::ivec4 & viewPort) const; + void setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const; + void setViewPort(const ViewPort & viewPort) const; }; diff --git a/gfx/gl/shaders/commonPoint.glsl b/gfx/gl/shaders/commonPoint.glsl index 35510e1..a9817fb 100644 --- a/gfx/gl/shaders/commonPoint.glsl +++ b/gfx/gl/shaders/commonPoint.glsl @@ -16,13 +16,11 @@ getMaterialDetail(uint midx) void main() { - vec4 worldPos = model * vec4(position, 1.0); - - FragPos = worldPos.xyz; + FragPos = (model * vec4(position, 1.0)).xyz + modelPos; TexCoords = texCoord; Normal = (model * vec4(normal, 0.0)).xyz; Colour = colour; Material = getMaterialDetail(material); - gl_Position = viewProjection * worldPos; + gl_Position = viewProjection * vec4(FragPos - viewPoint, 1); } diff --git a/gfx/gl/shaders/commonShadowPoint.glsl b/gfx/gl/shaders/commonShadowPoint.glsl index c7cbd3e..216642e 100644 --- a/gfx/gl/shaders/commonShadowPoint.glsl +++ b/gfx/gl/shaders/commonShadowPoint.glsl @@ -1,6 +1,7 @@ void main() { - gl_Position = viewProjection * model * vec4(position, 1.0); + vec4 worldPos = model * vec4(position, 1.0); + gl_Position = viewProjection * vec4(worldPos.xyz - viewPoint + modelPos, 1); gl_Position.z = max(gl_Position.z, -1); } diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs index ca10ef5..f36d83f 100644 --- a/gfx/gl/shaders/directionalLight.fs +++ b/gfx/gl/shaders/directionalLight.fs @@ -7,12 +7,13 @@ out vec3 FragColor; in vec2 TexCoords; -layout(binding = 0) uniform sampler2D gPosition; +layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; layout(binding = 2) uniform sampler2D shadowMap; uniform vec3 lightDirection; uniform vec3 lightColour; +uniform ivec3 lightPoint; uniform mat4 lightViewProjection[MAX_MAPS]; uniform vec4 shadowMapRegion[MAX_MAPS]; uniform uint lightViewProjectionCount; @@ -27,10 +28,10 @@ insideShadowCube(vec3 v) } float -isShaded(vec3 Position) +isShaded(vec4 Position) { for (uint m = 0u; m < lightViewProjectionCount; m++) { - vec3 PositionInLightSpace = (lightViewProjection[m] * vec4(Position, 1.0f)).xyz; + const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz; const float inside = insideShadowCube(PositionInLightSpace); if (inside > 0) { const float lightSpaceDepth @@ -44,7 +45,7 @@ isShaded(vec3 Position) void main() { - const vec3 Position = texture(gPosition, TexCoords).xyz; + const vec4 Position = vec4(texture(gPosition, TexCoords).xyz - lightPoint, 1); const vec3 Normal = texture(gNormal, TexCoords).rgb; const float shaded = isShaded(Position); FragColor = (1 - shaded) * max(dot(-lightDirection, Normal) * lightColour, 0); diff --git a/gfx/gl/shaders/dynamicPoint.vs b/gfx/gl/shaders/dynamicPoint.vs index 961535c..097cd11 100644 --- a/gfx/gl/shaders/dynamicPoint.vs +++ b/gfx/gl/shaders/dynamicPoint.vs @@ -5,6 +5,8 @@ include(`meshIn.glsl') include(`materialInterface.glsl') uniform mat4 viewProjection; +uniform ivec3 viewPoint; uniform mat4 model; +uniform ivec3 modelPos; include(`commonPoint.glsl') diff --git a/gfx/gl/shaders/dynamicPointInst.vs b/gfx/gl/shaders/dynamicPointInst.vs index 2d6cee5..529fe1d 100644 --- a/gfx/gl/shaders/dynamicPointInst.vs +++ b/gfx/gl/shaders/dynamicPointInst.vs @@ -5,6 +5,8 @@ include(`meshIn.glsl') include(`materialInterface.glsl') uniform mat4 viewProjection; +uniform ivec3 viewPoint; layout(location = 5) in mat4 model; +layout(location = 9) in ivec3 modelPos; include(`commonPoint.glsl') diff --git a/gfx/gl/shaders/fixedPoint.vs b/gfx/gl/shaders/fixedPoint.vs index ed78c96..3cea737 100644 --- a/gfx/gl/shaders/fixedPoint.vs +++ b/gfx/gl/shaders/fixedPoint.vs @@ -5,6 +5,8 @@ include(`meshIn.glsl') include(`materialInterface.glsl') uniform mat4 viewProjection; +uniform ivec3 viewPoint; const mat4 model = mat4(1); +const vec3 modelPos = ivec3(0); include(`commonPoint.glsl') diff --git a/gfx/gl/shaders/landmass.fs b/gfx/gl/shaders/landmass.fs index 4dc92bb..fc43bf2 100644 --- a/gfx/gl/shaders/landmass.fs +++ b/gfx/gl/shaders/landmass.fs @@ -11,9 +11,9 @@ const vec3 rock = vec3(.2, .2, .1); const vec3 sand = vec3(.76, .7, .5); const vec3 snow = vec3(.97, .97, .99); -const float beachline = .5; -const float snowline_low = 28; -const float snowline_high = 30; +const float beachline = 500; +const float snowline_low = 28000; +const float snowline_high = 30000; const float slope_min = .99; const float slope_mid = .95; @@ -60,7 +60,7 @@ main() } } - gPosition = vec4(FragPos, 1); + gPosition = ivec4(FragPos, 1); gNormal = vec4(Normal, 1); gAlbedoSpec = vec4(color, 1); } diff --git a/gfx/gl/shaders/material.fs b/gfx/gl/shaders/material.fs index 20fa8ab..5b93707 100644 --- a/gfx/gl/shaders/material.fs +++ b/gfx/gl/shaders/material.fs @@ -42,7 +42,7 @@ main() { vec4 textureColour = getTextureColour(Material, TexCoords); float opaque = step(0.5, mix(textureColour.a, 1, Colour.a)); - gPosition = vec4(FragPos, opaque); + gPosition = ivec4(FragPos, opaque); gNormal = vec4(Normal, opaque); gl_FragDepth = mix(1.0, gl_FragCoord.z, opaque); gAlbedoSpec = mix(textureColour, vec4(Colour.rgb, 1), Colour.a); diff --git a/gfx/gl/shaders/materialOut.glsl b/gfx/gl/shaders/materialOut.glsl index dc5f8e8..846825e 100644 --- a/gfx/gl/shaders/materialOut.glsl +++ b/gfx/gl/shaders/materialOut.glsl @@ -1,3 +1,3 @@ -layout(location = 0) out vec4 gPosition; +layout(location = 0) out ivec4 gPosition; layout(location = 1) out vec4 gNormal; layout(location = 2) out vec4 gAlbedoSpec; diff --git a/gfx/gl/shaders/pointLight.fs b/gfx/gl/shaders/pointLight.fs index bd32c05..ba327b6 100644 --- a/gfx/gl/shaders/pointLight.fs +++ b/gfx/gl/shaders/pointLight.fs @@ -3,7 +3,7 @@ out vec3 FragColor; -layout(binding = 0) uniform sampler2D gPosition; +layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; uniform ivec4 viewPort; uniform vec3 colour; @@ -26,5 +26,5 @@ main() if (normalDot < 0) { discard; } - FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist, 2))); + FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist / 1000.0, 2))); } diff --git a/gfx/gl/shaders/pointLight.gs b/gfx/gl/shaders/pointLight.gs index 03d131d..9c41ed4 100644 --- a/gfx/gl/shaders/pointLight.gs +++ b/gfx/gl/shaders/pointLight.gs @@ -19,6 +19,7 @@ const vec3[] cube = vec3[]( // http://www.cs.umd.edu/gvil/papers/av_ts.pdf vec3(1, 1, -1) // Back-top-right ); uniform mat4 viewProjection; +uniform ivec3 viewPoint; in vec3 centre[]; in float size[]; diff --git a/gfx/gl/shaders/pointLight.vs b/gfx/gl/shaders/pointLight.vs index 35682fa..00a34a3 100644 --- a/gfx/gl/shaders/pointLight.vs +++ b/gfx/gl/shaders/pointLight.vs @@ -4,6 +4,7 @@ layout(location = 0) in vec3 position; uniform vec3 colour; uniform float kq; +uniform ivec3 viewPoint; out vec3 centre; out float size; @@ -12,6 +13,6 @@ void main() { centre = position; - size = (8 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); - gl_Position = vec4(centre, 0); + size = (8000 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); + gl_Position = vec4(centre - viewPoint, 0); } diff --git a/gfx/gl/shaders/shadowDynamicPoint.vs b/gfx/gl/shaders/shadowDynamicPoint.vs index f3ed533..f21b3b6 100644 --- a/gfx/gl/shaders/shadowDynamicPoint.vs +++ b/gfx/gl/shaders/shadowDynamicPoint.vs @@ -3,6 +3,8 @@ include(`meshIn.glsl') uniform mat4 viewProjection; +uniform ivec3 viewPoint; uniform mat4 model; +uniform ivec3 modelPos; include(`commonShadowPoint.glsl') diff --git a/gfx/gl/shaders/shadowDynamicPointInst.vs b/gfx/gl/shaders/shadowDynamicPointInst.vs index 1bf74ef..28a62d9 100644 --- a/gfx/gl/shaders/shadowDynamicPointInst.vs +++ b/gfx/gl/shaders/shadowDynamicPointInst.vs @@ -3,6 +3,8 @@ include(`meshIn.glsl') uniform mat4 viewProjection; +uniform ivec3 viewPoint; layout(location = 5) in mat4 model; +layout(location = 9) in ivec3 modelPos; include(`commonShadowPoint.glsl') diff --git a/gfx/gl/shaders/shadowFixedPoint.vs b/gfx/gl/shaders/shadowFixedPoint.vs index 8921707..168d5f1 100644 --- a/gfx/gl/shaders/shadowFixedPoint.vs +++ b/gfx/gl/shaders/shadowFixedPoint.vs @@ -3,6 +3,8 @@ include(`meshIn.glsl') uniform mat4 viewProjection; +uniform ivec3 viewPoint; const mat4 model = mat4(1); +const ivec3 modelPos = ivec3(0); include(`commonShadowPoint.glsl') diff --git a/gfx/gl/shaders/spotLight.fs b/gfx/gl/shaders/spotLight.fs index add86fd..937f922 100644 --- a/gfx/gl/shaders/spotLight.fs +++ b/gfx/gl/shaders/spotLight.fs @@ -3,7 +3,7 @@ out vec3 FragColor; -layout(binding = 0) uniform sampler2D gPosition; +layout(binding = 0) uniform isampler2D gPosition; layout(binding = 1) uniform sampler2D gNormal; uniform ivec4 viewPort; uniform vec3 colour; @@ -30,5 +30,5 @@ main() if (normalDot < 0) { discard; } - FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist, 2))); + FragColor = (colour * normalDot) / (1 + (kq * pow(lightDist / 1000.0, 2))); } diff --git a/gfx/gl/shaders/spotLight.gs b/gfx/gl/shaders/spotLight.gs index ad65675..b58c169 100644 --- a/gfx/gl/shaders/spotLight.gs +++ b/gfx/gl/shaders/spotLight.gs @@ -10,6 +10,7 @@ const vec3[] pyramid = vec3[]( // four-sided vec3(1, -1, 1) // Front-right ); uniform mat4 viewProjection; +uniform ivec3 viewPoint; uniform float arc; in vec3 position[]; diff --git a/gfx/gl/shaders/spotLight.vs b/gfx/gl/shaders/spotLight.vs index dca0854..ac1d1db 100644 --- a/gfx/gl/shaders/spotLight.vs +++ b/gfx/gl/shaders/spotLight.vs @@ -6,6 +6,7 @@ uniform vec3 v_direction; uniform vec3 colour; uniform float kq; uniform float arc; +uniform ivec3 viewPoint; out vec3 position; out vec3 direction; @@ -17,7 +18,7 @@ main() { position = v_position; direction = normalize(v_direction); - size = (8 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); + size = (8000 * sqrt(max(max(colour.r, colour.g), colour.b))) / sqrt(kq); cosarc = cos(arc / 2); - gl_Position = vec4(position, 0); + gl_Position = vec4(position - viewPoint, 0); } diff --git a/gfx/gl/shaders/water.fs b/gfx/gl/shaders/water.fs index 04aa94c..8891733 100644 --- a/gfx/gl/shaders/water.fs +++ b/gfx/gl/shaders/water.fs @@ -5,13 +5,12 @@ include(`materialInterface.glsl') include(`materialOut.glsl') uniform sampler2D texture0; -uniform vec3 waves; void main() { - gPosition = vec4(FragPos, 1); + gPosition = ivec4(FragPos, 1); gNormal = vec4(Normal, 1); gAlbedoSpec = texture(texture0, TexCoords); - gAlbedoSpec.a *= clamp(-FragPos.z * .7, .1, 1.0); + gAlbedoSpec.a *= clamp(-FragPos.z * .0007, .1, 1.0); } diff --git a/gfx/gl/shaders/water.vs b/gfx/gl/shaders/water.vs index a21b49f..f6c7c8f 100644 --- a/gfx/gl/shaders/water.vs +++ b/gfx/gl/shaders/water.vs @@ -4,17 +4,18 @@ include(`meshIn.glsl') include(`materialInterface.glsl') uniform mat4 viewProjection; -uniform vec3 waves; +uniform ivec3 viewPoint; +uniform float waves; void main() { - vec4 wpos = vec4(position.x + cos(waves.x), position.y + cos(waves.x * waves.y / 2), - cos(waves.x + position.x + (position.y / 8)) * .3, 1.0); + vec3 wpos = vec3(position.x + (cos(waves) * 1000.0), position.y + (cos(waves * 0 / 2) * 1000.0), + cos(waves + (position.x / 1000000.0) + (position.y / 8000.0)) * 300.0); FragPos = vec3(wpos.xy, position.z); TexCoords = texCoord; Normal = normal; - gl_Position = viewProjection * wpos; + gl_Position = viewProjection * vec4(wpos - viewPoint, 1.0); } diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 79b39c0..1498bb0 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -15,7 +15,7 @@ #include <tuple> #include <vector> -ShadowMapper::ShadowMapper(const glm::ivec2 & s) : +ShadowMapper::ShadowMapper(const TextureAbsCoord & s) : fixedPoint {shadowFixedPoint_vs}, dynamicPointInst {shadowDynamicPointInst_vs}, size {s} { glBindTexture(GL_TEXTURE_2D, depthMap); @@ -24,7 +24,7 @@ ShadowMapper::ShadowMapper(const glm::ivec2 & s) : glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - static constexpr glm::vec4 border {std::numeric_limits<float>::infinity()}; + static constexpr RGBA border {std::numeric_limits<RGBA::value_type>::infinity()}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, glm::value_ptr(border)); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); @@ -37,7 +37,7 @@ ShadowMapper::ShadowMapper(const glm::ivec2 & s) : glBindFramebuffer(GL_FRAMEBUFFER, 0); } -constexpr std::array<std::array<glm::ivec4, ShadowMapper::SHADOW_BANDS>, ShadowMapper::SHADOW_BANDS> viewports {{ +constexpr std::array<std::array<TextureAbsRegion, ShadowMapper::SHADOW_BANDS>, ShadowMapper::SHADOW_BANDS> viewports {{ {{ {31, 31, 0, 0}, // full }}, @@ -57,33 +57,34 @@ constexpr std::array<std::array<glm::ivec4, ShadowMapper::SHADOW_BANDS>, ShadowM {1, 1, 1, 1}, // upper right }}, }}; -constexpr std::array<std::array<glm::vec4, ShadowMapper::SHADOW_BANDS>, ShadowMapper::SHADOW_BANDS> shadowMapRegions {{ - {{ - {0.5F, 0.5F, 0.5F, 0.5F}, // full - }}, - {{ - {0.5F, 0.25F, 0.5F, 0.25F}, // lower half - {0.5F, 0.25F, 0.5F, 0.75F}, // upper half - }}, - {{ - {0.5F, 0.25F, 0.5F, 0.25F}, // lower half - {0.25F, 0.25F, 0.25F, 0.75F}, // upper left - {0.25F, 0.25F, 0.75F, 0.75F}, // upper right - }}, - - {{ - {0.25F, 0.25F, 0.25F, 0.25F}, // lower left - {0.25F, 0.25F, 0.75F, 0.25F}, // lower right - {0.25F, 0.25F, 0.25F, 0.75F}, // upper left - {0.25F, 0.25F, 0.75F, 0.75F}, // upper right - }}, -}}; -constexpr std::array<float, ShadowMapper::SHADOW_BANDS + 1> shadowBands { - 1.F, - 250.F, - 750.F, - 2500.F, - 10000.F, +constexpr std::array<std::array<TextureRelRegion, ShadowMapper::SHADOW_BANDS>, ShadowMapper::SHADOW_BANDS> + shadowMapRegions {{ + {{ + {0.5F, 0.5F, 0.5F, 0.5F}, // full + }}, + {{ + {0.5F, 0.25F, 0.5F, 0.25F}, // lower half + {0.5F, 0.25F, 0.5F, 0.75F}, // upper half + }}, + {{ + {0.5F, 0.25F, 0.5F, 0.25F}, // lower half + {0.25F, 0.25F, 0.25F, 0.75F}, // upper left + {0.25F, 0.25F, 0.75F, 0.75F}, // upper right + }}, + + {{ + {0.25F, 0.25F, 0.25F, 0.25F}, // lower left + {0.25F, 0.25F, 0.75F, 0.25F}, // lower right + {0.25F, 0.25F, 0.25F, 0.75F}, // upper left + {0.25F, 0.25F, 0.75F, 0.75F}, // upper right + }}, + }}; +constexpr std::array<GlobalDistance, ShadowMapper::SHADOW_BANDS + 1> shadowBands { + 1000, + 250000, + 750000, + 2500000, + 10000000, }; static_assert(viewports.size() == shadowMapRegions.size()); static_assert(shadowBands.size() == shadowMapRegions.size() + 1); @@ -104,16 +105,16 @@ struct DefinitionsInserter { ShadowMapper::Definitions & out; }; -std::vector<std::array<glm::vec3, 4>> -ShadowMapper::getBandViewExtents(const Camera & camera, const glm::mat4 & lightView) +std::vector<std::array<RelativePosition3D, 4>> +ShadowMapper::getBandViewExtents(const Camera & camera, const glm::mat4 & lightViewDir) { - std::vector<std::array<glm::vec3, 4>> bandViewExtents; + std::vector<std::array<RelativePosition3D, 4>> bandViewExtents; for (const auto dist : shadowBands) { const auto extents = camera.extentsAtDist(dist); - bandViewExtents.emplace_back(extents * [&lightView](const auto & e) -> glm::vec3 { - return lightView * glm::vec4(glm::vec3 {e}, 1); + bandViewExtents.emplace_back(extents * [&lightViewDir, cameraPos = camera.getPosition()](const auto & e) { + return glm::mat3(lightViewDir) * (e.xyz() - cameraPos); }); - if (std::none_of(extents.begin(), extents.end(), [targetDist = dist * 0.99F](const glm::vec4 & e) { + if (std::none_of(extents.begin(), extents.end(), [targetDist = dist - 1](const auto & e) { return e.w > targetDist; })) { break; @@ -123,19 +124,21 @@ ShadowMapper::getBandViewExtents(const Camera & camera, const glm::mat4 & lightV } ShadowMapper::Definitions -ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const Camera & camera) const +ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const Camera & camera) const { glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glClear(GL_DEPTH_BUFFER_BIT); glCullFace(GL_FRONT); - const auto lightView = glm::lookAt(camera.getPosition(), camera.getPosition() + dir, up); - const auto bandViewExtents = getBandViewExtents(camera, lightView); + const auto lightViewDir = glm::lookAt({}, dir, up); + const auto lightViewPoint = camera.getPosition(); + const auto bandViewExtents = getBandViewExtents(camera, lightViewDir); Definitions out; std::transform(bandViewExtents.begin(), std::prev(bandViewExtents.end()), std::next(bandViewExtents.begin()), DefinitionsInserter {out}, - [&scene, this, &lightView, bands = bandViewExtents.size() - 2, &out](const auto & near, const auto & far) { + [&scene, this, bands = bandViewExtents.size() - 2, &out, &lightViewPoint, &lightViewDir]( + const auto & near, const auto & far) { const auto extents_minmax = [extents = std::span {near.begin(), far.end()}](auto && comp) { const auto mm = std::minmax_element(extents.begin(), extents.end(), comp); return std::make_pair(comp.get(*mm.first), comp.get(*mm.second)); @@ -145,16 +148,16 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const C return glm::ortho(x.first, x.second, y.first, y.second, -z.second, -z.first); }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); - const auto lightViewProjection = lightProjection * lightView; - fixedPoint.setViewProjection(lightViewProjection); - dynamicPoint.setViewProjection(lightViewProjection); - dynamicPointInst.setViewProjection(lightViewProjection); + const auto lightViewDirProjection = lightProjection * lightViewDir; + fixedPoint.setViewProjection(lightViewPoint, lightViewDirProjection); + dynamicPoint.setViewProjection(lightViewPoint, lightViewDirProjection); + dynamicPointInst.setViewProjection(lightViewPoint, lightViewDirProjection); const auto & viewport = viewports[bands][out.maps]; glViewport(size.x >> viewport.x, size.y >> viewport.y, size.x >> viewport.z, size.y >> viewport.w); scene.shadows(*this); - return std::make_pair(lightViewProjection, shadowMapRegions[bands][out.maps]); + return std::make_pair(lightViewDirProjection, shadowMapRegions[bands][out.maps]); }); glCullFace(GL_BACK); @@ -162,13 +165,17 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const C return out; } -ShadowMapper::FixedPoint::FixedPoint(const Shader & vs) : Program {vs}, viewProjectionLoc {*this, "viewProjection"} { } +ShadowMapper::FixedPoint::FixedPoint(const Shader & vs) : + Program {vs}, viewProjectionLoc {*this, "viewProjection"}, viewPointLoc {*this, "viewPoint"} +{ +} void -ShadowMapper::FixedPoint::setViewProjection(const glm::mat4 & viewProjection) const +ShadowMapper::FixedPoint::setViewProjection(const GlobalPosition3D viewPoint, const glm::mat4 & viewProjection) const { use(); glUniformMatrix4fv(viewProjectionLoc, 1, GL_FALSE, glm::value_ptr(viewProjection)); + glUniform3iv(viewPointLoc, 1, glm::value_ptr(viewPoint)); } void @@ -178,15 +185,17 @@ ShadowMapper::FixedPoint::use() const } ShadowMapper::DynamicPoint::DynamicPoint() : - Program {shadowDynamicPoint_vs}, viewProjectionLoc {*this, "viewProjection"}, modelLoc {*this, "model"} + Program {shadowDynamicPoint_vs}, viewProjectionLoc {*this, "viewProjection"}, viewPointLoc {*this, "viewPoint"}, + modelLoc {*this, "model"}, modelPosLoc {*this, "modelPos"} { } void -ShadowMapper::DynamicPoint::setViewProjection(const glm::mat4 & viewProjection) const +ShadowMapper::DynamicPoint::setViewProjection(const GlobalPosition3D viewPoint, const glm::mat4 & viewProjection) const { glUseProgram(*this); glUniformMatrix4fv(viewProjectionLoc, 1, GL_FALSE, glm::value_ptr(viewProjection)); + glUniform3iv(viewPointLoc, 1, glm::value_ptr(viewPoint)); } void @@ -194,13 +203,11 @@ ShadowMapper::DynamicPoint::use(const Location & location) const { glUseProgram(*this); setModel(location); - const auto model = glm::translate(location.pos) * rotate_ypr(location.rot); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); } void ShadowMapper::DynamicPoint::setModel(const Location & location) const { - const auto model = glm::translate(location.pos) * rotate_ypr(location.rot); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(rotate_ypr(location.rot))); + glUniform3iv(modelPosLoc, 1, glm::value_ptr(location.pos)); } diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index cd1b975..bf571f8 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include "lib/glArrays.h" #include "program.h" #include <glm/vec2.hpp> @@ -11,38 +12,39 @@ class Camera; class ShadowMapper { public: - explicit ShadowMapper(const glm::ivec2 & size); + explicit ShadowMapper(const TextureAbsCoord & size); static constexpr std::size_t SHADOW_BANDS {4}; struct Definitions { std::array<glm::mat4x4, SHADOW_BANDS> projections {}; - std::array<glm::vec4, SHADOW_BANDS> regions {}; + std::array<TextureRelRegion, SHADOW_BANDS> regions {}; size_t maps {}; }; - [[nodiscard]] Definitions update(const SceneProvider &, const glm::vec3 & direction, const Camera &) const; + [[nodiscard]] Definitions update(const SceneProvider &, const Direction3D & direction, const Camera &) const; class FixedPoint : public Program { public: FixedPoint(const Shader & vs); - void setViewProjection(const glm::mat4 &) const; + void setViewProjection(const GlobalPosition3D, const glm::mat4 &) const; void use() const; private: - RequiredUniformLocation viewProjectionLoc; + RequiredUniformLocation viewProjectionLoc, viewPointLoc; }; class DynamicPoint : public Program { public: DynamicPoint(); - void setViewProjection(const glm::mat4 &) const; + void setViewProjection(const GlobalPosition3D, const glm::mat4 &) const; void use(const Location &) const; void setModel(const Location &) const; private: - RequiredUniformLocation viewProjectionLoc; + RequiredUniformLocation viewProjectionLoc, viewPointLoc; RequiredUniformLocation modelLoc; + RequiredUniformLocation modelPosLoc; }; FixedPoint fixedPoint, dynamicPointInst; @@ -55,9 +57,9 @@ public: } private: - [[nodiscard]] static std::vector<std::array<glm::vec3, 4>> getBandViewExtents( + [[nodiscard]] static std::vector<std::array<RelativePosition3D, 4>> getBandViewExtents( const Camera &, const glm::mat4 & lightView); glFrameBuffer depthMapFBO; glTexture depthMap; - glm::ivec2 size; + TextureAbsCoord size; }; diff --git a/gfx/gl/uiShader.cpp b/gfx/gl/uiShader.cpp index 0b47211..dc4f4dc 100644 --- a/gfx/gl/uiShader.cpp +++ b/gfx/gl/uiShader.cpp @@ -23,7 +23,7 @@ UIShader::UIShader(size_t width, size_t height) : UIShader::UIShader(const glm::mat4 & viewProjection) : icon {viewProjection}, text {viewProjection} { } void -UIShader::TextProgram::use(const glm::vec3 & colour) const +UIShader::TextProgram::use(const RGB & colour) const { Program::use(); glUniform3fv(colorLoc, 1, glm::value_ptr(colour)); diff --git a/gfx/gl/uiShader.h b/gfx/gl/uiShader.h index 2766af8..362e90c 100644 --- a/gfx/gl/uiShader.h +++ b/gfx/gl/uiShader.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include "program.h" #include <cstddef> #include <glad/gl.h> @@ -33,7 +34,7 @@ private: class TextProgram : public UIProgram { public: explicit TextProgram(const glm::mat4 & vp); - void use(const glm::vec3 & colour) const; + void use(const RGB & colour) const; private: RequiredUniformLocation colorLoc; diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index b7f1bee..35f8d35 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -1,4 +1,5 @@ #include "texture.h" +#include "config/types.h" #include "glArrays.h" #include "tga.h" #include <cache.h> @@ -60,10 +61,10 @@ Texture::bind(GLenum unit) const glBindTexture(type, m_texture); } -glm::ivec2 +TextureAbsCoord Texture::getSize(const glTexture & texture) { - glm::ivec2 size; + TextureAbsCoord size; glGetTextureLevelParameteriv(texture, 0, GL_TEXTURE_WIDTH, &size.x); glGetTextureLevelParameteriv(texture, 0, GL_TEXTURE_HEIGHT, &size.y); return size; @@ -103,6 +104,12 @@ Texture::save(const glTexture & texture, const char * path) } void +Texture::savePosition(const glTexture & texture, const char * path) +{ + save(texture, GL_BGR_INTEGER, GL_UNSIGNED_BYTE, 3, path, 2); +} + +void Texture::saveDepth(const glTexture & texture, const char * path) { save(texture, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 1, path, 3); @@ -136,7 +143,7 @@ TextureAtlas::bind(GLenum unit) const } GLuint -TextureAtlas::add(glm::ivec2 position, glm::ivec2 size, void * data, TextureOptions to) +TextureAtlas::add(TextureAbsCoord position, TextureAbsCoord size, void * data, TextureOptions to) { glTextureSubImage2D(m_texture, 0, position.x, position.y, size.x, size.y, GL_RGBA, GL_UNSIGNED_BYTE, data); diff --git a/gfx/models/texture.h b/gfx/models/texture.h index 1b66c64..5d40b39 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -1,5 +1,6 @@ #pragma once +#include "config/types.h" #include <cache.h> #include <filesystem> #include <glArrays.h> @@ -39,10 +40,11 @@ public: static void save(const glTexture &, const char * path); static void saveDepth(const glTexture &, const char * path); static void saveNormal(const glTexture &, const char * path); + static void savePosition(const glTexture &, const char * path); protected: static void save(const glTexture &, GLenum, GLenum, uint8_t channels, const char * path, uint8_t tgaFormat); - static glm::ivec2 getSize(const glTexture &); + static TextureAbsCoord getSize(const glTexture &); glTexture m_texture; GLenum type; @@ -53,7 +55,7 @@ public: TextureAtlas(GLsizei width, GLsizei height, GLuint count); void bind(GLenum unit = GL_TEXTURE0) const override; - GLuint add(glm::ivec2 position, glm::ivec2 size, void * data, TextureOptions = {}); + GLuint add(TextureAbsCoord position, TextureAbsCoord size, void * data, TextureOptions = {}); private: glTexture m_atlas; diff --git a/gfx/models/tga.h b/gfx/models/tga.h index 52db220..3d072fb 100644 --- a/gfx/models/tga.h +++ b/gfx/models/tga.h @@ -4,10 +4,11 @@ #include <glm/vec2.hpp> struct TGAHead { + using XY = glm::vec<2, uint16_t>; uint8_t idLength {}, colorMapType {}, format {}; uint16_t __attribute__((packed)) colorMapFirst {}, colorMapLength {}; uint8_t colorMapEntrySize {}; - glm::vec<2, uint16_t> origin {}, size {}; + XY origin {}, size {}; uint8_t pixelDepth {}; uint8_t descriptor {}; }; diff --git a/gfx/models/vertex.h b/gfx/models/vertex.h index 0464ea7..3c6215f 100644 --- a/gfx/models/vertex.h +++ b/gfx/models/vertex.h @@ -1,23 +1,24 @@ #pragma once +#include "config/types.h" #include <glad/gl.h> -#include <glm/glm.hpp> class Vertex { public: #ifndef __cpp_aggregate_paren_init - constexpr Vertex(glm::vec3 pos, glm::vec2 texCoord, glm::vec3 normal, glm::vec4 colour = {}, GLuint material = 0) : - pos {std::move(pos)}, texCoord {std::move(texCoord)}, normal {std::move(normal)}, colour {std::move(colour)}, - material {material} + constexpr Vertex( + RelativePosition3D pos, TextureRelCoord texCoord, Normal3D normal, RGBA colour = {}, GLuint material = 0) : + pos {std::move(pos)}, + texCoord {std::move(texCoord)}, normal {std::move(normal)}, colour {std::move(colour)}, material {material} { } #endif bool operator==(const Vertex &) const = default; - glm::vec3 pos; - glm::vec2 texCoord; - glm::vec3 normal; - glm::vec4 colour {}; + RelativePosition3D pos {}; + TextureRelCoord texCoord {}; + Normal3D normal {}; + RGBA colour {}; GLuint material {}; }; |