"L. Spiro Engine"
|
00001 00025 #ifndef __LSG_VERTEXBUFFER_H__ 00026 #define __LSG_VERTEXBUFFER_H__ 00027 00028 #include "../LSGGraphicsLib.h" 00029 #ifdef LSG_DX9 00030 #include "../DirectX/LSGDirectX9VertexBuffer.h" 00031 #endif // #ifdef LSG_DX9 00032 #ifdef LSG_DX10 00033 #include "../DirectX/LSGDirectX10VertexBuffer.h" 00034 #endif // #ifdef LSG_DX10 00035 #ifdef LSG_DX11 00036 #include "../DirectX/LSGDirectX11VertexBuffer.h" 00037 #endif // #ifdef LSG_DX11 00038 #ifdef LSG_OGLES 00039 #include "../OpenGLES/LSGOpenGlEsVertexBuffer.h" 00040 #endif // #ifdef LSG_OGLES 00041 #ifdef LSG_OGL 00042 #include "../OpenGL/LSGOpenGlVertexBuffer.h" 00043 #endif // #ifdef LSG_OGL 00044 #include "Vector/LSMVector2.h" 00045 #include "Vector/LSTLVectorPoD.h" 00046 00047 namespace lsg { 00048 00064 class CVertexBuffer : public 00065 #ifdef LSG_OGL 00066 COpenGlVertexBuffer 00067 #endif // #ifdef LSG_OGL 00068 #ifdef LSG_OGLES 00069 COpenGlEsVertexBuffer 00070 #endif // #ifdef LSG_OGLES 00071 #ifdef LSG_DX9 00072 CDirectX9VertexBuffer 00073 #endif // #ifdef LSG_DX9 00074 #ifdef LSG_DX10 00075 CDirectX10VertexBuffer 00076 #endif // #ifdef LSG_DX10 00077 #ifdef LSG_DX11 00078 CDirectX11VertexBuffer 00079 #endif // #ifdef LSG_DX11 00080 { 00081 // Rendering of the vertex buffer is handled by CFnd. This allows the foundation to update its render state 00082 // in preparation for the render. 00083 friend class CFnd; 00084 public : 00085 // == Various constructors. 00086 LSE_CALLCTOR CVertexBuffer(); 00087 LSE_CALLCTOR ~CVertexBuffer(); 00088 00089 00090 // == Operators. 00097 CVertexBuffer & LSE_CALL operator = ( const CVertexBuffer &_vbOther ); 00098 00099 00100 // == Functions. 00111 LSBOOL LSE_CALL CreateVertexBuffer( const LSG_VB_CONTENTS &_vcContents, LSUINT32 _ui32UsageAndAccess = 0UL, 00112 LSUINT32 _ui32InitialElements = 0UL, CAllocator * _paAllocator = NULL ); 00113 00117 LSVOID LSE_CALL Reset(); 00118 00125 LSVOID LSE_CALL SetAllocator( CAllocator * _paAllocator ); 00126 00133 LSBOOL LSE_CALL Finalize(); 00134 00141 LSBOOL LSE_CALL Lock(); 00142 00146 LSVOID LSE_CALL UnLock(); 00147 00154 LSVOID LSE_CALL ChopTo( LSUINT32 _ui32Total ); 00155 00161 LSE_INLINE LSUINT32 LSE_CALL TotalPrimitives() const; 00162 00168 LSE_INLINE LSUINT32 LSE_CALL TotalElements() const; 00169 00175 LSE_INLINE LSUINT32 LSE_CALL Stride() const; 00176 00182 LSE_INLINE LSUINT32 LSE_CALL GetUsage() const; 00183 00189 LSE_INLINE LSUINT32 LSE_CALL GetId() const; 00190 00196 LSE_INLINE LSBOOL LSE_CALL Finalized() const; 00197 00209 LSBOOL LSE_CALL AddVertex( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00210 00221 LSE_INLINE LSVOID LSE_CALL SetVertex( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00222 00235 LSBOOL LSE_CALL AddVertex( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSREAL _fW, LSUINT32 _ui32Stream = 0UL ); 00236 00248 LSE_INLINE LSVOID LSE_CALL SetVertex( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSREAL _fW, LSUINT32 _ui32Stream = 0UL ); 00249 00261 LSBOOL LSE_CALL AddNormal( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00262 00273 LSE_INLINE LSVOID LSE_CALL SetNormal( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00274 00286 LSBOOL LSE_CALL AddColor( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00287 00298 LSE_INLINE LSVOID LSE_CALL SetColor( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00299 00309 LSBOOL LSE_CALL AddAlpha( LSUINT32 _ui32Index, LSREAL _fA, LSUINT32 _ui32Stream = 0UL ); 00310 00319 LSE_INLINE LSVOID LSE_CALL SetAlpha( LSUINT32 _ui32Index, LSREAL _fA, LSUINT32 _ui32Stream = 0UL ); 00320 00331 LSBOOL LSE_CALL AddTex( LSUINT32 _ui32Index, LSREAL _fU, LSREAL _fV, LSUINT32 _ui32Stream = 0UL ); 00332 00342 LSE_INLINE LSVOID LSE_CALL SetTex( LSUINT32 _ui32Index, LSREAL _fU, LSREAL _fV, LSUINT32 _ui32Stream = 0UL ); 00343 00355 LSBOOL LSE_CALL AddFog( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00356 00367 LSE_INLINE LSVOID LSE_CALL SetFog( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00368 00380 LSBOOL LSE_CALL AddBiNormal( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00381 00392 LSE_INLINE LSVOID LSE_CALL SetBiNormal( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00393 00405 LSBOOL LSE_CALL AddTangent( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00406 00417 LSE_INLINE LSVOID LSE_CALL SetTangent( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream = 0UL ); 00418 00431 LSBOOL LSE_CALL AddBlendWeight( LSUINT32 _ui32Index, LSREAL _f0, LSREAL _f1, LSREAL _f2, LSREAL _f3, LSUINT32 _ui32Stream = 0UL ); 00432 00444 LSE_INLINE LSVOID LSE_CALL SetBlendWeight( LSUINT32 _ui32Index, LSREAL _f0, LSREAL _f1, LSREAL _f2, LSREAL _f3, LSUINT32 _ui32Stream = 0UL ); 00445 00453 LSE_INLINE CVector3 LSE_CALL GetVertex3( LSUINT32 _ui32Index, LSUINT32 _ui32Slot = 0UL ); 00454 00462 LSE_INLINE CVector3 LSE_CALL GetNormal( LSUINT32 _ui32Index, LSUINT32 _ui32Slot = 0UL ); 00463 00471 LSE_INLINE CVector2 LSE_CALL GetTex2( LSUINT32 _ui32Index, LSUINT32 _ui32Slot = 0UL ); 00472 00478 LSE_INLINE LSUINT32 LSE_CALL GetElementSize() const; 00479 00487 LSE_INLINE const LSVOID * LSE_CALL GetElement( LSUINT32 _ui32Index ) const; 00488 00495 LSE_INLINE LSVOID LSE_CALL MoveElement( LSUINT32 _ui32Index, LSUINT32 _ui32DestIndex ); 00496 00507 LSBOOL LSE_CALL TransferElement( CVertexBuffer &_vbTarget, LSG_VERTEX_ATTRIBUTES _vaElementType, LSUINT32 _ui32Slot ); 00508 00516 static LSVOID LSE_CALL ActivateVertexBuffer( CVertexBuffer &_vbBuffer, LSUINT32 _ui32Stream = 0UL, LSUINT32 _ui32ElementOffset = 0UL ); 00517 00523 static LSVOID LSE_CALL DeActivateVertexBuffer( LSUINT32 _ui32Stream ); 00524 00525 // TEMPORARY. 00526 LSVOID * LSE_CALL GetBuffer() { return &m_vData[0]; } 00527 00528 // TEMPORARY. 00529 LSUINT32 LSE_CALL GetBufferLen() { return m_vData.Length(); } 00530 00531 00532 protected : 00533 // == Members. 00537 static CVertexBuffer * m_pvbVertexBuffers[LSG_VBM_MAX_STREAMS]; 00538 00542 static LSUINT32 m_ui32HighestVertexBuffer; 00543 00544 00545 // == Functions. 00553 LSBOOL LSE_CALL Activate( LSUINT32 _ui32Stream = 0UL, LSUINT32 _ui32ElementOffset = 0UL ); 00554 00561 LSUINT32 LSE_CALL GetAttributesSize( const LSG_VB_CONTENTS &_vcContents ); 00562 00566 LSVOID LSE_CALL CalculateAttributeOffsets(); 00567 00574 LSUINT32 LSE_CALL GetSingleAttributesSize( const LSG_VB_CONTENTS &_vcContents ); 00575 00582 LSBOOL LSE_CALL AddIndex( LSUINT32 _ui32Index ); 00583 00587 LSVOID LSE_CALL SortContents(); 00588 00597 static LSINT32 LSE_CCALL ContentsSort( LSVOID * _pvContext, LSUINT8 * _pui8Left, LSUINT8 * _pui8Right ); 00598 00602 static LSVOID LSE_CALL ClearVertexArray(); 00603 00604 private : 00605 #ifdef LSG_OGL 00606 typedef COpenGlVertexBuffer Parent; 00607 #endif // #ifdef LSG_OGL 00608 #ifdef LSG_OGLES 00609 typedef COpenGlEsVertexBuffer Parent; 00610 #endif // #ifdef LSG_OGLES 00611 #ifdef LSG_DX9 00612 typedef CDirectX9VertexBuffer Parent; 00613 friend class CDirectX9VertexBuffer; 00614 #endif // #ifdef LSG_DX9 00615 #ifdef LSG_DX10 00616 typedef CDirectX10VertexBuffer Parent; 00617 friend class CDirectX10VertexBuffer; 00618 #endif // #ifdef LSG_DX10 00619 #ifdef LSG_DX11 00620 typedef CDirectX11VertexBuffer Parent; 00621 friend class CDirectX11VertexBuffer; 00622 #endif // #ifdef LSG_DX11 00623 00624 }; 00625 00626 00627 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00628 // DEFINITIONS 00629 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00635 LSE_INLINE LSUINT32 LSE_CALL CVertexBuffer::TotalPrimitives() const { 00636 return m_ui32PrimitiveCount; 00637 } 00638 00644 LSE_INLINE LSUINT32 LSE_CALL CVertexBuffer::TotalElements() const { 00645 return m_ui32Elements; 00646 } 00647 00653 LSE_INLINE LSUINT32 LSE_CALL CVertexBuffer::Stride() const { 00654 return m_ui32Stride; 00655 } 00656 00662 LSE_INLINE LSUINT32 LSE_CALL CVertexBuffer::GetUsage() const { 00663 return m_ui16UsageAndAccess; 00664 } 00665 00671 LSE_INLINE LSUINT32 LSE_CALL CVertexBuffer::GetId() const { 00672 return m_ui32Id; 00673 } 00674 00680 LSE_INLINE LSBOOL LSE_CALL CVertexBuffer::Finalized() const { 00681 return m_bFinalized; 00682 } 00683 00694 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetVertex( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream ) { 00695 assert( m_ui32Lock ); 00696 00697 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_POSITION][_ui32Stream]+_ui32Index*m_ui32Stride]); 00698 (*pfLoc++) = static_cast<LSGREAL>(_fX); 00699 (*pfLoc++) = static_cast<LSGREAL>(_fY); 00700 (*pfLoc) = static_cast<LSGREAL>(_fZ); 00701 } 00702 00714 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetVertex( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSREAL _fW, LSUINT32 _ui32Stream ) { 00715 assert( m_ui32Lock ); 00716 00717 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_POSITIONW][_ui32Stream]+_ui32Index*m_ui32Stride]); 00718 (*pfLoc++) = static_cast<LSGREAL>(_fX); 00719 (*pfLoc++) = static_cast<LSGREAL>(_fY); 00720 (*pfLoc++) = static_cast<LSGREAL>(_fZ); 00721 (*pfLoc) = static_cast<LSGREAL>(_fW); 00722 } 00723 00734 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetNormal( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream ) { 00735 assert( m_ui32Lock ); 00736 00737 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_NORMAL][_ui32Stream]+_ui32Index*m_ui32Stride]); 00738 (*pfLoc++) = static_cast<LSGREAL>(_fX); 00739 (*pfLoc++) = static_cast<LSGREAL>(_fY); 00740 (*pfLoc) = static_cast<LSGREAL>(_fZ); 00741 } 00742 00753 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetColor( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream ) { 00754 assert( m_ui32Lock ); 00755 00756 LSVOID * pvLoc = reinterpret_cast<LSVOID *>(&m_vData[m_vAttributeOffsets[LSG_VA_COLOR][_ui32Stream]+_ui32Index*m_ui32Stride]); 00757 Parent::SetColorApi( _fX, _fY, _fZ, pvLoc ); 00758 } 00759 00768 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetAlpha( LSUINT32 _ui32Index, LSREAL _fA, LSUINT32 _ui32Stream ) { 00769 assert( m_ui32Lock ); 00770 00771 LSVOID * pvLoc = reinterpret_cast<LSVOID *>(&m_vData[m_vAttributeOffsets[LSG_VA_ALPHA][_ui32Stream]+_ui32Index*m_ui32Stride]); 00772 Parent::SetAlphaApi( _fA, pvLoc ); 00773 } 00774 00784 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetTex( LSUINT32 _ui32Index, LSREAL _fU, LSREAL _fV, LSUINT32 _ui32Stream ) { 00785 assert( m_ui32Lock ); 00786 00787 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_TEX][_ui32Stream]+_ui32Index*m_ui32Stride]); 00788 (*pfLoc++) = static_cast<LSGREAL>(_fU); 00789 (*pfLoc) = static_cast<LSGREAL>(_fV); 00790 } 00791 00802 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetFog( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream ) { 00803 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_FOGCOORD][_ui32Stream]+_ui32Index*m_ui32Stride]); 00804 (*pfLoc++) = static_cast<LSGREAL>(_fX); 00805 (*pfLoc++) = static_cast<LSGREAL>(_fY); 00806 (*pfLoc) = static_cast<LSGREAL>(_fZ); 00807 } 00808 00819 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetBiNormal( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream ) { 00820 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_BINORMAL][_ui32Stream]+_ui32Index*m_ui32Stride]); 00821 (*pfLoc++) = static_cast<LSGREAL>(_fX); 00822 (*pfLoc++) = static_cast<LSGREAL>(_fY); 00823 (*pfLoc) = static_cast<LSGREAL>(_fZ); 00824 } 00825 00836 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetTangent( LSUINT32 _ui32Index, LSREAL _fX, LSREAL _fY, LSREAL _fZ, LSUINT32 _ui32Stream ) { 00837 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_TANGENT][_ui32Stream]+_ui32Index*m_ui32Stride]); 00838 (*pfLoc++) = static_cast<LSGREAL>(_fX); 00839 (*pfLoc++) = static_cast<LSGREAL>(_fY); 00840 (*pfLoc) = static_cast<LSGREAL>(_fZ); 00841 } 00842 00854 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::SetBlendWeight( LSUINT32 _ui32Index, LSREAL _f0, LSREAL _f1, LSREAL _f2, LSREAL _f3, LSUINT32 _ui32Stream ) { 00855 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_BLENDWEIGHT][_ui32Stream]+_ui32Index*m_ui32Stride]); 00856 (*pfLoc++) = static_cast<LSGREAL>(_f0); 00857 (*pfLoc++) = static_cast<LSGREAL>(_f1); 00858 (*pfLoc++) = static_cast<LSGREAL>(_f2); 00859 (*pfLoc) = static_cast<LSGREAL>(_f3); 00860 } 00861 00869 LSE_INLINE CVector3 LSE_CALL CVertexBuffer::GetVertex3( LSUINT32 _ui32Index, LSUINT32 _ui32Slot ) { 00870 assert( m_ui32Lock ); 00871 00872 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_POSITION][_ui32Slot]+_ui32Index*m_ui32Stride]); 00873 return CVector3( static_cast<LSREAL>(pfLoc[0]), static_cast<LSREAL>(pfLoc[1]), static_cast<LSREAL>(pfLoc[2]) ); 00874 } 00875 00883 LSE_INLINE CVector3 LSE_CALL CVertexBuffer::GetNormal( LSUINT32 _ui32Index, LSUINT32 _ui32Slot ) { 00884 assert( m_ui32Lock ); 00885 00886 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_NORMAL][_ui32Slot]+_ui32Index*m_ui32Stride]); 00887 return CVector3( static_cast<LSREAL>(pfLoc[0]), static_cast<LSREAL>(pfLoc[1]), static_cast<LSREAL>(pfLoc[2]) ); 00888 } 00889 00897 LSE_INLINE CVector2 LSE_CALL CVertexBuffer::GetTex2( LSUINT32 _ui32Index, LSUINT32 _ui32Slot ) { 00898 assert( m_ui32Lock ); 00899 00900 LSGREAL * pfLoc = reinterpret_cast<LSGREAL *>(&m_vData[m_vAttributeOffsets[LSG_VA_TEX][_ui32Slot]+_ui32Index*m_ui32Stride]); 00901 return CVector2( static_cast<LSREAL>(pfLoc[0]), static_cast<LSREAL>(pfLoc[1]) ); 00902 } 00903 00909 LSE_INLINE LSUINT32 LSE_CALL CVertexBuffer::GetElementSize() const { 00910 return m_ui16ElementSize; 00911 } 00912 00920 LSE_INLINE const LSVOID * LSE_CALL CVertexBuffer::GetElement( LSUINT32 _ui32Index ) const { 00921 if ( _ui32Index >= m_vData.Length() / m_ui32Stride ) { return NULL; } 00922 return &m_vData[m_ui32Stride*_ui32Index]; 00923 } 00924 00931 LSE_INLINE LSVOID LSE_CALL CVertexBuffer::MoveElement( LSUINT32 _ui32Index, LSUINT32 _ui32DestIndex ) { 00932 if ( _ui32Index == _ui32DestIndex ) { return; } 00933 const LSVOID * pvSrc = GetElement( _ui32Index ); 00934 const LSVOID * pvDest = GetElement( _ui32DestIndex ); 00935 if ( pvSrc && pvDest ) { 00936 CStd::MemCpy( const_cast<LSVOID *>(pvDest), pvSrc, m_ui16ElementSize ); 00937 } 00938 } 00939 00940 } // namespace lsg 00941 00942 #endif // __LSG_VERTEXBUFFER_H__ 00943