clouds.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 "client/renderingengine.h"
  17. #include "client/shader.h"
  18. #include "clouds.h"
  19. #include "constants.h"
  20. #include "debug.h"
  21. #include "irrlicht_changes/printing.h"
  22. #include "noise.h"
  23. #include "profiler.h"
  24. #include "settings.h"
  25. #include <cmath>
  26. // Menu clouds are created later
  27. class Clouds;
  28. Clouds *g_menuclouds = NULL;
  29. scene::ISceneManager *g_menucloudsmgr = NULL;
  30. // Constant for now
  31. static constexpr const float cloud_size = BS * 64.0f;
  32. static void cloud_3d_setting_changed(const std::string &settingname, void *data)
  33. {
  34. ((Clouds *)data)->readSettings();
  35. }
  36. Clouds::Clouds(scene::ISceneManager* mgr, IShaderSource *ssrc,
  37. s32 id,
  38. u32 seed
  39. ):
  40. scene::ISceneNode(mgr->getRootSceneNode(), mgr, id),
  41. m_seed(seed)
  42. {
  43. m_enable_shaders = g_settings->getBool("enable_shaders");
  44. // menu clouds use shader-less clouds for simplicity (ssrc == NULL)
  45. m_enable_shaders = m_enable_shaders && ssrc;
  46. m_material.Lighting = false;
  47. m_material.BackfaceCulling = true;
  48. m_material.FogEnable = true;
  49. m_material.AntiAliasing = video::EAAM_SIMPLE;
  50. if (m_enable_shaders) {
  51. auto sid = ssrc->getShader("cloud_shader", TILE_MATERIAL_ALPHA);
  52. m_material.MaterialType = ssrc->getShaderInfo(sid).material;
  53. } else {
  54. m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  55. }
  56. m_params = SkyboxDefaults::getCloudDefaults();
  57. readSettings();
  58. g_settings->registerChangedCallback("enable_3d_clouds",
  59. &cloud_3d_setting_changed, this);
  60. updateBox();
  61. m_meshbuffer.reset(new scene::SMeshBuffer());
  62. m_meshbuffer->setHardwareMappingHint(scene::EHM_DYNAMIC);
  63. }
  64. Clouds::~Clouds()
  65. {
  66. g_settings->deregisterChangedCallback("enable_3d_clouds",
  67. &cloud_3d_setting_changed, this);
  68. }
  69. void Clouds::OnRegisterSceneNode()
  70. {
  71. if(IsVisible)
  72. {
  73. SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
  74. }
  75. ISceneNode::OnRegisterSceneNode();
  76. }
  77. void Clouds::updateMesh()
  78. {
  79. // Clouds move from Z+ towards Z-
  80. v2f camera_pos_2d(m_camera_pos.X, m_camera_pos.Z);
  81. // Position of cloud noise origin from the camera
  82. v2f cloud_origin_from_camera_f = m_origin - camera_pos_2d;
  83. // The center point of drawing in the noise
  84. v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
  85. // The integer center point of drawing in the noise
  86. v2s16 center_of_drawing_in_noise_i(
  87. std::floor(center_of_drawing_in_noise_f.X / cloud_size),
  88. std::floor(center_of_drawing_in_noise_f.Y / cloud_size)
  89. );
  90. // Only update mesh if it has moved enough, this saves lots of GPU buffer uploads.
  91. constexpr float max_d = 5 * BS;
  92. if (!m_mesh_valid) {
  93. // mesh was never created or invalidated
  94. } else if (m_mesh_origin.getDistanceFrom(m_origin) >= max_d) {
  95. // clouds moved
  96. } else if (center_of_drawing_in_noise_i != m_last_noise_center) {
  97. // noise offset changed
  98. // I think in practice this never happens due to the camera offset
  99. // being smaller than the cloud size.(?)
  100. } else {
  101. return;
  102. }
  103. ScopeProfiler sp(g_profiler, "Clouds::updateMesh()", SPT_AVG);
  104. m_mesh_origin = m_origin;
  105. m_last_noise_center = center_of_drawing_in_noise_i;
  106. m_mesh_valid = true;
  107. const u32 num_faces_to_draw = m_enable_3d ? 6 : 1;
  108. // The world position of the integer center point of drawing in the noise
  109. v2f world_center_of_drawing_in_noise_f = v2f(
  110. center_of_drawing_in_noise_i.X * cloud_size,
  111. center_of_drawing_in_noise_i.Y * cloud_size
  112. ) + m_origin;
  113. // Colors with primitive shading
  114. video::SColorf c_top_f(m_color);
  115. video::SColorf c_side_1_f(m_color);
  116. video::SColorf c_side_2_f(m_color);
  117. video::SColorf c_bottom_f(m_color);
  118. if (m_enable_shaders) {
  119. // shader mixes the base color, set via EmissiveColor
  120. c_top_f = c_side_1_f = c_side_2_f = c_bottom_f = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
  121. }
  122. c_side_1_f.r *= 0.95f;
  123. c_side_1_f.g *= 0.95f;
  124. c_side_1_f.b *= 0.95f;
  125. c_side_2_f.r *= 0.90f;
  126. c_side_2_f.g *= 0.90f;
  127. c_side_2_f.b *= 0.90f;
  128. c_bottom_f.r *= 0.80f;
  129. c_bottom_f.g *= 0.80f;
  130. c_bottom_f.b *= 0.80f;
  131. video::SColor c_top = c_top_f.toSColor();
  132. video::SColor c_side_1 = c_side_1_f.toSColor();
  133. video::SColor c_side_2 = c_side_2_f.toSColor();
  134. video::SColor c_bottom = c_bottom_f.toSColor();
  135. // Read noise
  136. std::vector<bool> grid(m_cloud_radius_i * 2 * m_cloud_radius_i * 2);
  137. for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
  138. u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
  139. for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
  140. u32 i = si + xi;
  141. grid[i] = gridFilled(
  142. xi + center_of_drawing_in_noise_i.X,
  143. zi + center_of_drawing_in_noise_i.Y
  144. );
  145. }
  146. }
  147. auto *mb = m_meshbuffer.get();
  148. {
  149. const u32 vertex_count = num_faces_to_draw * 16 * m_cloud_radius_i * m_cloud_radius_i;
  150. const u32 quad_count = vertex_count / 4;
  151. const u32 index_count = quad_count * 6;
  152. // reserve memory
  153. mb->Vertices.reallocate(vertex_count);
  154. mb->Indices.reallocate(index_count);
  155. }
  156. #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
  157. #define INAREA(x, z, radius) \
  158. ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
  159. mb->Vertices.set_used(0);
  160. for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
  161. for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
  162. {
  163. s16 zi = zi0;
  164. s16 xi = xi0;
  165. // Draw from back to front for proper transparency
  166. if(zi >= 0)
  167. zi = m_cloud_radius_i - zi - 1;
  168. if(xi >= 0)
  169. xi = m_cloud_radius_i - xi - 1;
  170. u32 i = GETINDEX(xi, zi, m_cloud_radius_i);
  171. if (!grid[i])
  172. continue;
  173. v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
  174. video::S3DVertex v[4] = {
  175. video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
  176. video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
  177. video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
  178. video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
  179. };
  180. const f32 rx = cloud_size / 2.0f;
  181. // if clouds are flat, the top layer should be at the given height
  182. const f32 ry = m_enable_3d ? m_params.thickness * BS : 0.0f;
  183. const f32 rz = cloud_size / 2;
  184. for(u32 i = 0; i < num_faces_to_draw; i++)
  185. {
  186. switch(i)
  187. {
  188. case 0: // top
  189. for (video::S3DVertex &vertex : v) {
  190. vertex.Normal.set(0,1,0);
  191. }
  192. v[0].Pos.set(-rx, ry,-rz);
  193. v[1].Pos.set(-rx, ry, rz);
  194. v[2].Pos.set( rx, ry, rz);
  195. v[3].Pos.set( rx, ry,-rz);
  196. break;
  197. case 1: // back
  198. if (INAREA(xi, zi - 1, m_cloud_radius_i)) {
  199. u32 j = GETINDEX(xi, zi - 1, m_cloud_radius_i);
  200. if(grid[j])
  201. continue;
  202. }
  203. for (video::S3DVertex &vertex : v) {
  204. vertex.Color = c_side_1;
  205. vertex.Normal.set(0,0,-1);
  206. }
  207. v[0].Pos.set(-rx, ry,-rz);
  208. v[1].Pos.set( rx, ry,-rz);
  209. v[2].Pos.set( rx, 0,-rz);
  210. v[3].Pos.set(-rx, 0,-rz);
  211. break;
  212. case 2: //right
  213. if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
  214. u32 j = GETINDEX(xi+1, zi, m_cloud_radius_i);
  215. if(grid[j])
  216. continue;
  217. }
  218. for (video::S3DVertex &vertex : v) {
  219. vertex.Color = c_side_2;
  220. vertex.Normal.set(1,0,0);
  221. }
  222. v[0].Pos.set( rx, ry,-rz);
  223. v[1].Pos.set( rx, ry, rz);
  224. v[2].Pos.set( rx, 0, rz);
  225. v[3].Pos.set( rx, 0,-rz);
  226. break;
  227. case 3: // front
  228. if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
  229. u32 j = GETINDEX(xi, zi + 1, m_cloud_radius_i);
  230. if(grid[j])
  231. continue;
  232. }
  233. for (video::S3DVertex &vertex : v) {
  234. vertex.Color = c_side_1;
  235. vertex.Normal.set(0,0,-1);
  236. }
  237. v[0].Pos.set( rx, ry, rz);
  238. v[1].Pos.set(-rx, ry, rz);
  239. v[2].Pos.set(-rx, 0, rz);
  240. v[3].Pos.set( rx, 0, rz);
  241. break;
  242. case 4: // left
  243. if (INAREA(xi-1, zi, m_cloud_radius_i)) {
  244. u32 j = GETINDEX(xi-1, zi, m_cloud_radius_i);
  245. if(grid[j])
  246. continue;
  247. }
  248. for (video::S3DVertex &vertex : v) {
  249. vertex.Color = c_side_2;
  250. vertex.Normal.set(-1,0,0);
  251. }
  252. v[0].Pos.set(-rx, ry, rz);
  253. v[1].Pos.set(-rx, ry,-rz);
  254. v[2].Pos.set(-rx, 0,-rz);
  255. v[3].Pos.set(-rx, 0, rz);
  256. break;
  257. case 5: // bottom
  258. for (video::S3DVertex &vertex : v) {
  259. vertex.Color = c_bottom;
  260. vertex.Normal.set(0,-1,0);
  261. }
  262. v[0].Pos.set( rx, 0, rz);
  263. v[1].Pos.set(-rx, 0, rz);
  264. v[2].Pos.set(-rx, 0,-rz);
  265. v[3].Pos.set( rx, 0,-rz);
  266. break;
  267. }
  268. v3f pos(p0.X, m_params.height * BS, p0.Y);
  269. for (video::S3DVertex &vertex : v) {
  270. vertex.Pos += pos;
  271. mb->Vertices.push_back(vertex);
  272. }
  273. }
  274. }
  275. mb->setDirty(scene::EBT_VERTEX);
  276. const u32 quad_count = mb->getVertexCount() / 4;
  277. const u32 index_count = quad_count * 6;
  278. // rewrite index array as needed
  279. if (mb->getIndexCount() > index_count) {
  280. mb->Indices.set_used(index_count);
  281. mb->setDirty(scene::EBT_INDEX);
  282. } else if (mb->getIndexCount() < index_count) {
  283. const u32 start = mb->getIndexCount() / 6;
  284. assert(start * 6 == mb->getIndexCount());
  285. for (u32 k = start; k < quad_count; k++) {
  286. mb->Indices.push_back(4 * k + 0);
  287. mb->Indices.push_back(4 * k + 1);
  288. mb->Indices.push_back(4 * k + 2);
  289. mb->Indices.push_back(4 * k + 2);
  290. mb->Indices.push_back(4 * k + 3);
  291. mb->Indices.push_back(4 * k + 0);
  292. }
  293. mb->setDirty(scene::EBT_INDEX);
  294. }
  295. tracestream << "Cloud::updateMesh(): " << mb->getVertexCount() << " vertices"
  296. << std::endl;
  297. }
  298. void Clouds::render()
  299. {
  300. if (m_params.density <= 0.0f)
  301. return; // no need to do anything
  302. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  303. if (SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
  304. return;
  305. updateMesh();
  306. // Update position
  307. {
  308. v2f off_origin = m_origin - m_mesh_origin;
  309. v3f rel(off_origin.X, 0, off_origin.Y);
  310. rel -= intToFloat(m_camera_offset, BS);
  311. setPosition(rel);
  312. updateAbsolutePosition();
  313. }
  314. m_material.BackfaceCulling = m_enable_3d;
  315. if (m_enable_shaders)
  316. m_material.EmissiveColor = m_color.toSColor();
  317. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  318. driver->setMaterial(m_material);
  319. const float cloud_full_radius = cloud_size * m_cloud_radius_i;
  320. // Get fog parameters for setting them back later
  321. video::SColor fog_color(0,0,0,0);
  322. video::E_FOG_TYPE fog_type = video::EFT_FOG_LINEAR;
  323. f32 fog_start = 0;
  324. f32 fog_end = 0;
  325. f32 fog_density = 0;
  326. bool fog_pixelfog = false;
  327. bool fog_rangefog = false;
  328. driver->getFog(fog_color, fog_type, fog_start, fog_end, fog_density,
  329. fog_pixelfog, fog_rangefog);
  330. // Set our own fog, unless it was already disabled
  331. if (fog_start < FOG_RANGE_ALL) {
  332. driver->setFog(fog_color, fog_type, cloud_full_radius * 0.5,
  333. cloud_full_radius*1.2, fog_density, fog_pixelfog, fog_rangefog);
  334. }
  335. driver->drawMeshBuffer(m_meshbuffer.get());
  336. // Restore fog settings
  337. driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
  338. fog_pixelfog, fog_rangefog);
  339. }
  340. void Clouds::step(float dtime)
  341. {
  342. m_origin = m_origin + dtime * BS * m_params.speed;
  343. }
  344. void Clouds::update(const v3f &camera_p, const video::SColorf &color_diffuse)
  345. {
  346. video::SColorf ambient(m_params.color_ambient);
  347. video::SColorf bright(m_params.color_bright);
  348. m_color.r = core::clamp(color_diffuse.r * bright.r, ambient.r, 1.0f);
  349. m_color.g = core::clamp(color_diffuse.g * bright.g, ambient.g, 1.0f);
  350. m_color.b = core::clamp(color_diffuse.b * bright.b, ambient.b, 1.0f);
  351. m_color.a = bright.a;
  352. // is the camera inside the cloud mesh?
  353. m_camera_pos = camera_p;
  354. m_camera_inside_cloud = false; // default
  355. if (m_enable_3d) {
  356. float camera_height = camera_p.Y - BS * m_camera_offset.Y;
  357. if (camera_height >= m_box.MinEdge.Y &&
  358. camera_height <= m_box.MaxEdge.Y) {
  359. v2f camera_in_noise;
  360. camera_in_noise.X = floor((camera_p.X - m_origin.X) / cloud_size + 0.5);
  361. camera_in_noise.Y = floor((camera_p.Z - m_origin.Y) / cloud_size + 0.5);
  362. bool filled = gridFilled(camera_in_noise.X, camera_in_noise.Y);
  363. m_camera_inside_cloud = filled;
  364. }
  365. }
  366. }
  367. void Clouds::readSettings()
  368. {
  369. // The code isn't designed to go over 64k vertices so the upper limits were
  370. // chosen to avoid exactly that.
  371. // refer to vertex_count in updateMesh()
  372. m_enable_3d = g_settings->getBool("enable_3d_clouds");
  373. const u16 maximum = m_enable_3d ? 62 : 25;
  374. m_cloud_radius_i = rangelim(g_settings->getU16("cloud_radius"), 1, maximum);
  375. invalidateMesh();
  376. }
  377. bool Clouds::gridFilled(int x, int y) const
  378. {
  379. float cloud_size_noise = cloud_size / (BS * 200.f);
  380. float noise = noise2d_perlin(
  381. (float)x * cloud_size_noise,
  382. (float)y * cloud_size_noise,
  383. m_seed, 3, 0.5);
  384. // normalize to 0..1 (given 3 octaves)
  385. static constexpr const float noise_bound = 1.0f + 0.5f + 0.25f;
  386. float density = noise / noise_bound * 0.5f + 0.5f;
  387. return (density < m_params.density);
  388. }