QGearsManualObject.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-17 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #include "common/QGearsManualObject.h"
  23. #include <cstring>
  24. #include <OgreException.h>
  25. #include <OgreHardwareBufferManager.h>
  26. #include <OgreRoot.h>
  27. namespace QGears
  28. {
  29. //-------------------------------------------------------------------------
  30. ManualObject::ManualObject( Ogre::Mesh *mesh ) :
  31. m_mesh( mesh )
  32. ,m_section( nullptr )
  33. ,m_bbox( mesh->getBounds() )
  34. ,m_radius( mesh->getBoundingSphereRadius() )
  35. ,m_position( nullptr )
  36. ,m_normal( nullptr )
  37. ,m_colour( nullptr )
  38. ,m_texture_coordinate( nullptr )
  39. ,m_index( nullptr )
  40. ,colour_type( Ogre::VertexElement::getBestColourVertexElementType() )
  41. {
  42. }
  43. //-------------------------------------------------------------------------
  44. ManualObject::~ManualObject()
  45. {
  46. }
  47. //-------------------------------------------------------------------------
  48. template<typename BufferType>
  49. BufferType*
  50. ManualObject::createBuffer( const BufferBinding binding
  51. ,Ogre::VertexElementType type
  52. ,Ogre::VertexElementSemantic semantic )
  53. {
  54. Ogre::VertexDeclaration* decl = m_section->vertexData->vertexDeclaration;
  55. Ogre::VertexBufferBinding* bind = m_section->vertexData->vertexBufferBinding;
  56. decl->addElement( binding, 0, type, semantic );
  57. size_t vertex_size( decl->getVertexSize( binding ) );
  58. VertexBuffer buffer = Ogre::HardwareBufferManager::getSingleton()
  59. .createVertexBuffer( vertex_size
  60. ,m_section->vertexData->vertexCount
  61. ,Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  62. bind->setBinding( binding, buffer );
  63. m_vertex_buffers[binding] = buffer;
  64. return static_cast<BufferType*>( buffer->lock( Ogre::HardwareBuffer::HBL_DISCARD ) );
  65. }
  66. //-------------------------------------------------------------------------
  67. void
  68. ManualObject::createPositionBuffer()
  69. {
  70. m_position = createBuffer<Ogre::Vector3>( BB_POSITION, Ogre::VET_FLOAT3, Ogre::VES_POSITION );
  71. }
  72. //-------------------------------------------------------------------------
  73. void
  74. ManualObject::createNormalBuffer()
  75. {
  76. m_normal = createBuffer<Ogre::Vector3>( BB_NORMAL, Ogre::VET_FLOAT3, Ogre::VES_NORMAL );
  77. }
  78. //-------------------------------------------------------------------------
  79. void
  80. ManualObject::createColourBuffer()
  81. {
  82. m_colour = createBuffer<uint32>( BB_COLOUR, colour_type, Ogre::VES_DIFFUSE );
  83. }
  84. //-------------------------------------------------------------------------
  85. void
  86. ManualObject::createTextureCoordinateBuffer()
  87. {
  88. m_texture_coordinate = createBuffer<Ogre::Vector2>( BB_TEXTURE, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES );
  89. }
  90. //-------------------------------------------------------------------------
  91. void
  92. ManualObject::createIndexBuffer()
  93. {
  94. m_index_buffer = Ogre::HardwareBufferManager::getSingleton()
  95. .createIndexBuffer( Ogre::HardwareIndexBuffer::IT_16BIT
  96. ,m_section->indexData->indexCount
  97. ,Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY );
  98. void *data( m_index_buffer->lock( Ogre::HardwareBuffer::HBL_DISCARD ) );
  99. m_index = static_cast< uint16* >( data );
  100. m_section->indexData->indexBuffer = m_index_buffer;
  101. }
  102. //-------------------------------------------------------------------------
  103. template<typename BufferSharedPtr>
  104. void
  105. ManualObject::resetBuffer( BufferSharedPtr &buffer ) const
  106. {
  107. if( buffer.isNull() ) return;
  108. if( buffer->isLocked() )
  109. {
  110. buffer->unlock();
  111. }
  112. buffer.setNull();
  113. }
  114. //-------------------------------------------------------------------------
  115. void
  116. ManualObject::resetBuffers()
  117. {
  118. for( int i(BINDING_COUNT); i--; )
  119. {
  120. resetBuffer( m_vertex_buffers[i] );
  121. }
  122. resetBuffer( m_index_buffer );
  123. m_position = nullptr;
  124. m_normal = nullptr;
  125. m_colour = nullptr;
  126. m_texture_coordinate = nullptr;
  127. m_index = nullptr;
  128. }
  129. //-------------------------------------------------------------------------
  130. void
  131. ManualObject::begin( const String &name, const String &material_name
  132. ,size_t vertex_count, size_t index_count )
  133. {
  134. if( m_mesh == nullptr )
  135. {
  136. OGRE_EXCEPT( Ogre::Exception::ERR_INVALIDPARAMS,
  137. "You cannot begin a section without a Mesh",
  138. "ManualObject::begin");
  139. }
  140. if( m_section != nullptr )
  141. {
  142. OGRE_EXCEPT( Ogre::Exception::ERR_INVALIDPARAMS,
  143. "You cannot begin a section without ending the old one first",
  144. "ManualObject::begin");
  145. }
  146. if( vertex_count == 0 )
  147. {
  148. OGRE_EXCEPT( Ogre::Exception::ERR_INVALIDPARAMS,
  149. "You cannot begin with a vertex count of 0",
  150. "ManualObject::begin");
  151. }
  152. m_section = m_mesh->createSubMesh( name );
  153. m_section->setMaterialName( material_name );
  154. m_section->useSharedVertices = false;
  155. m_section->operationType = Ogre::RenderOperation::OT_TRIANGLE_LIST;
  156. m_section->vertexData = new Ogre::VertexData();
  157. m_section->vertexData->vertexStart = 0;
  158. m_section->vertexData->vertexCount = vertex_count;
  159. m_section->indexData = new Ogre::IndexData();
  160. m_section->indexData->indexStart = 0;
  161. m_section->indexData->indexCount = index_count;
  162. }
  163. //-------------------------------------------------------------------------
  164. void
  165. ManualObject::position( const Ogre::Vector3 &position )
  166. {
  167. if( m_position == nullptr )
  168. {
  169. createPositionBuffer();
  170. }
  171. *(m_position++) = position;
  172. m_bbox.merge( position );
  173. m_radius = std::max( m_radius, position.length() );
  174. }
  175. //-------------------------------------------------------------------------
  176. void
  177. ManualObject::normal( const Ogre::Vector3 &normal )
  178. {
  179. if( m_normal == nullptr )
  180. {
  181. createNormalBuffer();
  182. }
  183. *(m_normal++) = normal;
  184. }
  185. //-------------------------------------------------------------------------
  186. void
  187. ManualObject::colour( const Ogre::ColourValue &colour )
  188. {
  189. if( m_colour == nullptr )
  190. {
  191. createColourBuffer();
  192. }
  193. *(m_colour++) = Ogre::VertexElement::convertColourValue( colour, colour_type );
  194. }
  195. //-------------------------------------------------------------------------
  196. void
  197. ManualObject::textureCoord( const Ogre::Vector2 &texture_coordinate )
  198. {
  199. if( m_texture_coordinate == nullptr )
  200. {
  201. createTextureCoordinateBuffer();
  202. }
  203. *(m_texture_coordinate++) = texture_coordinate;
  204. }
  205. //-------------------------------------------------------------------------
  206. void
  207. ManualObject::index( const uint32 idx )
  208. {
  209. if( m_index == nullptr )
  210. {
  211. createIndexBuffer();
  212. }
  213. *(m_index++) = idx;
  214. }
  215. //-------------------------------------------------------------------------
  216. void
  217. ManualObject::bone( const uint32 idx, const uint16 bone_handle
  218. ,const Ogre::Real weight )
  219. {
  220. Ogre::VertexBoneAssignment vba;
  221. vba.vertexIndex = idx;
  222. vba.boneIndex = bone_handle;
  223. vba.weight = weight;
  224. m_section->addBoneAssignment( vba );
  225. }
  226. //-------------------------------------------------------------------------
  227. void
  228. ManualObject::end()
  229. {
  230. if( m_section == nullptr )
  231. {
  232. OGRE_EXCEPT( Ogre::Exception::ERR_INVALIDPARAMS,
  233. "You cannot end a section without beginning one first",
  234. "ManualObject::end");
  235. }
  236. m_mesh->_setBounds( m_bbox );
  237. m_mesh->_setBoundingSphereRadius( m_radius );
  238. m_section = nullptr;
  239. resetBuffers();
  240. }
  241. //-------------------------------------------------------------------------
  242. }