Mesh Render.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /******************************************************************************
  2. 'MeshRender' is the hardware version of the 'MeshBase'.
  3. It contains vertexes and triangles.
  4. It is stored on the GPU memory.
  5. /******************************************************************************/
  6. #if EE_PRIVATE
  7. enum MSHR_FLAG
  8. {
  9. MSHR_COMPRESS =1<<0, // Nrm,Tan,Bin are compressed into VecB4
  10. MSHR_SIGNED =1<<1, // compressed VecB4 is stored as Signed Byte
  11. MSHR_BONE_SPLIT=1<<2, // vertex buffer is split into bone splits
  12. };
  13. #endif
  14. /******************************************************************************/
  15. struct MeshRender // Mesh Renderable (Hardware: contains Vertexes + Triangles)
  16. {
  17. // manage
  18. MeshRender& del();
  19. #if EE_PRIVATE
  20. void zero ();
  21. Bool create (Int vtxs, Int tris, UInt flag , Bool compress=true);
  22. Bool createRaw(C MeshBase &src , UInt flag_and=~0, Bool optimize=true , Bool compress=true);
  23. Bool create (C MeshBase &src , UInt flag_and=~0, Bool optimize=true , Bool compress=true);
  24. Bool create (C MeshRender &src );
  25. Bool create (C MeshRender *src[], Int elms, UInt flag_and=~0, Bool optimize=false, Bool compress=true); // create from 'src' array, 'flag_and'=MESH_BASE_FLAG
  26. #endif
  27. // get
  28. Bool is ()C {return vtxs() || tris();} // if has any data
  29. Int vtxs()C {return _vb.vtxs() ;} // get number of vertexes
  30. Int tris()C {return _tris ;} // get number of triangles
  31. UInt memUsage( )C {return _vb.memUsage()+_ib.memUsage();} // get memory usage of the mesh (in bytes)
  32. Int vtxSize ( )C {return _vb.vtxSize () ;} // get size of a single vertex
  33. Int vtxOfs (UInt elm )C; // get offset of a specified vertex component in the vertex data, -1 if not found, 'elm'=one of MESH_BASE_FLAG enums
  34. Bool indBit16( )C {return _ib.bit16() ;} // if indices are 16-bit (false for 32-bit)
  35. UInt flag ( )C {return _flag ;} // get MESH_BASE_FLAG that this mesh has
  36. Bool getBox (Box &box )C; // get box encapsulating the mesh, this method iterates through all vertexes, false on fail (if no vertexes are present)
  37. Flt area (Vec *center=null)C; // get surface area of all mesh faces, 'center'=if specified then it will be calculated as the average surface center
  38. // transform
  39. void scaleMove(C Vec &scale, C Vec &move=VecZero);
  40. // texture transform
  41. void texMove (C Vec2 &move , Byte tex_index=0);
  42. void texScale (C Vec2 &scale, Byte tex_index=0);
  43. void texRotate( Flt angle, Byte tex_index=0);
  44. // operations
  45. C Byte* vtxLockedData( )C {return _vb.lockedData( );} // get vertex data if it's already locked, null on fail
  46. C Byte* vtxLockedElm (UInt elm )C; // get vertex data if it's already locked offsetted by specified vertex component in the vertex data according to 'vtxOfs' method, 'elm'=one of MESH_BASE_FLAG enums, null on fail (if the vertex buffer is not locked or if the component was not found)
  47. Byte* vtxLock (LOCK_MODE lock=LOCK_READ_WRITE) {return _vb.lock (lock);} // lock vertex data and return it, this method may be used to directly modify values of hardware mesh vertexes after getting their offset in the data by using 'vtxOfs' method (currently you should use it only for 'VTX_POS' as 'Vec', 'VTX_TEX' as 'Vec2' and 'VTX_COLOR' as 'Color' components, as others may be stored in compressed format), null on fail
  48. C Byte* vtxLockRead ( )C {return _vb.lockRead ( );} // lock vertex data and return it, this method may be used to directly access values of hardware mesh vertexes after getting their offset in the data by using 'vtxOfs' method (currently you should use it only for 'VTX_POS' as 'Vec', 'VTX_TEX' as 'Vec2' and 'VTX_COLOR' as 'Color' components, as others may be stored in compressed format), null on fail
  49. void vtxUnlock ( )C { _vb.unlock ( );} // unlock vertex data
  50. CPtr indLockedData( )C {return _ib. lockedData( );} // get index data if it's already locked, null on fail
  51. Ptr indLock (LOCK_MODE lock=LOCK_READ_WRITE) {return _ib. lock (lock);} // lock index data and return it, this method may be used to directly access values of hardware mesh indexes, null on fail
  52. CPtr indLockRead ( )C {return _ib. lockRead ( );} // lock index data and return it, this method may be used to directly modify values of hardware mesh indexes, null on fail
  53. void indUnlock ( )C { _ib.unlock ( );} // unlock index data
  54. #if EE_PRIVATE
  55. Int triIndSize ()C {return indBit16() ? SIZE(U16)*3 : SIZE(U32)*3;} // get triangle indexes size
  56. Bool storageCompress ()C {return FlagTest(_storage, MSHR_COMPRESS );}
  57. Bool storageSigned ()C {return FlagTest(_storage, MSHR_SIGNED );}
  58. Bool storageBoneSplit()C {return FlagTest(_storage, MSHR_BONE_SPLIT);}
  59. void setUsedBones(Bool (&bones)[256])C;
  60. void includeUsedBones(Bool (&bones)[256])C;
  61. // draw
  62. #if DX9
  63. void drawFull ( )C {D3D->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vtxs(), 0, tris());}
  64. void drawRange (Int tris )C {D3D->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vtxs(), 0, tris );}
  65. void drawRange (Int tris, Int start_index)C {D3D->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vtxs(), start_index, tris );}
  66. void drawInstanced(Int instances )C {}
  67. #elif DX11
  68. void drawFull ( )C {D3DC->DrawIndexed (_ib._ind_num, 0, 0);}
  69. void drawRange (Int tris )C {D3DC->DrawIndexed (tris*3 , 0, 0);}
  70. void drawRange (Int tris, Int start_index)C {D3DC->DrawIndexed (tris*3 , start_index, 0);}
  71. void drawInstanced(Int instances )C {D3DC->DrawIndexedInstanced(_ib._ind_num, instances, 0, 0, 0);}
  72. #elif GL
  73. void drawFull ( )C {glDrawElements (GL_TRIANGLES, _ib._ind_num, _ib.bit16() ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, null);}
  74. void drawRange (Int tris )C {glDrawElements (GL_TRIANGLES, tris*3, _ib.bit16() ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, null);}
  75. void drawRange (Int tris, Int start_index)C {glDrawElements (GL_TRIANGLES, tris*3, _ib.bit16() ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, Ptr(start_index*(_ib.bit16() ? 2 : 4)));}
  76. void drawInstanced(Int instances )C {glDrawElementsInstanced(GL_TRIANGLES, _ib._ind_num, _ib.bit16() ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, null, instances);}
  77. #endif
  78. #if MAY_NEED_BONE_SPLITS
  79. void draw ()C; // this method supports BoneSplit's
  80. void drawFur()C; // this method supports BoneSplit's
  81. #else
  82. INLINE void draw ()C {drawFull();} // BoneSplit's aren't used
  83. INLINE void drawFur()C {drawFull();} // BoneSplit's aren't used
  84. #endif
  85. void drawBoneHighlight(Int bone, Shader *shader)C;
  86. // operations
  87. Bool setVF();
  88. C MeshRender& set()C;
  89. MeshRender& optimize (Bool faces=true, Bool vertexes=true); // this method will re-order elements for best rendering performance, 'faces'=if re-order faces, 'vertexes'=if re-order vertexes
  90. MeshRender& freeOpenGLESData(); // this method is used only under OpenGL ES (on other platforms it is ignored), the method frees the software copy of the GPU data which increases available memory, however after calling this method the data can no longer be accessed on the CPU (can no longer be locked or saved to file)
  91. void adjustToPlatform();
  92. // io
  93. #if EE_PRIVATE
  94. Bool saveData(File &f)C; // save binary, false on fail
  95. Bool loadData(File &f) ; // load binary, false on fail
  96. #endif
  97. void operator=(C Str &name) ; // load binary, Exit on fail
  98. Bool save (C Str &name)C; // save binary, false on fail
  99. Bool load (C Str &name) ; // load binary, false on fail
  100. Bool save ( File &f )C; // save binary, false on fail
  101. Bool load ( File &f ) ; // load binary, false on fail
  102. explicit MeshRender(C MeshBase &mshb, UInt flag_and=~0, Bool optimize=true) : MeshRender() {create(mshb, flag_and, optimize);}
  103. struct BoneSplit
  104. {
  105. Byte split_to_real[256];
  106. Int vtxs, tris, bones;
  107. Int realToSplit (Int bone)C; // -1 on fail
  108. Int realToSplit0(Int bone)C; // 0 on fail
  109. };
  110. #endif
  111. ~MeshRender() {del();}
  112. MeshRender();
  113. MeshRender(C MeshRender &src);
  114. MeshRender& operator =(C MeshRender &src); // create from 'src'
  115. MeshRender& operator+=(C MeshRender &src); // add 'src'
  116. #if !EE_PRIVATE
  117. private:
  118. #endif
  119. VtxBuf _vb;
  120. IndBuf _ib;
  121. Bool _vao_reset;
  122. Byte _storage;
  123. Int _tris, _bone_splits;
  124. UInt _flag;
  125. #if EE_PRIVATE
  126. BoneSplit *_bone_split;
  127. GPU_API(IDirect3DVertexDeclaration9 *_vf, ID3D11InputLayout *_vf, union{UInt _vao; VtxFormatGL *_vf;});
  128. #else
  129. Ptr _bone_split, _vf;
  130. #endif
  131. };
  132. /******************************************************************************/