clientmap.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #pragma once
  17. #include "irrlichttypes_extrabloated.h"
  18. #include "map.h"
  19. #include "camera.h"
  20. #include <set>
  21. #include <map>
  22. struct MapDrawControl
  23. {
  24. // Wanted drawing range
  25. float wanted_range = 0.0f;
  26. // Overrides limits by drawing everything
  27. bool range_all = false;
  28. // Allow rendering out of bounds
  29. bool allow_noclip = false;
  30. // show a wire frame for debugging
  31. bool show_wireframe = false;
  32. };
  33. class Client;
  34. class ITextureSource;
  35. class PartialMeshBuffer;
  36. /*
  37. ClientMap
  38. This is the only map class that is able to render itself on screen.
  39. */
  40. class ClientMap : public Map, public scene::ISceneNode
  41. {
  42. public:
  43. ClientMap(
  44. Client *client,
  45. RenderingEngine *rendering_engine,
  46. MapDrawControl &control,
  47. s32 id
  48. );
  49. virtual ~ClientMap();
  50. bool maySaveBlocks() override
  51. {
  52. return false;
  53. }
  54. void drop() override
  55. {
  56. ISceneNode::drop(); // calls destructor
  57. }
  58. void updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset, video::SColor light_color);
  59. /*
  60. Forcefully get a sector from somewhere
  61. */
  62. MapSector * emergeSector(v2s16 p) override;
  63. /*
  64. ISceneNode methods
  65. */
  66. virtual void OnRegisterSceneNode() override;
  67. virtual void render() override
  68. {
  69. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  70. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  71. renderMap(driver, SceneManager->getSceneNodeRenderPass());
  72. }
  73. virtual const aabb3f &getBoundingBox() const override
  74. {
  75. return m_box;
  76. }
  77. void getBlocksInViewRange(v3s16 cam_pos_nodes,
  78. v3s16 *p_blocks_min, v3s16 *p_blocks_max, float range=-1.0f);
  79. void updateDrawList();
  80. // @brief Calculate statistics about the map and keep the blocks alive
  81. void touchMapBlocks();
  82. void updateDrawListShadow(v3f shadow_light_pos, v3f shadow_light_dir, float radius, float length);
  83. // Returns true if draw list needs updating before drawing the next frame.
  84. bool needsUpdateDrawList() { return m_needs_update_drawlist; }
  85. void renderMap(video::IVideoDriver* driver, s32 pass);
  86. void renderMapShadows(video::IVideoDriver *driver,
  87. const video::SMaterial &material, s32 pass, int frame, int total_frames);
  88. int getBackgroundBrightness(float max_d, u32 daylight_factor,
  89. int oldvalue, bool *sunlight_seen_result);
  90. void renderPostFx(CameraMode cam_mode);
  91. // For debug printing
  92. void PrintInfo(std::ostream &out) override;
  93. const MapDrawControl & getControl() const { return m_control; }
  94. f32 getWantedRange() const { return m_control.wanted_range; }
  95. f32 getCameraFov() const { return m_camera_fov; }
  96. void onSettingChanged(const std::string &name);
  97. protected:
  98. void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override;
  99. private:
  100. bool isMeshOccluded(MapBlock *mesh_block, u16 mesh_size, v3s16 cam_pos_nodes);
  101. // update the vertex order in transparent mesh buffers
  102. void updateTransparentMeshBuffers();
  103. // Orders blocks by distance to the camera
  104. class MapBlockComparer
  105. {
  106. public:
  107. MapBlockComparer(const v3s16 &camera_block) : m_camera_block(camera_block) {}
  108. bool operator() (const v3s16 &left, const v3s16 &right) const
  109. {
  110. auto distance_left = left.getDistanceFromSQ(m_camera_block);
  111. auto distance_right = right.getDistanceFromSQ(m_camera_block);
  112. return distance_left > distance_right || (distance_left == distance_right && left > right);
  113. }
  114. private:
  115. v3s16 m_camera_block;
  116. };
  117. // reference to a mesh buffer used when rendering the map.
  118. struct DrawDescriptor {
  119. v3s16 m_pos;
  120. union {
  121. scene::IMeshBuffer *m_buffer;
  122. const PartialMeshBuffer *m_partial_buffer;
  123. };
  124. bool m_reuse_material:1;
  125. bool m_use_partial_buffer:1;
  126. DrawDescriptor(v3s16 pos, scene::IMeshBuffer *buffer, bool reuse_material) :
  127. m_pos(pos), m_buffer(buffer), m_reuse_material(reuse_material), m_use_partial_buffer(false)
  128. {}
  129. DrawDescriptor(v3s16 pos, const PartialMeshBuffer *buffer) :
  130. m_pos(pos), m_partial_buffer(buffer), m_reuse_material(false), m_use_partial_buffer(true)
  131. {}
  132. scene::IMeshBuffer* getBuffer();
  133. void draw(video::IVideoDriver* driver);
  134. };
  135. Client *m_client;
  136. RenderingEngine *m_rendering_engine;
  137. aabb3f m_box = aabb3f(-BS * 1000000, -BS * 1000000, -BS * 1000000,
  138. BS * 1000000, BS * 1000000, BS * 1000000);
  139. MapDrawControl &m_control;
  140. v3f m_camera_position = v3f(0,0,0);
  141. v3f m_camera_direction = v3f(0,0,1);
  142. f32 m_camera_fov = M_PI;
  143. v3s16 m_camera_offset;
  144. video::SColor m_camera_light_color = video::SColor(0xFFFFFFFF);
  145. bool m_needs_update_transparent_meshes = true;
  146. std::map<v3s16, MapBlock*, MapBlockComparer> m_drawlist;
  147. std::vector<MapBlock*> m_keeplist;
  148. std::map<v3s16, MapBlock*> m_drawlist_shadow;
  149. bool m_needs_update_drawlist;
  150. std::set<v2s16> m_last_drawn_sectors;
  151. bool m_cache_trilinear_filter;
  152. bool m_cache_bilinear_filter;
  153. bool m_cache_anistropic_filter;
  154. u16 m_cache_transparency_sorting_distance;
  155. bool m_loops_occlusion_culler;
  156. bool m_enable_raytraced_culling;
  157. };