Texture2D.h 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#ifndef __GPU__TEXTURE2D_H__
#define __GPU__TEXTURE2D_H__




#include "GPUdefs.h"
#include "InstantiatedObject.h"
#include <map>
#include <vector>


/*
 *
 *  CLASS DECLARATION.
 *
 */

namespace GPU
{
    class Texture
    {
    protected:
        GLenum              m_Target;
        GLuint              m_Id;
        GLint               m_TexUnit;

    public:
        inline              Texture() : m_Target(GL_NONE), m_Id(0), m_TexUnit(0) {}
        inline              Texture( GLenum target ) : m_Target(target), m_Id(0), m_TexUnit(0) {}
        virtual             ~Texture() {}

        inline GLuint       Id() const                      { return m_Id; }

        void                Bind( GLuint texUnit );
        void                Unbind() const;

        GLint               InternalFormat() const;

        inline void         SetBaseLevel( int lvl )
        {
50 51
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_BASE_LEVEL, lvl ) );
52 53 54
        }
        inline void         SetMaxLevel( int lvl )
        {
55 56
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_MAX_LEVEL, lvl ) );
57 58 59 60 61 62 63
        }
        inline void         SetBaseAndMaxLevels( int bothLvl )
        {
            SetBaseAndMaxLevels( bothLvl, bothLvl );
        }
        inline void         SetBaseAndMaxLevels( int baseLvl, int maxLvl )
        {
64 65 66
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_BASE_LEVEL, baseLvl ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_MAX_LEVEL , maxLvl  ) );
67 68 69 70
        }

        inline void         SetCompareMode( GLenum mode )
        {
71 72
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_COMPARE_MODE, mode ) );
73 74 75
        }
        inline void         SetCompareFunc( GLenum func )
        {
76 77
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_COMPARE_FUNC, func ) );
78 79 80 81
        }

        inline void         SetDepthTextureMode( GLenum mode )
        {
82 83
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_DEPTH_TEXTURE_MODE, mode ) );
84 85 86 87
        }

        inline void         SetFiltering( const GLenum minFilter, const GLenum magFilter )
        {
88 89 90
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_MIN_FILTER, minFilter ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_MAG_FILTER, magFilter ) );
91 92 93 94 95 96 97 98
        }
        inline void         SetFiltering( const GLenum bothFilters )
        {
            SetFiltering( bothFilters, bothFilters );
        }

        inline void         SetWrapS( GLenum s )
        {
99 100
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_WRAP_S, s ) );
101 102 103
        }
        inline void         SetWrapT( GLenum t )
        {
104 105
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_WRAP_T, t ) );
106 107 108
        }
        inline void         SetWrapR( GLenum r )
        {
109 110
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_WRAP_R, r ) );
111 112 113 114 115 116 117
        }
        inline void         SetWrapST( GLenum st )
        {
            SetWrapST( st, st );
        }
        inline void         SetWrapST( GLenum s, GLenum t )
        {
118 119 120
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_WRAP_S, s ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_WRAP_T, t ) );
121 122 123 124 125 126 127
        }
        inline void         SetWrapSTR( GLenum str )
        {
            SetWrapSTR( str, str, str );
        }
        inline void         SetWrapSTR( GLenum s, GLenum t, GLenum r )
        {
128 129 130 131
            gpuAssert( glBindTexture( m_Target, m_Id ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_WRAP_S, s ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_WRAP_T, t ) );
            gpuAssert( glTexParameteri( m_Target, GL_TEXTURE_WRAP_R, r ) );
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        }
    };



    class Texture2D : public InstantiatedObject, public Texture
    {
        /********************\
        | Member variable(s) |
        \********************/
    private:
        unsigned int        m_Width;
        unsigned int        m_Height;

        /*****************************\
        | Constructor(s) / destructor |
        \*****************************/
    public:
        inline              Texture2D() : InstantiatedObject(), Texture(GL_TEXTURE_2D), m_Width(0), m_Height(0) {}
        inline              ~Texture2D()                                    { Release(); }

        /********************\
        | Member function(s) |
        \********************/
    protected:
        bool                Allocate();
        bool                Unallocate();

    public:
        bool                Create();
        inline bool         Create( GLenum internalFormat,
                                    GLsizei width,
                                    GLsizei height,
                                    GLint border,
                                    GLenum format,
                                    GLenum type,
                                    const GLvoid* data )    { GLenum f[] = {internalFormat,GL_NONE}; return Create(f,width,height,border,format,type,data); }
        bool                Create( GLenum internalFormats[],
                                    GLsizei width,
                                    GLsizei height,
                                    GLint border,
                                    GLenum format,
                                    GLenum type,
                                    const GLvoid* data );
        inline bool         Create( GLenum internalFormat,
                                    GLsizei width,
                                    GLsizei height,
                                    GLenum format,
                                    GLenum type,
                                    const GLvoid* data )    { return Create(internalFormat,width,height,0,format,type,data); }
        inline bool         Create( GLenum internalFormats[],
                                    GLsizei width,
                                    GLsizei height,
                                    GLenum format,
                                    GLenum type,
                                    const GLvoid* data )    { return Create(internalFormats,width,height,0,format,type,data); }
        inline bool         Create( GLenum internalFormat,
                                    GLsizei width,
                                    GLsizei height )        { return Create(internalFormat,width,height,0,GL_RGB,GL_INT,NULL); }
        inline bool         Create( GLenum internalFormats[],
                                    GLsizei width,
                                    GLsizei height )        { return Create(internalFormats,width,height,0,GL_RGB,GL_INT,NULL); };

        inline bool         Load( GLenum internalFormat,
                                  GLsizei width,
                                  GLsizei height,
                                  GLint border,
                                  GLenum format,
                                  GLenum type,
                                  const GLvoid* data,
                                  GLint level = 0 )         { GLenum f[] = {internalFormat,GL_NONE}; return Load(f,width,height,border,format,type,data,level); }
        bool                Load( GLenum internalFormats[],
                                  GLsizei width,
                                  GLsizei height,
                                  GLint border,
                                  GLenum format,
                                  GLenum type,
                                  const GLvoid* data,
                                  GLint level = 0 );
        inline bool         Load( GLenum internalFormat,
                                  GLsizei width,
                                  GLsizei height,
                                  GLenum format,
                                  GLenum type,
                                  const GLvoid* data,
                                  GLint level = 0 )         { return Load(internalFormat,width,height,0,format,type,data,level); }
        inline bool         Load( GLenum internalFormats[],
                                  GLsizei width,
                                  GLsizei height,
                                  GLenum format,
                                  GLenum type,
                                  const GLvoid* data,
                                  GLint level = 0 )         { return Load(internalFormats,width,height,0,format,type,data,level); }
        inline bool         Load( GLenum internalFormat,
                                  GLsizei width,
                                  GLsizei height,
                                  GLint level = 0 )         { return Load(internalFormat,width,height,0,GL_RGB,GL_INT,NULL,level); }
        inline bool         Load( GLenum internalFormats[],
                                  GLsizei width,
                                  GLsizei height,
                                  GLint level = 0 )         { return Load(internalFormats,width,height,0,GL_RGB,GL_INT,NULL,level); }

        inline unsigned int Width() const                                       { return m_Width; }
        inline unsigned int Height() const                                      { return m_Height; }
        //unsigned int        RealWidth() const;
        //unsigned int        RealHeight() const;

        bool                DumpTo( void *buffer,
                                    const GLenum format,
                                    const GLenum type,
                                    const GLint level = 0 ) const;
        bool                DumpTo( void *buffer,
                                    const unsigned int x,
                                    const unsigned int y,
                                    const unsigned int w,
                                    const unsigned int h,
                                    const GLenum format,
                                    const GLenum type,
                                    const GLint level = 0 ) const;
    };
};




#endif //__GPU__TEXTURE2D_H__