亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

gaia源碼分析

系統(tǒng) 1926 0

最近在看《Real Time 3D Terrain Engines Using C++And DirectX 9》,不過是看網上翻譯的版本叫《實時地形引擎》 看英文實在蛋疼,還好有翻譯版的,要多多感謝承天一大哥!這本書講解了地形制作的一些技術,更吸引人的是實現了一個小型的渲染引擎,對于我這樣的游戲引擎初學者很適合。gaia正是書中構建的引擎的名字。這里來寫寫一些簡單的分析,作為學習筆記。

?

(一) 資源管理

1、? data_pool.h

cPoolGroup : 模板類,池組,顧名思義,就是裝載一組數據的對象

?

      template <
      
        class
      
       T>
      
class cPoolGroup
{
public :

cPoolGroup(uint16 maxCount);
~cPoolGroup(){};

void create();
void destroy();

uint16 addMember( const T& member);
uint16 nextMember();
void release(uint16 index);

uint16 totalOpen() const ;
uint16 totalUsed() const ;
uint16 firstOpen() const ;
bool isOpen(uint16 index) const ;
T& member(uint16 index);
const T& member(uint16 index) const ;
T* memberPtr(uint16 index);
const T* memberPtr(uint16 index) const ;

private :
uint16 m_totalOpen;
uint16 m_firstOpen;
uint16 m_maxCount;
uint16* m_nextOpenList;
T* m_memberList; // 數據對象鏈表
};

?


?

?

?

cDataPoolInterface :數據池接口,抽象類

      
        class
      
       cDataPoolInterface
      
{
public :
typedef void (*MEMBER_CALLBACK)(cDataPoolInterface* pPool, cPoolHandle handle, void * member);

cDataPoolInterface():m_initialized( false ){};
virtual ~cDataPoolInterface(){};

virtual void initialize(uint16 growSize)= 0 ;;
virtual void destroy()= 0 ;;
virtual void clear()= 0 ;
virtual cPoolHandle nextHandle()= 0 ;
virtual void release(cPoolHandle* pHandle)= 0 ;
virtual void forEach(MEMBER_CALLBACK function)= 0 ;

virtual void * getGenericPtr(cPoolHandle index)= 0 ;

bool isInitialized() const { return m_initialized;}

protected :
bool m_initialized;
};

?

?

cDataPool :模板類,繼承自 cDataPoolInterface ,數據池,它包括了若干個 cPoolGroup

?

      template <
      
        class
      
       T>
      
class cDataPool : public cDataPoolInterface
{
public :

// Data Types & Constants...
typedef T DATA_TYPE;
typedef cPoolGroup<T> MemberGroup;
typedef std::list<MemberGroup*> MemberGroupList;
typedef cDataPoolInterface::MEMBER_CALLBACK MEMBER_CALLBACK;

// Creators...
cDataPool();
~cDataPool();

void initialize(uint16 growSize);
void destroy();

const T& operator [](cPoolHandle handle) const { return get (handle);}
T& operator [](cPoolHandle handle) { return get (handle);}

// Mutators...
cPoolHandle add( const T& member);
void clear();
cPoolHandle nextHandle();
void release(cPoolHandle* pHandle);
void forEach(MEMBER_CALLBACK function);

// Accessors...
bool isHandleValid(cPoolHandle index) const ;
const T& get (cPoolHandle index) const ;
T* getPtr(cPoolHandle index);
void * getGenericPtr(cPoolHandle index);

uint16 totalMembers() const { return m_totalMembers;}
uint16 totalOpen() const { return m_totalOpen;}
uint16 totalUsed() const { return m_totalMembers-m_totalOpen;}

private :

// Private Data...
MemberGroupList m_groupList; // cPoolGroup鏈表
uint16 m_totalMembers;
uint16 m_totalOpen;
uint16 m_groupCount;
uint32 m_indexMask;
int m_indexShift;

// Private Functions...
explicit cDataPool( const cDataPool& Src);
cDataPool& operator =( const cDataPool& Src);

cPoolGroup<T>* addGroup();
cPoolGroup<T>* findOpenGroup(unsigned* groupNumber);
cPoolGroup<T>* getGroup(unsigned index);
const cPoolGroup<T>* getGroup(unsigned index) const ;

int getGroupNumber(cPoolHandle handle) const ;
int getItemIndex(cPoolHandle handle) const ;
cPoolHandle buildHandle( int group, int index) const ;

};

?

?

2、? resource_pool.h

?

?

cResourcePoolInterface :資源池接口,抽象類

      
        class
      
       cResourcePoolInterface
      
{
public :

union FILE_EXTENSION
{
char ext[ 4 ];
uint32 fourcc;
};

typedef std::map<cString, cPoolHandle> NAME_LOOKUP_MAP;

cResourcePoolInterface();
virtual ~cResourcePoolInterface(){};

// these functions must be proviided in the derived class (cResourceType)
virtual void initialize(uint16 growSize)= 0 ;
virtual void destroy()= 0 ;
virtual bool isInitialized() const = 0 ;

virtual void destroyAll()= 0 ;
virtual void disableAll()= 0 ;
virtual void restoreAll()= 0 ;
virtual void clean()= 0 ;

cResourcePoolItem* createResource( const cString& resourceName);
cResourcePoolItem* loadResource( const cString& filename);
cResourcePoolItem* getResource(cPoolHandle handle);
cResourcePoolItem* findResource( const cString& Name);
void destroyResource(cResourcePoolItem* pResource);

bool saveResource(cResourcePoolItem* pResource);

cPoolHandle findResourceHandle( const cString& Name);
const cString* findResourceName(cPoolHandle handle) const ;
void setResourceName(cPoolHandle handle, const tchar* name);

void registerResourcePool(cResourceCode code);
void unregisterResourcePool();

cResourceCode registrationCode() const ;

protected :
cResourceCode m_registrationCode;
NAME_LOOKUP_MAP m_nameMap;

private :
// Private Functions...
virtual cPoolHandle internalCreateResource( const cString& resourceName)= 0 ;
virtual void internalDestroyResource(cPoolHandle handle)= 0 ;
virtual cResourcePoolItem* internalGetResource(cPoolHandle handle)= 0 ;
};

?

cResourcePool:模板類,資源池,繼承自cResourcePoolInterface,成員中有一個DataPool可以看做是DataPool的擴充。

?

      template <
      
        class
      
       T>
      
class cResourcePool : public cResourcePoolInterface
{
public :

typedef T DataType;
typedef cDataPool<T> DataPool;

// Creators...
cResourcePool(){};
~cResourcePool(){};

// Base Class Overrides...
void initialize(uint16 growSize);
void destroy();
bool isInitialized() const ;

void destroyAll();
void disableAll();
void restoreAll();
void clean(); // delete items no longer referenced

DataType* createResource( const cString& resourceName);
DataType* loadResource( const cString& filename);
DataType* getResource(cPoolHandle handle);
DataType* findResource( const cString& Name);

// static data pool callbacks
static void callbackDestroy(cDataPoolInterface* pPool, cPoolHandle handle, void * resource);
static void callbackRestore(cDataPoolInterface* pPool, cPoolHandle handle, void * resource);
static void callbackDisable(cDataPoolInterface* pPool, cPoolHandle handle, void * resource);
static void callbackClean(cDataPoolInterface* pPool, cPoolHandle handle, void * resource);

private :

// Data...
DataPool m_dataPool;

// Private Functions...
cPoolHandle internalCreateResource( const cString& resourceName);
void internalDestroyResource(cPoolHandle handle);
cResourcePoolItem* internalGetResource(cPoolHandle handle);

// Nonexistant Functions...

cResourcePool( const cResourcePool& Src);
cResourcePool& operator =( const cResourcePool& Src);
};

?

?

cResourcePoolItem :資源池對象繼承自 cReferenceCounter

?

關于 cReferenceCounter 上一段英文:

?

/*??? cResourcePoolItem

?

------------------------------------------------------------------------------------------

?

??????

?

?????? A cResourcePoolItem is a simple base class for a shared resource such as a texture,

?

?????? animation or vertex buffer.

?

?

?

?????? Note that cResourcePoolItem is derived from cReferenceCounter. The cResourcePoolManager uses

?

?????? the value of the cReferenceCounter to decide when objects are no longer in use and can

?

?????? be destroyed during the cResourcePoolManager::clean() function. This means the application

?

?????? must update the reference count of the cResourcePoolItem whenever links between objects

?

?????? are created or removed between objects. The cReferenceCounter base class provides the

?

?????? functionality to manipulate the reference count.

?

??????

?

------------------------------------------------------------------------------------------

?

*/

?

      
        class
      
       cResourcePoolItem : 
      
        public
      
       cReferenceCounter
      
{
public :

// Data Types & Constants...
enum
{
nCreated= 0 , // the resource has been created
nLoaded, // the resource is filled with data and ready for use
nDisabled, // the resource is currently disabled for use
nAltered, // the resource has been altered since loaded
nTotalResourceFlags
};

// Creators...
cResourcePoolItem();
virtual ~cResourcePoolItem();

// User Functions
// These function prototypes are to be used by resources that use volatile or system-specific resources.
// Examples would be video-memory resident textures or hardware vertex buffers.
virtual bool createResource()= 0 ; // innitialize the resource (called once)
virtual bool destroyResource()= 0 ; // destroy the resource
virtual bool disableResource()= 0 ; // purge the resource from volatile memory
virtual bool restoreResource()= 0 ; // restore the resource to volatile memory
virtual bool loadResource( const tchar* filename= 0 )= 0 ; // load the resource from a file (or NULL to use the resource name)
virtual bool saveResource( const tchar* filename= 0 )= 0 ; // save the resource to a file (or NULL to use the resource name)

// Accessors...
cResourceCode resourceCode() const ;
cResourcePoolInterface* resourcePool() const ;
cPoolHandle resourceHandle() const ;
u32Flags resourceFlags() const ;

bool isResourceCreated() const ;
bool isResourceDisabled() const ;
bool isResourceLoaded() const ;
const cString* findResourceName() const ;

void setResourceName( const tchar* name);
void setAlteredFlag( bool onOff);
bool alteredFlag() const ;

// mimic COM interfaces
virtual int32 Release();

protected :

// only derrived classes are permitted to modify internal members
void setResourceCode(cResourceCode code);
void setResourcePool(cResourcePoolInterface* interfaePtr);
void setResourceHandle(cPoolHandle handle);
void setResourceFlag( int flagBit, bool Setting);

void notifyCreated();
void notifyDisabled();
void notifyLoaded();
void notifyUnloaded();
void notifyRestored();
void notifyDestroyed();
void notifySaved();

private :

// Data...
cResourceCode m_resourceCode;
cResourcePoolInterface* m_resourcePool;
cPoolHandle m_resourceHandle;
u32Flags m_resourceFlags;

// Private Functions...

// Nonexistant Functions...
cResourcePoolItem( const cResourcePoolItem& Src);
cResourcePoolItem& operator =( const cResourcePoolItem& Src);

friend cResourcePoolInterface;
};

?

cReferenceCounter:。。(我沒太理解它的作用,望大蝦指點。。)

?

/*??? cReferenceCounter

?

-----------------------------------------------------------------

?

???

?

?????? A cReferenceCounter is a base class which allows the program

?

?????? to count open instances.

?

??????

?

-----------------------------------------------------------------

?

*/

?

      
        class
      
       cReferenceCounter
      
{
public :

// Construction and Destruction...
cReferenceCounter()
{
m_nReferenceCount = 0 ;
}

cReferenceCounter( const cReferenceCounter& Src)
{
// Reference counts are tied to a specific instance
// i.e. they are never copied
m_nReferenceCount = 0 ;
}

virtual ~cReferenceCounter()
{
// assert if this object is still in use
assert(m_nReferenceCount == 0 );
}

// Operators...
cReferenceCounter& operator =( const cReferenceCounter& Src)
{
// this function is provided in order to allow
// derived classes to provide copy operators.
// the reference count itself is never copied.
return * this ;
}

// Mutators...
virtual int32 AddRef()
{
assert(m_nReferenceCount != MAX_INT32);
++m_nReferenceCount;
return (m_nReferenceCount);
};

virtual int32 Release()
{
assert(m_nReferenceCount > 0 );
--m_nReferenceCount;
return (m_nReferenceCount);
}

// Accessors...
int32 referenceCount() const { return m_nReferenceCount;}

private :

// the internal reference count, stored in 32bits
int32 m_nReferenceCount;
};

?

cResourcePoolManager :單件類,資源管理器,負責申請資源池,找到資源等資源管理功能。

?

?

      
        class
      
       cResourcePoolManager : 
      
        public
      
       cSingleton<cResourcePoolManager>
      
{
public :

typedef std::map<cResourceCode, cResourcePoolInterface*> ResourcePoolTypeMap;
typedef std::list<cResourcePoolInterface*> ResourcePoolFamilyList;

// Creators...
cResourcePoolManager();
~cResourcePoolManager(){};

// registration of resource manager interfaces
void registerResourcePool(cResourceCode code, cResourcePoolInterface* pInterface);
cResourcePoolInterface* unregisterResourcePool(cResourceCode code);

// operations for all resource types
void destroyAll();
void disableAll();
void restoreAll();
void clean(); // delete items no longer referenced

// operations on specific resource types
void destroyResourceFamily( int family);
void disableResourceFamily( int family);
void restoreResourceFamily( int family);
void cleanResourceFamily( int family);

void destroyResourceType(cResourceCode code);
void disableResourceType(cResourceCode code);
void restoreResourceType(cResourceCode code);
void cleanResourceType(cResourceCode code);

cResourcePoolInterface* findResourcePool(cResourceCode code) const ;
cPoolHandle findResourceHandle(cResourceCode code, const cString& Name) const ;
cResourcePoolItem* findResource(cResourceCode code, const cString& Name) const ;
cResourcePoolItem* findResource(cResourceCode code, cPoolHandle handle) const ;

private :

ResourcePoolFamilyList m_resourceFamilyList[k_nTotalResourceFamilies];
ResourcePoolTypeMap m_resourceTypeMap;
};

?

引擎中所有的資源都派生自 cResourcePoolItem ,也可以說是對 D3D 資源類的封裝。例如: texture.h 里的 cTexture:

?

      
        class
      
       cTexture : 
      
        public
      
       cResourcePoolItem
      
{
public :

// Data Types & Constants...
enum eTextureFlags
{
k_cubeMap= 0 ,
k_dynamicTexture,
k_renderTarget,
k_paletized,
k_mipMaps,
};

enum eForcedFormatFlags
{
k_forceMipLevels= 0 ,
k_forceFormat,
k_forceSize,
};

// Creators...

cTexture();
~cTexture();

// base resource overrides
bool createResource(); // innitialize the resource (called once)
bool destroyResource(); // destroy the resource
bool disableResource(); // purge the resource from volatile memory
bool restoreResource(); // prepare the resource for use (create any volatile memory objects needed)
bool loadResource( const tchar* filename= 0 ); // load the resource from a file (or NULL to use the resource name)
bool saveResource( const tchar* filename= 0 ); // save the resource to a file (or NULL to use the resource name)

bool createTexture(uint32 width, uint32 height, uint32 mipLevels, uint32 usage, D3DFORMAT Format, D3DPOOL pool);
bool createCubeTexture(uint32 size, uint32 mipLevels, uint32 usage, D3DFORMAT Format, D3DPOOL pool);
void generateMidpointNoise( float falloff);
void generatePerlinNoise( float scale, int octaves, float falloff);
bool generateNormalMap(LPDIRECT3DTEXTURE9 heightMap, uint32 channel, uint32 flags, float amplitude);
bool generateNormalizationCubeMap();
bool maskWithImage(cImage* pImage);

void releaseTexture();

bool convertToNormalMap(
uint32 channel,
uint32 flags,
float amplitude);

// Accessors...
LPDIRECT3DTEXTURE9 getTexture() const ;
bool uploadImage( const cImage* pImage, bool copyAll = true );
bool uploadCubeFace( const cImage* pImage, D3DCUBEMAP_FACES face, bool copyAll = true );

uint32 width() const { return m_width;}
uint32 height() const { return m_height;}
uint32 mipLevels() const { return m_mipLevels;}
uint32 d3dUsage() const { return m_d3dUsage;}
D3DFORMAT d3dFormat() const { return m_d3dFormat;}
D3DPOOL d3dPool() const { return m_d3dPool;}

bool getSurfaceLevel( int level, LPDIRECT3DSURFACE9* ppSurface);
private :

// Data...
uint32 m_width;
uint32 m_height;
uint32 m_mipLevels;
uint32 m_d3dUsage;
D3DFORMAT m_d3dFormat;
D3DPOOL m_d3dPool;
DWORD m_d3dFilter;
DWORD m_d3dMipFilter;
D3DCOLOR m_d3dColorKey;
D3DXIMAGE_INFO m_d3dImageInfo;
PALETTEENTRY* m_pPalette;
LPDIRECT3DTEXTURE9 m_pTexture;
LPDIRECT3DCUBETEXTURE9 m_pCubeTexture;

bool loadFromResourceFile(cFile& InputFile);
bool configureImageSettings();
bool checkTextureRequirements();
bool checkCubeTextureRequirements();

bool loadTextureFromImageFile( const tchar* filename);
bool loadTextureFromMemory(uint8* memory, uint32 size);
bool loadCubeTextureFromImageFile( const tchar* filename);
bool loadCubeTextureFromMemory(uint8* memory, uint32 size);

// Nonexistant Functions...
cTexture( const cTexture& Src);
cTexture& operator =( const cTexture& Src);

};

typedef cResourcePool<cTexture> cTextureManager;

?

?

此外還有 vertex_buffer.h 中的 cVertexBuffer model_resource.h 中的 cModelResource

?

P56 有資源的使用示例

?

?

類關系圖:

?

gaia源碼分析

cResourcePoolManager 統(tǒng)一管理。

?

?

書的第四章gaia引擎總覽有提到資源管理相關的內容,但是并沒用對源碼進行過多的解釋,這部分還是要自己硬著頭皮看啊。。。

不過看了這些基本理解了資源管理的原理和整個模塊的結構,受益匪淺。。

3D游戲引擎編程

?

posted @? 2012-02-15 18:55 ?☆凱子 閱讀(401) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-12-11 11:58 ?☆凱子 閱讀(710) |? 評論 (1) ? 編輯 ?|

?

posted @? 2011-11-29 17:48 ?☆凱子 閱讀(31) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-22 10:11 ?☆凱子 閱讀(27) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-17 21:17 ?☆凱子 閱讀(22) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-12 21:24 ?☆凱子 閱讀(33) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 21:52 ?☆凱子 閱讀(32) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 21:50 ?☆凱子 閱讀(124) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 21:01 ?☆凱子 閱讀(16) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 20:58 ?☆凱子 閱讀(36) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 20:52 ?☆凱子 閱讀(6) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 20:44 ?☆凱子 閱讀(9) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 20:39 ?☆凱子 閱讀(19) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 20:38 ?☆凱子 閱讀(27) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 20:37 ?☆凱子 閱讀(29) |? 評論 (0) ? 編輯 ?|

?

posted @? 2011-11-07 20:32 ?☆凱子 閱讀(20) |? 評論 (0) ? 編輯 ?|


gaia源碼分析


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 中文字幕永久视频 | 亚洲欧美日韩国产专区一区 | 澳门四虎影院 | 天天干夜夜欢 | 三级大黄 | 奇米影视网 | 欧美激情精品久久久久久久 | 久章草在线 | 亚洲欧美国产一区二区三区 | 99福利在线| 久久精品亚洲乱码伦伦中文 | 久久艹伊人 | 国产精品久久久久久 | 青青青国产免费全部免费观看 | 四虎4hu永久免费 | www久久久 | 国产麻豆精品aⅴ免费观看 国产麻豆精品hdvideoss | 日日夜夜天天操 | 凹凸精品视频分类国产品免费 | 湿湿影院在线观看 | 99精品视频不卡在线观看免费 | 亚洲天天做日日做天天看2018 | 视频黄色在线 | 韩国一级特黄毛片大 | 成人看的午夜免费毛片 | 成人在线不卡视频 | 午夜影院一级片 | 一级呦女专区毛片 | 天天干人人 | 色综合久久一区二区三区 | 夜夜夜爽爽爽久久久 | 99热在这里只有精品 | 九九99热久久精品在线6手机 | 亚洲欧美另类国产综合 | 纯欧美一级毛片_免费 | 日韩欧美综合在线二区三区 | 人人澡 人人澡 人人看欧美 | 久久香蕉网站 | 97婷婷狠狠成人免费视频 | 久久精品女人天堂 | 免费观看成人碰视频公开 |