From 03bfd9bf3168f1601bf0e6c257f84bdb317bc0e1 Mon Sep 17 00:00:00 2001 From: dibenedetto Date: Wed, 22 Aug 2012 12:38:09 +0000 Subject: [PATCH] added cube map textures to glw. --- wrap/glw/context.h | 25 +++- wrap/glw/framebuffer.h | 24 ++-- wrap/glw/texturecube.h | 312 +++++++++++++++++++++++++++++++++++++++++ wrap/glw/type.h | 1 + wrap/glw/utility.h | 12 ++ 5 files changed, 363 insertions(+), 11 deletions(-) create mode 100644 wrap/glw/texturecube.h diff --git a/wrap/glw/context.h b/wrap/glw/context.h index d402ea02..5fc55d45 100644 --- a/wrap/glw/context.h +++ b/wrap/glw/context.h @@ -10,6 +10,7 @@ #include "./program.h" #include "./renderbuffer.h" #include "./texture2d.h" +#include "./texturecube.h" #include "./framebuffer.h" #include @@ -249,6 +250,24 @@ class Context : public detail::NonCopyable this->bindTexture2D(nullHandle, unit); } + TextureCubeHandle createTextureCube(const TextureCubeArguments & args) + { + TextureCubeHandle handle = this->createHandle(); + handle->object()->create(args); + return handle; + } + + BoundTextureCubeHandle bindTextureCube(TextureCubeHandle & handle, GLint unit) + { + return this->bind(handle, TextureCubeBindingParams(unit)); + } + + void unbindTextureCube(GLint unit) + { + TextureCubeHandle nullHandle; + this->bindTextureCube(nullHandle, unit); + } + FramebufferHandle createFramebuffer(const FramebufferArguments & args) { FramebufferHandle handle = this->createHandle(); @@ -457,7 +476,8 @@ class Context : public detail::NonCopyable this->m_maxTextureUnits = int(texUnits); for (int i=0; im_maxTextureUnits; ++i) { - this->initializeTarget(Texture2DBindingParams(GLint(i))); + this->initializeTarget(Texture2DBindingParams (GLint(i))); + this->initializeTarget(TextureCubeBindingParams (GLint(i))); } } } @@ -496,7 +516,8 @@ class Context : public detail::NonCopyable { for (int i=0; im_maxTextureUnits; ++i) { - this->terminateTarget(Texture2DBindingParams(GLint(i))); + this->terminateTarget(Texture2DBindingParams (GLint(i))); + this->terminateTarget(TextureCubeBindingParams (GLint(i))); } this->m_maxTextureUnits = 0; } diff --git a/wrap/glw/framebuffer.h b/wrap/glw/framebuffer.h index 2f40439d..7f53d582 100644 --- a/wrap/glw/framebuffer.h +++ b/wrap/glw/framebuffer.h @@ -41,7 +41,7 @@ class RenderTarget : target (rTarget) , level (0) , layer (0) - , face (GL_NONE) + , face (GL_TEXTURE_CUBE_MAP_POSITIVE_X) { ; } @@ -51,7 +51,7 @@ class RenderTarget this->target.setNull(); this->level = 0; this->layer = -1; - this->face = GL_NONE; + this->face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; } bool isNull(void) const @@ -67,6 +67,11 @@ inline RenderTarget texture2DTarget(Texture2DHandle & handle, GLint level = 0) return RenderTarget(handle, level, 0, GL_NONE); } +inline RenderTarget textureCubeTarget(TextureCubeHandle & handle, GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X, GLint level = 0) +{ + return RenderTarget(handle, level, 0, face); +} + inline RenderTarget renderbufferTarget(RenderbufferHandle & handle) { return RenderTarget(handle, 0, 0, GL_NONE); @@ -163,10 +168,10 @@ class FramebufferArguments : public ObjectArguments void clear(void) { BaseType::clear(); - this->colorTargets .clear(); - this->depthTarget .clear(); - this->stencilTarget .clear(); - this->targetInputs .clear(); + this->colorTargets .clear(); + this->depthTarget .clear(); + this->stencilTarget .clear(); + this->targetInputs .clear(); } }; @@ -401,9 +406,10 @@ class Framebuffer : public Object switch (handle->type()) { - case RenderbufferType : glFramebufferRenderbuffer (target, attachment, GL_RENDERBUFFER, handle->name() ); break; - case Texture2DType : glFramebufferTexture2D (target, attachment, GL_TEXTURE_2D, handle->name(), renderTarget.level); break; - default : GLW_ASSERT(0); break; + case RenderbufferType : glFramebufferRenderbuffer (target, attachment, GL_RENDERBUFFER, handle->name() ); break; + case Texture2DType : glFramebufferTexture2D (target, attachment, GL_TEXTURE_2D, handle->name(), renderTarget.level); break; + case TextureCubeType : glFramebufferTexture2D (target, attachment, renderTarget.face, handle->name(), renderTarget.level); break; + default : GLW_ASSERT(0); break; } return true; diff --git a/wrap/glw/texturecube.h b/wrap/glw/texturecube.h new file mode 100644 index 00000000..ce57c59e --- /dev/null +++ b/wrap/glw/texturecube.h @@ -0,0 +1,312 @@ +#ifndef GLW_TEXTURECUBE_H +#define GLW_TEXTURECUBE_H + +#include "./texture.h" + +namespace glw +{ + +class TextureCubeArguments : public TextureArguments +{ + public: + + typedef TextureArguments BaseType; + typedef TextureCubeArguments ThisType; + + GLsizei size; + GLenum dataFormat; + GLenum dataType; + const void * data; + TextureSampleMode sampler; + + TextureCubeArguments(void) + { + this->clear(); + } + + void clear(void) + { + BaseType::clear(); + this->size = 0; + this->dataFormat = GL_NONE; + this->dataType = GL_NONE; + this->data = 0; + this->sampler.clear(); + } +}; + +class TextureCube : public Texture +{ + friend class Context; + + public: + + typedef Texture BaseType; + typedef TextureCube ThisType; + + virtual Type type(void) const + { + return TextureCubeType; + } + + virtual int imageDimensions(void) const + { + return 2; + } + + virtual bool isArray(void) const + { + return false; + } + + GLsizei size(void) const + { + return this->m_size; + } + + GLsizei width(void) const + { + return this->size(); + } + + GLsizei height(void) const + { + return this->size(); + } + + void setImage(GLenum target, GLint unit, GLint level, GLsizei size, GLenum dataFormat, GLenum dataType, const void * data) + { + (void)unit; + GLW_ASSERT(this->isValid()); + glTexImage2D(target, level, this->m_format, size, size, 0, dataFormat, dataType, data); + } + + void getImage(GLenum target, GLint unit, GLint level, GLenum dataFormat, GLenum dataType, void * data) + { + (void)unit; + GLW_ASSERT(this->isValid()); + glGetTexImage(target, level, dataFormat, dataType, data); + } + + void setSubImage(GLenum target, GLint unit, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) + { + (void)unit; + GLW_ASSERT(this->isValid()); + glTexSubImage2D(target, level, xoffset, yoffset, width, height, dataFormat, dataType, data); + } + + void generateMipmap(GLenum target, GLint unit) + { + (void)unit; + GLW_ASSERT(this->isValid()); + glGenerateMipmap(target); + } + + void setSampleMode(GLenum target, GLint unit, const TextureSampleMode & sampler) + { + (void)unit; + GLW_ASSERT(this->isValid()); + if (GLW_CARE_OF(sampler.minFilter)) glTexParameteri(target, GL_TEXTURE_MIN_FILTER, sampler.minFilter); + if (GLW_CARE_OF(sampler.magFilter)) glTexParameteri(target, GL_TEXTURE_MAG_FILTER, sampler.magFilter); + if (GLW_CARE_OF(sampler.wrapS )) glTexParameteri(target, GL_TEXTURE_WRAP_S, sampler.wrapS ); + if (GLW_CARE_OF(sampler.wrapT )) glTexParameteri(target, GL_TEXTURE_WRAP_T, sampler.wrapT ); + } + + protected: + + GLsizei m_size; + + TextureCube(Context * ctx) + : BaseType (ctx) + , m_size (0) + { + ; + } + + bool create(const TextureCubeArguments & args) + { + this->destroy(); + GLint boundName = 0; + glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &boundName); + glGenTextures(1, &(this->m_name)); + glBindTexture(GL_TEXTURE_CUBE_MAP, this->m_name); + for (int f=0; f<6; ++f) + { + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, 0, args.format, args.size, args.size, 0, args.dataFormat, args.dataType, args.data); + } + this->m_format = args.format; + this->m_size = args.size; + this->setSampleMode(GL_TEXTURE_CUBE_MAP, 0, args.sampler); + glBindTexture(GL_TEXTURE_CUBE_MAP, boundName); + return true; + } + + virtual void doDestroy(void) + { + BaseType::doDestroy(); + this->m_format = GL_NONE; + this->m_size = 0; + } + + virtual bool doIsValid(void) const + { + return ((this->m_format != GL_NONE) && (this->m_size > 0)); + } +}; + +namespace detail { template <> struct BaseOf { typedef Texture Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type TextureCubePtr; + +class SafeTextureCube : public SafeTexture +{ + friend class Context; + friend class BoundTextureCube; + + public: + + typedef SafeTexture BaseType; + typedef SafeTextureCube ThisType; + + SafeTextureCube(void) + : BaseType() + { + ; + } + + GLsizei size(void) const + { + return this->object()->size(); + } + + GLsizei width(void) const + { + return this->object()->width(); + } + + GLsizei height(void) const + { + return this->object()->height(); + } + + protected: + + SafeTextureCube(const TextureCubePtr & texture2D) + : BaseType(texture2D) + { + ; + } + + const TextureCubePtr & object(void) const + { + return static_cast(BaseType::object()); + } + + TextureCubePtr & object(void) + { + return static_cast(BaseType::object()); + } +}; + +namespace detail { template <> struct BaseOf { typedef SafeTexture Type; }; }; +namespace detail { template <> struct ObjectBase { typedef TextureCube Type; }; }; +namespace detail { template <> struct ObjectSafe { typedef SafeTextureCube Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type TextureCubeHandle; + +class TextureCubeBindingParams : public TextureBindingParams +{ + public: + + typedef TextureBindingParams BaseType; + typedef TextureCubeBindingParams ThisType; + + TextureCubeBindingParams(void) + : BaseType() + { + ; + } + + TextureCubeBindingParams(GLenum aUnit) + : BaseType(GL_TEXTURE_CUBE_MAP, aUnit) + { + ; + } +}; + +class BoundTextureCube : public BoundTexture +{ + friend class Context; + + public: + + typedef BoundTexture BaseType; + typedef BoundTextureCube ThisType; + + BoundTextureCube(void) + : BaseType() + { + ; + } + + const TextureCubeHandle & handle(void) const + { + return static_cast(BaseType::handle()); + } + + TextureCubeHandle & handle(void) + { + return static_cast(BaseType::handle()); + } + + void setSampleMode(const TextureSampleMode & sampler) + { + this->object()->setSampleMode(this->m_target, this->m_unit, sampler); + } + + void setImage(GLenum face, GLint level, GLsizei size, GLenum dataFormat, GLenum dataType, const void * data) + { + this->object()->setImage(face, this->m_unit, level, size, dataFormat, dataType, data); + } + + void getImage(GLenum face, GLint level, GLenum dataFormat, GLenum dataType, void * data) + { + this->object()->getImage(face, this->m_unit, level, dataFormat, dataType, data); + } + + void setSubImage(GLenum face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) + { + this->object()->setSubImage(face, this->m_unit, level, xoffset, yoffset, width, height, dataFormat, dataType, data); + } + + void generateMipmap(void) + { + this->object()->generateMipmap(this->m_target, this->m_unit); + } + + protected: + + BoundTextureCube(const TextureCubeHandle & handle, const TextureCubeBindingParams & params) + : BaseType(handle, params) + { + ; + } + + const TextureCubePtr & object(void) const + { + return this->handle()->object(); + } + + TextureCubePtr & object(void) + { + return this->handle()->object(); + } +}; + +namespace detail { template <> struct ParamsOf { typedef TextureCubeBindingParams Type; }; }; +namespace detail { template <> struct BaseOf { typedef BoundObject Type; }; }; +namespace detail { template <> struct ObjectBase { typedef TextureCube Type; }; }; +namespace detail { template <> struct ObjectBound { typedef BoundTextureCube Type; }; }; +typedef detail::ObjectSharedPointerTraits ::Type BoundTextureCubeHandle; + +}; + +#endif // GLW_TEXTURECUBE_H diff --git a/wrap/glw/type.h b/wrap/glw/type.h index c0dc799d..53925114 100644 --- a/wrap/glw/type.h +++ b/wrap/glw/type.h @@ -16,6 +16,7 @@ enum Type FragmentShaderType, ProgramType, Texture2DType, + TextureCubeType, FramebufferType }; diff --git a/wrap/glw/utility.h b/wrap/glw/utility.h index 702443af..b51fafae 100644 --- a/wrap/glw/utility.h +++ b/wrap/glw/utility.h @@ -210,6 +210,18 @@ inline Texture2DHandle createTexture2D(Context & ctx, GLenum format, GLsizei wid return ctx.createTexture2D(args); } +inline TextureCubeHandle createTextureCube(Context & ctx, GLenum format, GLsizei size, GLenum dataFormat, GLenum dataType, const void * data = 0, const TextureSampleMode & sampler = TextureSampleMode()) +{ + TextureCubeArguments args; + args.format = format; + args.size = size; + args.dataFormat = dataFormat; + args.dataType = dataType; + args.data = data; + args.sampler = sampler; + return ctx.createTextureCube(args); +} + inline FramebufferHandle createFramebufferWithDepthStencil ( Context & ctx,