mapblock_mesh.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "mapblock_mesh.h"
  17. #include "light.h"
  18. #include "mapblock.h"
  19. #include "map.h"
  20. #include "main.h" // for g_profiler
  21. #include "profiler.h"
  22. #include "nodedef.h"
  23. #include "gamedef.h"
  24. #include "mesh.h"
  25. #include "content_mapblock.h"
  26. #include "noise.h"
  27. #include "shader.h"
  28. #include "settings.h"
  29. #include "util/directiontables.h"
  30. static void applyFacesShading(video::SColor& color, float factor)
  31. {
  32. color.setRed(core::clamp(core::round32(color.getRed()*factor), 0, 255));
  33. color.setGreen(core::clamp(core::round32(color.getGreen()*factor), 0, 255));
  34. }
  35. /*
  36. MeshMakeData
  37. */
  38. MeshMakeData::MeshMakeData(IGameDef *gamedef):
  39. m_vmanip(),
  40. m_blockpos(-1337,-1337,-1337),
  41. m_crack_pos_relative(-1337, -1337, -1337),
  42. m_highlighted_pos_relative(-1337, -1337, -1337),
  43. m_smooth_lighting(false),
  44. m_gamedef(gamedef)
  45. {}
  46. void MeshMakeData::fill(MapBlock *block)
  47. {
  48. m_blockpos = block->getPos();
  49. v3s16 blockpos_nodes = m_blockpos*MAP_BLOCKSIZE;
  50. /*
  51. Copy data
  52. */
  53. // Allocate this block + neighbors
  54. m_vmanip.clear();
  55. m_vmanip.addArea(VoxelArea(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE,
  56. blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1)));
  57. {
  58. //TimeTaker timer("copy central block data");
  59. // 0ms
  60. // Copy our data
  61. block->copyTo(m_vmanip);
  62. }
  63. {
  64. //TimeTaker timer("copy neighbor block data");
  65. // 0ms
  66. /*
  67. Copy neighbors. This is lightning fast.
  68. Copying only the borders would be *very* slow.
  69. */
  70. // Get map
  71. Map *map = block->getParent();
  72. for(u16 i=0; i<26; i++)
  73. {
  74. const v3s16 &dir = g_26dirs[i];
  75. v3s16 bp = m_blockpos + dir;
  76. MapBlock *b = map->getBlockNoCreateNoEx(bp);
  77. if(b)
  78. b->copyTo(m_vmanip);
  79. }
  80. }
  81. }
  82. void MeshMakeData::fillSingleNode(MapNode *node)
  83. {
  84. m_blockpos = v3s16(0,0,0);
  85. v3s16 blockpos_nodes = v3s16(0,0,0);
  86. VoxelArea area(blockpos_nodes-v3s16(1,1,1)*MAP_BLOCKSIZE,
  87. blockpos_nodes+v3s16(1,1,1)*MAP_BLOCKSIZE*2-v3s16(1,1,1));
  88. s32 volume = area.getVolume();
  89. s32 our_node_index = area.index(1,1,1);
  90. // Allocate this block + neighbors
  91. m_vmanip.clear();
  92. m_vmanip.addArea(area);
  93. // Fill in data
  94. MapNode *data = new MapNode[volume];
  95. for(s32 i = 0; i < volume; i++)
  96. {
  97. if(i == our_node_index)
  98. {
  99. data[i] = *node;
  100. }
  101. else
  102. {
  103. data[i] = MapNode(CONTENT_AIR, LIGHT_MAX, 0);
  104. }
  105. }
  106. m_vmanip.copyFrom(data, area, area.MinEdge, area.MinEdge, area.getExtent());
  107. delete[] data;
  108. }
  109. void MeshMakeData::setCrack(int crack_level, v3s16 crack_pos)
  110. {
  111. if(crack_level >= 0)
  112. m_crack_pos_relative = crack_pos - m_blockpos*MAP_BLOCKSIZE;
  113. }
  114. void MeshMakeData::setHighlighted(v3s16 highlighted_pos, bool show_hud)
  115. {
  116. m_show_hud = show_hud;
  117. m_highlighted_pos_relative = highlighted_pos - m_blockpos*MAP_BLOCKSIZE;
  118. }
  119. void MeshMakeData::setSmoothLighting(bool smooth_lighting)
  120. {
  121. m_smooth_lighting = smooth_lighting;
  122. }
  123. /*
  124. Light and vertex color functions
  125. */
  126. /*
  127. Calculate non-smooth lighting at interior of node.
  128. Single light bank.
  129. */
  130. static u8 getInteriorLight(enum LightBank bank, MapNode n, s32 increment,
  131. INodeDefManager *ndef)
  132. {
  133. u8 light = n.getLight(bank, ndef);
  134. while(increment > 0)
  135. {
  136. light = undiminish_light(light);
  137. --increment;
  138. }
  139. while(increment < 0)
  140. {
  141. light = diminish_light(light);
  142. ++increment;
  143. }
  144. return decode_light(light);
  145. }
  146. /*
  147. Calculate non-smooth lighting at interior of node.
  148. Both light banks.
  149. */
  150. u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef)
  151. {
  152. u16 day = getInteriorLight(LIGHTBANK_DAY, n, increment, ndef);
  153. u16 night = getInteriorLight(LIGHTBANK_NIGHT, n, increment, ndef);
  154. return day | (night << 8);
  155. }
  156. /*
  157. Calculate non-smooth lighting at face of node.
  158. Single light bank.
  159. */
  160. static u8 getFaceLight(enum LightBank bank, MapNode n, MapNode n2,
  161. v3s16 face_dir, INodeDefManager *ndef)
  162. {
  163. u8 light;
  164. u8 l1 = n.getLight(bank, ndef);
  165. u8 l2 = n2.getLight(bank, ndef);
  166. if(l1 > l2)
  167. light = l1;
  168. else
  169. light = l2;
  170. // Boost light level for light sources
  171. u8 light_source = MYMAX(ndef->get(n).light_source,
  172. ndef->get(n2).light_source);
  173. if(light_source > light)
  174. light = light_source;
  175. return decode_light(light);
  176. }
  177. /*
  178. Calculate non-smooth lighting at face of node.
  179. Both light banks.
  180. */
  181. u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef)
  182. {
  183. u16 day = getFaceLight(LIGHTBANK_DAY, n, n2, face_dir, ndef);
  184. u16 night = getFaceLight(LIGHTBANK_NIGHT, n, n2, face_dir, ndef);
  185. return day | (night << 8);
  186. }
  187. /*
  188. Calculate smooth lighting at the XYZ- corner of p.
  189. Single light bank.
  190. */
  191. static u8 getSmoothLight(enum LightBank bank, v3s16 p, MeshMakeData *data)
  192. {
  193. static v3s16 dirs8[8] = {
  194. v3s16(0,0,0),
  195. v3s16(0,0,1),
  196. v3s16(0,1,0),
  197. v3s16(0,1,1),
  198. v3s16(1,0,0),
  199. v3s16(1,1,0),
  200. v3s16(1,0,1),
  201. v3s16(1,1,1),
  202. };
  203. INodeDefManager *ndef = data->m_gamedef->ndef();
  204. u16 ambient_occlusion = 0;
  205. u16 light = 0;
  206. u16 light_count = 0;
  207. u8 light_source_max = 0;
  208. for(u32 i=0; i<8; i++)
  209. {
  210. MapNode n = data->m_vmanip.getNodeNoEx(p - dirs8[i]);
  211. // if it's CONTENT_IGNORE we can't do any light calculations
  212. if (n.getContent() == CONTENT_IGNORE) {
  213. continue;
  214. }
  215. const ContentFeatures &f = ndef->get(n);
  216. if(f.light_source > light_source_max)
  217. light_source_max = f.light_source;
  218. // Check f.solidness because fast-style leaves look
  219. // better this way
  220. if(f.param_type == CPT_LIGHT && f.solidness != 2)
  221. {
  222. light += decode_light(n.getLight(bank, ndef));
  223. light_count++;
  224. }
  225. else {
  226. ambient_occlusion++;
  227. }
  228. }
  229. if(light_count == 0)
  230. return 255;
  231. light /= light_count;
  232. // Boost brightness around light sources
  233. if(decode_light(light_source_max) >= light)
  234. //return decode_light(undiminish_light(light_source_max));
  235. return decode_light(light_source_max);
  236. if(ambient_occlusion > 4)
  237. {
  238. //calculate table index for gamma space multiplier
  239. ambient_occlusion -= 5;
  240. //table of precalculated gamma space multiply factors
  241. //light^2.2 * factor (0.75, 0.5, 0.25, 0.0), so table holds factor ^ (1 / 2.2)
  242. const float light_amount[4] = {0.877424315, 0.729740053, 0.532520545, 0.0};
  243. light = core::clamp(core::round32(light*light_amount[ambient_occlusion]), 0, 255);
  244. }
  245. return light;
  246. }
  247. /*
  248. Calculate smooth lighting at the XYZ- corner of p.
  249. Both light banks.
  250. */
  251. static u16 getSmoothLight(v3s16 p, MeshMakeData *data)
  252. {
  253. u16 day = getSmoothLight(LIGHTBANK_DAY, p, data);
  254. u16 night = getSmoothLight(LIGHTBANK_NIGHT, p, data);
  255. return day | (night << 8);
  256. }
  257. /*
  258. Calculate smooth lighting at the given corner of p.
  259. Both light banks.
  260. */
  261. u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data)
  262. {
  263. if(corner.X == 1) p.X += 1;
  264. else assert(corner.X == -1);
  265. if(corner.Y == 1) p.Y += 1;
  266. else assert(corner.Y == -1);
  267. if(corner.Z == 1) p.Z += 1;
  268. else assert(corner.Z == -1);
  269. return getSmoothLight(p, data);
  270. }
  271. /*
  272. Converts from day + night color values (0..255)
  273. and a given daynight_ratio to the final SColor shown on screen.
  274. */
  275. static void finalColorBlend(video::SColor& result,
  276. u8 day, u8 night, u32 daynight_ratio)
  277. {
  278. s32 rg = (day * daynight_ratio + night * (1000-daynight_ratio)) / 1000;
  279. s32 b = rg;
  280. // Moonlight is blue
  281. b += (day - night) / 13;
  282. rg -= (day - night) / 23;
  283. // Emphase blue a bit in darker places
  284. // Each entry of this array represents a range of 8 blue levels
  285. static u8 emphase_blue_when_dark[32] = {
  286. 1, 4, 6, 6, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0,
  287. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  288. };
  289. b += emphase_blue_when_dark[b / 8];
  290. b = irr::core::clamp (b, 0, 255);
  291. // Artificial light is yellow-ish
  292. static u8 emphase_yellow_when_artificial[16] = {
  293. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 15, 15, 15
  294. };
  295. rg += emphase_yellow_when_artificial[night/16];
  296. rg = irr::core::clamp (rg, 0, 255);
  297. result.setRed(rg);
  298. result.setGreen(rg);
  299. result.setBlue(b);
  300. }
  301. /*
  302. Mesh generation helpers
  303. */
  304. /*
  305. vertex_dirs: v3s16[4]
  306. */
  307. static void getNodeVertexDirs(v3s16 dir, v3s16 *vertex_dirs)
  308. {
  309. /*
  310. If looked from outside the node towards the face, the corners are:
  311. 0: bottom-right
  312. 1: bottom-left
  313. 2: top-left
  314. 3: top-right
  315. */
  316. if(dir == v3s16(0,0,1))
  317. {
  318. // If looking towards z+, this is the face that is behind
  319. // the center point, facing towards z+.
  320. vertex_dirs[0] = v3s16(-1,-1, 1);
  321. vertex_dirs[1] = v3s16( 1,-1, 1);
  322. vertex_dirs[2] = v3s16( 1, 1, 1);
  323. vertex_dirs[3] = v3s16(-1, 1, 1);
  324. }
  325. else if(dir == v3s16(0,0,-1))
  326. {
  327. // faces towards Z-
  328. vertex_dirs[0] = v3s16( 1,-1,-1);
  329. vertex_dirs[1] = v3s16(-1,-1,-1);
  330. vertex_dirs[2] = v3s16(-1, 1,-1);
  331. vertex_dirs[3] = v3s16( 1, 1,-1);
  332. }
  333. else if(dir == v3s16(1,0,0))
  334. {
  335. // faces towards X+
  336. vertex_dirs[0] = v3s16( 1,-1, 1);
  337. vertex_dirs[1] = v3s16( 1,-1,-1);
  338. vertex_dirs[2] = v3s16( 1, 1,-1);
  339. vertex_dirs[3] = v3s16( 1, 1, 1);
  340. }
  341. else if(dir == v3s16(-1,0,0))
  342. {
  343. // faces towards X-
  344. vertex_dirs[0] = v3s16(-1,-1,-1);
  345. vertex_dirs[1] = v3s16(-1,-1, 1);
  346. vertex_dirs[2] = v3s16(-1, 1, 1);
  347. vertex_dirs[3] = v3s16(-1, 1,-1);
  348. }
  349. else if(dir == v3s16(0,1,0))
  350. {
  351. // faces towards Y+ (assume Z- as "down" in texture)
  352. vertex_dirs[0] = v3s16( 1, 1,-1);
  353. vertex_dirs[1] = v3s16(-1, 1,-1);
  354. vertex_dirs[2] = v3s16(-1, 1, 1);
  355. vertex_dirs[3] = v3s16( 1, 1, 1);
  356. }
  357. else if(dir == v3s16(0,-1,0))
  358. {
  359. // faces towards Y- (assume Z+ as "down" in texture)
  360. vertex_dirs[0] = v3s16( 1,-1, 1);
  361. vertex_dirs[1] = v3s16(-1,-1, 1);
  362. vertex_dirs[2] = v3s16(-1,-1,-1);
  363. vertex_dirs[3] = v3s16( 1,-1,-1);
  364. }
  365. }
  366. struct FastFace
  367. {
  368. TileSpec tile;
  369. video::S3DVertex vertices[4]; // Precalculated vertices
  370. };
  371. static void makeFastFace(TileSpec tile, u16 li0, u16 li1, u16 li2, u16 li3,
  372. v3f p, v3s16 dir, v3f scale, u8 light_source, std::vector<FastFace> &dest)
  373. {
  374. FastFace face;
  375. // Position is at the center of the cube.
  376. v3f pos = p * BS;
  377. float x0 = 0.0;
  378. float y0 = 0.0;
  379. float w = 1.0;
  380. float h = 1.0;
  381. v3f vertex_pos[4];
  382. v3s16 vertex_dirs[4];
  383. getNodeVertexDirs(dir, vertex_dirs);
  384. v3s16 t;
  385. u16 t1;
  386. switch (tile.rotation)
  387. {
  388. case 0:
  389. break;
  390. case 1: //R90
  391. t = vertex_dirs[0];
  392. vertex_dirs[0] = vertex_dirs[3];
  393. vertex_dirs[3] = vertex_dirs[2];
  394. vertex_dirs[2] = vertex_dirs[1];
  395. vertex_dirs[1] = t;
  396. t1=li0;
  397. li0=li3;
  398. li3=li2;
  399. li2=li1;
  400. li1=t1;
  401. break;
  402. case 2: //R180
  403. t = vertex_dirs[0];
  404. vertex_dirs[0] = vertex_dirs[2];
  405. vertex_dirs[2] = t;
  406. t = vertex_dirs[1];
  407. vertex_dirs[1] = vertex_dirs[3];
  408. vertex_dirs[3] = t;
  409. t1 = li0;
  410. li0 = li2;
  411. li2 = t1;
  412. t1 = li1;
  413. li1 = li3;
  414. li3 = t1;
  415. break;
  416. case 3: //R270
  417. t = vertex_dirs[0];
  418. vertex_dirs[0] = vertex_dirs[1];
  419. vertex_dirs[1] = vertex_dirs[2];
  420. vertex_dirs[2] = vertex_dirs[3];
  421. vertex_dirs[3] = t;
  422. t1 = li0;
  423. li0 = li1;
  424. li1 = li2;
  425. li2 = li3;
  426. li3 = t1;
  427. break;
  428. case 4: //FXR90
  429. t = vertex_dirs[0];
  430. vertex_dirs[0] = vertex_dirs[3];
  431. vertex_dirs[3] = vertex_dirs[2];
  432. vertex_dirs[2] = vertex_dirs[1];
  433. vertex_dirs[1] = t;
  434. t1 = li0;
  435. li0 = li3;
  436. li3 = li2;
  437. li2 = li1;
  438. li1 = t1;
  439. y0 += h;
  440. h *= -1;
  441. break;
  442. case 5: //FXR270
  443. t = vertex_dirs[0];
  444. vertex_dirs[0] = vertex_dirs[1];
  445. vertex_dirs[1] = vertex_dirs[2];
  446. vertex_dirs[2] = vertex_dirs[3];
  447. vertex_dirs[3] = t;
  448. t1 = li0;
  449. li0 = li1;
  450. li1 = li2;
  451. li2 = li3;
  452. li3 = t1;
  453. y0 += h;
  454. h *= -1;
  455. break;
  456. case 6: //FYR90
  457. t = vertex_dirs[0];
  458. vertex_dirs[0] = vertex_dirs[3];
  459. vertex_dirs[3] = vertex_dirs[2];
  460. vertex_dirs[2] = vertex_dirs[1];
  461. vertex_dirs[1] = t;
  462. t1 = li0;
  463. li0 = li3;
  464. li3 = li2;
  465. li2 = li1;
  466. li1 = t1;
  467. x0 += w;
  468. w *= -1;
  469. break;
  470. case 7: //FYR270
  471. t = vertex_dirs[0];
  472. vertex_dirs[0] = vertex_dirs[1];
  473. vertex_dirs[1] = vertex_dirs[2];
  474. vertex_dirs[2] = vertex_dirs[3];
  475. vertex_dirs[3] = t;
  476. t1 = li0;
  477. li0 = li1;
  478. li1 = li2;
  479. li2 = li3;
  480. li3 = t1;
  481. x0 += w;
  482. w *= -1;
  483. break;
  484. case 8: //FX
  485. y0 += h;
  486. h *= -1;
  487. break;
  488. case 9: //FY
  489. x0 += w;
  490. w *= -1;
  491. break;
  492. default:
  493. break;
  494. }
  495. for(u16 i=0; i<4; i++)
  496. {
  497. vertex_pos[i] = v3f(
  498. BS/2*vertex_dirs[i].X,
  499. BS/2*vertex_dirs[i].Y,
  500. BS/2*vertex_dirs[i].Z
  501. );
  502. }
  503. for(u16 i=0; i<4; i++)
  504. {
  505. vertex_pos[i].X *= scale.X;
  506. vertex_pos[i].Y *= scale.Y;
  507. vertex_pos[i].Z *= scale.Z;
  508. vertex_pos[i] += pos;
  509. }
  510. f32 abs_scale = 1.0;
  511. if (scale.X < 0.999 || scale.X > 1.001) abs_scale = scale.X;
  512. else if(scale.Y < 0.999 || scale.Y > 1.001) abs_scale = scale.Y;
  513. else if(scale.Z < 0.999 || scale.Z > 1.001) abs_scale = scale.Z;
  514. v3f normal(dir.X, dir.Y, dir.Z);
  515. u8 alpha = tile.alpha;
  516. face.vertices[0] = video::S3DVertex(vertex_pos[0], normal,
  517. MapBlock_LightColor(alpha, li0, light_source),
  518. core::vector2d<f32>(x0+w*abs_scale, y0+h));
  519. face.vertices[1] = video::S3DVertex(vertex_pos[1], normal,
  520. MapBlock_LightColor(alpha, li1, light_source),
  521. core::vector2d<f32>(x0, y0+h));
  522. face.vertices[2] = video::S3DVertex(vertex_pos[2], normal,
  523. MapBlock_LightColor(alpha, li2, light_source),
  524. core::vector2d<f32>(x0, y0));
  525. face.vertices[3] = video::S3DVertex(vertex_pos[3], normal,
  526. MapBlock_LightColor(alpha, li3, light_source),
  527. core::vector2d<f32>(x0+w*abs_scale, y0));
  528. face.tile = tile;
  529. dest.push_back(face);
  530. }
  531. /*
  532. Nodes make a face if contents differ and solidness differs.
  533. Return value:
  534. 0: No face
  535. 1: Face uses m1's content
  536. 2: Face uses m2's content
  537. equivalent: Whether the blocks share the same face (eg. water and glass)
  538. TODO: Add 3: Both faces drawn with backface culling, remove equivalent
  539. */
  540. static u8 face_contents(content_t m1, content_t m2, bool *equivalent,
  541. INodeDefManager *ndef)
  542. {
  543. *equivalent = false;
  544. if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
  545. return 0;
  546. bool contents_differ = (m1 != m2);
  547. const ContentFeatures &f1 = ndef->get(m1);
  548. const ContentFeatures &f2 = ndef->get(m2);
  549. // Contents don't differ for different forms of same liquid
  550. if(f1.sameLiquid(f2))
  551. contents_differ = false;
  552. u8 c1 = f1.solidness;
  553. u8 c2 = f2.solidness;
  554. bool solidness_differs = (c1 != c2);
  555. bool makes_face = contents_differ && solidness_differs;
  556. if(makes_face == false)
  557. return 0;
  558. if(c1 == 0)
  559. c1 = f1.visual_solidness;
  560. if(c2 == 0)
  561. c2 = f2.visual_solidness;
  562. if(c1 == c2){
  563. *equivalent = true;
  564. // If same solidness, liquid takes precense
  565. if(f1.isLiquid())
  566. return 1;
  567. if(f2.isLiquid())
  568. return 2;
  569. }
  570. if(c1 > c2)
  571. return 1;
  572. else
  573. return 2;
  574. }
  575. /*
  576. Gets nth node tile (0 <= n <= 5).
  577. */
  578. TileSpec getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data)
  579. {
  580. INodeDefManager *ndef = data->m_gamedef->ndef();
  581. TileSpec spec = ndef->get(mn).tiles[tileindex];
  582. // Apply temporary crack
  583. if (p == data->m_crack_pos_relative)
  584. spec.material_flags |= MATERIAL_FLAG_CRACK;
  585. return spec;
  586. }
  587. /*
  588. Gets node tile given a face direction.
  589. */
  590. TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data)
  591. {
  592. INodeDefManager *ndef = data->m_gamedef->ndef();
  593. // Direction must be (1,0,0), (-1,0,0), (0,1,0), (0,-1,0),
  594. // (0,0,1), (0,0,-1) or (0,0,0)
  595. assert(dir.X * dir.X + dir.Y * dir.Y + dir.Z * dir.Z <= 1);
  596. // Convert direction to single integer for table lookup
  597. // 0 = (0,0,0)
  598. // 1 = (1,0,0)
  599. // 2 = (0,1,0)
  600. // 3 = (0,0,1)
  601. // 4 = invalid, treat as (0,0,0)
  602. // 5 = (0,0,-1)
  603. // 6 = (0,-1,0)
  604. // 7 = (-1,0,0)
  605. u8 dir_i = ((dir.X + 2 * dir.Y + 3 * dir.Z) & 7)*2;
  606. // Get rotation for things like chests
  607. u8 facedir = mn.getFaceDir(ndef);
  608. if (facedir > 23)
  609. facedir = 0;
  610. static const u16 dir_to_tile[24 * 16] =
  611. {
  612. // 0 +X +Y +Z -Z -Y -X -> value=tile,rotation
  613. 0,0, 2,0 , 0,0 , 4,0 , 0,0, 5,0 , 1,0 , 3,0 , // rotate around y+ 0 - 3
  614. 0,0, 4,0 , 0,3 , 3,0 , 0,0, 2,0 , 1,1 , 5,0 ,
  615. 0,0, 3,0 , 0,2 , 5,0 , 0,0, 4,0 , 1,2 , 2,0 ,
  616. 0,0, 5,0 , 0,1 , 2,0 , 0,0, 3,0 , 1,3 , 4,0 ,
  617. 0,0, 2,3 , 5,0 , 0,2 , 0,0, 1,0 , 4,2 , 3,1 , // rotate around z+ 4 - 7
  618. 0,0, 4,3 , 2,0 , 0,1 , 0,0, 1,1 , 3,2 , 5,1 ,
  619. 0,0, 3,3 , 4,0 , 0,0 , 0,0, 1,2 , 5,2 , 2,1 ,
  620. 0,0, 5,3 , 3,0 , 0,3 , 0,0, 1,3 , 2,2 , 4,1 ,
  621. 0,0, 2,1 , 4,2 , 1,2 , 0,0, 0,0 , 5,0 , 3,3 , // rotate around z- 8 - 11
  622. 0,0, 4,1 , 3,2 , 1,3 , 0,0, 0,3 , 2,0 , 5,3 ,
  623. 0,0, 3,1 , 5,2 , 1,0 , 0,0, 0,2 , 4,0 , 2,3 ,
  624. 0,0, 5,1 , 2,2 , 1,1 , 0,0, 0,1 , 3,0 , 4,3 ,
  625. 0,0, 0,3 , 3,3 , 4,1 , 0,0, 5,3 , 2,3 , 1,3 , // rotate around x+ 12 - 15
  626. 0,0, 0,2 , 5,3 , 3,1 , 0,0, 2,3 , 4,3 , 1,0 ,
  627. 0,0, 0,1 , 2,3 , 5,1 , 0,0, 4,3 , 3,3 , 1,1 ,
  628. 0,0, 0,0 , 4,3 , 2,1 , 0,0, 3,3 , 5,3 , 1,2 ,
  629. 0,0, 1,1 , 2,1 , 4,3 , 0,0, 5,1 , 3,1 , 0,1 , // rotate around x- 16 - 19
  630. 0,0, 1,2 , 4,1 , 3,3 , 0,0, 2,1 , 5,1 , 0,0 ,
  631. 0,0, 1,3 , 3,1 , 5,3 , 0,0, 4,1 , 2,1 , 0,3 ,
  632. 0,0, 1,0 , 5,1 , 2,3 , 0,0, 3,1 , 4,1 , 0,2 ,
  633. 0,0, 3,2 , 1,2 , 4,2 , 0,0, 5,2 , 0,2 , 2,2 , // rotate around y- 20 - 23
  634. 0,0, 5,2 , 1,3 , 3,2 , 0,0, 2,2 , 0,1 , 4,2 ,
  635. 0,0, 2,2 , 1,0 , 5,2 , 0,0, 4,2 , 0,0 , 3,2 ,
  636. 0,0, 4,2 , 1,1 , 2,2 , 0,0, 3,2 , 0,3 , 5,2
  637. };
  638. u16 tile_index=facedir*16 + dir_i;
  639. TileSpec spec = getNodeTileN(mn, p, dir_to_tile[tile_index], data);
  640. spec.rotation=dir_to_tile[tile_index + 1];
  641. spec.texture = data->m_gamedef->tsrc()->getTexture(spec.texture_id);
  642. return spec;
  643. }
  644. static void getTileInfo(
  645. // Input:
  646. MeshMakeData *data,
  647. v3s16 p,
  648. v3s16 face_dir,
  649. // Output:
  650. bool &makes_face,
  651. v3s16 &p_corrected,
  652. v3s16 &face_dir_corrected,
  653. u16 *lights,
  654. TileSpec &tile,
  655. u8 &light_source
  656. )
  657. {
  658. VoxelManipulator &vmanip = data->m_vmanip;
  659. INodeDefManager *ndef = data->m_gamedef->ndef();
  660. v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
  661. MapNode n0 = vmanip.getNodeNoEx(blockpos_nodes + p);
  662. // Don't even try to get n1 if n0 is already CONTENT_IGNORE
  663. if (n0.getContent() == CONTENT_IGNORE ) {
  664. makes_face = false;
  665. return;
  666. }
  667. MapNode n1 = vmanip.getNodeNoEx(blockpos_nodes + p + face_dir);
  668. // This is hackish
  669. bool equivalent = false;
  670. u8 mf = face_contents(n0.getContent(), n1.getContent(),
  671. &equivalent, ndef);
  672. if(mf == 0)
  673. {
  674. makes_face = false;
  675. return;
  676. }
  677. makes_face = true;
  678. if(mf == 1)
  679. {
  680. tile = getNodeTile(n0, p, face_dir, data);
  681. p_corrected = p;
  682. face_dir_corrected = face_dir;
  683. light_source = ndef->get(n0).light_source;
  684. }
  685. else
  686. {
  687. tile = getNodeTile(n1, p + face_dir, -face_dir, data);
  688. p_corrected = p + face_dir;
  689. face_dir_corrected = -face_dir;
  690. light_source = ndef->get(n1).light_source;
  691. }
  692. // eg. water and glass
  693. if(equivalent)
  694. tile.material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
  695. if(data->m_smooth_lighting == false)
  696. {
  697. lights[0] = lights[1] = lights[2] = lights[3] =
  698. getFaceLight(n0, n1, face_dir, ndef);
  699. }
  700. else
  701. {
  702. v3s16 vertex_dirs[4];
  703. getNodeVertexDirs(face_dir_corrected, vertex_dirs);
  704. for(u16 i=0; i<4; i++)
  705. {
  706. lights[i] = getSmoothLight(
  707. blockpos_nodes + p_corrected,
  708. vertex_dirs[i], data);
  709. }
  710. }
  711. return;
  712. }
  713. /*
  714. startpos:
  715. translate_dir: unit vector with only one of x, y or z
  716. face_dir: unit vector with only one of x, y or z
  717. */
  718. static void updateFastFaceRow(
  719. MeshMakeData *data,
  720. v3s16 startpos,
  721. v3s16 translate_dir,
  722. v3f translate_dir_f,
  723. v3s16 face_dir,
  724. v3f face_dir_f,
  725. std::vector<FastFace> &dest)
  726. {
  727. v3s16 p = startpos;
  728. u16 continuous_tiles_count = 0;
  729. bool makes_face = false;
  730. v3s16 p_corrected;
  731. v3s16 face_dir_corrected;
  732. u16 lights[4] = {0,0,0,0};
  733. TileSpec tile;
  734. u8 light_source = 0;
  735. getTileInfo(data, p, face_dir,
  736. makes_face, p_corrected, face_dir_corrected,
  737. lights, tile, light_source);
  738. for(u16 j=0; j<MAP_BLOCKSIZE; j++)
  739. {
  740. // If tiling can be done, this is set to false in the next step
  741. bool next_is_different = true;
  742. v3s16 p_next;
  743. bool next_makes_face = false;
  744. v3s16 next_p_corrected;
  745. v3s16 next_face_dir_corrected;
  746. u16 next_lights[4] = {0,0,0,0};
  747. TileSpec next_tile;
  748. u8 next_light_source = 0;
  749. // If at last position, there is nothing to compare to and
  750. // the face must be drawn anyway
  751. if(j != MAP_BLOCKSIZE - 1)
  752. {
  753. p_next = p + translate_dir;
  754. getTileInfo(data, p_next, face_dir,
  755. next_makes_face, next_p_corrected,
  756. next_face_dir_corrected, next_lights,
  757. next_tile, next_light_source);
  758. if(next_makes_face == makes_face
  759. && next_p_corrected == p_corrected + translate_dir
  760. && next_face_dir_corrected == face_dir_corrected
  761. && next_lights[0] == lights[0]
  762. && next_lights[1] == lights[1]
  763. && next_lights[2] == lights[2]
  764. && next_lights[3] == lights[3]
  765. && next_tile == tile
  766. && tile.rotation == 0
  767. && next_light_source == light_source)
  768. {
  769. next_is_different = false;
  770. }
  771. else{
  772. /*if(makes_face){
  773. g_profiler->add("Meshgen: diff: next_makes_face != makes_face",
  774. next_makes_face != makes_face ? 1 : 0);
  775. g_profiler->add("Meshgen: diff: n_p_corr != p_corr + t_dir",
  776. (next_p_corrected != p_corrected + translate_dir) ? 1 : 0);
  777. g_profiler->add("Meshgen: diff: next_f_dir_corr != f_dir_corr",
  778. next_face_dir_corrected != face_dir_corrected ? 1 : 0);
  779. g_profiler->add("Meshgen: diff: next_lights[] != lights[]",
  780. (next_lights[0] != lights[0] ||
  781. next_lights[0] != lights[0] ||
  782. next_lights[0] != lights[0] ||
  783. next_lights[0] != lights[0]) ? 1 : 0);
  784. g_profiler->add("Meshgen: diff: !(next_tile == tile)",
  785. !(next_tile == tile) ? 1 : 0);
  786. }*/
  787. }
  788. /*g_profiler->add("Meshgen: Total faces checked", 1);
  789. if(makes_face)
  790. g_profiler->add("Meshgen: Total makes_face checked", 1);*/
  791. } else {
  792. /*if(makes_face)
  793. g_profiler->add("Meshgen: diff: last position", 1);*/
  794. }
  795. continuous_tiles_count++;
  796. if(next_is_different)
  797. {
  798. /*
  799. Create a face if there should be one
  800. */
  801. if(makes_face)
  802. {
  803. // Floating point conversion of the position vector
  804. v3f pf(p_corrected.X, p_corrected.Y, p_corrected.Z);
  805. // Center point of face (kind of)
  806. v3f sp = pf - ((f32)continuous_tiles_count / 2.0 - 0.5) * translate_dir_f;
  807. if(continuous_tiles_count != 1)
  808. sp += translate_dir_f;
  809. v3f scale(1,1,1);
  810. if(translate_dir.X != 0) {
  811. scale.X = continuous_tiles_count;
  812. }
  813. if(translate_dir.Y != 0) {
  814. scale.Y = continuous_tiles_count;
  815. }
  816. if(translate_dir.Z != 0) {
  817. scale.Z = continuous_tiles_count;
  818. }
  819. makeFastFace(tile, lights[0], lights[1], lights[2], lights[3],
  820. sp, face_dir_corrected, scale, light_source,
  821. dest);
  822. g_profiler->avg("Meshgen: faces drawn by tiling", 0);
  823. for(int i = 1; i < continuous_tiles_count; i++){
  824. g_profiler->avg("Meshgen: faces drawn by tiling", 1);
  825. }
  826. }
  827. continuous_tiles_count = 0;
  828. makes_face = next_makes_face;
  829. p_corrected = next_p_corrected;
  830. face_dir_corrected = next_face_dir_corrected;
  831. lights[0] = next_lights[0];
  832. lights[1] = next_lights[1];
  833. lights[2] = next_lights[2];
  834. lights[3] = next_lights[3];
  835. tile = next_tile;
  836. light_source = next_light_source;
  837. }
  838. p = p_next;
  839. }
  840. }
  841. static void updateAllFastFaceRows(MeshMakeData *data,
  842. std::vector<FastFace> &dest)
  843. {
  844. /*
  845. Go through every y,z and get top(y+) faces in rows of x+
  846. */
  847. for(s16 y = 0; y < MAP_BLOCKSIZE; y++) {
  848. for(s16 z = 0; z < MAP_BLOCKSIZE; z++) {
  849. updateFastFaceRow(data,
  850. v3s16(0,y,z),
  851. v3s16(1,0,0), //dir
  852. v3f (1,0,0),
  853. v3s16(0,1,0), //face dir
  854. v3f (0,1,0),
  855. dest);
  856. }
  857. }
  858. /*
  859. Go through every x,y and get right(x+) faces in rows of z+
  860. */
  861. for(s16 x = 0; x < MAP_BLOCKSIZE; x++) {
  862. for(s16 y = 0; y < MAP_BLOCKSIZE; y++) {
  863. updateFastFaceRow(data,
  864. v3s16(x,y,0),
  865. v3s16(0,0,1), //dir
  866. v3f (0,0,1),
  867. v3s16(1,0,0), //face dir
  868. v3f (1,0,0),
  869. dest);
  870. }
  871. }
  872. /*
  873. Go through every y,z and get back(z+) faces in rows of x+
  874. */
  875. for(s16 z = 0; z < MAP_BLOCKSIZE; z++) {
  876. for(s16 y = 0; y < MAP_BLOCKSIZE; y++) {
  877. updateFastFaceRow(data,
  878. v3s16(0,y,z),
  879. v3s16(1,0,0), //dir
  880. v3f (1,0,0),
  881. v3s16(0,0,1), //face dir
  882. v3f (0,0,1),
  883. dest);
  884. }
  885. }
  886. }
  887. /*
  888. MapBlockMesh
  889. */
  890. MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
  891. m_mesh(new scene::SMesh()),
  892. m_gamedef(data->m_gamedef),
  893. m_animation_force_timer(0), // force initial animation
  894. m_last_crack(-1),
  895. m_crack_materials(),
  896. m_highlighted_materials(),
  897. m_last_daynight_ratio((u32) -1),
  898. m_daynight_diffs()
  899. {
  900. m_enable_shaders = g_settings->getBool("enable_shaders");
  901. m_enable_highlighting = g_settings->getBool("enable_node_highlighting");
  902. // 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)
  903. // 24-155ms for MAP_BLOCKSIZE=32 (NOTE: probably outdated)
  904. //TimeTaker timer1("MapBlockMesh()");
  905. std::vector<FastFace> fastfaces_new;
  906. /*
  907. We are including the faces of the trailing edges of the block.
  908. This means that when something changes, the caller must
  909. also update the meshes of the blocks at the leading edges.
  910. NOTE: This is the slowest part of this method.
  911. */
  912. {
  913. // 4-23ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)
  914. //TimeTaker timer2("updateAllFastFaceRows()");
  915. updateAllFastFaceRows(data, fastfaces_new);
  916. }
  917. // End of slow part
  918. /*
  919. Convert FastFaces to MeshCollector
  920. */
  921. MeshCollector collector;
  922. {
  923. // avg 0ms (100ms spikes when loading textures the first time)
  924. // (NOTE: probably outdated)
  925. //TimeTaker timer2("MeshCollector building");
  926. for(u32 i=0; i<fastfaces_new.size(); i++)
  927. {
  928. FastFace &f = fastfaces_new[i];
  929. const u16 indices[] = {0,1,2,2,3,0};
  930. const u16 indices_alternate[] = {0,1,3,2,3,1};
  931. if(f.tile.texture == NULL)
  932. continue;
  933. const u16 *indices_p = indices;
  934. /*
  935. Revert triangles for nicer looking gradient if vertices
  936. 1 and 3 have same color or 0 and 2 have different color.
  937. getRed() is the day color.
  938. */
  939. if(f.vertices[0].Color.getRed() != f.vertices[2].Color.getRed()
  940. || f.vertices[1].Color.getRed() == f.vertices[3].Color.getRed())
  941. indices_p = indices_alternate;
  942. collector.append(f.tile, f.vertices, 4, indices_p, 6);
  943. }
  944. }
  945. /*
  946. Add special graphics:
  947. - torches
  948. - flowing water
  949. - fences
  950. - whatever
  951. */
  952. mapblock_mesh_generate_special(data, collector);
  953. m_highlight_mesh_color = data->m_highlight_mesh_color;
  954. /*
  955. Convert MeshCollector to SMesh
  956. */
  957. ITextureSource *tsrc = m_gamedef->tsrc();
  958. IShaderSource *shdrsrc = m_gamedef->getShaderSource();
  959. for(u32 i = 0; i < collector.prebuffers.size(); i++)
  960. {
  961. PreMeshBuffer &p = collector.prebuffers[i];
  962. // Generate animation data
  963. // - Cracks
  964. if(p.tile.material_flags & MATERIAL_FLAG_CRACK)
  965. {
  966. // Find the texture name plus ^[crack:N:
  967. std::ostringstream os(std::ios::binary);
  968. os<<tsrc->getTextureName(p.tile.texture_id)<<"^[crack";
  969. if(p.tile.material_flags & MATERIAL_FLAG_CRACK_OVERLAY)
  970. os<<"o"; // use ^[cracko
  971. os<<":"<<(u32)p.tile.animation_frame_count<<":";
  972. m_crack_materials.insert(std::make_pair(i, os.str()));
  973. // Replace tile texture with the cracked one
  974. p.tile.texture = tsrc->getTexture(
  975. os.str()+"0",
  976. &p.tile.texture_id);
  977. }
  978. // - Texture animation
  979. if(p.tile.material_flags & MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
  980. {
  981. // Add to MapBlockMesh in order to animate these tiles
  982. m_animation_tiles[i] = p.tile;
  983. m_animation_frames[i] = 0;
  984. if(g_settings->getBool("desynchronize_mapblock_texture_animation")){
  985. // Get starting position from noise
  986. m_animation_frame_offsets[i] = 100000 * (2.0 + noise3d(
  987. data->m_blockpos.X, data->m_blockpos.Y,
  988. data->m_blockpos.Z, 0));
  989. } else {
  990. // Play all synchronized
  991. m_animation_frame_offsets[i] = 0;
  992. }
  993. // Replace tile texture with the first animation frame
  994. FrameSpec animation_frame = p.tile.frames.find(0)->second;
  995. p.tile.texture = animation_frame.texture;
  996. }
  997. if(m_enable_highlighting && p.tile.material_flags & MATERIAL_FLAG_HIGHLIGHTED)
  998. m_highlighted_materials.push_back(i);
  999. for(u32 j = 0; j < p.vertices.size(); j++)
  1000. {
  1001. // Note applyFacesShading second parameter is precalculated sqrt
  1002. // value for speed improvement
  1003. // Skip it for lightsources and top faces.
  1004. video::SColor &vc = p.vertices[j].Color;
  1005. if (!vc.getBlue()) {
  1006. if (p.vertices[j].Normal.Y < -0.5) {
  1007. applyFacesShading (vc, 0.447213);
  1008. } else if (p.vertices[j].Normal.X > 0.5) {
  1009. applyFacesShading (vc, 0.670820);
  1010. } else if (p.vertices[j].Normal.X < -0.5) {
  1011. applyFacesShading (vc, 0.670820);
  1012. } else if (p.vertices[j].Normal.Z > 0.5) {
  1013. applyFacesShading (vc, 0.836660);
  1014. } else if (p.vertices[j].Normal.Z < -0.5) {
  1015. applyFacesShading (vc, 0.836660);
  1016. }
  1017. }
  1018. // - Classic lighting
  1019. // Set initial real color and store for later updates
  1020. u8 day = vc.getRed();
  1021. u8 night = vc.getGreen();
  1022. finalColorBlend(vc, day, night, 1000);
  1023. m_daynight_diffs[i][j] = std::make_pair(day, night);
  1024. }
  1025. // Create material
  1026. video::SMaterial material;
  1027. material.setFlag(video::EMF_LIGHTING, false);
  1028. material.setFlag(video::EMF_BACK_FACE_CULLING, true);
  1029. material.setFlag(video::EMF_BILINEAR_FILTER, false);
  1030. material.setFlag(video::EMF_FOG_ENABLE, true);
  1031. material.setTexture(0, p.tile.texture);
  1032. if (p.tile.material_flags & MATERIAL_FLAG_HIGHLIGHTED) {
  1033. material.MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
  1034. } else {
  1035. if (m_enable_shaders) {
  1036. material.MaterialType = shdrsrc->getShaderInfo(p.tile.shader_id).material;
  1037. p.tile.applyMaterialOptionsWithShaders(material);
  1038. if (p.tile.normal_texture) {
  1039. material.setTexture(1, p.tile.normal_texture);
  1040. material.setTexture(2, tsrc->getTexture("enable_img.png"));
  1041. } else {
  1042. material.setTexture(2, tsrc->getTexture("disable_img.png"));
  1043. }
  1044. } else {
  1045. p.tile.applyMaterialOptions(material);
  1046. }
  1047. }
  1048. // Create meshbuffer
  1049. // This is a "Standard MeshBuffer",
  1050. // it's a typedeffed CMeshBuffer<video::S3DVertex>
  1051. scene::SMeshBuffer *buf = new scene::SMeshBuffer();
  1052. // Set material
  1053. buf->Material = material;
  1054. // Add to mesh
  1055. m_mesh->addMeshBuffer(buf);
  1056. // Mesh grabbed it
  1057. buf->drop();
  1058. buf->append(&p.vertices[0], p.vertices.size(),
  1059. &p.indices[0], p.indices.size());
  1060. }
  1061. m_camera_offset = camera_offset;
  1062. /*
  1063. Do some stuff to the mesh
  1064. */
  1065. translateMesh(m_mesh, intToFloat(data->m_blockpos * MAP_BLOCKSIZE - camera_offset, BS));
  1066. if(m_mesh)
  1067. {
  1068. #if 0
  1069. // Usually 1-700 faces and 1-7 materials
  1070. std::cout<<"Updated MapBlock has "<<fastfaces_new.size()<<" faces "
  1071. <<"and uses "<<m_mesh->getMeshBufferCount()
  1072. <<" materials (meshbuffers)"<<std::endl;
  1073. #endif
  1074. // Use VBO for mesh (this just would set this for ever buffer)
  1075. // This will lead to infinite memory usage because or irrlicht.
  1076. //m_mesh->setHardwareMappingHint(scene::EHM_STATIC);
  1077. /*
  1078. NOTE: If that is enabled, some kind of a queue to the main
  1079. thread should be made which would call irrlicht to delete
  1080. the hardware buffer and then delete the mesh
  1081. */
  1082. }
  1083. //std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl;
  1084. // Check if animation is required for this mesh
  1085. m_has_animation =
  1086. !m_crack_materials.empty() ||
  1087. !m_daynight_diffs.empty() ||
  1088. !m_animation_tiles.empty() ||
  1089. !m_highlighted_materials.empty();
  1090. }
  1091. MapBlockMesh::~MapBlockMesh()
  1092. {
  1093. m_mesh->drop();
  1094. m_mesh = NULL;
  1095. }
  1096. bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_ratio)
  1097. {
  1098. if(!m_has_animation)
  1099. {
  1100. m_animation_force_timer = 100000;
  1101. return false;
  1102. }
  1103. m_animation_force_timer = myrand_range(5, 100);
  1104. // Cracks
  1105. if(crack != m_last_crack)
  1106. {
  1107. for(std::map<u32, std::string>::iterator
  1108. i = m_crack_materials.begin();
  1109. i != m_crack_materials.end(); i++)
  1110. {
  1111. scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first);
  1112. std::string basename = i->second;
  1113. // Create new texture name from original
  1114. ITextureSource *tsrc = m_gamedef->getTextureSource();
  1115. std::ostringstream os;
  1116. os<<basename<<crack;
  1117. u32 new_texture_id = 0;
  1118. video::ITexture *new_texture =
  1119. tsrc->getTexture(os.str(), &new_texture_id);
  1120. buf->getMaterial().setTexture(0, new_texture);
  1121. // If the current material is also animated,
  1122. // update animation info
  1123. std::map<u32, TileSpec>::iterator anim_iter =
  1124. m_animation_tiles.find(i->first);
  1125. if(anim_iter != m_animation_tiles.end()){
  1126. TileSpec &tile = anim_iter->second;
  1127. tile.texture = new_texture;
  1128. tile.texture_id = new_texture_id;
  1129. // force animation update
  1130. m_animation_frames[i->first] = -1;
  1131. }
  1132. }
  1133. m_last_crack = crack;
  1134. }
  1135. // Texture animation
  1136. for(std::map<u32, TileSpec>::iterator
  1137. i = m_animation_tiles.begin();
  1138. i != m_animation_tiles.end(); i++)
  1139. {
  1140. const TileSpec &tile = i->second;
  1141. // Figure out current frame
  1142. int frameoffset = m_animation_frame_offsets[i->first];
  1143. int frame = (int)(time * 1000 / tile.animation_frame_length_ms
  1144. + frameoffset) % tile.animation_frame_count;
  1145. // If frame doesn't change, skip
  1146. if(frame == m_animation_frames[i->first])
  1147. continue;
  1148. m_animation_frames[i->first] = frame;
  1149. scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first);
  1150. ITextureSource *tsrc = m_gamedef->getTextureSource();
  1151. FrameSpec animation_frame = tile.frames.find(frame)->second;
  1152. buf->getMaterial().setTexture(0, animation_frame.texture);
  1153. if (m_enable_shaders) {
  1154. if (animation_frame.normal_texture) {
  1155. buf->getMaterial().setTexture(1, animation_frame.normal_texture);
  1156. buf->getMaterial().setTexture(2, tsrc->getTexture("enable_img.png"));
  1157. } else {
  1158. buf->getMaterial().setTexture(2, tsrc->getTexture("disable_img.png"));
  1159. }
  1160. }
  1161. }
  1162. // Day-night transition
  1163. if(daynight_ratio != m_last_daynight_ratio)
  1164. {
  1165. for(std::map<u32, std::map<u32, std::pair<u8, u8> > >::iterator
  1166. i = m_daynight_diffs.begin();
  1167. i != m_daynight_diffs.end(); i++)
  1168. {
  1169. scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first);
  1170. video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
  1171. for(std::map<u32, std::pair<u8, u8 > >::iterator
  1172. j = i->second.begin();
  1173. j != i->second.end(); j++)
  1174. {
  1175. u32 vertexIndex = j->first;
  1176. u8 day = j->second.first;
  1177. u8 night = j->second.second;
  1178. finalColorBlend(vertices[vertexIndex].Color,
  1179. day, night, daynight_ratio);
  1180. }
  1181. }
  1182. m_last_daynight_ratio = daynight_ratio;
  1183. }
  1184. // Node highlighting
  1185. if (m_enable_highlighting) {
  1186. u8 day = m_highlight_mesh_color.getRed();
  1187. u8 night = m_highlight_mesh_color.getGreen();
  1188. video::SColor hc;
  1189. finalColorBlend(hc, day, night, daynight_ratio);
  1190. float sin_r = 0.07 * sin(1.5 * time);
  1191. float sin_g = 0.07 * sin(1.5 * time + irr::core::PI * 0.5);
  1192. float sin_b = 0.07 * sin(1.5 * time + irr::core::PI);
  1193. hc.setRed(core::clamp(core::round32(hc.getRed() * (0.8 + sin_r)), 0, 255));
  1194. hc.setGreen(core::clamp(core::round32(hc.getGreen() * (0.8 + sin_g)), 0, 255));
  1195. hc.setBlue(core::clamp(core::round32(hc.getBlue() * (0.8 + sin_b)), 0, 255));
  1196. for(std::list<u32>::iterator
  1197. i = m_highlighted_materials.begin();
  1198. i != m_highlighted_materials.end(); i++)
  1199. {
  1200. scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(*i);
  1201. video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
  1202. for (u32 j = 0; j < buf->getVertexCount() ;j++)
  1203. vertices[j].Color = hc;
  1204. }
  1205. }
  1206. return true;
  1207. }
  1208. void MapBlockMesh::updateCameraOffset(v3s16 camera_offset)
  1209. {
  1210. if (camera_offset != m_camera_offset) {
  1211. translateMesh(m_mesh, intToFloat(m_camera_offset-camera_offset, BS));
  1212. m_camera_offset = camera_offset;
  1213. }
  1214. }
  1215. /*
  1216. MeshCollector
  1217. */
  1218. void MeshCollector::append(const TileSpec &tile,
  1219. const video::S3DVertex *vertices, u32 numVertices,
  1220. const u16 *indices, u32 numIndices)
  1221. {
  1222. if(numIndices > 65535)
  1223. {
  1224. dstream<<"FIXME: MeshCollector::append() called with numIndices="<<numIndices<<" (limit 65535)"<<std::endl;
  1225. return;
  1226. }
  1227. PreMeshBuffer *p = NULL;
  1228. for(u32 i=0; i<prebuffers.size(); i++)
  1229. {
  1230. PreMeshBuffer &pp = prebuffers[i];
  1231. if(pp.tile != tile)
  1232. continue;
  1233. if(pp.indices.size() + numIndices > 65535)
  1234. continue;
  1235. p = &pp;
  1236. break;
  1237. }
  1238. if(p == NULL)
  1239. {
  1240. PreMeshBuffer pp;
  1241. pp.tile = tile;
  1242. prebuffers.push_back(pp);
  1243. p = &prebuffers[prebuffers.size()-1];
  1244. }
  1245. u32 vertex_count = p->vertices.size();
  1246. for(u32 i=0; i<numIndices; i++)
  1247. {
  1248. u32 j = indices[i] + vertex_count;
  1249. p->indices.push_back(j);
  1250. }
  1251. for(u32 i=0; i<numVertices; i++)
  1252. {
  1253. p->vertices.push_back(vertices[i]);
  1254. }
  1255. }