1. 程式人生 > >Ogre原始碼分析與學習筆記-2 紋理

Ogre原始碼分析與學習筆記-2 紋理

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 to
			another 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);
     };