From 715d9910a8bd5eca5058a8744862d6046d14f026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 5 Dec 2011 16:38:06 +0100 Subject: [PATCH] Support for texture layers. --- src/AbstractTexture.cpp | 14 ++++++++++++++ src/AbstractTexture.h | 7 ++++++- src/CubeMapTexture.h | 3 ++- src/Texture.h | 3 ++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/AbstractTexture.cpp b/src/AbstractTexture.cpp index 5c1682fa2..51df1e600 100644 --- a/src/AbstractTexture.cpp +++ b/src/AbstractTexture.cpp @@ -17,6 +17,19 @@ namespace Magnum { +#ifndef DOXYGEN_GENERATING_OUTPUT +/* Check corretness of GL_TEXTURE0 + N == GL_TEXTUREN */ +#define texture_assert(num) static_assert(GL_TEXTURE0 + num == GL_TEXTURE##num,\ + "Unsupported constants for GL texture layers") +texture_assert(0); texture_assert(8); texture_assert(16); texture_assert(24); +texture_assert(1); texture_assert(9); texture_assert(17); texture_assert(25); +texture_assert(2); texture_assert(10); texture_assert(18); texture_assert(26); +texture_assert(3); texture_assert(11); texture_assert(19); texture_assert(27); +texture_assert(4); texture_assert(12); texture_assert(20); texture_assert(28); +texture_assert(5); texture_assert(13); texture_assert(21); texture_assert(29); +texture_assert(6); texture_assert(14); texture_assert(22); texture_assert(30); +texture_assert(7); texture_assert(15); texture_assert(23); texture_assert(31); + /* Check correctness of binary OR in setMinificationFilter(). If nobody fucks anything up, this assert should produce the same results on all dimensions, thus testing only on AbstractTexture. */ @@ -27,6 +40,7 @@ static_assert(((AbstractTexture::NearestNeighborFilter|AbstractTexture::BaseMipL ((AbstractTexture::LinearFilter|AbstractTexture::NearestMipLevel) == GL_LINEAR_MIPMAP_NEAREST) && ((AbstractTexture::LinearFilter|AbstractTexture::LinearMipInterpolation) == GL_LINEAR_MIPMAP_LINEAR), "Unsupported constants for GL texture filtering"); +#endif void AbstractTexture::setMinificationFilter(Filter filter, Mipmap mipmap) { /* Only base mip level is supported on rectangle textures */ diff --git a/src/AbstractTexture.h b/src/AbstractTexture.h index 2fb59e011..5bcdb3d99 100644 --- a/src/AbstractTexture.h +++ b/src/AbstractTexture.h @@ -108,11 +108,13 @@ class AbstractTexture { /** * @brief Constructor + * @param layer Texture layer (number between 0 and 31) * @param target Target, e.g. @c GL_TEXTURE_2D. * * Creates one OpenGL texture. */ - inline AbstractTexture(GLenum target): target(target) { + inline AbstractTexture(GLint layer, GLenum target): target(target), _layer(layer) { + glActiveTexture(GL_TEXTURE0 + layer); glGenTextures(1, &texture); } @@ -127,6 +129,7 @@ class AbstractTexture { /** @brief Bind texture for usage / rendering */ inline void bind() const { + glActiveTexture(GL_TEXTURE0 + _layer); glBindTexture(target, texture); } @@ -137,6 +140,7 @@ class AbstractTexture { * particular one. */ inline void unbind() const { + glActiveTexture(GL_TEXTURE0 + _layer); glBindTexture(target, 0); } @@ -231,6 +235,7 @@ class AbstractTexture { }; private: + const GLint _layer; GLuint texture; }; diff --git a/src/CubeMapTexture.h b/src/CubeMapTexture.h index a68c1d974..4c40ba4bf 100644 --- a/src/CubeMapTexture.h +++ b/src/CubeMapTexture.h @@ -48,10 +48,11 @@ class CubeMapTexture: public Texture2D { public: /** * @brief Constructor + * @param layer Texture layer (number between 0 and 31) * * Creates texture with target @c GL_TEXTURE_CUBE_MAP. */ - inline CubeMapTexture(): Texture2D(GL_TEXTURE_CUBE_MAP) {} + inline CubeMapTexture(GLint layer = 0): Texture2D(layer, GL_TEXTURE_CUBE_MAP) {} protected: /** @brief Deleted. Use setDataPositiveX() and others instead. */ diff --git a/src/Texture.h b/src/Texture.h index ce9d79c6f..5961d49b1 100644 --- a/src/Texture.h +++ b/src/Texture.h @@ -53,13 +53,14 @@ template class Texture: public AbstractTexture { /** * @brief Constructor + * @param layer Texture layer (number between 0 and 31) * @param target Target, e.g. @c GL_TEXTURE_RECTANGLE. If not set, * target is based on dimension count (@c GL_TEXTURE_1D, * @c GL_TEXTURE_2D, @c GL_TEXTURE_3D). * * Creates one OpenGL texture. */ - inline Texture(GLenum target = DataHelper::target()): AbstractTexture(target) {} + inline Texture(GLint layer = 0, GLenum target = DataHelper::target()): AbstractTexture(layer, target) {} /** * @brief Set wrapping