GlTextureResource.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2009 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef GLTEXTURERESOURCE_H
00011 #define GLTEXTURERESOURCE_H
00012 
00013 #include "AppVerify.h"
00014 #include "Resource.h"
00015 
00016 #include <QtOpenGL/QGLContext>
00017 
00018 /**
00019  * A class for managing OpenGL textures.
00020  * When created, the object records the current OpenGL context and allocates textures.
00021  * When destroyed, it makes the recorded context current, deletes the textures, and restores the context.
00022  */
00023 class GlTextureObject
00024 {
00025 public:
00026    /**
00027    * This is an implementation detail of the GlTextureObject class. It is used 
00028    * for storing the parameters required by GlTextureResource.
00029    */
00030    class Args
00031    {
00032    public:
00033       Args(GLsizei count) :
00034          mpContext(count <= 0 ? NULL : const_cast<QGLContext*>(QGLContext::currentContext())),
00035          mCount(count < 0 ? 0 : count)
00036       {}
00037 
00038       QGLContext* mpContext;
00039       GLsizei mCount;
00040    };
00041 
00042    /**
00043     * Allocates one or more texture IDs.
00044     *
00045     * @param  args
00046     *         The arguments for obtaining the resource. Should be of type GlTextureObject::Args.
00047     *
00048     * @return A pointer to the texture IDs or \c NULL.
00049     */
00050    GLuint* obtainResource(const Args& args) const
00051    {
00052       if (args.mCount <= 0)
00053       {
00054          return NULL;
00055       }
00056 
00057       GLuint* pTextures = new GLuint[args.mCount];
00058       glGenTextures(args.mCount, pTextures);
00059       return pTextures;
00060    }
00061 
00062    /**
00063     * Releases one or more texture IDs.
00064     *
00065     * @param  args
00066     *         The arguments for releasing the resource. Should be of type GlTextureObject::Args.
00067     *
00068     * @param  pTextures
00069     *         A pointer to the texture IDs to be freed.
00070     */
00071    void releaseResource(const Args& args, GLuint* pTextures) const
00072    {
00073       if (pTextures != NULL)
00074       {
00075          GlContextSave contextSave(args.mpContext);
00076          glDeleteTextures(args.mCount, pTextures);
00077          delete [] pTextures;
00078       }
00079    }
00080 };
00081 
00082 /**
00083  * This is a %Resource class that allocates and deletes OpenGL textures.
00084  *
00085  * This class has a conversion operator to allow a %GlTextureResource object to be
00086  * used wherever a GLuint* would normally be used.
00087 */
00088 class GlTextureResource : public Resource<GLuint, GlTextureObject>
00089 {
00090 public:
00091    /**
00092    * Constructs a Resource object that wraps OpenGL textures.
00093    *
00094    * @param   count
00095    *          The number of textures to create.
00096    */
00097    GlTextureResource(GLsizei count) :
00098       Resource<GLuint, GlTextureObject>(GlTextureObject::Args(count))
00099    {}
00100 
00101    /**
00102     * Gets the context which was active when the object was created.
00103     *
00104     * @return The context.
00105     *
00106     * @see GlContextSave
00107     */
00108    inline QGLContext* getContext() const
00109    {
00110       return getArgs().mpContext;
00111    }
00112 
00113    /**
00114     * Gets the number of textures.
00115     *
00116     * @return The number of textures.
00117     */
00118    inline GLsizei getCount() const
00119    {
00120       return getArgs().mCount;
00121    }
00122 
00123    /**
00124     * Convenience operator which allows this object to be used wherever a const GLuint* would normally be used.
00125     *
00126     * @return A pointer to all textures in the list or \c NULL if no textures exist.
00127     */
00128    operator const GLuint*() const
00129    {
00130       return get();
00131    }
00132 
00133    /**
00134     * Convenience operator which allows this object to be used wherever a GLuint would normally be used.
00135     *
00136     * @return The first texture in the list or 0 if no textures exist.
00137     */
00138    operator GLuint() const
00139    {
00140       return getCount() <= 0 ? 0 : *get();
00141    }
00142 };
00143 
00144 #endif

Software Development Kit - Opticks 4.9.0 Build 16218