MeshFile.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #include "MeshFile.h"
  2. #include "common/Logger.h"
  3. #include "common/OgreBase.h"
  4. float tex_width = 512.0f;
  5. float tex_height = 256.0f;
  6. //---------------------------------------------------------------------
  7. MeshFile::MeshFile(const Ogre::String& file):
  8. File(file)
  9. {
  10. }
  11. //---------------------------------------------------------------------
  12. MeshFile::MeshFile(File* pFile):
  13. File(pFile)
  14. {
  15. }
  16. //---------------------------------------------------------------------
  17. MeshFile::MeshFile(File* pFile, const u32 offset, const u32 length):
  18. File(pFile, offset, length)
  19. {
  20. }
  21. //---------------------------------------------------------------------
  22. MeshFile::MeshFile(u8* pBuffer, const u32 offset, const u32 length):
  23. File(pBuffer, offset, length)
  24. {
  25. }
  26. //---------------------------------------------------------------------
  27. MeshFile::~MeshFile(void)
  28. {
  29. }
  30. //---------------------------------------------------------------------
  31. Ogre::ColourValue
  32. MeshFile::readColor( const u32 offset )
  33. {
  34. u8 r( GetU8( offset + 0 ) )
  35. ,g( GetU8( offset + 1 ) )
  36. ,b( GetU8( offset + 2 ) )
  37. ,a( GetU8( offset + 3 ) );
  38. return Ogre::ColourValue( r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f );
  39. }
  40. //---------------------------------------------------------------------
  41. void
  42. MeshFile::readQuad( const u32 offset_to_poly, std::vector<Ogre::Vector3>& vertexes, float* &pPos, u16* &index, u32 &cur_vertex, Ogre::RGBA* &cur_colour, bool multi_color )
  43. {
  44. u8 i1( GetU8( offset_to_poly + 0x00 ) )
  45. ,i2( GetU8( offset_to_poly + 0x01 ) )
  46. ,i3( GetU8( offset_to_poly + 0x02 ) )
  47. ,i4( GetU8( offset_to_poly + 0x03 ) );
  48. // set vertexes
  49. Ogre::Vector3 a( vertexes[ i1 ] );
  50. Ogre::Vector3 b( vertexes[ i2 ] );
  51. Ogre::Vector3 c( vertexes[ i3 ] );
  52. Ogre::Vector3 d( vertexes[ i4 ] );
  53. *pPos++ = a.x; *pPos++ = a.y; *pPos++ = a.z;
  54. *pPos++ = b.x; *pPos++ = b.y; *pPos++ = b.z;
  55. *pPos++ = c.x; *pPos++ = c.y; *pPos++ = c.z;
  56. *pPos++ = d.x; *pPos++ = d.y; *pPos++ = d.z;
  57. Ogre::ColourValue col_a = readColor( offset_to_poly + 0x04);
  58. Ogre::ColourValue col_b = col_a;
  59. Ogre::ColourValue col_c = col_a;
  60. Ogre::ColourValue col_d = col_a;
  61. if( multi_color ){
  62. col_b = readColor( offset_to_poly + 0x08 );
  63. col_c = readColor( offset_to_poly + 0x0c );
  64. col_d = readColor( offset_to_poly + 0x10 );
  65. };
  66. Ogre::RenderSystem* rs = Ogre::Root::getSingleton().getRenderSystem();
  67. rs->convertColourValue( col_a, cur_colour++ );
  68. rs->convertColourValue( col_b, cur_colour++ );
  69. rs->convertColourValue( col_c, cur_colour++ );
  70. rs->convertColourValue( col_d, cur_colour++ );
  71. *(index++) = cur_vertex + 0;
  72. *(index++) = cur_vertex + 2;
  73. *(index++) = cur_vertex + 3;
  74. *(index++) = cur_vertex + 3;
  75. *(index++) = cur_vertex + 1;
  76. *(index++) = cur_vertex + 0;
  77. cur_vertex += 4;
  78. }
  79. //---------------------------------------------------------------------
  80. void
  81. MeshFile::readTriangle( const u32 offset_to_poly, std::vector<Ogre::Vector3>& vertexes, float* &pPos, u16* &index, u32 &cur_vertex, Ogre::RGBA* &cur_colour, bool multi_color )
  82. {
  83. u8 i1( GetU8( offset_to_poly + 0x00 ) )
  84. ,i2( GetU8( offset_to_poly + 0x01 ) )
  85. ,i3( GetU8( offset_to_poly + 0x02 ) );
  86. // set vertexes
  87. Ogre::Vector3 a = vertexes[ i1 ];
  88. Ogre::Vector3 b = vertexes[ i2 ];
  89. Ogre::Vector3 c = vertexes[ i3 ];
  90. *pPos++ = a.x; *pPos++ = a.y; *pPos++ = a.z;
  91. *pPos++ = b.x; *pPos++ = b.y; *pPos++ = b.z;
  92. *pPos++ = c.x; *pPos++ = c.y; *pPos++ = c.z;
  93. Ogre::ColourValue col_a = readColor( offset_to_poly + 0x04 );
  94. Ogre::ColourValue col_b = col_a;
  95. Ogre::ColourValue col_c = col_a;
  96. if( multi_color ){
  97. col_b = readColor( offset_to_poly + 0x08);
  98. col_c = readColor( offset_to_poly + 0x0c);
  99. };
  100. Ogre::RenderSystem* rs = Ogre::Root::getSingleton().getRenderSystem();
  101. rs->convertColourValue( col_a, cur_colour++ );
  102. rs->convertColourValue( col_b, cur_colour++ );
  103. rs->convertColourValue( col_c, cur_colour++ );
  104. *(index++) = cur_vertex + 2;
  105. *(index++) = cur_vertex + 1;
  106. *(index++) = cur_vertex + 0;
  107. cur_vertex += 3;
  108. }
  109. //---------------------------------------------------------------------
  110. void
  111. MeshFile::readPart( const Ogre::String& name, const u32 i, const u32 offset_to_part, Ogre::MeshPtr mesh, VectorTexForGenBsx& textures )
  112. {
  113. Ogre::String material_name( "ffvii/model/field/" + name );
  114. // add vertexes to array
  115. u8 number_of_vertex = GetU8(offset_to_part + 0x02);
  116. u16 offset_to_vertexes = GetU16LE(offset_to_part + 0x18);
  117. //LOGGER->Log(LOGGER_INFO, "number_of_vertex: %02x", number_of_vertex);
  118. std::vector<Ogre::Vector3> vertexes;
  119. for (u32 j = 0; j < number_of_vertex; ++j)
  120. {
  121. u16 offset_to_vertex = offset_to_vertexes + j * 0x08;
  122. Ogre::Vector3 point;
  123. point.x = -( s16 )GetU16LE( offset_to_vertex + 4 + 0x00 );
  124. point.z = -( s16 )GetU16LE( offset_to_vertex + 4 + 0x02 );
  125. point.y = -( s16 )GetU16LE( offset_to_vertex + 4 + 0x04 );
  126. point /= 1024.0f;
  127. vertexes.push_back( point );
  128. }
  129. u8 number_of_quad_t = GetU8(offset_to_part + 0x04);
  130. u8 number_of_triangle_t = GetU8(offset_to_part + 0x05);
  131. u8 number_of_quad_t2 = GetU8(offset_to_part + 0x06);
  132. u8 number_of_triangle_t2 = GetU8(offset_to_part + 0x07);
  133. u8 number_of_triangle_m = GetU8(offset_to_part + 0x08);
  134. u8 number_of_quad_m = GetU8(offset_to_part + 0x09);
  135. u8 number_of_triangle = GetU8(offset_to_part + 0x0A);
  136. u8 number_of_quads = GetU8(offset_to_part + 0x0B);
  137. size_t quad_count( number_of_quad_t
  138. + number_of_quad_t2
  139. + number_of_quad_m
  140. + number_of_quads );
  141. size_t triangle_count( number_of_triangle_t
  142. + number_of_triangle_t2
  143. + number_of_triangle_m
  144. + number_of_triangle );
  145. Ogre::SubMesh* sub_mesh = mesh->createSubMesh(Ogre::StringConverter::toString(i));
  146. sub_mesh->setMaterialName( material_name );
  147. sub_mesh->useSharedVertices = false;
  148. sub_mesh->operationType = Ogre::RenderOperation::OT_TRIANGLE_LIST;
  149. Ogre::HardwareBufferManager& buffer_manager( Ogre::HardwareBufferManager::getSingleton() );
  150. // Allocate and prepare vertex data
  151. sub_mesh->vertexData = new Ogre::VertexData();
  152. sub_mesh->vertexData->vertexStart = 0;
  153. sub_mesh->vertexData->vertexCount = triangle_count * 3 + quad_count * 4;
  154. sub_mesh->indexData = new Ogre::IndexData();
  155. sub_mesh->indexData->indexStart = 0;
  156. sub_mesh->indexData->indexCount = triangle_count * 3 + quad_count * 6;
  157. sub_mesh->indexData->indexBuffer = buffer_manager.createIndexBuffer(
  158. Ogre::HardwareIndexBuffer::IT_16BIT,
  159. sub_mesh->indexData->indexCount,
  160. Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  161. u16* cur_index = static_cast< u16* >(sub_mesh->indexData->indexBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD));
  162. u32 cur_vertex = 0;
  163. Ogre::VertexDeclaration* decl = sub_mesh->vertexData->vertexDeclaration;
  164. Ogre::VertexBufferBinding* bind = sub_mesh->vertexData->vertexBufferBinding;
  165. // position buffer
  166. decl->addElement(POSITION_BINDING, 0, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
  167. Ogre::HardwareVertexBufferSharedPtr vbuf0 = buffer_manager.createVertexBuffer(
  168. decl->getVertexSize(POSITION_BINDING),
  169. sub_mesh->vertexData->vertexCount,
  170. Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  171. bind->setBinding(POSITION_BINDING, vbuf0);
  172. // vertex color buffer
  173. decl->addElement(COLOUR_BINDING, 0, Ogre::VET_COLOUR, Ogre::VES_DIFFUSE);
  174. Ogre::HardwareVertexBufferSharedPtr vbuf1 = buffer_manager.createVertexBuffer(
  175. decl->getVertexSize(COLOUR_BINDING),
  176. sub_mesh->vertexData->vertexCount,
  177. Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  178. // Set vertex buffer binding so buffer 1 is bound to our colour buffer
  179. bind->setBinding(COLOUR_BINDING, vbuf1);
  180. // texture coordinate buffer
  181. decl->addElement(TEXTURE_BINDING, 0, Ogre::VET_FLOAT2, Ogre::VES_TEXTURE_COORDINATES, 0);
  182. Ogre::HardwareVertexBufferSharedPtr vbuf2 = buffer_manager.createVertexBuffer(
  183. decl->getVertexSize(TEXTURE_BINDING),
  184. sub_mesh->vertexData->vertexCount,
  185. Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);
  186. bind->setBinding(TEXTURE_BINDING, vbuf2);
  187. float* pPos = static_cast<float*>(vbuf0->lock(Ogre::HardwareBuffer::HBL_DISCARD));
  188. float* tPos = static_cast<float*>(vbuf2->lock(Ogre::HardwareBuffer::HBL_DISCARD));
  189. Ogre::RenderSystem* rs = Ogre::Root::getSingleton().getRenderSystem();
  190. std::vector<Ogre::RGBA> coloursVec(sub_mesh->vertexData->vertexCount);
  191. Ogre::RGBA* colours = coloursVec.data();
  192. Ogre::RGBA* cur_colour( colours );
  193. u32 offset_to_poly = offset_to_vertexes + GetU16LE( offset_to_part + 0x0E );
  194. u32 offset_to_texture_coords = offset_to_vertexes + GetU16LE( offset_to_part + 0x10 );
  195. offset_to_4 = offset_to_vertexes + GetU16LE( offset_to_part + 0x12 );
  196. offset_to_5 = offset_to_vertexes + GetU16LE( offset_to_part + 0x14 );
  197. // add textured quad
  198. for( u32 j = 0; j < number_of_quad_t; ++j )
  199. {
  200. readQuad( offset_to_poly, vertexes, pPos, cur_index, cur_vertex, cur_colour, true );
  201. TexForGenBsx texture;
  202. AddTexture( texture, textures );
  203. Ogre::Vector2 at;
  204. Ogre::Vector2 bt;
  205. Ogre::Vector2 ct;
  206. Ogre::Vector2 dt;
  207. u8 texture_coords_id = GetU8( offset_to_poly + 0x14 );
  208. int x = GetU8( offset_to_texture_coords + texture_coords_id * 0x02 + 0x00 ) + texture.start_x;
  209. at.x = x / tex_width;
  210. int y = GetU8( offset_to_texture_coords + texture_coords_id * 0x02 + 0x01 ) + texture.start_y;
  211. at.y = y / tex_height;
  212. texture_coords_id = GetU8( offset_to_poly + 0x15 );
  213. x = GetU8( offset_to_texture_coords + texture_coords_id * 0x02 + 0x00 ) + texture.start_x;
  214. bt.x = x / tex_width;
  215. y = GetU8( offset_to_texture_coords + texture_coords_id * 0x02 + 0x01 ) + texture.start_y;
  216. bt.y = y / tex_height;
  217. texture_coords_id = GetU8(offset_to_poly + 0x16);
  218. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  219. ct.x = x / tex_width;
  220. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  221. ct.y = y / tex_height;
  222. texture_coords_id = GetU8(offset_to_poly + 0x17);
  223. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  224. dt.x = x / tex_width;
  225. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  226. dt.y = y / tex_height;
  227. *tPos++ = at.x; *tPos++ = at.y;
  228. *tPos++ = bt.x; *tPos++ = bt.y;
  229. *tPos++ = ct.x; *tPos++ = ct.y;
  230. *tPos++ = dt.x; *tPos++ = dt.y;
  231. offset_to_poly += 0x18;
  232. }
  233. // add textured triangle
  234. for (u32 j = 0; j < number_of_triangle_t; ++j)
  235. {
  236. readTriangle( offset_to_poly, vertexes, pPos, cur_index, cur_vertex, cur_colour, true );
  237. TexForGenBsx texture;
  238. AddTexture( texture, textures );
  239. Ogre::Vector2 at;
  240. Ogre::Vector2 bt;
  241. Ogre::Vector2 ct;
  242. u8 texture_coords_id = GetU8(offset_to_poly + 0x10);
  243. int x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  244. at.x = x / tex_width;
  245. int y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  246. at.y = y / tex_height;
  247. texture_coords_id = GetU8(offset_to_poly + 0x11);
  248. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  249. bt.x = x / tex_width;
  250. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  251. bt.y = y / tex_height;
  252. texture_coords_id = GetU8(offset_to_poly + 0x12);
  253. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  254. ct.x = x / tex_width;
  255. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  256. ct.y = y / tex_height;
  257. *tPos++ = at.x; *tPos++ = at.y;
  258. *tPos++ = bt.x; *tPos++ = bt.y;
  259. *tPos++ = ct.x; *tPos++ = ct.y;
  260. offset_to_poly += 0x14;
  261. }
  262. // add textured monochrome quad
  263. for (u32 j = 0; j < number_of_quad_t2; ++j)
  264. {
  265. readQuad( offset_to_poly, vertexes, pPos, cur_index, cur_vertex, cur_colour, false );
  266. TexForGenBsx texture;
  267. AddTexture( texture, textures );
  268. Ogre::Vector2 at;
  269. Ogre::Vector2 bt;
  270. Ogre::Vector2 ct;
  271. Ogre::Vector2 dt;
  272. u8 texture_coords_id = GetU8(offset_to_poly + 0x8);
  273. int x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  274. at.x = x / tex_width;
  275. int y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  276. at.y = y / tex_height;
  277. texture_coords_id = GetU8(offset_to_poly + 0x9);
  278. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  279. bt.x = x / tex_width;
  280. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  281. bt.y = y / tex_height;
  282. texture_coords_id = GetU8(offset_to_poly + 0xa);
  283. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  284. dt.x = x / tex_width;
  285. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  286. dt.y = y / tex_height;
  287. texture_coords_id = GetU8(offset_to_poly + 0xb);
  288. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  289. ct.x = x / tex_width;
  290. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  291. ct.y = y / tex_height;
  292. *tPos++ = at.x; *tPos++ = at.y;
  293. *tPos++ = bt.x; *tPos++ = bt.y;
  294. *tPos++ = ct.x; *tPos++ = ct.y;
  295. *tPos++ = dt.x; *tPos++ = dt.y;
  296. offset_to_poly += 0x0C;
  297. }
  298. // add textured monochrome triangle
  299. for (u32 j = 0; j < number_of_triangle_t2; ++j)
  300. {
  301. readTriangle( offset_to_poly, vertexes, pPos, cur_index, cur_vertex, cur_colour, false );
  302. TexForGenBsx texture;
  303. AddTexture( texture, textures );
  304. Ogre::Vector2 at;
  305. Ogre::Vector2 bt;
  306. Ogre::Vector2 ct;
  307. u8 texture_coords_id = GetU8(offset_to_poly + 0x8);
  308. int x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  309. at.x = x / tex_width;
  310. int y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  311. at.y = y / tex_height;
  312. texture_coords_id = GetU8(offset_to_poly + 0x9);
  313. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  314. bt.x = x / tex_width;
  315. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  316. bt.y = y / tex_height;
  317. texture_coords_id = GetU8(offset_to_poly + 0xa);
  318. x = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x00) + texture.start_x;
  319. ct.x = x / tex_width;
  320. y = GetU8(offset_to_texture_coords + texture_coords_id * 0x02 + 0x01) + texture.start_y;
  321. ct.y = y / tex_height;
  322. *tPos++ = at.x; *tPos++ = at.y;
  323. *tPos++ = bt.x; *tPos++ = bt.y;
  324. *tPos++ = ct.x; *tPos++ = ct.y;
  325. offset_to_poly += 0x0C;
  326. }
  327. // add monochrome triangle
  328. for (u32 j = 0; j < number_of_triangle_m; ++j)
  329. {
  330. readTriangle( offset_to_poly, vertexes, pPos, cur_index, cur_vertex, cur_colour, false );
  331. Ogre::Vector2 at(0.99, 0.99);
  332. Ogre::Vector2 bt(0.99, 0.99);
  333. Ogre::Vector2 ct(0.99, 0.99);
  334. *tPos++ = at.x; *tPos++ = at.y;
  335. *tPos++ = bt.x; *tPos++ = bt.y;
  336. *tPos++ = ct.x; *tPos++ = ct.y;
  337. offset_to_poly += 0x08;
  338. }
  339. // add monochrome quad
  340. for (u32 j = 0; j < number_of_quad_m; ++j)
  341. {
  342. readQuad( offset_to_poly, vertexes, pPos, cur_index, cur_vertex, cur_colour, false );
  343. Ogre::Vector2 at(0.99, 0.99);
  344. Ogre::Vector2 bt(0.99, 0.99);
  345. Ogre::Vector2 ct(0.99, 0.99);
  346. Ogre::Vector2 dt(0.99, 0.99);
  347. *tPos++ = at.x; *tPos++ = at.y;
  348. *tPos++ = bt.x; *tPos++ = bt.y;
  349. *tPos++ = ct.x; *tPos++ = ct.y;
  350. *tPos++ = dt.x; *tPos++ = dt.y;
  351. offset_to_poly += 0x08;
  352. }
  353. // add color triangle
  354. for (u32 j = 0; j < number_of_triangle; ++j)
  355. {
  356. readTriangle( offset_to_poly, vertexes, pPos, cur_index, cur_vertex, cur_colour, true );
  357. Ogre::Vector2 at(0.99, 0.99);
  358. Ogre::Vector2 bt(0.99, 0.99);
  359. Ogre::Vector2 ct(0.99, 0.99);
  360. *tPos++ = at.x; *tPos++ = at.y;
  361. *tPos++ = bt.x; *tPos++ = bt.y;
  362. *tPos++ = ct.x; *tPos++ = ct.y;
  363. offset_to_poly += 0x10;
  364. }
  365. // add color quad
  366. for (u32 j = 0; j < number_of_quads; ++j)
  367. {
  368. readQuad( offset_to_poly, vertexes, pPos, cur_index, cur_vertex, cur_colour, true );
  369. Ogre::Vector2 a(0.99, 0.99);
  370. Ogre::Vector2 b(0.99, 0.99);
  371. Ogre::Vector2 c(0.99, 0.99);
  372. Ogre::Vector2 d(0.99, 0.99);
  373. *tPos++ = a.x; *tPos++ = a.y;
  374. *tPos++ = b.x; *tPos++ = b.y;
  375. *tPos++ = c.x; *tPos++ = c.y;
  376. *tPos++ = d.x; *tPos++ = d.y;
  377. offset_to_poly += 0x14;
  378. }
  379. vbuf0->unlock();
  380. vbuf1->writeData(0, vbuf1->getSizeInBytes(), colours, true);
  381. vbuf2->unlock();
  382. sub_mesh->indexData->indexBuffer->unlock();
  383. // Optimize index data
  384. sub_mesh->indexData->optimiseVertexCacheTriList();
  385. int vertex_number = sub_mesh->vertexData->vertexCount;
  386. u8 bone_index( GetU8(offset_to_part + 0x01) );
  387. //LOGGER->Log(LOGGER_INFO, "vertex_number %08x", vertex_number);
  388. for (int i = 0; i < vertex_number; ++i)
  389. {
  390. Ogre::VertexBoneAssignment vba;
  391. vba.vertexIndex = i;
  392. vba.boneIndex = bone_index * 2 + 2;
  393. vba.weight = 1.0f;
  394. sub_mesh->addBoneAssignment(vba);
  395. }
  396. }
  397. //---------------------------------------------------------------------
  398. void
  399. MeshFile::GetData(const Ogre::String& name, const int offset_to_parts, const int number_of_parts, Ogre::MeshPtr mesh, VectorTexForGenBsx& textures)
  400. {
  401. int part_end( number_of_parts );
  402. int part_begin = 0;
  403. for (u32 i = part_begin; i < number_of_parts && i < part_end; ++i)
  404. {
  405. u32 offset_to_part = offset_to_parts + i * 0x20;
  406. readPart( name, i, offset_to_part, mesh, textures );
  407. }
  408. }
  409. //---------------------------------------------------------------------
  410. void
  411. MeshFile::AddTexture( TexForGenBsx& texture, VectorTexForGenBsx& textures )
  412. {
  413. u8 control = GetU8(offset_to_5);
  414. //LOGGER->Log(LOGGER_INFO, "quad control: %02x", control);
  415. ++offset_to_5;
  416. u8 blend = ( control >> 4 ) & 0x3;
  417. u8 flag_id = control & 0xf;
  418. u32 flag = GetU32LE( offset_to_4 + flag_id * 4 );
  419. int type = flag & 0x3f;
  420. int bpp = (flag >> 0x6) & 0x3;
  421. int vram_x = ((flag >> 0x8) & 0xf) * 64;
  422. int vram_y = ((flag >> 0xc) & 0x1) * 256;
  423. int clut_x = ((flag >> 0x10) & 0x3f) * 16;
  424. int clut_y = (flag >> 0x16) & 0x1ff;
  425. texture.texture_x = vram_x;
  426. texture.texture_y = vram_y;
  427. texture.palette_x = clut_x;
  428. texture.palette_y = clut_y;
  429. texture.bpp = ( BPP )bpp;
  430. texture.type = type;
  431. for( int i = 0; i < textures.size(); i++ )
  432. {
  433. if( texture == textures[ i ] )
  434. {
  435. texture.start_x = textures[ i ].start_x;
  436. texture.start_y = textures[ i ].start_y;
  437. return;
  438. }
  439. }
  440. LOGGER->Log( "New Texture: X:0x" + HexToString( texture.texture_x, 4, '0' ) + ", Y:0x" + HexToString( texture.texture_y, 4, '0' ) + ", CLUTX:0x" + HexToString( texture.palette_x, 4, '0' ) + ", CLUTY:0x" + HexToString( texture.palette_y, 4, '0' ) + ", bpp:0x" + HexToString( texture.bpp, 2, '0' ) + ", type:0x" + HexToString( texture.type, 2, '0' ) + "\n" );
  441. texture.start_x = 0;
  442. texture.start_y = 0;
  443. for (int i = 0; i < textures.size(); i++)
  444. {
  445. if (texture.start_x <= textures[i].start_x)
  446. {
  447. texture.start_x = textures[i].start_x + 256;
  448. }
  449. }
  450. textures.push_back( texture );
  451. //LOGGER->Log(LOGGER_INFO, "Startx:0x%04x, Starty:0x%04x", texture.start_x, texture.start_y);
  452. }
  453. //---------------------------------------------------------------------