DX中有很多种方式可以表示颜色:
1 DWORD:
定义:typedef unsigned long DWORD;
即为一个无符号32位(32机器)长整数,有四个字节,我们从左到右叫他1,2,3,4字节,每一个字节的范围是0~255。第一个字节表示alpha值,即透明度。如果是255,表示不透明,0表示完全透明(看不到),其他分别是R,G,B值。
获取:
可通过下列方法获得每个字节的值:
- int A = (int)((DWORD & 0xFF000000) >> 24);
- int R = (int)((DWORD & 0x00FF0000) >> 16);
- int G = (int)((DWORD & 0x0000FF00) >> 8);
- int B = (int)(DWORD & 0x000000FF);
使用:
- struct CUSTOMVERTEX
- {
- D3DXVECTOR3 position;
- float rhw;
- DWORD color;
- };
- vertices[0].position = D3DXVECTOR3(150.0f, 50.0f, 1.0f);//四个点,注意三角形的构造顺序
- vertices[0].rhw = 1.0f;
- vertices[0].color = 0xffff0000;
2 D3DCOLOR
这个就不需要讲了,看了他的定义就知道了:
typedef DWORD D3DCOLOR;
3 D3DCOLOR_ARGB
定义:
- // maps unsigned 8 bits/channel to D3DCOLOR
- #define D3DCOLOR_ARGB(a,r,g,b) /
- ((D3DCOLOR)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))
定义这个宏的目的是使用方便,一看就知道A,R,G,B的值,这个宏也比较简单,就是位移,然后合并。其实最后还是D3DCOLOR
使用:
- struct CUSTOMVERTEX
- {
- D3DXVECTOR3 position;
- float rhw;
- D3DCOLOR color;
- };
- vertices[0].color = D3DCOLOR_ARGB(255,0,155,155);
还有D3DCOLOR_RGBA和D3DCOLOR_XRGB与上面差不多,看定义:
- #define D3DCOLOR_RGBA(r,g,b,a) D3DCOLOR_ARGB(a,r,g,b)
- #define D3DCOLOR_XRGB(r,g,b) D3DCOLOR_ARGB(0xff,r,g,b)
3 D3DCOLORVALUE
定义:
- typedef struct _D3DCOLORVALUE {
- float r;
- float g;
- float b;
- float a;
- } D3DCOLORVALUE;
与上面说的最大的区别就是他的值为0~1
使用:
- struct CUSTOMVERTEX
- {
- D3DXVECTOR3 position;
- float rhw;
- D3DCOLORVALUE color;
- };
- vertices[0].position = D3DXVECTOR3(150.0f, 50.0f, 1.0f);//四个点,注意三角形的构造顺序
- vertices[0].rhw = 1.0f;
- D3DCOLORVALUE d={1,0,0.5,0.2};
- vertices[0].color = d;
4 D3DXCOLOR
这个封装了下,看定义就知道用法了:
- typedef struct D3DXCOLOR
- {
- #ifdef __cplusplus
- public:
- D3DXCOLOR() {}
- D3DXCOLOR( DWORD argb );
- D3DXCOLOR( CONST FLOAT * );
- D3DXCOLOR( CONST D3DXFLOAT16 * );
- D3DXCOLOR( CONST D3DCOLORVALUE& );
- D3DXCOLOR( FLOAT r, FLOAT g, FLOAT b, FLOAT a );
- // casting
- operator DWORD () const;
- operator FLOAT* ();
- operator CONST FLOAT* () const;
- operator D3DCOLORVALUE* ();
- operator CONST D3DCOLORVALUE* () const;
- operator D3DCOLORVALUE& ();
- operator CONST D3DCOLORVALUE& () const;
- // assignment operators
- D3DXCOLOR& operator += ( CONST D3DXCOLOR& );
- D3DXCOLOR& operator -= ( CONST D3DXCOLOR& );
- D3DXCOLOR& operator *= ( FLOAT );
- D3DXCOLOR& operator /= ( FLOAT );
- // unary operators
- D3DXCOLOR operator + () const;
- D3DXCOLOR operator - () const;
- // binary operators
- D3DXCOLOR operator + ( CONST D3DXCOLOR& ) const;
- D3DXCOLOR operator - ( CONST D3DXCOLOR& ) const;
- D3DXCOLOR operator * ( FLOAT ) const;
- D3DXCOLOR operator / ( FLOAT ) const;
- friend D3DXCOLOR operator * ( FLOAT, CONST D3DXCOLOR& );
- BOOL operator == ( CONST D3DXCOLOR& ) const;
- BOOL operator != ( CONST D3DXCOLOR& ) const;
- #endif //__cplusplus
- FLOAT r, g, b, a;
- } D3DXCOLOR, *LPD3DXCOLOR;
可以对颜色加减了,使用起来有时候很方便,取值也是0~1,当然也可以取这个范围之外的值。
使用:
- struct CUSTOMVERTEX
- {
- D3DXVECTOR3 position;
- float rhw;
- D3DCOLOR color;
- };
- vertices[0].position = D3DXVECTOR3(150.0f, 50.0f, 1.0f);//四个点,注意三角形的构造顺序
- vertices[0].rhw = 1.0f;
- vertices[0].color = D3DXCOLOR(1,1,1,0);