From 64ede41ebaade64ad6705f7f55ca4a778a156481 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 2 Mar 2026 13:17:28 +0000 Subject: Wrap up some low level texture operations in glTexture class Fixes previously hard coded billboard texture size. --- gfx/gl/billboardPainter.cpp | 6 +++--- gfx/gl/glTexture.cpp | 18 ++++++++++++++++++ gfx/gl/glTexture.h | 17 +++++++++++++++++ gfx/gl/sceneRenderer.cpp | 32 ++++++++++++-------------------- gfx/gl/shadowMapper.cpp | 2 +- gfx/gl/shadowMapper.h | 6 +++--- gfx/gl/shadowStenciller.cpp | 5 ++--- 7 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 gfx/gl/glTexture.cpp create mode 100644 gfx/gl/glTexture.h (limited to 'gfx/gl') diff --git a/gfx/gl/billboardPainter.cpp b/gfx/gl/billboardPainter.cpp index e72e72d..06e13ca 100644 --- a/gfx/gl/billboardPainter.cpp +++ b/gfx/gl/billboardPainter.cpp @@ -40,8 +40,8 @@ BillboardPainter::createBillBoardTextures(GLsizei width, GLsizei height) glTextures<3> textures; glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - const auto configuregdata = [width, height](const GLuint texture, const GLint iformat, const GLenum format) { - glBindTexture(GL_TEXTURE_2D_ARRAY, texture); + const auto configuregdata = [width, height](const auto & texture, const GLint iformat, const GLenum format) { + texture.bind(GL_TEXTURE_2D_ARRAY); glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -78,7 +78,7 @@ BillboardPainter::renderBillBoard( } glUseProgram(program); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - const TextureDimensions billboardSize = {256, 256, 8}; // Texture::getSize(billboard[0]); + const TextureDimensions billboardSize = billboard[0].getSize(); glViewport(0, 0, billboardSize.x, billboardSize.y); const auto & centre = mesh.getDimensions().centre; const auto & size = mesh.getDimensions().size; diff --git a/gfx/gl/glTexture.cpp b/gfx/gl/glTexture.cpp new file mode 100644 index 0000000..8dcf16f --- /dev/null +++ b/gfx/gl/glTexture.cpp @@ -0,0 +1,18 @@ +#include "glTexture.h" + +TextureDimensions +Impl::glTexture::getSize() const +{ + TextureDimensions size {}; + glGetTextureLevelParameteriv(name, 0, GL_TEXTURE_WIDTH, &size.x); + glGetTextureLevelParameteriv(name, 0, GL_TEXTURE_HEIGHT, &size.y); + glGetTextureLevelParameteriv(name, 0, GL_TEXTURE_DEPTH, &size.z); + return size; +} + +void +Impl::glTexture::bind(GLenum type, GLenum unit) const +{ + glActiveTexture(unit); + glBindTexture(type, name); +} diff --git a/gfx/gl/glTexture.h b/gfx/gl/glTexture.h new file mode 100644 index 0000000..c482198 --- /dev/null +++ b/gfx/gl/glTexture.h @@ -0,0 +1,17 @@ +#pragma once + +#include "config/types.h" +#include "glArrays.h" + +namespace Impl { + // NOLINTNEXTLINE(readability-identifier-naming) + struct glTexture : Detail::glNamed { + [[nodiscard]] TextureDimensions getSize() const; + void bind(GLenum type = GL_TEXTURE_2D, GLenum unit = GL_TEXTURE0) const; + }; +} + +// NOLINTBEGIN(readability-identifier-naming) +template using glTextures = glManagedArray; +using glTexture = glManagedSingle; +// NOLINTEND(readability-identifier-naming) diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index e693787..efa08ef 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -24,9 +24,9 @@ SceneRenderer::SceneRenderer(ScreenAbsCoord s, GLuint o, glDebugScope) : shader.setViewPort({0, 0, size.x, size.y}); VertexArrayObject {displayVAO}.addAttribs(displayVBO, displayVAOdata); - const auto configuregdata = [this](const GLuint data, const std::initializer_list iformats, + const auto configuregdata = [this](const auto & data, const std::initializer_list iformats, const GLenum format, const GLenum attachment) { - glBindTexture(GL_TEXTURE_2D, data); + data.bind(); 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) { @@ -65,8 +65,8 @@ SceneRenderer::resize(ScreenAbsCoord newSize) glDebugScope _ {output}; size = newSize; camera.setAspect(ratio(size)); - const auto configuregdata = [this](const GLuint data, const GLint iformat, const GLenum format) { - glBindTexture(GL_TEXTURE_2D, data); + const auto configuregdata = [this](const auto & data, const GLint iformat, const GLenum format) { + data.bind(); glTexImage2D(GL_TEXTURE_2D, 0, iformat, size.x, size.y, 0, format, GL_BYTE, nullptr); }; configuregdata(gPosition, GL_RGB32I, GL_RGB_INTEGER); @@ -124,12 +124,9 @@ SceneRenderer::render(const SceneProvider & scene) const // * per light - reads normal and position, writes illumination glBindFramebuffer(GL_FRAMEBUFFER, gBufferIll); glBlendFunc(GL_ONE, GL_ONE); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, gPosition); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, gNormal); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D_ARRAY, shadowMapper); + gPosition.bind(GL_TEXTURE_2D, GL_TEXTURE0); + gNormal.bind(GL_TEXTURE_2D, GL_TEXTURE1); + shadowMapper.bind(GL_TEXTURE2); glDisable(GL_DEPTH_TEST); scene.lights(shader); } @@ -141,10 +138,8 @@ SceneRenderer::render(const SceneProvider & scene) const glCullFace(GL_BACK); glDisable(GL_BLEND); glDisable(GL_DEPTH_TEST); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, gAlbedoSpec); - glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, gIllumination); + gAlbedoSpec.bind(GL_TEXTURE_2D, GL_TEXTURE2); + gIllumination.bind(GL_TEXTURE_2D, GL_TEXTURE3); lighting.use(); renderQuad(); } @@ -168,12 +163,9 @@ SceneRenderer::setDirectionalLight( const auto lvp = shadowMapper.update(scene, direction, camera); glBindFramebuffer(GL_FRAMEBUFFER, gBufferIll); glBlendFunc(GL_ONE, GL_ONE); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, gPosition); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, gNormal); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D_ARRAY, shadowMapper); + gPosition.bind(GL_TEXTURE_2D, GL_TEXTURE0); + gNormal.bind(GL_TEXTURE_2D, GL_TEXTURE1); + shadowMapper.bind(GL_TEXTURE2); glViewport(0, 0, size.x, size.y); dirLight.use(); dirLight.setDirectionalLight(colour, direction.vector(), camera.getPosition(), lvp); diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 35c225b..03851f5 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -32,7 +32,7 @@ ShadowMapper::ShadowMapper(const TextureAbsCoord & s) : size {s}, frustum {{}, {}, {}} { glDebugScope _ {depthMap}; - glBindTexture(GL_TEXTURE_2D_ARRAY, depthMap); + depthMap.bind(GL_TEXTURE_2D_ARRAY); glTexImage3D( GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT, size.x, size.y, 4, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST); diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index 8b5e0b6..0d9763a 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -68,10 +68,10 @@ public: DynamicPoint dynamicPoint; StencilShadowProgram stencilShadowProgram; - // NOLINTNEXTLINE(hicpp-explicit-conversions) - operator GLuint() const + void + bind(GLenum unit) const { - return depthMap; + depthMap.bind(GL_TEXTURE_2D_ARRAY, unit); } private: diff --git a/gfx/gl/shadowStenciller.cpp b/gfx/gl/shadowStenciller.cpp index aee7161..7a0fc9b 100644 --- a/gfx/gl/shadowStenciller.cpp +++ b/gfx/gl/shadowStenciller.cpp @@ -1,7 +1,6 @@ #include "shadowStenciller.h" #include "gfx/lightDirection.h" #include "gfx/models/mesh.h" -#include "glArrays.h" #include "gl_traits.h" #include "gldebug.h" #include "maths.h" @@ -34,7 +33,7 @@ glTexture ShadowStenciller::createStencilTexture(GLsizei width, GLsizei height) { glTexture stencil; - glBindTexture(GL_TEXTURE_2D_ARRAY, stencil); + stencil.bind(GL_TEXTURE_2D_ARRAY); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameter(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); @@ -62,7 +61,7 @@ ShadowStenciller::renderStencil(const glTexture & stencil, const MeshBase & mesh } glUseProgram(shadowCaster); glClear(GL_DEPTH_BUFFER_BIT); - const auto stencilSize = Texture::getSize(stencil); + const auto stencilSize = stencil.getSize(); glViewport(0, 0, stencilSize.x, stencilSize.y); const auto & centre = mesh.getDimensions().centre; const auto & size = mesh.getDimensions().size; -- cgit v1.3