WorldMapModule.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. #include "modules/worldmap/WorldMapModule.h"
  2. #include "data/worldmap/MapFileSerializer.h"
  3. #include "data/worldmap/MapFile.h"
  4. #include <OgreSceneManager.h>
  5. #include <OgreManualObject.h>
  6. #include <OgreRoot.h>
  7. #include "core/CameraManager.h"
  8. #include <OgreHardwarePixelBuffer.h>
  9. #include <OgreEntity.h>
  10. #include <OgreLogManager.h>
  11. #include <OgreTextureManager.h>
  12. #include <OgreMaterialManager.h>
  13. #include <OgreTechnique.h>
  14. #include "data/worldmap/TxzFile.h"
  15. #include "data/worldmap/TxzFileSerializer.h"
  16. #include "data/worldmap/MapFileManager.h"
  17. template<> QGears::WorldMapModule *Ogre::Singleton< QGears::WorldMapModule >::msSingleton = nullptr;
  18. namespace QGears {
  19. using namespace Ogre;
  20. struct harcoded_texture_info
  21. {
  22. uint32 id;
  23. const char* mName;
  24. uint32 mWidth;
  25. uint32 mHeight;
  26. uint32 mU;
  27. uint32 mV;
  28. };
  29. static harcoded_texture_info gTextures[] =
  30. {
  31. { 0, "pond", 32, 32, 0, 352 },
  32. { 1, "riv_m2", 32, 32, 128, 64 },
  33. { 2, "was_gs", 64, 64, 64, 192 },
  34. { 3, "wpcltr", 32, 128, 0, 256 },
  35. { 4, "wpcltr2", 32, 32, 160, 64 },
  36. { 5, "bzdun", 64, 64, 192, 192 },
  37. { 6, "bone", 32, 32, 224, 384 },
  38. { 7, "bone2", 32, 32, 224, 416 },
  39. { 8, "bornwd", 64, 64, 160, 320 },
  40. { 9, "bridge", 32, 64, 192, 0 },
  41. { 10, "bridge2", 32, 32, 224, 0 },
  42. { 11, "cave", 32, 32, 224, 448 },
  43. { 12, "cave2", 32, 32, 224, 320 },
  44. { 13, "cave_s", 32, 32, 160, 224 },
  45. { 14, "cdl_cl2", 64, 32, 96, 96 },
  46. { 15, "cf01", 64, 32, 192, 288 },
  47. { 16, "clf_bgs", 64, 32, 192, 384 },
  48. { 17, "clf_ggl", 64, 64, 128, 256 },
  49. { 18, "clf_ggs", 64, 32, 192, 352 },
  50. { 19, "clf_l", 64, 64, 0, 0 },
  51. { 20, "clf_ld", 64, 64, 64, 0 },
  52. { 21, "clf_lf", 64, 64, 128, 0 },
  53. { 22, "clf_lg", 32, 64, 0, 96 },
  54. { 23, "clf_lr", 32, 64, 128, 0 },
  55. { 24, "clf_lsg", 32, 64, 64, 64 },
  56. { 25, "clf_r", 32, 32, 0, 96 },
  57. { 26, "clf_s", 64, 32, 192, 0 },
  58. { 27, "clf_sd", 64, 32, 192, 32 },
  59. { 28, "clf_sf", 64, 32, 0, 64 },
  60. { 29, "clf_sg", 32, 32, 32, 96 },
  61. { 30, "clf_sg2", 32, 32, 0, 160 },
  62. { 31, "clf_sr", 32, 32, 32, 96 },
  63. { 32, "clf_ss", 32, 32, 32, 128 },
  64. { 33, "clf_ssd", 32, 32, 0, 224 },
  65. { 34, "clf_ssw", 32, 32, 224, 32 },
  66. { 35, "clf_sw", 32, 32, 192, 32 },
  67. { 36, "clf_w02", 64, 64, 128, 64 },
  68. { 37, "clf_w03", 64, 64, 192, 64 },
  69. { 38, "clf_was", 64, 32, 64, 64 },
  70. { 39, "clfeg", 32, 32, 192, 320 },
  71. { 40, "clfegd", 32, 32, 0, 320 },
  72. { 41, "clftop", 64, 32, 192, 64 },
  73. { 42, "clftop2", 32, 32, 128, 64 },
  74. { 43, "cndl_cl", 64, 32, 96, 128 },
  75. { 44, "cndlf", 64, 64, 160, 64 },
  76. { 45, "cndlf02", 64, 64, 208, 64 },
  77. { 46, "comtr", 16, 32, 144, 96 },
  78. { 47, "cosinn", 32, 32, 224, 416 },
  79. { 48, "cosinn2", 32, 32, 192, 448 },
  80. { 49, "csmk", 32, 32, 64, 64 },
  81. { 50, "csmk2", 32, 32, 96, 64 },
  82. { 51, "cstds01", 32, 32, 224, 160 },
  83. { 52, "cstds02", 64, 64, 0, 448 },
  84. { 53, "des01", 32, 32, 160, 320 },
  85. { 54, "desert", 64, 64, 128, 128 },
  86. { 55, "desor", 64, 32, 160, 64 },
  87. { 56, "ds1", 32, 32, 0, 256 },
  88. { 57, "ds_s1", 32, 32, 192, 288 },
  89. { 58, "dsee1", 32, 32, 96, 288 },
  90. { 59, "dsrt_d", 32, 32, 224, 0 },
  91. { 60, "dsrt_e", 64, 128, 64, 128 },
  92. { 61, "edes01", 32, 32, 224, 320 },
  93. { 62, "elm01", 32, 32, 160, 0 },
  94. { 63, "elm02", 32, 32, 64, 96 },
  95. { 64, "elm_gro", 64, 64, 0, 96 },
  96. { 65, "elm_r", 32, 32, 192, 0 },
  97. { 66, "elm_r2", 32, 32, 224, 0 },
  98. { 67, "fall1", 32, 32, 128, 256 },
  99. { 68, "farm01", 32, 32, 160, 32 },
  100. { 69, "farm02", 32, 32, 192, 32 },
  101. { 70, "farm_g", 32, 32, 128, 64 },
  102. { 71, "farm_r", 32, 16, 128, 48 },
  103. { 72, "fld", 64, 64, 64, 96 },
  104. { 73, "fld_02", 64, 64, 0, 64 },
  105. { 74, "fld_s", 64, 64, 0, 160 },
  106. { 75, "fld_s2", 32, 32, 224, 256 },
  107. { 76, "fld_sw", 64, 64, 128, 192 },
  108. { 77, "fld_v", 128, 128, 0, 128 },
  109. { 78, "fld_vd", 32, 64, 96, 128 },
  110. { 79, "fld_vd2", 32, 64, 192, 416 },
  111. { 80, "fvedge", 32, 64, 0, 0 },
  112. { 81, "gclf_d", 128, 64, 128, 128 },
  113. { 82, "gclf_g", 32, 64, 224, 128 },
  114. { 83, "gclfwa", 128, 64, 64, 320 },
  115. { 84, "gclfwa2", 32, 64, 160, 320 },
  116. { 85, "gclfwag", 32, 64, 32, 320 },
  117. { 86, "gg_gro", 64, 64, 64, 448 },
  118. { 87, "gg_mts", 64, 128, 0, 128 },
  119. { 88, "ggmk", 64, 64, 128, 448 },
  120. { 89, "ggmt", 128, 128, 0, 0 },
  121. { 90, "ggmt_e", 128, 32, 0, 96 },
  122. { 91, "ggmt_ed", 128, 32, 128, 96 },
  123. { 92, "ggmt_eg", 32, 32, 96, 224 },
  124. { 93, "ggmtd", 128, 128, 128, 0 },
  125. { 94, "ggs_g", 32, 32, 32, 64 },
  126. { 95, "ggshr", 32, 32, 192, 96 },
  127. { 96, "ggshrg", 32, 32, 224, 96 },
  128. { 97, "gia", 64, 32, 64, 224 },
  129. { 98, "gia2", 64, 32, 0, 224 },
  130. { 99, "gia_d", 64, 32, 128, 224 },
  131. { 100, "gia_d2", 64, 32, 64, 224 },
  132. { 101, "gia_g", 32, 32, 192, 224 },
  133. { 102, "gia_g2", 32, 32, 128, 224 },
  134. { 103, "gmt_eda", 32, 32, 0, 352 },
  135. { 104, "gonclf", 128, 64, 96, 64 },
  136. { 105, "gredge", 32, 32, 192, 192 },
  137. { 106, "hyouga", 64, 64, 192, 64 },
  138. { 107, "iceclf", 64, 32, 64, 96 },
  139. { 108, "iceclfd", 64, 32, 128, 96 },
  140. { 109, "iceclfg", 32, 32, 32, 224 },
  141. { 110, "jun", 64, 64, 192, 192 },
  142. { 111, "jun_d", 64, 64, 128, 192 },
  143. { 112, "jun_e", 64, 16, 0, 240 },
  144. { 113, "jun_gro", 64, 64, 0, 64 },
  145. { 114, "junmk", 32, 32, 0, 96 },
  146. { 115, "junn01", 32, 32, 160, 112 },
  147. { 116, "junn02", 32, 32, 192, 112 },
  148. { 117, "junn03", 32, 32, 224, 112 },
  149. { 118, "junn04", 32, 32, 64, 128 },
  150. { 119, "jutmpl01", 64, 64, 128, 192 },
  151. { 120, "lake-e", 32, 32, 96, 192 },
  152. { 121, "lake_ef", 32, 32, 128, 224 },
  153. { 122, "lake_fl", 128, 32, 160, 224 },
  154. { 123, "lostclf", 32, 64, 128, 384 },
  155. { 124, "lostmt", 128, 32, 128, 448 },
  156. { 125, "lostmtd", 128, 32, 128, 480 },
  157. { 126, "lostmts", 64, 32, 160, 384 },
  158. { 127, "lostwd_e", 32, 32, 64, 480 },
  159. { 128, "lostwod", 64, 64, 0, 448 },
  160. { 129, "lst1", 32, 32, 192, 256 },
  161. { 130, "lstwd_e2", 32, 32, 96, 480 },
  162. { 131, "mzes", 32, 32, 224, 128 },
  163. { 132, "mzmt_e", 128, 64, 128, 64 },
  164. { 133, "mzmt_ed", 128, 32, 128, 128 },
  165. { 134, "mzmt_edw", 128, 32, 128, 160 },
  166. { 135, "mzmt_ew", 128, 32, 0, 128 },
  167. { 136, "mzmt_o", 128, 32, 64, 416 },
  168. { 137, "mzmt_od", 128, 32, 64, 448 },
  169. { 138, "mzmt_s", 128, 32, 0, 192 },
  170. { 139, "mzmt_sd", 128, 32, 0, 160 },
  171. { 140, "md01", 32, 32, 96, 16 },
  172. { 141, "md02", 128, 128, 0, 0 },
  173. { 142, "md03", 16, 16, 112, 64 },
  174. { 143, "md04", 32, 32, 128, 16 },
  175. { 144, "md05", 64, 16, 96, 0 },
  176. { 145, "md06", 16, 32, 96, 48 },
  177. { 146, "md07", 16, 16, 112, 48 },
  178. { 147, "md_mt", 128, 128, 128, 0 },
  179. { 148, "md_mtd", 128, 128, 0, 0 },
  180. { 149, "md_mts", 64, 128, 64, 160 },
  181. { 150, "md_snow", 128, 32, 128, 0 },
  182. { 151, "md_snw2", 128, 32, 128, 32 },
  183. { 152, "md_snwd", 128, 64, 0, 128 },
  184. { 153, "md_snwe", 64, 64, 96, 320 },
  185. { 154, "md_snws", 64, 64, 128, 128 },
  186. { 155, "md_snwt", 128, 32, 0, 192 },
  187. { 156, "md_snww", 32, 32, 224, 224 },
  188. { 157, "md_sw_s", 128, 128, 0, 0 },
  189. { 158, "md_swd2", 32, 32, 192, 256 },
  190. { 159, "md_swnp", 128, 128, 0, 96 },
  191. { 160, "mdsrt_e", 128, 32, 128, 192 },
  192. { 161, "mdsrt_ed", 128, 32, 128, 224 },
  193. { 162, "mdsrt_eg", 32, 32, 64, 224 },
  194. { 163, "midil", 32, 32, 32, 192 },
  195. { 164, "midild", 32, 32, 224, 192 },
  196. { 165, "mt_ewg", 32, 32, 64, 96 },
  197. { 166, "mt_road", 64, 64, 192, 128 },
  198. { 167, "mt_se", 32, 32, 160, 416 },
  199. { 168, "mt_se2", 64, 64, 128, 256 },
  200. { 169, "mt_sg01", 32, 32, 0, 224 },
  201. { 170, "mt_sg02", 32, 32, 32, 224 },
  202. { 171, "mt_sg03", 32, 32, 0, 192 },
  203. { 172, "mt_sg04", 32, 32, 160, 384 },
  204. { 173, "mtcoin", 64, 64, 0, 256 },
  205. { 174, "mtwas_e", 128, 32, 0, 224 },
  206. { 175, "mtwas_ed", 128, 32, 0, 224 },
  207. { 176, "ncol_gro", 64, 64, 64, 384 },
  208. { 177, "ncole01", 32, 32, 224, 384 },
  209. { 178, "ncole02", 32, 32, 192, 416 },
  210. { 179, "nivl_gro", 64, 64, 128, 384 },
  211. { 180, "nivl_mt", 128, 64, 0, 0 },
  212. { 181, "nivl_top", 32, 32, 0, 64 },
  213. { 182, "nivlr", 32, 32, 192, 384 },
  214. { 183, "port", 32, 32, 96, 224 },
  215. { 184, "port_d", 32, 32, 160, 0 },
  216. { 185, "rzclf02", 64, 64, 128, 128 },
  217. { 186, "rct_gro", 64, 128, 0, 416 },
  218. { 187, "riv_cls", 64, 64, 64, 0 },
  219. { 188, "riv_l1", 32, 32, 96, 320 },
  220. { 189, "riv_m", 32, 32, 0, 224 },
  221. { 190, "rivr", 32, 32, 64, 224 },
  222. { 191, "rivrclf", 64, 64, 128, 192 },
  223. { 192, "rivs1", 32, 32, 128, 320 },
  224. { 193, "rivshr", 64, 64, 192, 192 },
  225. { 194, "rivssr", 64, 32, 192, 224 },
  226. { 195, "rivstrt", 32, 32, 192, 160 },
  227. { 196, "rm1", 32, 32, 32, 288 },
  228. { 197, "rocet", 32, 32, 128, 160 },
  229. { 198, "rs_ss", 32, 32, 96, 224 },
  230. { 199, "sango", 32, 32, 224, 320 },
  231. { 200, "sango2", 32, 32, 224, 352 },
  232. { 201, "sango3", 32, 32, 128, 384 },
  233. { 202, "sango4", 64, 64, 0, 384 },
  234. { 203, "sdun", 64, 64, 0, 160 },
  235. { 204, "sdun02", 64, 64, 64, 160 },
  236. { 205, "sh1", 32, 32, 32, 256 },
  237. { 206, "sh_s1", 32, 32, 224, 288 },
  238. { 207, "shedge", 32, 64, 160, 160 },
  239. { 208, "shlm_1", 32, 32, 192, 320 },
  240. { 209, "shol", 128, 128, 128, 96 },
  241. { 210, "shol_s", 64, 64, 192, 192 },
  242. { 211, "shor", 128, 128, 0, 0 },
  243. { 212, "shor_s", 64, 64, 128, 192 },
  244. { 213, "shor_s2", 32, 32, 224, 416 },
  245. { 214, "shor_v", 32, 32, 192, 0 },
  246. { 215, "silo", 32, 32, 224, 32 },
  247. { 216, "slope", 128, 32, 0, 384 },
  248. { 217, "snow_es", 32, 32, 192, 480 },
  249. { 218, "snow_es2", 32, 32, 224, 480 },
  250. { 219, "snow_es3", 32, 32, 224, 448 },
  251. { 220, "snw_mt", 128, 128, 0, 0 },
  252. { 221, "snw_mtd", 128, 128, 128, 0 },
  253. { 222, "snw_mte", 64, 32, 0, 96 },
  254. { 223, "snw_mted", 64, 32, 64, 96 },
  255. { 224, "snw_mts", 64, 128, 64, 0 },
  256. { 225, "snw_mts2", 64, 32, 128, 192 },
  257. { 226, "snwfld", 64, 64, 0, 64 },
  258. { 227, "snwfld_s", 64, 32, 128, 128 },
  259. { 228, "snwgra", 64, 64, 192, 192 },
  260. { 229, "snwhm01", 32, 32, 32, 0 },
  261. { 230, "snwhm02", 32, 32, 32, 32 },
  262. { 231, "snwods", 32, 32, 224, 192 },
  263. { 232, "snwood", 64, 64, 192, 128 },
  264. { 233, "snwtrk", 32, 64, 96, 256 },
  265. { 234, "sse_s1", 32, 32, 32, 320 },
  266. { 235, "ssee1", 32, 32, 64, 288 },
  267. { 236, "sst1", 32, 32, 224, 256 },
  268. { 237, "stown_r", 32, 32, 192, 256 },
  269. { 238, "stw_gro", 64, 64, 0, 384 },
  270. { 239, "subrg2", 32, 32, 224, 160 },
  271. { 240, "susbrg", 64, 64, 192, 96 },
  272. { 241, "sw_se", 64, 64, 0, 0 },
  273. { 242, "swclf_l", 64, 64, 64, 128 },
  274. { 243, "swclf_ld", 64, 64, 192, 128 },
  275. { 244, "swclf_lg", 32, 64, 0, 192 },
  276. { 245, "swclf_s", 64, 32, 128, 96 },
  277. { 246, "swclf_sd", 64, 32, 192, 96 },
  278. { 247, "swclf_sg", 32, 32, 32, 192 },
  279. { 248, "swclf_wg", 32, 32, 192, 192 },
  280. { 249, "swfld_s2", 64, 32, 128, 160 },
  281. { 250, "swfld_s3", 32, 32, 160, 192 },
  282. { 251, "swmd_cg", 32, 32, 128, 192 },
  283. { 252, "swmd_clf", 64, 32, 64, 192 },
  284. { 253, "swp1", 32, 32, 0, 288 },
  285. { 254, "trk", 64, 64, 128, 0 },
  286. { 255, "tyo_f", 128, 128, 128, 128 },
  287. { 256, "tyosnw", 64, 128, 64, 384 },
  288. { 257, "uf1", 32, 32, 160, 256 },
  289. { 258, "utai01", 32, 32, 32, 96 },
  290. { 259, "utai02", 32, 32, 224, 64 },
  291. { 260, "utai_gro", 64, 64, 128, 96 },
  292. { 261, "utaimt", 32, 32, 0, 128 },
  293. { 262, "utaimtd", 32, 32, 96, 96 },
  294. { 263, "utaimtg", 32, 32, 96, 128 },
  295. { 264, "wa1", 32, 32, 192, 320 },
  296. { 265, "wzs1", 32, 32, 128, 288 },
  297. { 266, "wzshr", 32, 32, 160, 32 },
  298. { 267, "wzshr2", 32, 32, 32, 128 },
  299. { 268, "wzshrs", 32, 32, 32, 160 },
  300. { 269, "was", 128, 128, 0, 96 },
  301. { 270, "was_d", 64, 32, 0, 224 },
  302. { 271, "was_g", 64, 64, 0, 192 },
  303. { 272, "was_s", 128, 128, 128, 0 },
  304. { 273, "wasfld", 64, 64, 64, 256 },
  305. { 274, "wdedge", 64, 64, 64, 160 },
  306. { 275, "we1", 32, 32, 96, 256 },
  307. { 276, "we_s1", 32, 32, 160, 288 },
  308. { 277, "wedged", 32, 64, 128, 160 },
  309. { 278, "wod-e2", 32, 32, 64, 224 },
  310. { 279, "wood", 64, 64, 192, 0 },
  311. { 280, "wood_d", 64, 64, 192, 160 },
  312. { 281, "wtrk", 32, 64, 64, 96 }
  313. };
  314. static const char* kWorldResourceGroup = "FFVII_World";
  315. static void createReferenceTextureFileInstance(QGears::TxzFileSerializer& s)
  316. {
  317. class TestFile : public QGears::TxzFile
  318. {
  319. public:
  320. TestFile() : QGears::TxzFile( nullptr, "", 0, "" ) {}
  321. };
  322. const char* file_name = "/home/paul/qgears/data/cd1/world/wm0.txz";
  323. std::ifstream ifs( file_name, std::ifstream::binary );
  324. Ogre::DataStreamPtr stream( OGRE_NEW Ogre::FileStreamDataStream( &ifs, false ) );
  325. // Ogre::LogManager log;
  326. TestFile f;
  327. s.import( stream, f );
  328. }
  329. static void createReferenceFileInstance(std::function<void(Ogre::DataStreamPtr& stream, QGears::WorldMapFile& file)> callBack)
  330. {
  331. class TestFile : public QGears::WorldMapFile
  332. {
  333. public:
  334. TestFile() : QGears::WorldMapFile( nullptr, "", 0, "" ) {}
  335. };
  336. const char* file_name = "./data/wm/WM0.MAP";
  337. std::ifstream ifs( file_name, std::ifstream::binary );
  338. if (!ifs.is_open())
  339. {
  340. LogManager::getSingleton().logMessage("ERROR: Failed to open ./data/wm/WM0.MAP");
  341. return;
  342. }
  343. {
  344. // BOOST_REQUIRE( ifs.is_open() );
  345. Ogre::DataStreamPtr stream( OGRE_NEW Ogre::FileStreamDataStream( &ifs, false ) );
  346. // BOOST_REQUIRE( stream->isReadable() );
  347. //Ogre::LogManager log;
  348. TestFile f;
  349. // log.createLog( "Default Log", true, true, true );
  350. callBack(stream, f);
  351. // log.destroyLog( "Default Log" );
  352. }
  353. }
  354. static float From16BitFixedPoint(QGears::sint16 coord)
  355. {
  356. // return float(coord);
  357. return (float(coord) / 4096.0f);
  358. }
  359. void test(const std::vector<std::vector<TxzFileSerializer::rgba>>& data)
  360. {
  361. //Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().load("clf_l.png.png", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  362. // kWorldResourceGroup
  363. Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().createManual("BackgroundTex",
  364. Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
  365. Ogre::TEX_TYPE_2D,
  366. 640, 256, 0,
  367. Ogre::PF_B8G8R8, Ogre:: TU_DYNAMIC);
  368. Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("BackgroundMat",Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  369. material->getTechnique(0)->getPass(0)->createTextureUnitState("BackgroundTex");
  370. //material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_COLOUR);
  371. Ogre::HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer();
  372. pixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
  373. const Ogre::PixelBox& pixelBox = pixelBuffer->getCurrentLock();
  374. Ogre::uint8* pDest = static_cast<Ogre::uint8*>(pixelBox.data);
  375. for(size_t i=0; i < 256; i++)
  376. {
  377. for(size_t j=0; j < 640; j++)
  378. {
  379. *pDest++ = data[i][j].b;
  380. *pDest++ = data[i][j].g;
  381. *pDest++ = data[i][j].r;
  382. *pDest++ = 0;
  383. }
  384. }
  385. pixelBuffer->unlock();
  386. }
  387. float ToTextureCoordU(uint8 coord, uint16 tId)
  388. {
  389. return (float(coord) - float(gTextures[tId].mU)) / float(gTextures[tId].mWidth);
  390. //return (float(gTextures[tId].mU) + float(coord)) / 256.0f;
  391. }
  392. float ToTextureCoordV(uint8 coord, uint16 tId)
  393. {
  394. return (float(coord) - float(gTextures[tId].mV)) / float(gTextures[tId].mHeight);
  395. //return (float(gTextures[tId].mV) + float(coord)) / 640.0f;
  396. }
  397. std::vector<TexturePtr> gLoadedTextures;
  398. std::vector<MaterialPtr> gMats;
  399. std::map<int, ManualObject*> gObjToTextureId;
  400. static void LoadTextures()
  401. {
  402. if (gLoadedTextures.empty())
  403. {
  404. for (int i=0; i<sizeof(gTextures)/sizeof(gTextures[0]); i++)
  405. {
  406. std::string textureName = std::string(gTextures[i].mName) + ".tex";
  407. ResourceGroupManager::getSingleton().declareResource(textureName.c_str(), "Texture", "TEST");
  408. TexturePtr tmp = Ogre::TextureManager::getSingleton().load(textureName.c_str(), "TEST", TEX_TYPE_2D, 1, 1.0f);
  409. gLoadedTextures.emplace_back(tmp);
  410. Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create(gTextures[i].mName ,Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  411. material->getTechnique(0)->getPass(0)->createTextureUnitState(textureName.c_str());
  412. material->getTechnique(0)->getPass(0)->setTextureFiltering(TFO_NONE);
  413. gMats.emplace_back(material);
  414. }
  415. }
  416. }
  417. void createTestMap(SceneManager* mSceneMgr)
  418. {
  419. std::vector<QGears::MapFileSerializer::SBlock> blocks;
  420. createReferenceFileInstance([&](Ogre::DataStreamPtr& stream, QGears::WorldMapFile& file)
  421. {
  422. QGears::MapFileSerializer s;
  423. s.importMapFile( stream, file );
  424. blocks = s.mBlocks;
  425. });
  426. if (blocks.empty())
  427. {
  428. // Load failed
  429. return;
  430. }
  431. /*
  432. // TODO: Get PSX textures working
  433. QGears::TxzFileSerializer s;
  434. createReferenceTextureFileInstance(s);
  435. std::vector<std::vector<TxzFileSerializer::rgba>> data = s.GetWorldMapTexture(19);
  436. test(data);
  437. */
  438. LoadTextures();
  439. int c = 0;
  440. const auto kSpace = From16BitFixedPoint(8182);
  441. size_t numBlocks = blocks.size();
  442. int mapX = 0;
  443. int mapY = 0;
  444. for ( unsigned int k=0; k<numBlocks; k++ )
  445. {
  446. // 7x9 size
  447. QGears::MapFileSerializer::SBlock& block = blocks[k];
  448. int blockX = 0;
  449. int blockY = 0;
  450. for ( size_t i=0; i<block.mMeshes.size(); i++ )
  451. {
  452. QGears::MapFileSerializer::SBlockPart& part = block.mMeshes[i];
  453. for ( size_t j=0; j<part.mTris.size(); j++)
  454. {
  455. QGears::MapFileSerializer::BlockTriangle& tri = part.mTris.at(j);
  456. ManualObject* manual = nullptr;
  457. auto it = gObjToTextureId.find(tri.TextureInfo);
  458. if (it==gObjToTextureId.end())
  459. {
  460. manual = mSceneMgr->createManualObject(("zzz_manual" + std::to_string(c++)).c_str());
  461. manual->begin(gTextures[tri.TextureInfo].mName, RenderOperation::OT_TRIANGLE_LIST);
  462. manual->setDebugDisplayEnabled(true);
  463. manual->setDynamic(false);
  464. gObjToTextureId[tri.TextureInfo] = manual;
  465. }
  466. else
  467. {
  468. manual = it->second;
  469. }
  470. const float xPos = (blockX * kSpace) + (mapX * kSpace * 4);
  471. const float yPos = (blockY * kSpace) + (mapY * kSpace * 4);
  472. // v1
  473. QGears::MapFileSerializer::Vertex v1 = part.mVertices.at(tri.Vertex2Index);
  474. manual->position(From16BitFixedPoint(v1.X)+xPos, From16BitFixedPoint(v1.Y), From16BitFixedPoint(v1.Z)+yPos);
  475. manual->textureCoord(ToTextureCoordU(tri.uVertex2, tri.TextureInfo), ToTextureCoordV(tri.vVertex2, tri.TextureInfo));
  476. // v2
  477. v1 = part.mVertices.at(tri.Vertex1Index);
  478. manual->position(From16BitFixedPoint(v1.X)+xPos, From16BitFixedPoint(v1.Y), From16BitFixedPoint(v1.Z)+yPos);
  479. manual->textureCoord(ToTextureCoordU(tri.uVertex1, tri.TextureInfo), ToTextureCoordV(tri.vVertex1, tri.TextureInfo));
  480. // v3
  481. v1 = part.mVertices.at(tri.Vertex0Index);
  482. manual->position(From16BitFixedPoint(v1.X)+xPos, From16BitFixedPoint(v1.Y), From16BitFixedPoint(v1.Z)+yPos);
  483. manual->textureCoord(ToTextureCoordU(tri.uVertex0, tri.TextureInfo), ToTextureCoordV(tri.vVertex0, tri.TextureInfo));
  484. }
  485. blockX++;
  486. if (blockX >= 4 )
  487. {
  488. blockX = 0;
  489. blockY++;
  490. }
  491. }
  492. mapX++;
  493. if (mapX >= 9 )
  494. {
  495. mapX = 0;
  496. mapY++;
  497. }
  498. }
  499. for (auto& it : gObjToTextureId)
  500. {
  501. it.second->end();
  502. SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
  503. node->attachObject(it.second);
  504. node->setPosition( 0, 0, 0);
  505. }
  506. }
  507. void WorldMapModule::Init()
  508. {
  509. createTestMap(Root::getSingleton().getSceneManager("Scene"));
  510. CameraManager::getSingleton().EnableWireFrame(true);
  511. }
  512. } // namespace QGears