3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "clientmap.h"
22 #include "mapblock_mesh.h"
23 #include <IMaterialRenderer.h>
26 #include "mapsector.h"
27 #include "main.h" // dout_client, g_settings
32 #include "util/mathconstants.h"
35 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
40 MapDrawControl &control,
41 scene::ISceneNode* parent,
42 scene::ISceneManager* mgr,
45 Map(dout_client, gamedef),
46 scene::ISceneNode(parent, mgr, id),
49 m_camera_position(0,0,0),
50 m_camera_direction(0,0,1),
53 m_box = core::aabbox3d<f32>(-BS*1000000,-BS*1000000,-BS*1000000,
54 BS*1000000,BS*1000000,BS*1000000);
57 ClientMap::~ClientMap()
59 /*JMutexAutoLock lock(mesh_mutex);
68 MapSector * ClientMap::emergeSector(v2s16 p2d)
70 DSTACK(__FUNCTION_NAME);
71 // Check that it doesn't exist already
73 return getSectorNoGenerate(p2d);
75 catch(InvalidPositionException &e)
80 ClientMapSector *sector = new ClientMapSector(this, p2d, m_gamedef);
83 //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
84 m_sectors[p2d] = sector;
91 void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is)
93 DSTACK(__FUNCTION_NAME);
94 ClientMapSector *sector = NULL;
96 //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
98 core::map<v2s16, MapSector*>::Node *n = m_sectors.find(p2d);
102 sector = (ClientMapSector*)n->getValue();
103 assert(sector->getId() == MAPSECTOR_CLIENT);
107 sector = new ClientMapSector(this, p2d);
109 //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out
110 m_sectors.insert(p2d, sector);
114 sector->deSerialize(is);
118 void ClientMap::OnRegisterSceneNode()
122 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
123 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
126 ISceneNode::OnRegisterSceneNode();
129 static bool isOccluded(Map *map, v3s16 p0, v3s16 p1, float step, float stepfac,
130 float start_off, float end_off, u32 needed_count, INodeDefManager *nodemgr)
132 float d0 = (float)BS * p0.getDistanceFrom(p1);
134 v3f uf = v3f(u0.X, u0.Y, u0.Z) * BS;
136 v3f p0f = v3f(p0.X, p0.Y, p0.Z) * BS;
138 for(float s=start_off; s<d0+end_off; s+=step){
139 v3f pf = p0f + uf * s;
140 v3s16 p = floatToInt(pf, BS);
141 MapNode n = map->getNodeNoEx(p);
142 bool is_transparent = false;
143 const ContentFeatures &f = nodemgr->get(n);
145 is_transparent = (f.visual_solidness != 2);
147 is_transparent = (f.solidness != 2);
150 if(count >= needed_count)
158 void ClientMap::updateDrawList(video::IVideoDriver* driver)
160 ScopeProfiler sp(g_profiler, "CM::updateDrawList()", SPT_AVG);
161 g_profiler->add("CM::updateDrawList() count", 1);
163 INodeDefManager *nodemgr = m_gamedef->ndef();
165 for(std::map<v3s16, MapBlock*>::iterator
166 i = m_drawlist.begin();
167 i != m_drawlist.end(); ++i)
169 MapBlock *block = i->second;
174 m_camera_mutex.Lock();
175 v3f camera_position = m_camera_position;
176 v3f camera_direction = m_camera_direction;
177 f32 camera_fov = m_camera_fov;
178 v3s16 camera_offset = m_camera_offset;
179 m_camera_mutex.Unlock();
181 // Use a higher fov to accomodate faster camera movements.
182 // Blocks are cropped better when they are drawn.
183 // Or maybe they aren't? Well whatever.
186 v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
187 v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1);
188 v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d;
189 v3s16 p_nodes_max = cam_pos_nodes + box_nodes_d;
190 // Take a fair amount as we will be dropping more out later
191 // Umm... these additions are a bit strange but they are needed.
193 p_nodes_min.X / MAP_BLOCKSIZE - 3,
194 p_nodes_min.Y / MAP_BLOCKSIZE - 3,
195 p_nodes_min.Z / MAP_BLOCKSIZE - 3);
197 p_nodes_max.X / MAP_BLOCKSIZE + 1,
198 p_nodes_max.Y / MAP_BLOCKSIZE + 1,
199 p_nodes_max.Z / MAP_BLOCKSIZE + 1);
201 // Number of blocks in rendering range
202 u32 blocks_in_range = 0;
203 // Number of blocks occlusion culled
204 u32 blocks_occlusion_culled = 0;
205 // Number of blocks in rendering range but don't have a mesh
206 u32 blocks_in_range_without_mesh = 0;
207 // Blocks that had mesh that would have been drawn according to
208 // rendering range (if max blocks limit didn't kick in)
209 u32 blocks_would_have_drawn = 0;
210 // Blocks that were drawn and had a mesh
211 u32 blocks_drawn = 0;
212 // Blocks which had a corresponding meshbuffer for this pass
213 //u32 blocks_had_pass_meshbuf = 0;
214 // Blocks from which stuff was actually drawn
215 //u32 blocks_without_stuff = 0;
216 // Distance to farthest drawn block
217 float farthest_drawn = 0;
219 for(std::map<v2s16, MapSector*>::iterator
220 si = m_sectors.begin();
221 si != m_sectors.end(); ++si)
223 MapSector *sector = si->second;
224 v2s16 sp = sector->getPos();
226 if(m_control.range_all == false)
228 if(sp.X < p_blocks_min.X
229 || sp.X > p_blocks_max.X
230 || sp.Y < p_blocks_min.Z
231 || sp.Y > p_blocks_max.Z)
235 std::list< MapBlock * > sectorblocks;
236 sector->getBlocks(sectorblocks);
239 Loop through blocks in sector
242 u32 sector_blocks_drawn = 0;
244 std::list< MapBlock * >::iterator i;
245 for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++)
247 MapBlock *block = *i;
250 Compare block position to camera position, skip
251 if not seen on display
254 if (block->mesh != NULL)
255 block->mesh->updateCameraOffset(m_camera_offset);
257 float range = 100000 * BS;
258 if(m_control.range_all == false)
259 range = m_control.wanted_range * BS;
262 if(isBlockInSight(block->getPos(), camera_position,
263 camera_direction, camera_fov,
269 // This is ugly (spherical distance limit?)
270 /*if(m_control.range_all == false &&
271 d - 0.5*BS*MAP_BLOCKSIZE > range)
277 Ignore if mesh doesn't exist
280 //JMutexAutoLock lock(block->mesh_mutex);
282 if(block->mesh == NULL){
283 blocks_in_range_without_mesh++;
292 // No occlusion culling when free_move is on and camera is
294 bool occlusion_culling_enabled = true;
295 if(g_settings->getBool("free_move")){
296 MapNode n = getNodeNoEx(cam_pos_nodes);
297 if(n.getContent() == CONTENT_IGNORE ||
298 nodemgr->get(n).solidness == 2)
299 occlusion_culling_enabled = false;
302 v3s16 cpn = block->getPos() * MAP_BLOCKSIZE;
303 cpn += v3s16(MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2);
306 float startoff = BS*1;
307 float endoff = -BS*MAP_BLOCKSIZE*1.42*1.42;
308 v3s16 spn = cam_pos_nodes + v3s16(0,0,0);
309 s16 bs2 = MAP_BLOCKSIZE/2 + 1;
310 u32 needed_count = 1;
312 occlusion_culling_enabled &&
313 isOccluded(this, spn, cpn + v3s16(0,0,0),
314 step, stepfac, startoff, endoff, needed_count, nodemgr) &&
315 isOccluded(this, spn, cpn + v3s16(bs2,bs2,bs2),
316 step, stepfac, startoff, endoff, needed_count, nodemgr) &&
317 isOccluded(this, spn, cpn + v3s16(bs2,bs2,-bs2),
318 step, stepfac, startoff, endoff, needed_count, nodemgr) &&
319 isOccluded(this, spn, cpn + v3s16(bs2,-bs2,bs2),
320 step, stepfac, startoff, endoff, needed_count, nodemgr) &&
321 isOccluded(this, spn, cpn + v3s16(bs2,-bs2,-bs2),
322 step, stepfac, startoff, endoff, needed_count, nodemgr) &&
323 isOccluded(this, spn, cpn + v3s16(-bs2,bs2,bs2),
324 step, stepfac, startoff, endoff, needed_count, nodemgr) &&
325 isOccluded(this, spn, cpn + v3s16(-bs2,bs2,-bs2),
326 step, stepfac, startoff, endoff, needed_count, nodemgr) &&
327 isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,bs2),
328 step, stepfac, startoff, endoff, needed_count, nodemgr) &&
329 isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,-bs2),
330 step, stepfac, startoff, endoff, needed_count, nodemgr)
333 blocks_occlusion_culled++;
337 // This block is in range. Reset usage timer.
338 block->resetUsageTimer();
340 // Limit block count in case of a sudden increase
341 blocks_would_have_drawn++;
342 if(blocks_drawn >= m_control.wanted_max_blocks
343 && m_control.range_all == false
344 && d > m_control.wanted_min_range * BS)
349 m_drawlist[block->getPos()] = block;
351 sector_blocks_drawn++;
353 if(d/BS > farthest_drawn)
354 farthest_drawn = d/BS;
356 } // foreach sectorblocks
358 if(sector_blocks_drawn != 0)
359 m_last_drawn_sectors.insert(sp);
362 m_control.blocks_would_have_drawn = blocks_would_have_drawn;
363 m_control.blocks_drawn = blocks_drawn;
364 m_control.farthest_drawn = farthest_drawn;
366 g_profiler->avg("CM: blocks in range", blocks_in_range);
367 g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled);
368 if(blocks_in_range != 0)
369 g_profiler->avg("CM: blocks in range without mesh (frac)",
370 (float)blocks_in_range_without_mesh/blocks_in_range);
371 g_profiler->avg("CM: blocks drawn", blocks_drawn);
372 g_profiler->avg("CM: farthest drawn", farthest_drawn);
373 g_profiler->avg("CM: wanted max blocks", m_control.wanted_max_blocks);
379 std::list<scene::IMeshBuffer*> bufs;
382 struct MeshBufListList
384 std::list<MeshBufList> lists;
391 void add(scene::IMeshBuffer *buf)
393 for(std::list<MeshBufList>::iterator i = lists.begin();
394 i != lists.end(); ++i){
396 if(l.m == buf->getMaterial()){
397 l.bufs.push_back(buf);
402 l.m = buf->getMaterial();
403 l.bufs.push_back(buf);
408 void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
410 DSTACK(__FUNCTION_NAME);
412 bool is_transparent_pass = pass == scene::ESNRP_TRANSPARENT;
415 if(pass == scene::ESNRP_SOLID)
416 prefix = "CM: solid: ";
418 prefix = "CM: transparent: ";
421 This is called two times per frame, reset on the non-transparent one
423 if(pass == scene::ESNRP_SOLID)
425 m_last_drawn_sectors.clear();
428 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
429 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
430 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
433 Get time for measuring timeout.
435 Measuring time is very useful for long delays when the
436 machine is swapping a lot.
441 Get animation parameters
443 float animation_time = m_client->getAnimationTime();
444 int crack = m_client->getCrackLevel();
445 u32 daynight_ratio = m_client->getEnv().getDayNightRatio();
447 m_camera_mutex.Lock();
448 v3f camera_position = m_camera_position;
449 v3f camera_direction = m_camera_direction;
450 f32 camera_fov = m_camera_fov;
451 m_camera_mutex.Unlock();
454 Get all blocks and draw all visible ones
457 v3s16 cam_pos_nodes = floatToInt(camera_position, BS);
459 v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1);
461 v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d;
462 v3s16 p_nodes_max = cam_pos_nodes + box_nodes_d;
464 // Take a fair amount as we will be dropping more out later
465 // Umm... these additions are a bit strange but they are needed.
467 p_nodes_min.X / MAP_BLOCKSIZE - 3,
468 p_nodes_min.Y / MAP_BLOCKSIZE - 3,
469 p_nodes_min.Z / MAP_BLOCKSIZE - 3);
471 p_nodes_max.X / MAP_BLOCKSIZE + 1,
472 p_nodes_max.Y / MAP_BLOCKSIZE + 1,
473 p_nodes_max.Z / MAP_BLOCKSIZE + 1);
475 u32 vertex_count = 0;
476 u32 meshbuffer_count = 0;
478 // For limiting number of mesh animations per frame
479 u32 mesh_animate_count = 0;
480 u32 mesh_animate_count_far = 0;
482 // Blocks that were drawn and had a mesh
483 u32 blocks_drawn = 0;
484 // Blocks which had a corresponding meshbuffer for this pass
485 u32 blocks_had_pass_meshbuf = 0;
486 // Blocks from which stuff was actually drawn
487 u32 blocks_without_stuff = 0;
490 Draw the selected MapBlocks
494 ScopeProfiler sp(g_profiler, prefix+"drawing blocks", SPT_AVG);
496 MeshBufListList drawbufs;
498 for(std::map<v3s16, MapBlock*>::iterator
499 i = m_drawlist.begin();
500 i != m_drawlist.end(); ++i)
502 MapBlock *block = i->second;
504 // If the mesh of the block happened to get deleted, ignore it
505 if(block->mesh == NULL)
509 if(isBlockInSight(block->getPos(), camera_position,
510 camera_direction, camera_fov,
511 100000*BS, &d) == false)
518 //JMutexAutoLock lock(block->mesh_mutex);
519 MapBlockMesh *mapBlockMesh = block->mesh;
520 assert(mapBlockMesh);
521 // Pretty random but this should work somewhat nicely
522 bool faraway = d >= BS*50;
523 //bool faraway = d >= m_control.wanted_range * BS;
524 if(mapBlockMesh->isAnimationForced() ||
526 mesh_animate_count_far < (m_control.range_all ? 200 : 50))
528 bool animated = mapBlockMesh->animate(
534 mesh_animate_count++;
535 if(animated && faraway)
536 mesh_animate_count_far++;
540 mapBlockMesh->decreaseAnimationForceTimer();
545 Get the meshbuffers of the block
548 //JMutexAutoLock lock(block->mesh_mutex);
550 MapBlockMesh *mapBlockMesh = block->mesh;
551 assert(mapBlockMesh);
553 scene::SMesh *mesh = mapBlockMesh->getMesh();
556 u32 c = mesh->getMeshBufferCount();
557 for(u32 i=0; i<c; i++)
559 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
561 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
562 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
563 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
565 const video::SMaterial& material = buf->getMaterial();
566 video::IMaterialRenderer* rnd =
567 driver->getMaterialRenderer(material.MaterialType);
568 bool transparent = (rnd && rnd->isTransparent());
569 if(transparent == is_transparent_pass)
571 if(buf->getVertexCount() == 0)
572 errorstream<<"Block ["<<analyze_block(block)
573 <<"] contains an empty meshbuf"<<std::endl;
580 std::list<MeshBufList> &lists = drawbufs.lists;
582 int timecheck_counter = 0;
583 for(std::list<MeshBufList>::iterator i = lists.begin();
584 i != lists.end(); ++i)
588 if(timecheck_counter > 50)
590 timecheck_counter = 0;
592 if(time2 > time1 + 4)
594 infostream<<"ClientMap::renderMap(): "
595 "Rendering takes ages, returning."
602 MeshBufList &list = *i;
604 driver->setMaterial(list.m);
606 for(std::list<scene::IMeshBuffer*>::iterator j = list.bufs.begin();
607 j != list.bufs.end(); ++j)
609 scene::IMeshBuffer *buf = *j;
610 driver->drawMeshBuffer(buf);
611 vertex_count += buf->getVertexCount();
616 Draw the faces of the block
619 //JMutexAutoLock lock(block->mesh_mutex);
621 MapBlockMesh *mapBlockMesh = block->mesh;
622 assert(mapBlockMesh);
624 scene::SMesh *mesh = mapBlockMesh->getMesh();
627 u32 c = mesh->getMeshBufferCount();
628 bool stuff_actually_drawn = false;
629 for(u32 i=0; i<c; i++)
631 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
632 const video::SMaterial& material = buf->getMaterial();
633 video::IMaterialRenderer* rnd =
634 driver->getMaterialRenderer(material.MaterialType);
635 bool transparent = (rnd && rnd->isTransparent());
636 // Render transparent on transparent pass and likewise.
637 if(transparent == is_transparent_pass)
639 if(buf->getVertexCount() == 0)
640 errorstream<<"Block ["<<analyze_block(block)
641 <<"] contains an empty meshbuf"<<std::endl;
643 This *shouldn't* hurt too much because Irrlicht
644 doesn't change opengl textures if the old
645 material has the same texture.
647 driver->setMaterial(buf->getMaterial());
648 driver->drawMeshBuffer(buf);
649 vertex_count += buf->getVertexCount();
651 stuff_actually_drawn = true;
654 if(stuff_actually_drawn)
655 blocks_had_pass_meshbuf++;
657 blocks_without_stuff++;
663 // Log only on solid pass because values are the same
664 if(pass == scene::ESNRP_SOLID){
665 g_profiler->avg("CM: animated meshes", mesh_animate_count);
666 g_profiler->avg("CM: animated meshes (far)", mesh_animate_count_far);
669 g_profiler->avg(prefix+"vertices drawn", vertex_count);
670 if(blocks_had_pass_meshbuf != 0)
671 g_profiler->avg(prefix+"meshbuffers per block",
672 (float)meshbuffer_count / (float)blocks_had_pass_meshbuf);
673 if(blocks_drawn != 0)
674 g_profiler->avg(prefix+"empty blocks (frac)",
675 (float)blocks_without_stuff / blocks_drawn);
677 /*infostream<<"renderMap(): is_transparent_pass="<<is_transparent_pass
678 <<", rendered "<<vertex_count<<" vertices."<<std::endl;*/
681 static bool getVisibleBrightness(Map *map, v3f p0, v3f dir, float step,
682 float step_multiplier, float start_distance, float end_distance,
683 INodeDefManager *ndef, u32 daylight_factor, float sunlight_min_d,
684 int *result, bool *sunlight_seen)
686 int brightness_sum = 0;
687 int brightness_count = 0;
688 float distance = start_distance;
691 pf += dir * distance;
693 bool nonlight_seen = false;
694 bool allow_allowing_non_sunlight_propagates = false;
695 bool allow_non_sunlight_propagates = false;
696 // Check content nearly at camera position
698 v3s16 p = floatToInt(p0 /*+ dir * 3*BS*/, BS);
699 MapNode n = map->getNodeNoEx(p);
700 if(ndef->get(n).param_type == CPT_LIGHT &&
701 !ndef->get(n).sunlight_propagates)
702 allow_allowing_non_sunlight_propagates = true;
704 // If would start at CONTENT_IGNORE, start closer
706 v3s16 p = floatToInt(pf, BS);
707 MapNode n = map->getNodeNoEx(p);
708 if(n.getContent() == CONTENT_IGNORE){
710 pf = p0 + dir * 2*newd;
715 for(int i=0; distance < end_distance; i++){
718 step *= step_multiplier;
720 v3s16 p = floatToInt(pf, BS);
721 MapNode n = map->getNodeNoEx(p);
722 if(allow_allowing_non_sunlight_propagates && i == 0 &&
723 ndef->get(n).param_type == CPT_LIGHT &&
724 !ndef->get(n).sunlight_propagates){
725 allow_non_sunlight_propagates = true;
727 if(ndef->get(n).param_type != CPT_LIGHT ||
728 (!ndef->get(n).sunlight_propagates &&
729 !allow_non_sunlight_propagates)){
730 nonlight_seen = true;
736 if(distance >= sunlight_min_d && *sunlight_seen == false
737 && nonlight_seen == false)
738 if(n.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN)
739 *sunlight_seen = true;
741 brightness_sum += decode_light(n.getLightBlend(daylight_factor, ndef));
745 if(brightness_count == 0)
747 *result = brightness_sum / brightness_count;
748 /*std::cerr<<"Sampled "<<brightness_count<<" points; result="
749 <<(*result)<<std::endl;*/
753 int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
754 int oldvalue, bool *sunlight_seen_result)
756 const bool debugprint = false;
757 INodeDefManager *ndef = m_gamedef->ndef();
758 static v3f z_directions[50] = {
761 static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = {
764 if(z_directions[0].X < -99){
765 for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
766 z_directions[i] = v3f(
767 0.01 * myrand_range(-100, 100),
769 0.01 * myrand_range(-100, 100)
771 z_offsets[i] = 0.01 * myrand_range(0,100);
775 std::cerr<<"In goes "<<PP(m_camera_direction)<<", out comes ";
776 int sunlight_seen_count = 0;
777 float sunlight_min_d = max_d*0.8;
778 if(sunlight_min_d > 35*BS)
779 sunlight_min_d = 35*BS;
780 std::vector<int> values;
781 for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){
782 v3f z_dir = z_directions[i];
784 core::CMatrix4<f32> a;
785 a.buildRotateFromTo(v3f(0,1,0), z_dir);
786 v3f dir = m_camera_direction;
791 step = max_d / 35 * 1.5;
792 float off = step * z_offsets[i];
793 bool sunlight_seen_now = false;
794 bool ok = getVisibleBrightness(this, m_camera_position, dir,
795 step, 1.0, max_d*0.6+off, max_d, ndef, daylight_factor,
797 &br, &sunlight_seen_now);
798 if(sunlight_seen_now)
799 sunlight_seen_count++;
802 values.push_back(br);
803 // Don't try too much if being in the sun is clear
804 if(sunlight_seen_count >= 20)
807 int brightness_sum = 0;
808 int brightness_count = 0;
809 std::sort(values.begin(), values.end());
810 u32 num_values_to_use = values.size();
811 if(num_values_to_use >= 10)
812 num_values_to_use -= num_values_to_use/2;
813 else if(num_values_to_use >= 7)
814 num_values_to_use -= num_values_to_use/3;
815 u32 first_value_i = (values.size() - num_values_to_use) / 2;
817 for(u32 i=0; i < first_value_i; i++)
818 std::cerr<<values[i]<<" ";
821 for(u32 i=first_value_i; i < first_value_i+num_values_to_use; i++){
823 std::cerr<<values[i]<<" ";
824 brightness_sum += values[i];
829 for(u32 i=first_value_i+num_values_to_use; i < values.size(); i++)
830 std::cerr<<values[i]<<" ";
833 if(brightness_count == 0){
834 MapNode n = getNodeNoEx(floatToInt(m_camera_position, BS));
835 if(ndef->get(n).param_type == CPT_LIGHT){
836 ret = decode_light(n.getLightBlend(daylight_factor, ndef));
839 //ret = blend_light(255, 0, daylight_factor);
842 /*float pre = (float)brightness_sum / (float)brightness_count;
848 ret = MYMAX(0, MYMIN(255, preint));*/
849 ret = brightness_sum / brightness_count;
852 std::cerr<<"Result: "<<ret<<" sunlight_seen_count="
853 <<sunlight_seen_count<<std::endl;
854 *sunlight_seen_result = (sunlight_seen_count > 0);
858 void ClientMap::renderPostFx()
860 INodeDefManager *nodemgr = m_gamedef->ndef();
862 // Sadly ISceneManager has no "post effects" render pass, in that case we
863 // could just register for that and handle it in renderMap().
865 m_camera_mutex.Lock();
866 v3f camera_position = m_camera_position;
867 m_camera_mutex.Unlock();
869 MapNode n = getNodeNoEx(floatToInt(camera_position, BS));
871 // - If the player is in a solid node, make everything black.
872 // - If the player is in liquid, draw a semi-transparent overlay.
873 const ContentFeatures& features = nodemgr->get(n);
874 video::SColor post_effect_color = features.post_effect_color;
875 if(features.solidness == 2 && !(g_settings->getBool("noclip") && m_gamedef->checkLocalPrivilege("noclip")))
877 post_effect_color = video::SColor(255, 0, 0, 0);
879 if (post_effect_color.getAlpha() != 0)
881 // Draw a full-screen rectangle
882 video::IVideoDriver* driver = SceneManager->getVideoDriver();
883 v2u32 ss = driver->getScreenSize();
884 core::rect<s32> rect(0,0, ss.X, ss.Y);
885 driver->draw2DRectangle(post_effect_color, rect);
889 void ClientMap::PrintInfo(std::ostream &out)