summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
Diffstat (limited to 'gfx')
-rw-r--r--gfx/gl/bufferedLocation.cpp77
-rw-r--r--gfx/gl/bufferedLocation.h30
-rw-r--r--gfx/gl/instanceVertices.h2
-rw-r--r--gfx/gl/shaders/commonPoint.glsl13
-rw-r--r--gfx/gl/shaders/commonShadowPoint.glsl6
-rw-r--r--gfx/gl/shaders/dynamicPoint.vs14
-rw-r--r--gfx/gl/shaders/dynamicPointInst.vs16
-rw-r--r--gfx/gl/shaders/fixedPoint.vs13
-rw-r--r--gfx/gl/shaders/shadowDynamicPoint.vs7
-rw-r--r--gfx/gl/shaders/shadowDynamicPointInst.vs8
-rw-r--r--gfx/gl/shaders/shadowFixedPoint.vs8
-rw-r--r--gfx/gl/shadowMapper.cpp7
-rw-r--r--gfx/gl/shadowMapper.h4
-rw-r--r--gfx/models/mesh.cpp20
-rw-r--r--gfx/models/mesh.h3
15 files changed, 161 insertions, 67 deletions
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 <glm/gtx/transform.hpp>
+
+BufferedLocation::BufferedLocation(InstanceVertices<glm::mat4> & i, glm::vec3 p, glm::vec3 r) :
+ BufferedLocation {i, Location {p, r}}
+{
+}
+
+BufferedLocation::BufferedLocation(InstanceVertices<glm::mat4> & 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 <glm/mat4x4.hpp>
+#include <glm/vec3.hpp>
+
+class BufferedLocation {
+public:
+ BufferedLocation(InstanceVertices<glm::mat4> &, glm::vec3 = {}, glm::vec3 = {});
+ BufferedLocation(InstanceVertices<glm::mat4> &, 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<glm::mat4>::InstanceProxy buffer;
+};
diff --git a/gfx/gl/instanceVertices.h b/gfx/gl/instanceVertices.h
index 228020d..8bb2f21 100644
--- a/gfx/gl/instanceVertices.h
+++ b/gfx/gl/instanceVertices.h
@@ -1,6 +1,7 @@
#pragma once
#include "glArrays.h"
+#include <cassert>
#include <iterator>
#include <span>
#include <special_members.hpp>
@@ -186,6 +187,7 @@ protected:
{
if (!data) {
data = static_cast<T *>(glMapNamedBuffer(buffer, GL_READ_WRITE));
+ assert(data);
}
}
diff --git a/gfx/gl/shaders/commonPoint.glsl b/gfx/gl/shaders/commonPoint.glsl
new file mode 100644
index 0000000..ecf5fc9
--- /dev/null
+++ b/gfx/gl/shaders/commonPoint.glsl
@@ -0,0 +1,13 @@
+void
+main()
+{
+ vec4 worldPos = model * vec4(position, 1.0);
+
+ FragPos = worldPos.xyz;
+ TexCoords = texCoord;
+ Normal = (model * vec4(normal, 0.0)).xyz;
+ Colour = colour;
+ Material = material;
+
+ gl_Position = viewProjection * worldPos;
+}
diff --git a/gfx/gl/shaders/commonShadowPoint.glsl b/gfx/gl/shaders/commonShadowPoint.glsl
new file mode 100644
index 0000000..c7cbd3e
--- /dev/null
+++ b/gfx/gl/shaders/commonShadowPoint.glsl
@@ -0,0 +1,6 @@
+void
+main()
+{
+ gl_Position = viewProjection * model * vec4(position, 1.0);
+ gl_Position.z = max(gl_Position.z, -1);
+}
diff --git a/gfx/gl/shaders/dynamicPoint.vs b/gfx/gl/shaders/dynamicPoint.vs
index e1701ed..ffa8275 100644
--- a/gfx/gl/shaders/dynamicPoint.vs
+++ b/gfx/gl/shaders/dynamicPoint.vs
@@ -6,16 +6,4 @@ include(`materialInterface.glsl')
uniform mat4 viewProjection;
uniform mat4 model;
-void
-main()
-{
- vec4 worldPos = model * vec4(position, 1.0);
-
- FragPos = worldPos.xyz;
- TexCoords = texCoord;
- Normal = (model * vec4(normal, 0.0)).xyz;
- Colour = colour;
- Material = material;
-
- gl_Position = viewProjection * worldPos;
-}
+include(`commonPoint.glsl')
diff --git a/gfx/gl/shaders/dynamicPointInst.vs b/gfx/gl/shaders/dynamicPointInst.vs
index 1c66979..e4c8526 100644
--- a/gfx/gl/shaders/dynamicPointInst.vs
+++ b/gfx/gl/shaders/dynamicPointInst.vs
@@ -1,21 +1,9 @@
#version 330 core
include(`meshIn.glsl')
-layout(location = 5) in mat4 model;
include(`materialInterface.glsl')
uniform mat4 viewProjection;
+layout(location = 5) in mat4 model;
-void
-main()
-{
- vec4 worldPos = model * vec4(position, 1.0);
-
- FragPos = worldPos.xyz;
- TexCoords = texCoord;
- Normal = (model * vec4(normal, 0.0)).xyz;
- Colour = colour;
- Material = material;
-
- gl_Position = viewProjection * worldPos;
-}
+include(`commonPoint.glsl')
diff --git a/gfx/gl/shaders/fixedPoint.vs b/gfx/gl/shaders/fixedPoint.vs
index 0cc8153..6f517ca 100644
--- a/gfx/gl/shaders/fixedPoint.vs
+++ b/gfx/gl/shaders/fixedPoint.vs
@@ -4,15 +4,6 @@ include(`meshIn.glsl')
include(`materialInterface.glsl')
uniform mat4 viewProjection;
+const mat4 model = mat4(1);
-void
-main()
-{
- FragPos = position;
- TexCoords = texCoord;
- Normal = normal;
- Colour = colour;
- Material = material;
-
- gl_Position = viewProjection * vec4(position, 1.0);
-}
+include(`commonPoint.glsl')
diff --git a/gfx/gl/shaders/shadowDynamicPoint.vs b/gfx/gl/shaders/shadowDynamicPoint.vs
index 531d8de..f3ed533 100644
--- a/gfx/gl/shaders/shadowDynamicPoint.vs
+++ b/gfx/gl/shaders/shadowDynamicPoint.vs
@@ -5,9 +5,4 @@ include(`meshIn.glsl')
uniform mat4 viewProjection;
uniform mat4 model;
-void
-main()
-{
- gl_Position = viewProjection * model * vec4(position, 1.0);
- gl_Position.z = max(gl_Position.z, -1);
-}
+include(`commonShadowPoint.glsl')
diff --git a/gfx/gl/shaders/shadowDynamicPointInst.vs b/gfx/gl/shaders/shadowDynamicPointInst.vs
new file mode 100644
index 0000000..1bf74ef
--- /dev/null
+++ b/gfx/gl/shaders/shadowDynamicPointInst.vs
@@ -0,0 +1,8 @@
+#version 330 core
+
+include(`meshIn.glsl')
+
+uniform mat4 viewProjection;
+layout(location = 5) in mat4 model;
+
+include(`commonShadowPoint.glsl')
diff --git a/gfx/gl/shaders/shadowFixedPoint.vs b/gfx/gl/shaders/shadowFixedPoint.vs
index c9fa19b..8921707 100644
--- a/gfx/gl/shaders/shadowFixedPoint.vs
+++ b/gfx/gl/shaders/shadowFixedPoint.vs
@@ -3,10 +3,6 @@
include(`meshIn.glsl')
uniform mat4 viewProjection;
+const mat4 model = mat4(1);
-void
-main()
-{
- gl_Position = viewProjection * vec4(position, 1.0);
- gl_Position.z = max(gl_Position.z, -1);
-}
+include(`commonShadowPoint.glsl')
diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp
index 7649a54..9f1e420 100644
--- a/gfx/gl/shadowMapper.cpp
+++ b/gfx/gl/shadowMapper.cpp
@@ -2,6 +2,7 @@
#include "camera.h"
#include "collections.hpp"
#include "gfx/gl/shaders/vs-shadowDynamicPoint.h"
+#include "gfx/gl/shaders/vs-shadowDynamicPointInst.h"
#include "gfx/gl/shaders/vs-shadowFixedPoint.h"
#include "location.hpp"
#include "maths.h"
@@ -14,7 +15,8 @@
#include <tuple>
#include <vector>
-ShadowMapper::ShadowMapper(const glm::ivec2 & s) : size {s}
+ShadowMapper::ShadowMapper(const glm::ivec2 & s) :
+ fixedPoint {shadowFixedPoint_vs}, dynamicPointInst {shadowDynamicPointInst_vs}, size {s}
{
glBindTexture(GL_TEXTURE_2D, depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
@@ -144,6 +146,7 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const C
const auto lightViewProjection = lightProjection * lightView;
fixedPoint.setViewProjection(lightViewProjection);
dynamicPoint.setViewProjection(lightViewProjection);
+ dynamicPointInst.setViewProjection(lightViewProjection);
const auto & viewport = viewports[bands][out.maps];
glViewport(size.x >> viewport.x, size.y >> viewport.y, size.x >> viewport.z, size.y >> viewport.w);
@@ -157,7 +160,7 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const C
return out;
}
-ShadowMapper::FixedPoint::FixedPoint() : Program {shadowFixedPoint_vs}, viewProjectionLoc {*this, "viewProjection"} { }
+ShadowMapper::FixedPoint::FixedPoint(const Shader & vs) : Program {vs}, viewProjectionLoc {*this, "viewProjection"} { }
void
ShadowMapper::FixedPoint::setViewProjection(const glm::mat4 & viewProjection) const
{
diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h
index a5c2c7b..36371eb 100644
--- a/gfx/gl/shadowMapper.h
+++ b/gfx/gl/shadowMapper.h
@@ -22,7 +22,7 @@ public:
class FixedPoint : public Program {
public:
- FixedPoint();
+ FixedPoint(const Shader & vs);
void setViewProjection(const glm::mat4 &) const;
void use() const;
@@ -40,7 +40,7 @@ public:
RequiredUniformLocation viewProjectionLoc;
RequiredUniformLocation modelLoc;
};
- FixedPoint fixedPoint;
+ FixedPoint fixedPoint, dynamicPointInst;
DynamicPoint dynamicPoint;
// NOLINTNEXTLINE(hicpp-explicit-conversions)
diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp
index 55759cb..52c9275 100644
--- a/gfx/models/mesh.cpp
+++ b/gfx/models/mesh.cpp
@@ -21,24 +21,22 @@ Mesh::configureVAO(VertexArrayObject && vao) const
.addIndices(m_vertexArrayBuffers[1]);
}
-GLsizei
-Mesh::count() const
+void
+Mesh::Draw() const
{
- return m_numIndices;
-}
+ glBindVertexArray(m_vertexArrayObject);
-GLenum
-Mesh::type() const
-{
- return mode;
+ glDrawElements(mode, m_numIndices, GL_UNSIGNED_INT, nullptr);
+
+ glBindVertexArray(0);
}
void
-Mesh::Draw() const
+Mesh::DrawInstanced(GLuint vao, GLsizei count) const
{
- glBindVertexArray(m_vertexArrayObject);
+ glBindVertexArray(vao);
- glDrawElements(mode, m_numIndices, GL_UNSIGNED_INT, nullptr);
+ glDrawElementsInstanced(mode, m_numIndices, GL_UNSIGNED_INT, nullptr, count);
glBindVertexArray(0);
}
diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h
index 472b7ed..538c57a 100644
--- a/gfx/models/mesh.h
+++ b/gfx/models/mesh.h
@@ -14,9 +14,8 @@ public:
Mesh(const std::span<const Vertex> vertices, const std::span<const unsigned int> indices, GLenum = GL_TRIANGLES);
void Draw() const;
+ void DrawInstanced(GLuint vao, GLsizei count) const;
VertexArrayObject & configureVAO(VertexArrayObject &&) const;
- GLsizei count() const;
- GLenum type() const;
private:
glVertexArray m_vertexArrayObject;