Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

sglTexture.hpp

00001 /*****************************************************************************
00002  * SGL: A Scene Graph Library
00003  *
00004  * Copyright (C) 1997-2001  Scott McMillan   All Rights Reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free
00018  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *****************************************************************************
00020  *     File: sglTexture.hpp
00021  *  Created: 29 September 1997
00022  *  Summary: base class container for image data used for texturing (1D,2D,3D)
00023  *****************************************************************************/
00024 
00025 #ifndef __SGL_TEXTURE_HPP
00026 #define __SGL_TEXTURE_HPP
00027 
00028 #include <sgl.h>
00029 #include <vector>
00030 #include <string>
00031 #include <set>
00032 #include <sglStatelet.hpp>
00033 #include <sglVector.hpp>
00034 
00035 //============================================================================
00036 
00041 class SGL_DLL_API sglTexture : public sglTexturingStatelet
00042 {
00043 public:
00045    enum WrapEnum
00046    {
00047       eREPEAT,
00048       eCLAMP,
00049       eCLAMP_TO_EDGE    // same as eCLAMP when not supported
00050    };
00051 
00053    enum FilterEnum
00054    {
00055       eNEAREST                = GL_NEAREST,
00056       eLINEAR                 = GL_LINEAR,
00057       eNEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
00058       eNEAREST_MIPMAP_LINEAR  = GL_NEAREST_MIPMAP_LINEAR,
00059       eLINEAR_MIPMAP_NEAREST  = GL_LINEAR_MIPMAP_NEAREST,
00060       eLINEAR_MIPMAP_LINEAR   = GL_LINEAR_MIPMAP_LINEAR
00061    };
00062 
00064    enum ExternalFormatEnum
00065    {
00066       eRGB              = GL_RGB,
00067       eRGBA             = GL_RGBA,
00068       eRED              = GL_RED,
00069       eGREEN            = GL_GREEN,
00070       eBLUE             = GL_BLUE,
00071       eALPHA            = GL_ALPHA,
00072       eLUMINANCE        = GL_LUMINANCE,
00073       eLUMINANCE_ALPHA  = GL_LUMINANCE_ALPHA
00074    };
00075 
00077    enum TypeEnum
00078    {
00079       eBYTE             = GL_BYTE,
00080       eUNSIGNED_BYTE    = GL_UNSIGNED_BYTE,
00081       eSHORT            = GL_SHORT,
00082       eFLOAT            = GL_FLOAT,
00083       eUNSIGNED_SHORT   = GL_UNSIGNED_SHORT,
00084       eINT              = GL_INT,
00085       eUNSIGNED_INT     = GL_UNSIGNED_INT,
00086       eBITMAP           = GL_BITMAP
00087    };
00088 
00090    enum InternalFormatEnum
00091    {
00092       eINT_ALPHA4              = GL_ALPHA4,
00093       eINT_LUMINANCE4          = GL_LUMINANCE4,
00094       eINT_INTENSITY4          = GL_INTENSITY4,
00095       eINT_ALPHA               = GL_ALPHA,
00096       eINT_ALPHA8              = GL_ALPHA8,
00097       eINT_LUMINANCE           = GL_LUMINANCE,
00098       eINT_LUMINANCE8          = GL_LUMINANCE8,
00099       eINT_LUMINANCE4_ALPHA4   = GL_LUMINANCE4_ALPHA4,
00100       eINT_LUMINANCE6_ALPHA2   = GL_LUMINANCE6_ALPHA2,
00101       eINT_INTENSITY           = GL_INTENSITY,
00102       eINT_INTENSITY8          = GL_INTENSITY8,
00103       eINT_R3_G3_B2            = GL_R3_G3_B2,
00104       eINT_RGBA2               = GL_RGBA2,
00105       eINT_ALPHA12             = GL_ALPHA12,
00106       eINT_LUMINANCE12         = GL_LUMINANCE12,
00107       eINT_INTENSITY12         = GL_INTENSITY12,
00108       eINT_RGB4                = GL_RGB4,
00109       eINT_ALPHA16             = GL_ALPHA16,
00110       eINT_LUMINANCE16         = GL_LUMINANCE16,
00111       eINT_LUMINANCE_ALPHA     = GL_LUMINANCE_ALPHA,
00112       eINT_LUMINANCE8_ALPHA8   = GL_LUMINANCE8_ALPHA8,
00113       eINT_LUMINANCE12_ALPHA4  = GL_LUMINANCE12_ALPHA4,
00114       eINT_INTENSITY16         = GL_INTENSITY16,
00115       eINT_RGBA4               = GL_RGBA4,
00116       eINT_RGB5_A1             = GL_RGB5_A1,
00117       eINT_LUMINANCE12_ALPHA12 = GL_LUMINANCE12_ALPHA12,
00118       eINT_RGB                 = GL_RGB,
00119       eINT_RGB8                = GL_RGB8,
00120       eINT_LUMINANCE16_ALPHA16 = GL_LUMINANCE16_ALPHA16,
00121       eINT_RGBA                = GL_RGBA,
00122       eINT_RGBA8               = GL_RGBA8,
00123       eINT_RGB10_A2            = GL_RGB10_A2,
00124       eINT_RGB5                = GL_RGB5,
00125       eINT_RGB10               = GL_RGB10,
00126       eINT_RGB12               = GL_RGB12,
00127       eINT_RGB16               = GL_RGB16,
00128       eINT_RGBA12              = GL_RGBA12,
00129       eINT_RGBA16              = GL_RGBA16
00130    };
00131 
00132    enum CompressionLevelEnum
00133    {
00134       eCOMPRESS_NONE = 0,     // highest quality
00135       eCOMPRESS_MIN = 1,
00136       eCOMPRESS_MED = 2,
00137       eCOMPRESS_MAX = 3       // lowest quality, highest compression
00138    };
00139 
00140 public:
00142    virtual ~sglTexture();
00143 
00148    static void bindDirtyTextures();
00149 
00154    static void setAutoBind(bool on);
00155 
00157    static bool getAutoBind();
00158 
00162    static unsigned int getBitsPerTexel(InternalFormatEnum format);
00163 
00168    virtual bool setMipmapLevels(const vector<void *> &images) = 0;
00169 
00176    virtual bool replaceImage(void *new_image, GLint level = 0) = 0;
00177 
00182    void setInternalFormat(InternalFormatEnum format);
00183 
00185    InternalFormatEnum getInternalFormat() const { return m_internal_format; }
00186 
00188    ExternalFormatEnum getExternalFormat() const { return m_external_format; }
00189 
00191    void setCompressionLevel( CompressionLevelEnum fact );
00192 
00194    CompressionLevelEnum getCompressionLevel() { return m_compression_flag; }
00195 
00199    void setLODMinMax( float minLOD, float maxLOD );
00200    void getLODMinMax( float &minLOD, float &maxLOD ) const
00201       { minLOD = m_texture_lod_min; maxLOD = m_texture_lod_max; }
00202 
00204    void setLODBaseLevel( GLuint base );
00205    GLuint getLODBaseLevel() const { return m_texture_lod_base_level; }
00206 
00208    void setLODMaxLevel( GLuint maxLevel );
00209    GLuint getLODMaxLevel() const { return m_texture_lod_max_level; }
00210 
00214    virtual unsigned int getIdealInternalSize() const = 0;
00215 
00220    virtual unsigned int getLikelyInternalSize() const = 0;
00221 
00225    void setBorderColor(const sglVec4f &color);
00226 
00228    void getBorderColor(sglVec4f &color) const {color = m_border_color;}
00229 
00233    void setMinFilter(FilterEnum filter);
00234 
00236    FilterEnum getMinFilter() const { return m_min_filter; }
00237 
00241    void setMagFilter(FilterEnum filter);
00242 
00244    FilterEnum getMagFilter() const { return m_mag_filter; }
00245 
00251    void setMaxAnisotropy(float max);
00253    float getMaxAnisotropy() const { return m_max_anisotropy; }
00254 
00258    static float getMaxSupportedMaxAnisotropy()
00259       { return sgl::s_max_texture_max_anisotropy; }
00260 
00264    void setAutoMipmapFlag(bool on);
00265 
00267    bool getAutoMipmapFlag() { return (m_auto_mipmap_flag)?true:false; }
00268 
00272    inline bool isValid() const {return m_valid_flag;}
00273 
00279    virtual bool checkProxy() = 0;
00280 
00286    virtual void bind() = 0;
00287 
00289    void unbind();
00290 
00295    void reset();
00296 
00298    inline bool isBound() const {return (m_dirty_flag == 0); }
00299 
00303    void applyToUnit(sglCurrState*, unsigned int) const;
00304 
00308    virtual bool getSorted() const { return true; }
00309 
00311    typedef void (*callback)(sglTexture*);
00312 
00317    void setPreBindCallback(callback func);
00318 
00320    callback getPreBindCallback() const { return m_pre_bind_callback; }
00321 
00327    void setPostBindCallback(callback func);
00328 
00330    callback getPostBindCallback() const { return m_post_bind_callback; }
00331 
00333    static unsigned int getNumComponents(ExternalFormatEnum external_format);
00334 
00340    virtual bool buildMipmaps(vector<void*> &mipmaps) = 0;
00341 
00342    // See sglObject::printInfo() for details.
00343    virtual void printInfo(ostream &ostrm, const char *indent_string) const;
00344 
00345 protected:
00347    sglTexture(GLenum target);
00348 
00349    void dirty(unsigned int mask);
00350 
00351    void buildAutoMipmaps(unsigned int mask);
00352 
00353    // flags indicating which aspects of the texture need to be updated on the
00354    // bind
00355    enum DirtyEnum
00356    {
00357         eDIRTY_IMAGE           = 0x0001,
00358         eDIRTY_SUBIMAGE        = 0x0002,
00359         eDIRTY_INTERNAL_FORMAT = 0x0004,
00360         eDIRTY_WRAP_S_MODE     = 0x0008,
00361         eDIRTY_WRAP_T_MODE     = 0x0010,
00362         eDIRTY_BORDER_COLOR    = 0x0020,
00363         eDIRTY_MIN_FILTER      = 0x0040,
00364         eDIRTY_MAG_FILTER      = 0x0080,
00365         eDIRTY_MIPMAP          = 0x0100,
00366         eDIRTY_ANISOTROPY      = 0x0200,
00367         eDIRTY_REPLACE         = 0x0400,
00368         eDIRTY_LOD             = 0x0800,
00369         eDIRTY_RESET           = 0x8000
00370    };
00371 
00372    // flags for hardware mip
00373    enum MipStateEnum
00374    {
00375       eMIP_NONE = 0,
00376       eMIP_SW   = 1,
00377       eMIP_HW   = 2
00378    };
00379 
00380    struct MipmapStruct
00381    {
00382       MipmapStruct(void *image, unsigned int mask)
00383          : m_image(image), m_dirty(mask) {}
00384 
00385       void          *m_image;
00386       unsigned int   m_dirty;
00387    };
00388 
00389 private:
00390    sglTexture();                                // NOT IMPLEMENTED
00391    sglTexture(const sglTexture&);               // NOT IMPLEMENTED
00392    sglTexture& operator=(const sglTexture &);   // NOT IMPLEMENTED
00393 
00394 protected:
00395    static set<sglTexture *> s_dirty_textures;
00396    static bool              s_auto_bind;
00397    static sglLock           s_lock;
00398 
00399    const GLenum             m_target;        // GL_TEXTURE_*D
00400    unsigned int             m_dirty_flag;
00401    ExternalFormatEnum       m_external_format;
00402    void*                    m_image;         // image data
00403    bool                     m_own_image;
00404    vector<MipmapStruct>     m_mipmaps;
00405    GLint                    m_border;
00406 
00407    sglVec4f                 m_border_color;
00408    FilterEnum               m_mag_filter;
00409    FilterEnum               m_min_filter;
00410    float                    m_max_anisotropy;
00411 
00412    TypeEnum                 m_type;
00413    InternalFormatEnum       m_internal_format;
00414    bool                     m_internal_format_flag;
00415 
00416    GLuint                   m_texture_object;
00417    bool                     m_valid_flag;
00418    MipStateEnum             m_auto_mipmap_flag;
00419 
00420    CompressionLevelEnum     m_compression_flag;
00421    GLenum                   m_compression_enum; // actual enum to bind call
00422 
00423    // texture lod
00424    bool                     m_texture_lod_active; // opt: using lod?
00425    float                    m_texture_lod_min;
00426    float                    m_texture_lod_max;
00427    GLuint                   m_texture_lod_base_level;
00428    GLuint                   m_texture_lod_max_level;
00429 
00430    // true if mipmaps have been bound since the m_texture_object was created
00431    bool                     m_bound_mipmaps;
00432 
00433    callback                 m_pre_bind_callback;
00434    callback                 m_post_bind_callback;
00435 
00436    mutable unsigned int     m_internal_size;
00437    mutable bool             m_internal_size_dirty;
00438 };
00439 
00440 #endif

Generated at Mon Jul 1 18:00:06 2002 for SGL by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001