home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / geometry / DrawVert.h next >
C/C++ Source or Header  |  2005-11-14  |  5KB  |  154 lines

  1.  
  2. #ifndef __DRAWVERT_H__
  3. #define __DRAWVERT_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     Draw Vertex.
  9.  
  10. ===============================================================================
  11. */
  12.  
  13. class idDrawVert {
  14. public:
  15.     idVec3            xyz;
  16.     byte            color[4];
  17.     idVec3            normal;
  18.     byte            color2[4];
  19.     idVec3            tangents[2];
  20.     idVec2            st;
  21.  
  22.     float            operator[]( const int index ) const;
  23.     float &            operator[]( const int index );
  24.  
  25.     void            Clear( void );
  26.  
  27.     const idVec3 &    GetNormal( void ) const;
  28.     void            SetNormal( float x, float y, float z );
  29.     void            SetNormal( const idVec3 &n );
  30.  
  31.     const idVec3 &    GetTangent( void ) const;
  32.     void            SetTangent( float x, float y, float z );
  33.     void            SetTangent( const idVec3 &t );
  34.  
  35.     const idVec3 &     GetBiTangent( void ) const;            // derived from normal, tangent, and tangent flag
  36.     void            SetBiTangent( float x, float y, float z );
  37.     void            SetBiTangent( const idVec3 &t );
  38.     void            SetBiTangentSign( float sign );        // either 1.0f or -1.0f
  39.  
  40.     void            Lerp( const idDrawVert &a, const idDrawVert &b, const float f );
  41.     void            LerpAll( const idDrawVert &a, const idDrawVert &b, const float f );
  42.  
  43.     void            Normalize( void );
  44.  
  45.     void            SetColor( dword color );
  46.     dword            GetColor( void ) const;
  47. };
  48.  
  49. // offsets for SIMD code
  50. #define DRAWVERT_SIZE                        64            // sizeof( idDrawVert )
  51. #define DRAWVERT_SIZE_SHIFT                    6            // log2( sizeof( idDrawVert ) )
  52. #define DRAWVERT_XYZ_OFFSET                    (0*4)        // offsetof( idDrawVert, xyz )
  53. #define DRAWVERT_COLOR_OFFSET                (3*4)        // offsetof( idDrawVert, color )
  54. #define DRAWVERT_NORMAL_OFFSET                (4*4)        // offsetof( idDrawVert, normal )
  55. #define DRAWVERT_COLOR2_OFFSET                (7*4)        // offsetof( idDrawVert, color2 )
  56. #define DRAWVERT_TANGENT0_OFFSET            (8*4)        // offsetof( idDrawVert, tangents[0] )
  57. #define DRAWVERT_TANGENT1_OFFSET            (11*4)        // offsetof( idDrawVert, tangents[1] )
  58. #define DRAWVERT_ST_OFFSET                    (14*4)        // offsetof( idDrawVert, st )
  59.  
  60. assert_sizeof( idDrawVert,                    DRAWVERT_SIZE );
  61. assert_sizeof( idDrawVert,                    (1<<DRAWVERT_SIZE_SHIFT) );
  62. assert_offsetof( idDrawVert, xyz,            DRAWVERT_XYZ_OFFSET );
  63. assert_offsetof( idDrawVert, color,            DRAWVERT_COLOR_OFFSET );
  64. assert_offsetof( idDrawVert, normal,        DRAWVERT_NORMAL_OFFSET );
  65. assert_offsetof( idDrawVert, color2,        DRAWVERT_COLOR2_OFFSET );
  66. //assert_offsetof( idDrawVert, tangents[0],    DRAWVERT_TANGENT0_OFFSET );
  67. //assert_offsetof( idDrawVert, tangents[1],    DRAWVERT_TANGENT1_OFFSET );
  68. assert_offsetof( idDrawVert, st,            DRAWVERT_ST_OFFSET );
  69.  
  70. ID_INLINE float idDrawVert::operator[]( const int index ) const {
  71.     assert( index >= 0 && index < 5 );
  72.     return ((float *)(&xyz))[index];
  73. }
  74. ID_INLINE float    &idDrawVert::operator[]( const int index ) {
  75.     assert( index >= 0 && index < 5 );
  76.     return ((float *)(&xyz))[index];
  77. }
  78.  
  79. ID_INLINE void idDrawVert::Clear( void ) {
  80.     xyz.Zero();
  81.     st.Zero();
  82.     normal.Zero();
  83.     tangents[0].Zero();
  84.     tangents[1].Zero();
  85.     color[0] = color[1] = color[2] = color[3] = 0;
  86. }
  87.  
  88. ID_INLINE const idVec3 &idDrawVert::GetNormal( void ) const {
  89.     return normal;
  90. }
  91.  
  92. ID_INLINE void idDrawVert::SetNormal( const idVec3 &n ) {
  93.     normal = n;
  94. }
  95.  
  96. ID_INLINE void idDrawVert::SetNormal( float x, float y, float z ) {
  97.     normal.Set( x, y, z );
  98. }
  99.  
  100. ID_INLINE const idVec3 &idDrawVert::GetTangent( void ) const {
  101.     return tangents[0];
  102. }
  103.  
  104. ID_INLINE void idDrawVert::SetTangent( float x, float y, float z ) {
  105.     tangents[0].Set( x, y, z );
  106. }
  107.  
  108. ID_INLINE void idDrawVert::SetTangent( const idVec3 &t ) {
  109.     tangents[0] = t;
  110. }
  111.  
  112. ID_INLINE const idVec3 &idDrawVert::GetBiTangent( void ) const {
  113.     return tangents[1];
  114. }
  115.  
  116. ID_INLINE void idDrawVert::SetBiTangent( float x, float y, float z ) {
  117.     tangents[1].Set( x, y, z );
  118. }
  119.  
  120. ID_INLINE void idDrawVert::SetBiTangent( const idVec3 &t ) {
  121.     tangents[1] = t;
  122. }
  123.  
  124. ID_INLINE void idDrawVert::SetBiTangentSign( float sign ) {
  125. //    tangents[0][3] = sign;
  126. }
  127.  
  128. ID_INLINE void idDrawVert::Lerp( const idDrawVert &a, const idDrawVert &b, const float f ) {
  129.     xyz = a.xyz + f * ( b.xyz - a.xyz );
  130.     st = a.st + f * ( b.st - a.st );
  131. }
  132.  
  133. ID_INLINE void idDrawVert::LerpAll( const idDrawVert &a, const idDrawVert &b, const float f ) {
  134.     xyz = a.xyz + f * ( b.xyz - a.xyz );
  135.     st = a.st + f * ( b.st - a.st );
  136.     normal = a.normal + f * ( b.normal - a.normal );
  137.     tangents[0] = a.tangents[0] + f * ( b.tangents[0] - a.tangents[0] );
  138.     tangents[1] = a.tangents[1] + f * ( b.tangents[1] - a.tangents[1] );
  139.     color[0] = (byte)( a.color[0] + f * ( b.color[0] - a.color[0] ) );
  140.     color[1] = (byte)( a.color[1] + f * ( b.color[1] - a.color[1] ) );
  141.     color[2] = (byte)( a.color[2] + f * ( b.color[2] - a.color[2] ) );
  142.     color[3] = (byte)( a.color[3] + f * ( b.color[3] - a.color[3] ) );
  143. }
  144.  
  145. ID_INLINE void idDrawVert::SetColor( dword color ) {
  146.     *reinterpret_cast<dword *>(this->color) = color;
  147. }
  148.  
  149. ID_INLINE dword idDrawVert::GetColor( void ) const {
  150.     return *reinterpret_cast<const dword *>(this->color);
  151. }
  152.  
  153. #endif /* !__DRAWVERT_H__ */
  154.