clientmap.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. struct MeshBufList
  34. {
  35. video::SMaterial m;
  36. std::vector<std::pair<v3s16,scene::IMeshBuffer*>> bufs;
  37. };
  38. struct MeshBufListList
  39. {
  40. /*!
  41. * Stores the mesh buffers of the world.
  42. * The array index is the material's layer.
  43. * The vector part groups vertices by material.
  44. */
  45. std::vector<MeshBufList> lists[MAX_TILE_LAYERS];
  46. void clear();
  47. void add(scene::IMeshBuffer *buf, v3s16 position, u8 layer);
  48. };
  49. class Client;
  50. class ITextureSource;
  51. class PartialMeshBuffer;
  52. /*
  53. ClientMap
  54. This is the only map class that is able to render itself on screen.
  55. */
  56. class ClientMap : public Map, public scene::ISceneNode
  57. {
  58. public:
  59. ClientMap(
  60. Client *client,
  61. RenderingEngine *rendering_engine,
  62. MapDrawControl &control,
  63. s32 id
  64. );
  65. virtual ~ClientMap() = default;
  66. bool maySaveBlocks() override
  67. {
  68. return false;
  69. }
  70. void drop() override
  71. {
  72. ISceneNode::drop(); // calls destructor
  73. }
  74. void updateCamera(v3f pos, v3f dir, f32 fov, v3s16 offset);
  75. /*
  76. Forcefully get a sector from somewhere
  77. */
  78. MapSector * emergeSector(v2s16 p) override;
  79. /*
  80. ISceneNode methods
  81. */
  82. virtual void OnRegisterSceneNode() override;
  83. virtual void render() override
  84. {
  85. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  86. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  87. renderMap(driver, SceneManager->getSceneNodeRenderPass());
  88. }
  89. virtual const aabb3f &getBoundingBox() const override
  90. {
  91. return m_box;
  92. }
  93. void getBlocksInViewRange(v3s16 cam_pos_nodes,
  94. v3s16 *p_blocks_min, v3s16 *p_blocks_max, float range=-1.0f);
  95. void updateDrawList();
  96. void updateDrawListShadow(v3f shadow_light_pos, v3f shadow_light_dir, float radius, float length);
  97. // Returns true if draw list needs updating before drawing the next frame.
  98. bool needsUpdateDrawList() { return m_needs_update_drawlist; }
  99. void renderMap(video::IVideoDriver* driver, s32 pass);
  100. void renderMapShadows(video::IVideoDriver *driver,
  101. const video::SMaterial &material, s32 pass, int frame, int total_frames);
  102. int getBackgroundBrightness(float max_d, u32 daylight_factor,
  103. int oldvalue, bool *sunlight_seen_result);
  104. void renderPostFx(CameraMode cam_mode);
  105. // For debug printing
  106. void PrintInfo(std::ostream &out) override;
  107. const MapDrawControl & getControl() const { return m_control; }
  108. f32 getWantedRange() const { return m_control.wanted_range; }
  109. f32 getCameraFov() const { return m_camera_fov; }
  110. private:
  111. // update the vertex order in transparent mesh buffers
  112. void updateTransparentMeshBuffers();
  113. // Orders blocks by distance to the camera
  114. class MapBlockComparer
  115. {
  116. public:
  117. MapBlockComparer(const v3s16 &camera_block) : m_camera_block(camera_block) {}
  118. bool operator() (const v3s16 &left, const v3s16 &right) const
  119. {
  120. auto distance_left = left.getDistanceFromSQ(m_camera_block);
  121. auto distance_right = right.getDistanceFromSQ(m_camera_block);
  122. return distance_left > distance_right || (distance_left == distance_right && left > right);
  123. }
  124. private:
  125. v3s16 m_camera_block;
  126. };
  127. // reference to a mesh buffer used when rendering the map.
  128. struct DrawDescriptor {
  129. v3s16 m_pos;
  130. union {
  131. scene::IMeshBuffer *m_buffer;
  132. const PartialMeshBuffer *m_partial_buffer;
  133. };
  134. bool m_reuse_material:1;
  135. bool m_use_partial_buffer:1;
  136. DrawDescriptor(v3s16 pos, scene::IMeshBuffer *buffer, bool reuse_material) :
  137. m_pos(pos), m_buffer(buffer), m_reuse_material(reuse_material), m_use_partial_buffer(false)
  138. {}
  139. DrawDescriptor(v3s16 pos, const PartialMeshBuffer *buffer) :
  140. m_pos(pos), m_partial_buffer(buffer), m_reuse_material(false), m_use_partial_buffer(true)
  141. {}
  142. scene::IMeshBuffer* getBuffer();
  143. void draw(video::IVideoDriver* driver);
  144. };
  145. Client *m_client;
  146. RenderingEngine *m_rendering_engine;
  147. aabb3f m_box = aabb3f(-BS * 1000000, -BS * 1000000, -BS * 1000000,
  148. BS * 1000000, BS * 1000000, BS * 1000000);
  149. MapDrawControl &m_control;
  150. v3f m_camera_position = v3f(0,0,0);
  151. v3f m_camera_direction = v3f(0,0,1);
  152. f32 m_camera_fov = M_PI;
  153. v3s16 m_camera_offset;
  154. bool m_needs_update_transparent_meshes = true;
  155. std::map<v3s16, MapBlock*, MapBlockComparer> m_drawlist;
  156. std::map<v3s16, MapBlock*> m_drawlist_shadow;
  157. bool m_needs_update_drawlist;
  158. std::set<v2s16> m_last_drawn_sectors;
  159. bool m_cache_trilinear_filter;
  160. bool m_cache_bilinear_filter;
  161. bool m_cache_anistropic_filter;
  162. bool m_added_to_shadow_renderer{false};
  163. u16 m_cache_transparency_sorting_distance;
  164. };