Ogre源码分析与学习笔记-2 纹理

来源:互联网 发布:guts and glory mac 编辑:程序博客网 时间:2024/06/02 02:14

Ogre纹理的继承关系:

 

Resource -> Texture -> GLTexture

 

1. Resource , 顾名思义就是资源, 包括纹理, 材质, 三角形网格等. Resource由ResourceManager管理, 几个核心函数:

    class _OgreExport Resource     {    public:        /** Basic constructor.             @warn                Subclasses must init mName and mSize!        */        Resource()             : mIsLoaded( false ), mSize( 0 )        {         }        /** Virtual destructor. Shouldn't need to be overloaded, as the resource            deallocation code should reside in unload()            @see                Resource::unload()        */        virtual ~Resource()         {             if( mIsLoaded )                unload();         }        /** Loads the resource, if it is not already.        */        virtual void load() = 0;        /** Unloads the resource, but retains data to recreate.        */        virtual void unload() {};        /** Gets the last time the resource was 'touched'.        */        time_t getLastAccess(void) const         {             return mLastAccess;         }        /** A method to make the resource delete itself.            @note                This exists because Resource objects could be created in other processes,                and they need to be destroyed in the process that created them.        */        virtual void destroy()        {            delete this;        }    };

一个Resource的生命周期: Create -> Load -> Unload -> Destroy.
资源对象被构造出来时, 并不会马上把资源内容Load到内存, 而是根据需要Load, 当一个ResourceManager使用的内存达到上限时, 会主动Unload一些资源, 但不会Destroy它. Unload的根据是最后使用时间

    //-----------------------------------------------------------------------    void ResourceManager::load(Resource *res, int priority)    {        res->load();        res->touch();        mResources.insert( ResourceMap::value_type( res->getName(), res ) );    }


Texture类没有实现Resource类的纯虚方法, 而是加入了Texture对象的必要属性:

    class _OgreExport Texture : public Resource    {    public:        /** Blits the contents of src on the texture.            @deprecated                This feature is superseded by the blitImage function.            @param                src the image with the source data        */        virtual void blitToTexture(             const Image &src, unsigned uStartX, unsigned uStartY ) = 0;        /** Blits a rect from an image to the texture.            @param                src The image with the source data.            @param                imgRect The data rect to be copied from the image.            @param                texRect The rect in which to copy the data in the texture.        */        virtual void blitImage(            const Image& src, const Image::Rect imgRect, const Image::Rect texRect )        {        }/** Copies (and maybe scales to fit) the contents of this texture toanother texture. */virtual void copyToTexture( Texture * target ) {};        /** Loads the data from an image.        */        virtual void loadImage( const Image &img ) = 0;    protected:        unsigned long mHeight;        unsigned long mWidth;        unsigned short mNumMipMaps;        float mGamma;        TextureType mTextureType;PixelFormat mFormat;        TextureUsage mUsage;        unsigned short mSrcBpp;        unsigned long mSrcWidth, mSrcHeight;        unsigned short mFinalBpp;        bool mHasAlpha;    };

GLTexture 继承自Texture, 并且不再被继承

    class GLTexture : public Texture    {    public:                void load();        void loadImage( const Image &img );        void loadImages( const std::vector<Image> images );        void unload();        void createRenderTexture();        void blitToTexture( const Image& src,             unsigned uStartX, unsigned uStartY );        GLuint getGLID() const        { return mTextureID; }    };

它实现了最终的OpenGL可以使用的texture, 由于平台相关性, GLTexture 需要使用 GLTextureManager来产生:

    class GLTextureManager : public TextureManager    {    public:        /** Creates a SDLTexture resource.         */        virtual Texture* create( const String& name, TextureType texType);     };


 



 

原创粉丝点击