QGearsPFile.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-11 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 "data/QGearsPFile.h"
  23. #include <OgreBone.h>
  24. #include <OgreLogManager.h>
  25. #include <OgreStringConverter.h>
  26. #include "common/QGearsStringUtil.h"
  27. #include "data/QGearsPFileSerializer.h"
  28. namespace QGears
  29. {
  30. //---------------------------------------------------------------------
  31. const String PFile::RESOURCE_TYPE( "QGearsPFile" );
  32. const Ogre::Quaternion PFile::STATIC_ROTATION( PFile::createStaticRotation() );
  33. //---------------------------------------------------------------------
  34. PFile::PFile( Ogre::ResourceManager *creator
  35. ,const String &name, Ogre::ResourceHandle handle
  36. ,const String &group, bool isManual
  37. ,Ogre::ManualResourceLoader *loader ) :
  38. Ogre::Resource( creator, name, handle, group, isManual, loader )
  39. {
  40. createParamDictionary( RESOURCE_TYPE );
  41. }
  42. //---------------------------------------------------------------------
  43. PFile::~PFile()
  44. {
  45. unload();
  46. }
  47. //---------------------------------------------------------------------
  48. void
  49. PFile::loadImpl()
  50. {
  51. PFileSerializer serializer;
  52. Ogre::DataStreamPtr stream( Ogre::ResourceGroupManager::getSingleton().openResource( mName, mGroup, true, this ) );
  53. serializer.importPFile( stream, this );
  54. }
  55. //---------------------------------------------------------------------
  56. void
  57. PFile::unloadImpl()
  58. {
  59. m_vertices.clear();
  60. m_normals.clear();
  61. m_unknown1.clear();
  62. m_texture_coordinates.clear();
  63. m_vertex_colors.clear();
  64. m_polygon_colors.clear();
  65. m_edges.clear();
  66. m_polygon_definitions.clear();
  67. m_groups.clear();
  68. m_bboxes.clear();
  69. }
  70. //---------------------------------------------------------------------
  71. size_t
  72. PFile::calculateSize() const
  73. {
  74. return m_vertices.size() * sizeof( m_vertices.front() )
  75. +m_normals.size() * sizeof( m_normals.front() )
  76. +m_unknown1.size() * sizeof( m_unknown1.front() )
  77. +m_texture_coordinates.size() * sizeof( m_texture_coordinates.front() )
  78. +m_vertex_colors.size() * sizeof( m_vertex_colors.front() )
  79. +m_polygon_colors.size() * sizeof( m_polygon_colors.front() )
  80. +m_edges.size() * sizeof( m_edges.front() )
  81. +m_polygon_definitions.size() * sizeof( m_polygon_definitions.front() )
  82. +m_groups.size() * sizeof( m_groups.front() )
  83. +m_bboxes.size() * sizeof( m_bboxes.front() )
  84. ;
  85. }
  86. //---------------------------------------------------------------------
  87. bool
  88. PFile::isValid( void )
  89. {
  90. return true
  91. && isPolygonDefinitionListValid();
  92. }
  93. //---------------------------------------------------------------------
  94. bool
  95. PFile::isPolygonDefinitionListValid( void )
  96. {
  97. Ogre::Log::Stream log( Ogre::LogManager::getSingleton().stream() );
  98. size_t vertex_count( m_vertices.size() )
  99. ,normal_count( m_normals.size() )
  100. ,edge_count ( m_edges.size() );
  101. for( size_t p( m_polygon_definitions.size() ); p--; )
  102. {
  103. PolygonDefinition& def( m_polygon_definitions[p] );
  104. for( int i(3); i--; )
  105. {
  106. if( def.vertex[i] >= vertex_count )
  107. {
  108. log << "Error: index to vertex is out of Bounds "
  109. << " m_polygon_definitions[" << p << "]"
  110. << ".vertex[" << i << "]: " << def.vertex[i];
  111. return false;
  112. }
  113. if( def.normal[i] >= normal_count )
  114. {
  115. log << "Error: index to normal is out of Bounds "
  116. << " m_polygon_definitions[" << p << "]"
  117. << ".normal[" << i << "]: " << def.normal[i];
  118. return false;
  119. }
  120. if( def.edge[i] >= edge_count )
  121. {
  122. log << "Error: index to edge is out of Bounds "
  123. << " m_polygon_definitions[" << p << "]"
  124. << ".edge[" << i << "]: " << def.edge[i];
  125. return false;
  126. }
  127. }
  128. }
  129. return true;
  130. }
  131. //---------------------------------------------------------------------
  132. void
  133. PFile::addGroups( Ogre::Mesh *mesh, const String &bone_name
  134. ,const RSDFilePtr &rsd ) const
  135. {
  136. const Ogre::SkeletonPtr skeleton( mesh->getSkeleton() );
  137. const String material_base_name( rsd->getMaterialBaseName() );
  138. String rsd_base;
  139. StringUtil::splitBase( rsd->getName(), rsd_base );
  140. ManualObject mo( mesh );
  141. for( size_t g(0); g < m_groups.size(); ++g )
  142. {
  143. const String sub_name( bone_name + "/" + rsd_base + "/" + Ogre::StringConverter::toString(g) );
  144. addGroup( m_groups[g], mo, sub_name, material_base_name, skeleton->getBone( bone_name ) );
  145. }
  146. }
  147. //---------------------------------------------------------------------
  148. Ogre::Vector3
  149. PFile::getPosition( const Ogre::Bone *bone ) const
  150. {
  151. Ogre::Vector3 pos( 0, 0, 0 );
  152. const Ogre::Node* node( bone );
  153. while( node )
  154. {
  155. pos.z += node->getPosition().y;
  156. node = node->getParent();
  157. }
  158. return pos;
  159. }
  160. //---------------------------------------------------------------------
  161. void
  162. PFile::addGroup( const Group &group, ManualObject &mo
  163. ,const String &sub_name, const String &material_base_name
  164. ,const Ogre::Bone *bone ) const
  165. {
  166. size_t material_index( 0 );
  167. if( group.has_texture )
  168. {
  169. material_index = group.texture_index + 1;
  170. }
  171. String material_name( material_base_name + "/" + Ogre::StringConverter::toString( material_index ) );
  172. const uint16 bone_handle( bone->getHandle() );
  173. const Ogre::Vector3 bone_position( getPosition( bone ) );
  174. size_t index( 0 );
  175. size_t vertex_count( group.num_polygons * 3 );
  176. size_t index_count( vertex_count );
  177. size_t polygon_end_index( group.polygon_start_index + group.num_polygons );
  178. mo.begin( sub_name, material_name, vertex_count, index_count );
  179. for( size_t p( group.polygon_start_index ); p < polygon_end_index; ++p )
  180. {
  181. const PolygonDefinition& polygon( m_polygon_definitions[p] );
  182. for( int i(3); i--; )
  183. {
  184. uint32 v( group.vertex_start_index
  185. +polygon.vertex[i] )
  186. ,n( 0 + polygon.normal[i] )
  187. ,t( group.texture_coordinate_start_index
  188. +polygon.vertex[i] );
  189. Ogre::Vector3 pos( m_vertices[ v ] );
  190. mo.position( ( STATIC_ROTATION * pos ) + bone_position );
  191. mo.colour( m_vertex_colors[ v ] );
  192. mo.normal( STATIC_ROTATION * m_normals[ n ] );
  193. if( group.has_texture )
  194. {
  195. mo.textureCoord( m_texture_coordinates[t] );
  196. }
  197. mo.bone( index, bone_handle ); mo.index( index++ );
  198. }
  199. }
  200. mo.end();
  201. }
  202. //---------------------------------------------------------------------
  203. Ogre::Quaternion
  204. PFile::createStaticRotation()
  205. {
  206. Ogre::Radian angle( Ogre::Degree( 180 ) );
  207. return Ogre::Quaternion( angle, Ogre::Vector3::UNIT_X );
  208. }
  209. //---------------------------------------------------------------------
  210. }