clientmap.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 "clientmap.h"
  17. #include "client.h"
  18. #include "mapblock_mesh.h"
  19. #include <IMaterialRenderer.h>
  20. #include <matrix4.h>
  21. #include "log.h"
  22. #include "mapsector.h"
  23. #include "nodedef.h"
  24. #include "mapblock.h"
  25. #include "profiler.h"
  26. #include "settings.h"
  27. #include "camera.h" // CameraModes
  28. #include "util/basic_macros.h"
  29. #include <algorithm>
  30. ClientMap::ClientMap(
  31. Client *client,
  32. MapDrawControl &control,
  33. scene::ISceneNode* parent,
  34. scene::ISceneManager* mgr,
  35. s32 id
  36. ):
  37. Map(dout_client, client),
  38. scene::ISceneNode(parent, mgr, id),
  39. m_client(client),
  40. m_control(control),
  41. m_camera_position(0,0,0),
  42. m_camera_direction(0,0,1),
  43. m_camera_fov(M_PI)
  44. {
  45. m_box = aabb3f(-BS*1000000,-BS*1000000,-BS*1000000,
  46. BS*1000000,BS*1000000,BS*1000000);
  47. /* TODO: Add a callback function so these can be updated when a setting
  48. * changes. At this point in time it doesn't matter (e.g. /set
  49. * is documented to change server settings only)
  50. *
  51. * TODO: Local caching of settings is not optimal and should at some stage
  52. * be updated to use a global settings object for getting thse values
  53. * (as opposed to the this local caching). This can be addressed in
  54. * a later release.
  55. */
  56. m_cache_trilinear_filter = g_settings->getBool("trilinear_filter");
  57. m_cache_bilinear_filter = g_settings->getBool("bilinear_filter");
  58. m_cache_anistropic_filter = g_settings->getBool("anisotropic_filter");
  59. }
  60. ClientMap::~ClientMap()
  61. {
  62. }
  63. MapSector * ClientMap::emergeSector(v2s16 p2d)
  64. {
  65. DSTACK(FUNCTION_NAME);
  66. // Check that it doesn't exist already
  67. try{
  68. return getSectorNoGenerate(p2d);
  69. }
  70. catch(InvalidPositionException &e)
  71. {
  72. }
  73. // Create a sector
  74. ClientMapSector *sector = new ClientMapSector(this, p2d, m_gamedef);
  75. {
  76. //MutexAutoLock lock(m_sector_mutex); // Bulk comment-out
  77. m_sectors[p2d] = sector;
  78. }
  79. return sector;
  80. }
  81. void ClientMap::OnRegisterSceneNode()
  82. {
  83. if(IsVisible)
  84. {
  85. SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
  86. SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
  87. }
  88. ISceneNode::OnRegisterSceneNode();
  89. }
  90. void ClientMap::getBlocksInViewRange(v3s16 cam_pos_nodes,
  91. v3s16 *p_blocks_min, v3s16 *p_blocks_max)
  92. {
  93. v3s16 box_nodes_d = m_control.wanted_range * v3s16(1, 1, 1);
  94. // Define p_nodes_min/max as v3s32 because 'cam_pos_nodes -/+ box_nodes_d'
  95. // can exceed the range of v3s16 when a large view range is used near the
  96. // world edges.
  97. v3s32 p_nodes_min(
  98. cam_pos_nodes.X - box_nodes_d.X,
  99. cam_pos_nodes.Y - box_nodes_d.Y,
  100. cam_pos_nodes.Z - box_nodes_d.Z);
  101. v3s32 p_nodes_max(
  102. cam_pos_nodes.X + box_nodes_d.X,
  103. cam_pos_nodes.Y + box_nodes_d.Y,
  104. cam_pos_nodes.Z + box_nodes_d.Z);
  105. // Take a fair amount as we will be dropping more out later
  106. // Umm... these additions are a bit strange but they are needed.
  107. *p_blocks_min = v3s16(
  108. p_nodes_min.X / MAP_BLOCKSIZE - 3,
  109. p_nodes_min.Y / MAP_BLOCKSIZE - 3,
  110. p_nodes_min.Z / MAP_BLOCKSIZE - 3);
  111. *p_blocks_max = v3s16(
  112. p_nodes_max.X / MAP_BLOCKSIZE + 1,
  113. p_nodes_max.Y / MAP_BLOCKSIZE + 1,
  114. p_nodes_max.Z / MAP_BLOCKSIZE + 1);
  115. }
  116. void ClientMap::updateDrawList(video::IVideoDriver* driver)
  117. {
  118. ScopeProfiler sp(g_profiler, "CM::updateDrawList()", SPT_AVG);
  119. g_profiler->add("CM::updateDrawList() count", 1);
  120. for (std::map<v3s16, MapBlock*>::iterator i = m_drawlist.begin();
  121. i != m_drawlist.end(); ++i) {
  122. MapBlock *block = i->second;
  123. block->refDrop();
  124. }
  125. m_drawlist.clear();
  126. v3f camera_position = m_camera_position;
  127. v3f camera_direction = m_camera_direction;
  128. f32 camera_fov = m_camera_fov;
  129. // Use a higher fov to accomodate faster camera movements.
  130. // Blocks are cropped better when they are drawn.
  131. // Or maybe they aren't? Well whatever.
  132. camera_fov *= 1.2;
  133. v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
  134. v3s16 p_blocks_min;
  135. v3s16 p_blocks_max;
  136. getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
  137. // Number of blocks in rendering range
  138. u32 blocks_in_range = 0;
  139. // Number of blocks occlusion culled
  140. u32 blocks_occlusion_culled = 0;
  141. // Number of blocks in rendering range but don't have a mesh
  142. u32 blocks_in_range_without_mesh = 0;
  143. // Blocks that had mesh that would have been drawn according to
  144. // rendering range (if max blocks limit didn't kick in)
  145. u32 blocks_would_have_drawn = 0;
  146. // Blocks that were drawn and had a mesh
  147. u32 blocks_drawn = 0;
  148. // Blocks which had a corresponding meshbuffer for this pass
  149. //u32 blocks_had_pass_meshbuf = 0;
  150. // Blocks from which stuff was actually drawn
  151. //u32 blocks_without_stuff = 0;
  152. // Distance to farthest drawn block
  153. float farthest_drawn = 0;
  154. // No occlusion culling when free_move is on and camera is
  155. // inside ground
  156. bool occlusion_culling_enabled = true;
  157. if (g_settings->getBool("free_move")) {
  158. MapNode n = getNodeNoEx(cam_pos_nodes);
  159. if (n.getContent() == CONTENT_IGNORE ||
  160. m_nodedef->get(n).solidness == 2)
  161. occlusion_culling_enabled = false;
  162. }
  163. for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
  164. si != m_sectors.end(); ++si) {
  165. MapSector *sector = si->second;
  166. v2s16 sp = sector->getPos();
  167. if (m_control.range_all == false) {
  168. if (sp.X < p_blocks_min.X || sp.X > p_blocks_max.X ||
  169. sp.Y < p_blocks_min.Z || sp.Y > p_blocks_max.Z)
  170. continue;
  171. }
  172. MapBlockVect sectorblocks;
  173. sector->getBlocks(sectorblocks);
  174. /*
  175. Loop through blocks in sector
  176. */
  177. u32 sector_blocks_drawn = 0;
  178. for (MapBlockVect::iterator i = sectorblocks.begin();
  179. i != sectorblocks.end(); ++i) {
  180. MapBlock *block = *i;
  181. /*
  182. Compare block position to camera position, skip
  183. if not seen on display
  184. */
  185. if (block->mesh)
  186. block->mesh->updateCameraOffset(m_camera_offset);
  187. float range = 100000 * BS;
  188. if (!m_control.range_all)
  189. range = m_control.wanted_range * BS;
  190. float d = 0.0;
  191. if (!isBlockInSight(block->getPos(), camera_position,
  192. camera_direction, camera_fov, range, &d))
  193. continue;
  194. blocks_in_range++;
  195. /*
  196. Ignore if mesh doesn't exist
  197. */
  198. if (!block->mesh) {
  199. blocks_in_range_without_mesh++;
  200. continue;
  201. }
  202. /*
  203. Occlusion culling
  204. */
  205. if (occlusion_culling_enabled && isBlockOccluded(block, cam_pos_nodes)) {
  206. blocks_occlusion_culled++;
  207. continue;
  208. }
  209. // This block is in range. Reset usage timer.
  210. block->resetUsageTimer();
  211. // Limit block count in case of a sudden increase
  212. blocks_would_have_drawn++;
  213. if (blocks_drawn >= m_control.wanted_max_blocks &&
  214. !m_control.range_all &&
  215. d > m_control.wanted_range * BS)
  216. continue;
  217. // Add to set
  218. block->refGrab();
  219. m_drawlist[block->getPos()] = block;
  220. sector_blocks_drawn++;
  221. blocks_drawn++;
  222. if (d / BS > farthest_drawn)
  223. farthest_drawn = d / BS;
  224. } // foreach sectorblocks
  225. if (sector_blocks_drawn != 0)
  226. m_last_drawn_sectors.insert(sp);
  227. }
  228. m_control.blocks_would_have_drawn = blocks_would_have_drawn;
  229. m_control.blocks_drawn = blocks_drawn;
  230. m_control.farthest_drawn = farthest_drawn;
  231. g_profiler->avg("CM: blocks in range", blocks_in_range);
  232. g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled);
  233. if (blocks_in_range != 0)
  234. g_profiler->avg("CM: blocks in range without mesh (frac)",
  235. (float)blocks_in_range_without_mesh / blocks_in_range);
  236. g_profiler->avg("CM: blocks drawn", blocks_drawn);
  237. g_profiler->avg("CM: farthest drawn", farthest_drawn);
  238. g_profiler->avg("CM: wanted max blocks", m_control.wanted_max_blocks);
  239. }
  240. struct MeshBufList
  241. {
  242. /*!
  243. * Specifies in which layer the list is.
  244. * All lists which are in a lower layer are rendered before this list.
  245. */
  246. u8 layer;
  247. video::SMaterial m;
  248. std::vector<scene::IMeshBuffer*> bufs;
  249. };
  250. struct MeshBufListList
  251. {
  252. std::vector<MeshBufList> lists;
  253. void clear()
  254. {
  255. lists.clear();
  256. }
  257. void add(scene::IMeshBuffer *buf, u8 layer)
  258. {
  259. const video::SMaterial &m = buf->getMaterial();
  260. for(std::vector<MeshBufList>::iterator i = lists.begin();
  261. i != lists.end(); ++i){
  262. MeshBufList &l = *i;
  263. // comparing a full material is quite expensive so we don't do it if
  264. // not even first texture is equal
  265. if (l.m.TextureLayer[0].Texture != m.TextureLayer[0].Texture)
  266. continue;
  267. if(l.layer != layer)
  268. continue;
  269. if (l.m == m) {
  270. l.bufs.push_back(buf);
  271. return;
  272. }
  273. }
  274. MeshBufList l;
  275. l.layer = layer;
  276. l.m = m;
  277. l.bufs.push_back(buf);
  278. lists.push_back(l);
  279. }
  280. };
  281. void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
  282. {
  283. DSTACK(FUNCTION_NAME);
  284. bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
  285. std::string prefix;
  286. if (pass == scene::ESNRP_SOLID)
  287. prefix = "CM: solid: ";
  288. else
  289. prefix = "CM: transparent: ";
  290. /*
  291. This is called two times per frame, reset on the non-transparent one
  292. */
  293. if (pass == scene::ESNRP_SOLID)
  294. m_last_drawn_sectors.clear();
  295. /*
  296. Get time for measuring timeout.
  297. Measuring time is very useful for long delays when the
  298. machine is swapping a lot.
  299. */
  300. int time1 = time(0);
  301. /*
  302. Get animation parameters
  303. */
  304. float animation_time = m_client->getAnimationTime();
  305. int crack = m_client->getCrackLevel();
  306. u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
  307. v3f camera_position = m_camera_position;
  308. v3f camera_direction = m_camera_direction;
  309. f32 camera_fov = m_camera_fov;
  310. /*
  311. Get all blocks and draw all visible ones
  312. */
  313. u32 vertex_count = 0;
  314. u32 meshbuffer_count = 0;
  315. // For limiting number of mesh animations per frame
  316. u32 mesh_animate_count = 0;
  317. u32 mesh_animate_count_far = 0;
  318. // Blocks that were drawn and had a mesh
  319. u32 blocks_drawn = 0;
  320. // Blocks which had a corresponding meshbuffer for this pass
  321. u32 blocks_had_pass_meshbuf = 0;
  322. // Blocks from which stuff was actually drawn
  323. u32 blocks_without_stuff = 0;
  324. /*
  325. Draw the selected MapBlocks
  326. */
  327. {
  328. ScopeProfiler sp(g_profiler, prefix + "drawing blocks", SPT_AVG);
  329. MeshBufListList drawbufs;
  330. for (std::map<v3s16, MapBlock*>::iterator i = m_drawlist.begin();
  331. i != m_drawlist.end(); ++i) {
  332. MapBlock *block = i->second;
  333. // If the mesh of the block happened to get deleted, ignore it
  334. if (!block->mesh)
  335. continue;
  336. float d = 0.0;
  337. if (!isBlockInSight(block->getPos(), camera_position,
  338. camera_direction, camera_fov, 100000 * BS, &d))
  339. continue;
  340. // Mesh animation
  341. if (pass == scene::ESNRP_SOLID) {
  342. //MutexAutoLock lock(block->mesh_mutex);
  343. MapBlockMesh *mapBlockMesh = block->mesh;
  344. assert(mapBlockMesh);
  345. // Pretty random but this should work somewhat nicely
  346. bool faraway = d >= BS * 50;
  347. //bool faraway = d >= m_control.wanted_range * BS;
  348. if (mapBlockMesh->isAnimationForced() || !faraway ||
  349. mesh_animate_count_far < (m_control.range_all ? 200 : 50)) {
  350. bool animated = mapBlockMesh->animate(faraway, animation_time,
  351. crack, daynight_ratio);
  352. if (animated)
  353. mesh_animate_count++;
  354. if (animated && faraway)
  355. mesh_animate_count_far++;
  356. } else {
  357. mapBlockMesh->decreaseAnimationForceTimer();
  358. }
  359. }
  360. /*
  361. Get the meshbuffers of the block
  362. */
  363. {
  364. //MutexAutoLock lock(block->mesh_mutex);
  365. MapBlockMesh *mapBlockMesh = block->mesh;
  366. assert(mapBlockMesh);
  367. for (int layer = 0; layer < MAX_TILE_LAYERS; layer++) {
  368. scene::IMesh *mesh = mapBlockMesh->getMesh(layer);
  369. assert(mesh);
  370. u32 c = mesh->getMeshBufferCount();
  371. for (u32 i = 0; i < c; i++) {
  372. scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
  373. video::SMaterial& material = buf->getMaterial();
  374. video::IMaterialRenderer* rnd =
  375. driver->getMaterialRenderer(material.MaterialType);
  376. bool transparent = (rnd && rnd->isTransparent());
  377. if (transparent == is_transparent_pass) {
  378. if (buf->getVertexCount() == 0)
  379. errorstream << "Block [" << analyze_block(block)
  380. << "] contains an empty meshbuf" << std::endl;
  381. material.setFlag(video::EMF_TRILINEAR_FILTER,
  382. m_cache_trilinear_filter);
  383. material.setFlag(video::EMF_BILINEAR_FILTER,
  384. m_cache_bilinear_filter);
  385. material.setFlag(video::EMF_ANISOTROPIC_FILTER,
  386. m_cache_anistropic_filter);
  387. material.setFlag(video::EMF_WIREFRAME,
  388. m_control.show_wireframe);
  389. drawbufs.add(buf, layer);
  390. }
  391. }
  392. }
  393. }
  394. }
  395. std::vector<MeshBufList> &lists = drawbufs.lists;
  396. int timecheck_counter = 0;
  397. for (std::vector<MeshBufList>::iterator i = lists.begin();
  398. i != lists.end(); ++i) {
  399. timecheck_counter++;
  400. if (timecheck_counter > 50) {
  401. timecheck_counter = 0;
  402. int time2 = time(0);
  403. if (time2 > time1 + 4) {
  404. infostream << "ClientMap::renderMap(): "
  405. "Rendering takes ages, returning."
  406. << std::endl;
  407. return;
  408. }
  409. }
  410. MeshBufList &list = *i;
  411. driver->setMaterial(list.m);
  412. for (std::vector<scene::IMeshBuffer*>::iterator j = list.bufs.begin();
  413. j != list.bufs.end(); ++j) {
  414. scene::IMeshBuffer *buf = *j;
  415. driver->drawMeshBuffer(buf);
  416. vertex_count += buf->getVertexCount();
  417. meshbuffer_count++;
  418. }
  419. }
  420. } // ScopeProfiler
  421. // Log only on solid pass because values are the same
  422. if (pass == scene::ESNRP_SOLID) {
  423. g_profiler->avg("CM: animated meshes", mesh_animate_count);
  424. g_profiler->avg("CM: animated meshes (far)", mesh_animate_count_far);
  425. }
  426. g_profiler->avg(prefix + "vertices drawn", vertex_count);
  427. if (blocks_had_pass_meshbuf != 0)
  428. g_profiler->avg(prefix + "meshbuffers per block",
  429. (float)meshbuffer_count / (float)blocks_had_pass_meshbuf);
  430. if (blocks_drawn != 0)
  431. g_profiler->avg(prefix + "empty blocks (frac)",
  432. (float)blocks_without_stuff / blocks_drawn);
  433. /*infostream<<"renderMap(): is_transparent_pass="<<is_transparent_pass
  434. <<", rendered "<<vertex_count<<" vertices."<<std::endl;*/
  435. }
  436. static bool getVisibleBrightness(Map *map, v3f p0, v3f dir, float step,
  437. float step_multiplier, float start_distance, float end_distance,
  438. INodeDefManager *ndef, u32 daylight_factor, float sunlight_min_d,
  439. int *result, bool *sunlight_seen)
  440. {
  441. int brightness_sum = 0;
  442. int brightness_count = 0;
  443. float distance = start_distance;
  444. dir.normalize();
  445. v3f pf = p0;
  446. pf += dir * distance;
  447. int noncount = 0;
  448. bool nonlight_seen = false;
  449. bool allow_allowing_non_sunlight_propagates = false;
  450. bool allow_non_sunlight_propagates = false;
  451. // Check content nearly at camera position
  452. {
  453. v3s16 p = floatToInt(p0 /*+ dir * 3*BS*/, BS);
  454. MapNode n = map->getNodeNoEx(p);
  455. if(ndef->get(n).param_type == CPT_LIGHT &&
  456. !ndef->get(n).sunlight_propagates)
  457. allow_allowing_non_sunlight_propagates = true;
  458. }
  459. // If would start at CONTENT_IGNORE, start closer
  460. {
  461. v3s16 p = floatToInt(pf, BS);
  462. MapNode n = map->getNodeNoEx(p);
  463. if(n.getContent() == CONTENT_IGNORE){
  464. float newd = 2*BS;
  465. pf = p0 + dir * 2*newd;
  466. distance = newd;
  467. sunlight_min_d = 0;
  468. }
  469. }
  470. for(int i=0; distance < end_distance; i++){
  471. pf += dir * step;
  472. distance += step;
  473. step *= step_multiplier;
  474. v3s16 p = floatToInt(pf, BS);
  475. MapNode n = map->getNodeNoEx(p);
  476. if(allow_allowing_non_sunlight_propagates && i == 0 &&
  477. ndef->get(n).param_type == CPT_LIGHT &&
  478. !ndef->get(n).sunlight_propagates){
  479. allow_non_sunlight_propagates = true;
  480. }
  481. if(ndef->get(n).param_type != CPT_LIGHT ||
  482. (!ndef->get(n).sunlight_propagates &&
  483. !allow_non_sunlight_propagates)){
  484. nonlight_seen = true;
  485. noncount++;
  486. if(noncount >= 4)
  487. break;
  488. continue;
  489. }
  490. if(distance >= sunlight_min_d && *sunlight_seen == false
  491. && nonlight_seen == false)
  492. if(n.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN)
  493. *sunlight_seen = true;
  494. noncount = 0;
  495. brightness_sum += decode_light(n.getLightBlend(daylight_factor, ndef));
  496. brightness_count++;
  497. }
  498. *result = 0;
  499. if(brightness_count == 0)
  500. return false;
  501. *result = brightness_sum / brightness_count;
  502. /*std::cerr<<"Sampled "<<brightness_count<<" points; result="
  503. <<(*result)<<std::endl;*/
  504. return true;
  505. }
  506. int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
  507. int oldvalue, bool *sunlight_seen_result)
  508. {
  509. const bool debugprint = false;
  510. static v3f z_directions[50] = {
  511. v3f(-100, 0, 0)
  512. };
  513. static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = {
  514. -1000,
  515. };
  516. if(z_directions[0].X < -99){
  517. for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
  518. z_directions[i] = v3f(
  519. 0.01 * myrand_range(-100, 100),
  520. 1.0,
  521. 0.01 * myrand_range(-100, 100)
  522. );
  523. z_offsets[i] = 0.01 * myrand_range(0,100);
  524. }
  525. }
  526. if(debugprint)
  527. std::cerr<<"In goes "<<PP(m_camera_direction)<<", out comes ";
  528. int sunlight_seen_count = 0;
  529. float sunlight_min_d = max_d*0.8;
  530. if(sunlight_min_d > 35*BS)
  531. sunlight_min_d = 35*BS;
  532. std::vector<int> values;
  533. for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
  534. v3f z_dir = z_directions[i];
  535. z_dir.normalize();
  536. core::CMatrix4<f32> a;
  537. a.buildRotateFromTo(v3f(0,1,0), z_dir);
  538. v3f dir = m_camera_direction;
  539. a.rotateVect(dir);
  540. int br = 0;
  541. float step = BS*1.5;
  542. if(max_d > 35*BS)
  543. step = max_d / 35 * 1.5;
  544. float off = step * z_offsets[i];
  545. bool sunlight_seen_now = false;
  546. bool ok = getVisibleBrightness(this, m_camera_position, dir,
  547. step, 1.0, max_d*0.6+off, max_d, m_nodedef, daylight_factor,
  548. sunlight_min_d,
  549. &br, &sunlight_seen_now);
  550. if(sunlight_seen_now)
  551. sunlight_seen_count++;
  552. if(!ok)
  553. continue;
  554. values.push_back(br);
  555. // Don't try too much if being in the sun is clear
  556. if(sunlight_seen_count >= 20)
  557. break;
  558. }
  559. int brightness_sum = 0;
  560. int brightness_count = 0;
  561. std::sort(values.begin(), values.end());
  562. u32 num_values_to_use = values.size();
  563. if(num_values_to_use >= 10)
  564. num_values_to_use -= num_values_to_use/2;
  565. else if(num_values_to_use >= 7)
  566. num_values_to_use -= num_values_to_use/3;
  567. u32 first_value_i = (values.size() - num_values_to_use) / 2;
  568. if(debugprint){
  569. for(u32 i=0; i < first_value_i; i++)
  570. std::cerr<<values[i]<<" ";
  571. std::cerr<<"[";
  572. }
  573. for(u32 i=first_value_i; i < first_value_i+num_values_to_use; i++){
  574. if(debugprint)
  575. std::cerr<<values[i]<<" ";
  576. brightness_sum += values[i];
  577. brightness_count++;
  578. }
  579. if(debugprint){
  580. std::cerr<<"]";
  581. for(u32 i=first_value_i+num_values_to_use; i < values.size(); i++)
  582. std::cerr<<values[i]<<" ";
  583. }
  584. int ret = 0;
  585. if(brightness_count == 0){
  586. MapNode n = getNodeNoEx(floatToInt(m_camera_position, BS));
  587. if(m_nodedef->get(n).param_type == CPT_LIGHT){
  588. ret = decode_light(n.getLightBlend(daylight_factor, m_nodedef));
  589. } else {
  590. ret = oldvalue;
  591. }
  592. } else {
  593. /*float pre = (float)brightness_sum / (float)brightness_count;
  594. float tmp = pre;
  595. const float d = 0.2;
  596. pre *= 1.0 + d*2;
  597. pre -= tmp * d;
  598. int preint = pre;
  599. ret = MYMAX(0, MYMIN(255, preint));*/
  600. ret = brightness_sum / brightness_count;
  601. }
  602. if(debugprint)
  603. std::cerr<<"Result: "<<ret<<" sunlight_seen_count="
  604. <<sunlight_seen_count<<std::endl;
  605. *sunlight_seen_result = (sunlight_seen_count > 0);
  606. return ret;
  607. }
  608. void ClientMap::renderPostFx(CameraMode cam_mode)
  609. {
  610. // Sadly ISceneManager has no "post effects" render pass, in that case we
  611. // could just register for that and handle it in renderMap().
  612. MapNode n = getNodeNoEx(floatToInt(m_camera_position, BS));
  613. // - If the player is in a solid node, make everything black.
  614. // - If the player is in liquid, draw a semi-transparent overlay.
  615. // - Do not if player is in third person mode
  616. const ContentFeatures& features = m_nodedef->get(n);
  617. video::SColor post_effect_color = features.post_effect_color;
  618. if(features.solidness == 2 && !(g_settings->getBool("noclip") &&
  619. m_client->checkLocalPrivilege("noclip")) &&
  620. cam_mode == CAMERA_MODE_FIRST)
  621. {
  622. post_effect_color = video::SColor(255, 0, 0, 0);
  623. }
  624. if (post_effect_color.getAlpha() != 0)
  625. {
  626. // Draw a full-screen rectangle
  627. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  628. v2u32 ss = driver->getScreenSize();
  629. core::rect<s32> rect(0,0, ss.X, ss.Y);
  630. driver->draw2DRectangle(post_effect_color, rect);
  631. }
  632. }
  633. void ClientMap::PrintInfo(std::ostream &out)
  634. {
  635. out<<"ClientMap: ";
  636. }