游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2635|回复: 2

累死我了,终于搞定了FVF模板

[复制链接]

6

主题

74

帖子

230

积分

中级会员

Rank: 3Rank: 3

积分
230
发表于 2004-5-10 00:29:00 | 显示全部楼层 |阅读模式
以前方泓写过一个 ,那个是用宏来实现,因为不符合C++ stand只能在特定编译器上用(用代码替换实现模板的,我试过vc6和gcc是可以的),vc7下编译不能通过。
最近研究c+模板,另外写了个可以在vc7上编译的。
这里还有个学WTL时写的fvf工具,用来自动生产struct和format的代码。可以用这个生成宏来做模板参数.

因为某些顶点格式本人并未用过,可能代码会有bug,如有问题请告知在下。

*-------------------------代码-----------------------------------*
//D3d9Fvf struct generate templates
//Author Kevinqing <qingzl@cdgwbn.com.cn>
//if you find any bugs,please let me know.

#include <d3d9.h>
//-----------posoition-------------
namespace        _D3DFvf{
template        <bool b>struct  pos_t;
template        <>        struct pos_t<true>{
        union {
                D3DVECTOR v;
                struct{
                        float x,y,z;
                };
        };
};
//-----------RHW-------------------
template        <bool b>struct rhw_t;
template        <>        struct rhw_t<false>{};
template        <>        struct rhw_t<true>{
        float        rhw;
};
//-----------W---------------------
template        <bool b>struct w_t;
template        <>        struct w_t<false>{};
template        <>        struct w_t<true>{
        DWORD        w;
};
//-----------normal----------------
template        <bool b>struct n_t;
template        <>        struct n_t<false>{};
template        <>        struct n_t<true>{
        D3DVECTOR        n;
};

//-----------psize-----------------
template        <bool b>struct psize_t;
template        <>        struct psize_t<false>{};
template        <>        struct psize_t<true>{
        float        psize;
};
//-----------diff-------------------
template        <bool b>struct diff_t;
template        <>        struct diff_t<false>{};
template        <>        struct diff_t<true>{
        D3DCOLOR        diff;
};

//-----------spec-------------------
template        <bool b>struct spec_t;
template        <>        struct spec_t<false>{};
template        <>        struct spec_t<true>{
        D3DCOLOR        spec;
};

#define TestFlag(a,b,m)        (bool((a&m)==b))
#define tp(a,b)        TestFlag(a,b,D3DFVF_POSITION_MASK)

#define TestMask(a,b)        (bool((a&b)!=0))
//----------b/i----------------------
#define IsXYZBn(a)        (tp(a,D3DFVF_XYZB1)||\
                                        tp(a,D3DFVF_XYZB2)||\
                                        tp(a,D3DFVF_XYZB3)||\
                                        tp(a,D3DFVF_XYZB4)||\
                                        tp(a,D3DFVF_XYZB5))

#define XYZBn(a)        (tp(a,D3DFVF_XYZB1)? 1\
                                        tp(a,D3DFVF_XYZB2)?        2:(\
                                        tp(a,D3DFVF_XYZB3)?        3:(\
                                        tp(a,D3DFVF_XYZB4)?        4:(\
                                        tp(a,D3DFVF_XYZB5)?        5:0)))))

template        <bool b,int i>struct b_t;
template        <int i>struct b_t<false,i>{};
template        <>struct b_t<true,1>{
        union{
                float        b;
                DWORD        i;
        };
};
template        <>struct b_t<true,2>{
        union{
                float        b[2];
                DWORD        i[2];
        };
};
template        <>struct b_t<true,3>{
        union{
                float        b[3];
                DWORD        i[3];
        };
};
template        <>struct b_t<true,4>{
        union{
                float        b[4];
                DWORD        i[4];
        };
};
template        <>struct b_t<true,5>{
        union{
                float        b[5];
                DWORD        i[5];
        };
};

//--------------TEX-----------
#define TEXn(a)        ((a&D3DFVF_TEXCOUNT_MASK)>>D3DFVF_TEXCOUNT_SHIFT)
#define _D3DFVF_TEXFMT(a,l)        ((a>>(l*2+16))&3)
#define TEX_CORDn(a,l)        ((_D3DFVF_TEXFMT(a,l)==D3DFVF_TEXTUREFORMAT1) ?1:(\
                                                (_D3DFVF_TEXFMT(a,l)==D3DFVF_TEXTUREFORMAT2) ?2:(\
                                                (_D3DFVF_TEXFMT(a,l)==D3DFVF_TEXTUREFORMAT3) ?3:4)))

#define DECL_TEXCORD_T(N)        \
template        <bool b,int n>struct texcord ## N ##_t;\
template        <int n>struct texcord##N##_t<false,n>{};\
template        <>struct texcord##N##_t<true,1>{\
        float        tu##N;\
};\
template        <>struct texcord##N##_t<true,2>{\
        float        tu##N;\
        float        tv##N;\
};\
template        <>struct texcord##N##_t<true,3>{\
        float        tu##N;\
        float        tv##N;\
        float        ta##N;\
};\
template        <>struct texcord##N##_t<true,4>{\
        float        tu##N;\
        float        tv##N;\
        float        ta##N;\
        float        tb##N;\
};

DECL_TEXCORD_T(0)
DECL_TEXCORD_T(1)
DECL_TEXCORD_T(2)
DECL_TEXCORD_T(3)
DECL_TEXCORD_T(4)
DECL_TEXCORD_T(5)
DECL_TEXCORD_T(6)
DECL_TEXCORD_T(7)
template        <const DWORD flag>
struct tex_t:        public        texcord0_t<(TEXn(flag)>0),TEX_CORDn(flag,0)>,
                                public        texcord1_t<(TEXn(flag)>1),TEX_CORDn(flag,1)>,
                                public        texcord2_t<(TEXn(flag)>2),TEX_CORDn(flag,2)>,
                                public        texcord3_t<(TEXn(flag)>3),TEX_CORDn(flag,3)>,
                                public        texcord4_t<(TEXn(flag)>4),TEX_CORDn(flag,4)>,
                                public        texcord5_t<(TEXn(flag)>5),TEX_CORDn(flag,5)>,
                                public        texcord6_t<(TEXn(flag)>6),TEX_CORDn(flag,6)>,
                                public        texcord7_t<(TEXn(flag)>7),TEX_CORDn(flag,7)>
{

};

};//end of _D3DFvf

template <const DWORD Flags>
struct D3DFvf:        public        _D3DFvf::pos_t<TestMask(Flags,D3DFVF_POSITION_MASK)>,
                                public        _D3DFvf::rhw_t<tp(Flags,D3DFVF_XYZRHW)>,
                                public        _D3DFvf::w_t<tp(Flags,D3DFVF_XYZW)>,
                                public        _D3DFvf::b_t<IsXYZBn(Flags),XYZBn(Flags)>,
                                public        _D3DFvf::n_t<TestMask(Flags,D3DFVF_NORMAL)>,
                                public        _D3DFvf::psize_t<TestMask(Flags,D3DFVF_PSIZE)>,
                                public        _D3DFvf::diff_t<TestMask(Flags,D3DFVF_DIFFUSE)>,
                                public        _D3DFvf::spec_t<TestMask(Flags,D3DFVF_SPECULAR)>,
                                public        _D3DFvf::tex_t<Flags>



{

};
#undef TestFlag
#undef TestMask
#undef tp
#undef XYZBn
#undef IsXYZBn
#undef TEXn
#undef _D3DFVF_TEXFMT
#undef TEX_CORDn
#undef DECL_TEXCORD_T

*---------------------------------------------------------*
[em3] [em3] [em3]

sf_20045100293.zip

43.08 KB, 下载次数:

6

主题

74

帖子

230

积分

中级会员

Rank: 3Rank: 3

积分
230
 楼主| 发表于 2004-5-10 00:33:00 | 显示全部楼层

Re:累死我了,终于搞定了FVF模板

附件是WTL写的工具.模板的用法
const DWORD myFvfFmt=D3DFVF_....|....;
typedef D3DFVf<myFvfFmt> MY_FVFFMT;
typedef D3DFVf<myFvfFmt> *PMY_FVFFMT;
....

6

主题

74

帖子

230

积分

中级会员

Rank: 3Rank: 3

积分
230
 楼主| 发表于 2004-6-20 11:13:00 | 显示全部楼层

Re: 累死我了,终于搞定了FVF模板

没事

sf_2004620111344.zip

2.69 KB, 下载次数:

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-7-2 06:30

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表