diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-04-17 17:09:40 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-04-17 17:09:40 +0100 |
commit | da1c1336596d361678b3f641a1d23ab89e078789 (patch) | |
tree | c2f8b0fb1693cccc381e43f759cb27a19cd46208 /gfx/gl | |
parent | Fix up the way spotlight shader works (diff) | |
download | ilt-da1c1336596d361678b3f641a1d23ab89e078789.tar.bz2 ilt-da1c1336596d361678b3f641a1d23ab89e078789.tar.xz ilt-da1c1336596d361678b3f641a1d23ab89e078789.zip |
Revamp how VertexArrayObject configures attributes and data
Diffstat (limited to 'gfx/gl')
-rw-r--r-- | gfx/gl/sceneRenderer.cpp | 6 | ||||
-rw-r--r-- | gfx/gl/sceneShader.cpp | 8 | ||||
-rw-r--r-- | gfx/gl/vertexArrayObject.hpp | 78 |
3 files changed, 50 insertions, 42 deletions
diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index 873dc5b..6542bea 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -18,7 +18,7 @@ SceneRenderer::SceneRenderer(glm::ivec2 s, GLuint o) : lighting {lighting_vs, lighting_fs}, shadowMapper {{2048, 2048}} { shader.setViewPort({0, 0, size.x, size.y}); - VertexArrayObject<glm::i8vec4>::configure(displayVAO, displayVBO, displayVAOdata); + VertexArrayObject {displayVAO}.addAttribs<glm::i8vec4>(displayVBO, displayVAOdata); glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); const auto configuregdata @@ -128,8 +128,8 @@ SceneRenderer::renderQuad() const SceneRenderer::DirectionalLightProgram::DirectionalLightProgram() : Program {lighting_vs, directionalLight_fs}, directionLoc {*this, "lightDirection"}, colourLoc {*this, "lightColour"}, lightViewProjectionLoc {*this, "lightViewProjection"}, - lightViewProjectionCountLoc {*this, "lightViewProjectionCount"}, lightViewShadowMapRegionLoc { - *this, "shadowMapRegion"} + lightViewProjectionCountLoc {*this, "lightViewProjectionCount"}, + lightViewShadowMapRegionLoc {*this, "shadowMapRegion"} { } diff --git a/gfx/gl/sceneShader.cpp b/gfx/gl/sceneShader.cpp index 37eacbd..bccd970 100644 --- a/gfx/gl/sceneShader.cpp +++ b/gfx/gl/sceneShader.cpp @@ -88,7 +88,7 @@ SceneShader::WaterProgram::use(float waveCycle) const SceneShader::PointLightShader::PointLightShader() : SceneProgram {pointLight_vs, pointLight_gs, pointLight_fs}, colourLoc {*this, "colour"}, kqLoc {*this, "kq"} { - VertexArrayObject<glm::vec3>::configure(va, b); + VertexArrayObject {va}.addAttribs<glm::vec3>(b); } void @@ -99,7 +99,7 @@ SceneShader::PointLightShader::add(const glm::vec3 & position, const glm::vec3 & glBindBuffer(GL_ARRAY_BUFFER, b); glUniform3fv(colourLoc, 1, glm::value_ptr(colour)); glUniform1f(kqLoc, kq); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::vec3), glm::value_ptr(position)); + glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3), glm::value_ptr(position), GL_DYNAMIC_DRAW); glDrawArrays(GL_POINTS, 0, 1); } @@ -108,7 +108,7 @@ SceneShader::SpotLightShader::SpotLightShader() : colourLoc {*this, "colour"}, kqLoc {*this, "kq"}, arcLoc {*this, "arc"} { using v3pair = std::pair<glm::vec3, glm::vec3>; - VertexArrayObject<v3pair>::configure<&v3pair::first, &v3pair::second>(va, b); + VertexArrayObject {va}.addAttribs<v3pair, &v3pair::first, &v3pair::second>(b); } void @@ -122,6 +122,6 @@ SceneShader::SpotLightShader::add(const glm::vec3 & position, const glm::vec3 & glUniform3fv(directionLoc, 1, glm::value_ptr(direction)); glUniform1f(kqLoc, kq); glUniform1f(arcLoc, arc); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::vec3), glm::value_ptr(position)); + glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3), glm::value_ptr(position), GL_DYNAMIC_DRAW); glDrawArrays(GL_POINTS, 0, 1); } diff --git a/gfx/gl/vertexArrayObject.hpp b/gfx/gl/vertexArrayObject.hpp index 5b9fc60..3e2a18b 100644 --- a/gfx/gl/vertexArrayObject.hpp +++ b/gfx/gl/vertexArrayObject.hpp @@ -2,47 +2,56 @@ #include "collections.hpp" #include "gl_traits.hpp" +#include "special_members.hpp" #include <GL/glew.h> -#include <glm/common.hpp> -template<typename Vertex> class VertexArrayObject { +class VertexArrayObject { public: - template<auto Vertex::*... attribs> - static void - configure(const GLuint arrayObject, const GLuint arrayBuffer, const GLuint indexBuffer, - const SequentialCollection<Vertex> auto & vertices, const SequentialCollection<unsigned int> auto & indices) + [[nodiscard]] VertexArrayObject(const GLuint arrayObject) { glBindVertexArray(arrayObject); - - configure_attribs<attribs...>(arrayBuffer); - data(vertices, arrayBuffer, GL_ARRAY_BUFFER); - data(indices, indexBuffer, GL_ELEMENT_ARRAY_BUFFER); - + } + ~VertexArrayObject() + { glBindVertexArray(0); } + NO_MOVE(VertexArrayObject); + NO_COPY(VertexArrayObject); - template<auto Vertex::*... attribs> - static void - configure(const GLuint arrayObject, const GLuint arrayBuffer, const SequentialCollection<Vertex> auto & vertices) - { - glBindVertexArray(arrayObject); + template<typename m, typename T> struct MP { + constexpr MP(m T::*p) : P {p} { } + operator void *() const + { + return &(static_cast<T *>(nullptr)->*P); + } + m T::*P; + using value_type = m; + }; + template<typename m, typename T> MP(m T::*) -> MP<m, T>; - configure_attribs<attribs...>(arrayBuffer); + template<typename VertexT, MP... attribs> + VertexArrayObject & + addAttribs(const GLuint arrayBuffer, const SequentialCollection<VertexT> auto & vertices) + { + addAttribs<VertexT, attribs...>(arrayBuffer); data(vertices, arrayBuffer, GL_ARRAY_BUFFER); - - glBindVertexArray(0); + return *this; } - template<auto Vertex::*... attribs> - static void - configure(const GLuint arrayObject, const GLuint arrayBuffer) + template<typename VertexT, MP... attribs> + VertexArrayObject & + addAttribs(const GLuint arrayBuffer) { - glBindVertexArray(arrayObject); - - configure_attribs<attribs...>(arrayBuffer); - glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(sizeof(Vertex)), nullptr, GL_DYNAMIC_DRAW); + configure_attribs<VertexT, attribs...>(arrayBuffer); + return *this; + } - glBindVertexArray(0); + template<typename Indices> + VertexArrayObject & + addIndices(const GLuint arrayBuffer, const Indices & indices) + { + data(indices, arrayBuffer, GL_ELEMENT_ARRAY_BUFFER); + return *this; } private: @@ -55,34 +64,33 @@ private: glBufferData(target, static_cast<GLsizeiptr>(sizeof(Value) * data.size()), data.data(), GL_STATIC_DRAW); } - template<typename T> + template<typename VertexT, typename T> static void set_pointer(const GLuint vertexArrayId, const void * ptr) { glEnableVertexAttribArray(vertexArrayId); using traits = gl_traits<T>; - traits::vertexAttribFunc(vertexArrayId, traits::size, traits::type, sizeof(Vertex), ptr); + traits::vertexAttribFunc(vertexArrayId, traits::size, traits::type, sizeof(VertexT), ptr); } - template<auto Vertex::*attrib> + template<typename VertexT, MP attrib> static void set_pointer(const GLuint vertexArrayId) { - set_pointer<std::decay_t<decltype(std::declval<Vertex>().*attrib)>>( - vertexArrayId, &(static_cast<const Vertex *>(nullptr)->*attrib)); + set_pointer<VertexT, typename decltype(attrib)::value_type>(vertexArrayId, attrib); } - template<auto Vertex::*... attribs> + template<typename VertexT, MP... attribs> static void configure_attribs(const GLuint arrayBuffer) { glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); if constexpr (sizeof...(attribs) == 0) { - set_pointer<Vertex>(0, nullptr); + set_pointer<VertexT, VertexT>(0, nullptr); } else { GLuint vertexArrayId {}; - (set_pointer<attribs>(vertexArrayId++), ...); + (set_pointer<VertexT, attribs>(vertexArrayId++), ...); } } }; |