最近想做个directx渲染系统所以设计了如下方案:
1、封装接口中材质结构如下:
//!Identythecolorofmaterial
typedefID_MATERIAL_COLOR;
//!Identythestateofmaterial
typedefID_MATERIAL_STATE;
//!Identythetextureofmaterial
typedefID_MATERIAL_TEXTURE;
//!Identitythewholematerial
typedefID_MATERIAL;
//!DefaultmaterialID
constID_MATERIALCONST_DEFAULT_MATERIAL_ID=-1;
//!Maxtexturenumberforonematerial
constCONST_MATERIAL_MAX_TEXTURES=4;
//!
structSMaterialColor
{
SColordfuse;
SColorambient;
SColorspecular;
SColoremissive;
floatfPower;
};
//!
structSMaterialState
{
//texturestagestateting...
//alphaornot...
//...
};
//!
structSMaterialTexture
{
union
{
struct
{
ITexture*pTex0;
ITexture*pTex1;
ITexture*pTex2;
ITexture*pTex3;
};
ITexture*pTexArray[CONST_MATERIAL_MAX_TEXTURES];
};
};
//!
structSMaterial
{
ID_MATERIAL_COLOR colorId;
ID_MATERIAL_STATE stateId;
ID_MATERIAL_TEXTURE texId;
};
2、封装directx实现材质结构如下:[Page]
structSD3D9MaterialColor
{
D3DCOLORVALUED3DDfuse;
D3DCOLORVALUED3DAmbient;
D3DCOLORVALUED3DSpecular;
D3DCOLORVALUED3DEmissive;
floatfPower;
};
structSD3D9MaterialState
{
};
structSD3D9MaterialTexture
{
union
{
struct
{
IDirect3DBaseTexture9*pD3DTex0;
IDirect3DBaseTexture9
*pD3DTex1;
IDirect3DBaseTexture9*pD3DTex2;
IDirect3DBaseTexture9*pD3DTex3;
};
IDirect3DBaseTexture9*pD3DTexArray[CONST_MATERIAL_MAX_TEXTURES];
};
};
structSD3D9Material
{
SD3D9MaterialColor *pMColor;
SD3D9MaterialState *pMState;
SD3D9MaterialTexture*pMTexture;
};
3、接口IRender设计:
IRender下添加如下思路方法:
//!
virtualID_MATERIAL_COLORregisterMaterialColor(SMaterialColormatColor)=0;
//!
virtualID_MATERIAL_STATEregisterMaterialState(SMaterialStatematState)=0;
//!
virtualID_MATERIAL_TEXTUREregisterMaterialTexture(SMaterialTexturematTex)=0;
//!
virtualID_MATERIALregisterMaterial(SMaterialmaterial)=0;
4、接口IRender子类CD3D9Render对各种材质结构保存形式如下:[Page]
//--materialstructure--
.gif' />S<SD3D9MaterialColor*> m_pD3DMaterialColorArrayS;
.gif' />S<SD3D9MaterialState*> m_pD3DMaterialStateArrayS;
.gif' />S<SD3D9MaterialTexture*> m_pD3DMaterialTextureArrayS;
.gif' />S<SD3D9Material*> m_pD3DMaterialArrayS;
5、在CD3D9Render中添加如下结构用来临时保存要渲染数据:
.gif' />S<SPrimitiveDataProxy*>m_pPrimitiveDataProxyArrayS;
其中有关SPrimitiveDataProxy结构定义如下:
//!
structSPrimitiveDataProxy
{
ID_MATERIALm_iMaterialId;
EVertexType m_EVertexType;
EPrimitiveType m_EPrimType;
void* m_pVertexData;
unsigned*m_pIndexData;
unsigned m_uiPrimitiveCount;
unsigned m_uiVertexStride;
};
并且m_pPrimitiveDataProxyArrayS长度和m_pD3DMaterialArrayS长度保持相等
6、具体渲染流程如下:
(1)、定义自己要用材质SMaterial mat;
(2)、接口IRender思路方法ID_MATERIALregisterMaterial(SMaterialmaterial)将自定义材质mat注册到系统内部在系统内部对应SMaterial结构将被转变为
SD3D9Material结构并存到m_pD3DMaterialArrayS中;(由于该步为预处理所以效率问题不重要)
(3)、不断重复(1)、(2)步于是在预处理阶段就在系统内部建立起了个保存各类材质m_pD3DMaterialArrayS;
(4)、在系统内部对[Page]m_pD3DMaterialArrayS中所有元素进行排序建立个排序后索引其中对 SD3D9Material排序内部又涉及到SD3D9MaterialTexture、SD3D9MaterialState和SD3D9MaterialColor共3个成员排序这里原则是:排序时SD3D9MaterialTexture优先级最高SD3D9MaterialState次的SD3D9MaterialColor最后;
(5)、此时到了渲染网格阶段每渲染网格时都会IRender思路方法void renderPrimitives(SPrimitiveDataProxy& primDataProxy)将要渲染数据暂存到m_pPrimitiveDataProxyArrayS中其中m_iMaterialId相同SPrimitiveDataProxy将合并到起当如下条件:
如:渲染图元顶点类型或图元类型改变从而不能进行合并
全局些渲染状态已经改变比如:剪切类型、雾态、等等
directxpresent被
等等
满足时才真正把渲染数据送到显卡渲染;
(6)
以上就是主要设计思想不知从合理度、效率等方面考虑是否可行?
最新评论