From e98fdea344f173c59b11549432b20114081a58b1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 25 Jan 2024 23:19:03 +0000 Subject: Add traits helpers for glTexParameter functions --- gfx/gl/sceneRenderer.cpp | 4 ++-- gfx/gl/shadowMapper.cpp | 10 +++++----- gfx/models/texture.cpp | 17 +++++++++-------- lib/gl_traits.h | 22 ++++++++++++++++++++++ test/testRenderOutput.cpp | 5 +++-- ui/font.cpp | 11 +++++------ ui/icon.cpp | 10 +++++----- 7 files changed, 51 insertions(+), 28 deletions(-) diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index 4ae4297..4c96902 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -24,8 +24,8 @@ SceneRenderer::SceneRenderer(ScreenAbsCoord s, GLuint o) : const auto configuregdata = [this](const GLuint data, const std::initializer_list 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); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameter(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); diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index ae374fe..ab4ac50 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -21,12 +21,12 @@ ShadowMapper::ShadowMapper(const TextureAbsCoord & 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); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - 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); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); static constexpr RGBA border {std::numeric_limits::infinity()}; - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, glm::value_ptr(border)); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0); diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index 35f8d35..0ebba08 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -46,11 +47,11 @@ Texture::Texture(GLsizei width, GLsizei height, const void * data, TextureOption glBindTexture(type, m_texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexParameteri(type, GL_TEXTURE_WRAP_S, TextureOptions::glMapMode(to.wrapU)); - glTexParameteri(type, GL_TEXTURE_WRAP_T, TextureOptions::glMapMode(to.wrapV)); + glTexParameter(type, GL_TEXTURE_WRAP_S, TextureOptions::glMapMode(to.wrapU)); + glTexParameter(type, GL_TEXTURE_WRAP_T, TextureOptions::glMapMode(to.wrapV)); - glTexParameteri(type, GL_TEXTURE_MIN_FILTER, to.minFilter); - glTexParameteri(type, GL_TEXTURE_MAG_FILTER, to.magFilter); + glTexParameter(type, GL_TEXTURE_MIN_FILTER, to.minFilter); + glTexParameter(type, GL_TEXTURE_MAG_FILTER, to.magFilter); glTexImage2D(type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); } @@ -125,11 +126,11 @@ TextureAtlas::TextureAtlas(GLsizei width, GLsizei height, GLuint count) : Textur { glBindTexture(GL_TEXTURE_RECTANGLE, m_atlas); - glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameter(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameter(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameter(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameter(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA16UI, 2, static_cast(count), 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, nullptr); } diff --git a/lib/gl_traits.h b/lib/gl_traits.h index f641131..14ac9d8 100644 --- a/lib/gl_traits.h +++ b/lib/gl_traits.h @@ -42,6 +42,8 @@ template<> struct gl_traits : public gl_traits_float { static constexpr auto glUniformFunc {&glUniform1f}; static constexpr std::array glUniformvFunc {&glUniform1fv, &glUniform2fv, &glUniform3fv, &glUniform4fv}; static constexpr std::array glUniformmFunc {&glUniformMatrix2fv, &glUniformMatrix3fv, &glUniformMatrix4fv}; + static constexpr auto glTexParameterFunc {&glTexParameterf}; + static constexpr auto glTexParameterfFunc {&glTexParameterfv}; }; template<> struct gl_traits : public gl_traits_longfloat { @@ -60,6 +62,8 @@ template<> struct gl_traits : public gl_traits_integer { static constexpr GLenum type {GL_INT}; static constexpr auto glUniformFunc {&glUniform1i}; static constexpr std::array glUniformvFunc {&glUniform1iv, &glUniform2iv, &glUniform3iv, &glUniform4iv}; + static constexpr auto glTexParameterFunc {&glTexParameteri}; + static constexpr auto glTexParameterfFunc {&glTexParameteriv}; }; template<> struct gl_traits : public gl_traits_integer { @@ -104,6 +108,10 @@ template concept has_glUniformNv = requires { gl_traits::glUniformvFunc; }; template concept has_glUniformMatrixNv = requires { gl_traits::glUniformmFunc; }; +template +concept has_glTexParameter = requires { gl_traits::glTexParameterFunc; }; +template +concept has_glTexParameterf = requires { gl_traits::glTexParameterfFunc; }; template void @@ -147,3 +155,17 @@ glUniform(GLint location, std::span> v) (*gl_traits::glUniformmFunc[L - 2])( location, static_cast(v.size()), GL_FALSE, glm::value_ptr(v.front())); } + +template +void +glTexParameter(GLenum target, GLenum pname, T param) +{ + (*gl_traits::glTexParameterFunc)(target, pname, param); +} + +template +void +glTexParameter(GLenum target, GLenum pname, const glm::vec & param) +{ + (*gl_traits::glTexParameterfFunc)(target, pname, glm::value_ptr(param)); +} diff --git a/test/testRenderOutput.cpp b/test/testRenderOutput.cpp index 9af4451..68b46f6 100644 --- a/test/testRenderOutput.cpp +++ b/test/testRenderOutput.cpp @@ -1,4 +1,5 @@ #include "testRenderOutput.h" +#include #include TestRenderOutput::TestRenderOutput(TextureAbsCoord s) : size {s} @@ -8,8 +9,8 @@ TestRenderOutput::TestRenderOutput(TextureAbsCoord s) : size {s} = [this](const GLuint data, const GLint format, const GLenum type, const GLenum attachment) { glBindTexture(GL_TEXTURE_2D, data); glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, GL_RGBA, type, nullptr); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, data, 0); }; configuregdata(outImage, GL_RGBA, GL_UNSIGNED_BYTE, GL_COLOR_ATTACHMENT0); diff --git a/ui/font.cpp b/ui/font.cpp index ebd856d..305e0f3 100644 --- a/ui/font.cpp +++ b/ui/font.cpp @@ -4,10 +4,9 @@ #include #include #include FT_FREETYPE_H -#include "glArrays.h" +#include "gl_traits.h" #include #include -#include #include #include #include @@ -124,10 +123,10 @@ Font::getTextureWithSpace(unsigned int adv) const glBindTexture(GL_TEXTURE_2D, texture.texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, static_cast(size.x), static_cast(size.y), 0, GL_RED, GL_UNSIGNED_BYTE, nullptr); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); return fontTextures.size() - 1; } diff --git a/ui/icon.cpp b/ui/icon.cpp index 38f4bf9..c3b5078 100644 --- a/ui/icon.cpp +++ b/ui/icon.cpp @@ -1,5 +1,5 @@ #include "icon.h" -#include "glArrays.h" +#include "gl_traits.h" #include #include #include @@ -13,11 +13,11 @@ Icon::Icon(const Image & tex) : size {tex.width, tex.height} { glBindTexture(GL_TEXTURE_2D, m_texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast(tex.width), static_cast(tex.height), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data.data()); } -- cgit v1.2.3