clouds.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 "clouds.h"
  18. #include "noise.h"
  19. #include "constants.h"
  20. #include "debug.h"
  21. #include "profiler.h"
  22. #include "settings.h"
  23. #include <cmath>
  24. // Menu clouds are created later
  25. class Clouds;
  26. Clouds *g_menuclouds = NULL;
  27. scene::ISceneManager *g_menucloudsmgr = NULL;
  28. // Constant for now
  29. static constexpr const float cloud_size = BS * 64.0f;
  30. static void cloud_3d_setting_changed(const std::string &settingname, void *data)
  31. {
  32. ((Clouds *)data)->readSettings();
  33. }
  34. Clouds::Clouds(scene::ISceneManager* mgr,
  35. s32 id,
  36. u32 seed
  37. ):
  38. scene::ISceneNode(mgr->getRootSceneNode(), mgr, id),
  39. m_seed(seed)
  40. {
  41. m_material.setFlag(video::EMF_LIGHTING, false);
  42. //m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
  43. m_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
  44. m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
  45. m_material.setFlag(video::EMF_FOG_ENABLE, true);
  46. m_material.setFlag(video::EMF_ANTI_ALIASING, true);
  47. //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
  48. m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  49. m_params.height = 120;
  50. m_params.density = 0.4f;
  51. m_params.thickness = 16.0f;
  52. m_params.color_bright = video::SColor(229, 240, 240, 255);
  53. m_params.color_ambient = video::SColor(255, 0, 0, 0);
  54. m_params.speed = v2f(0.0f, -2.0f);
  55. readSettings();
  56. g_settings->registerChangedCallback("enable_3d_clouds",
  57. &cloud_3d_setting_changed, this);
  58. updateBox();
  59. }
  60. Clouds::~Clouds()
  61. {
  62. g_settings->deregisterChangedCallback("enable_3d_clouds",
  63. &cloud_3d_setting_changed, this);
  64. }
  65. void Clouds::OnRegisterSceneNode()
  66. {
  67. if(IsVisible)
  68. {
  69. SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
  70. //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
  71. }
  72. ISceneNode::OnRegisterSceneNode();
  73. }
  74. void Clouds::render()
  75. {
  76. if (m_params.density <= 0.0f)
  77. return; // no need to do anything
  78. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  79. if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
  80. //if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
  81. return;
  82. ScopeProfiler sp(g_profiler, "Clouds::render()", SPT_AVG);
  83. int num_faces_to_draw = m_enable_3d ? 6 : 1;
  84. m_material.setFlag(video::EMF_BACK_FACE_CULLING, m_enable_3d);
  85. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  86. driver->setMaterial(m_material);
  87. /*
  88. Clouds move from Z+ towards Z-
  89. */
  90. const float cloud_full_radius = cloud_size * m_cloud_radius_i;
  91. v2f camera_pos_2d(m_camera_pos.X, m_camera_pos.Z);
  92. // Position of cloud noise origin from the camera
  93. v2f cloud_origin_from_camera_f = m_origin - camera_pos_2d;
  94. // The center point of drawing in the noise
  95. v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
  96. // The integer center point of drawing in the noise
  97. v2s16 center_of_drawing_in_noise_i(
  98. std::floor(center_of_drawing_in_noise_f.X / cloud_size),
  99. std::floor(center_of_drawing_in_noise_f.Y / cloud_size)
  100. );
  101. // The world position of the integer center point of drawing in the noise
  102. v2f world_center_of_drawing_in_noise_f = v2f(
  103. center_of_drawing_in_noise_i.X * cloud_size,
  104. center_of_drawing_in_noise_i.Y * cloud_size
  105. ) + m_origin;
  106. /*video::SColor c_top(128,b*240,b*240,b*255);
  107. video::SColor c_side_1(128,b*230,b*230,b*255);
  108. video::SColor c_side_2(128,b*220,b*220,b*245);
  109. video::SColor c_bottom(128,b*205,b*205,b*230);*/
  110. video::SColorf c_top_f(m_color);
  111. video::SColorf c_side_1_f(m_color);
  112. video::SColorf c_side_2_f(m_color);
  113. video::SColorf c_bottom_f(m_color);
  114. c_side_1_f.r *= 0.95;
  115. c_side_1_f.g *= 0.95;
  116. c_side_1_f.b *= 0.95;
  117. c_side_2_f.r *= 0.90;
  118. c_side_2_f.g *= 0.90;
  119. c_side_2_f.b *= 0.90;
  120. c_bottom_f.r *= 0.80;
  121. c_bottom_f.g *= 0.80;
  122. c_bottom_f.b *= 0.80;
  123. video::SColor c_top = c_top_f.toSColor();
  124. video::SColor c_side_1 = c_side_1_f.toSColor();
  125. video::SColor c_side_2 = c_side_2_f.toSColor();
  126. video::SColor c_bottom = c_bottom_f.toSColor();
  127. // Get fog parameters for setting them back later
  128. video::SColor fog_color(0,0,0,0);
  129. video::E_FOG_TYPE fog_type = video::EFT_FOG_LINEAR;
  130. f32 fog_start = 0;
  131. f32 fog_end = 0;
  132. f32 fog_density = 0;
  133. bool fog_pixelfog = false;
  134. bool fog_rangefog = false;
  135. driver->getFog(fog_color, fog_type, fog_start, fog_end, fog_density,
  136. fog_pixelfog, fog_rangefog);
  137. // Set our own fog
  138. driver->setFog(fog_color, fog_type, cloud_full_radius * 0.5,
  139. cloud_full_radius*1.2, fog_density, fog_pixelfog, fog_rangefog);
  140. // Read noise
  141. std::vector<bool> grid(m_cloud_radius_i * 2 * m_cloud_radius_i * 2);
  142. std::vector<video::S3DVertex> vertices;
  143. vertices.reserve(16 * m_cloud_radius_i * m_cloud_radius_i);
  144. for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
  145. u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
  146. for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
  147. u32 i = si + xi;
  148. grid[i] = gridFilled(
  149. xi + center_of_drawing_in_noise_i.X,
  150. zi + center_of_drawing_in_noise_i.Y
  151. );
  152. }
  153. }
  154. #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
  155. #define INAREA(x, z, radius) \
  156. ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
  157. for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
  158. for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
  159. {
  160. s16 zi = zi0;
  161. s16 xi = xi0;
  162. // Draw from back to front for proper transparency
  163. if(zi >= 0)
  164. zi = m_cloud_radius_i - zi - 1;
  165. if(xi >= 0)
  166. xi = m_cloud_radius_i - xi - 1;
  167. u32 i = GETINDEX(xi, zi, m_cloud_radius_i);
  168. if (!grid[i])
  169. continue;
  170. v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
  171. video::S3DVertex v[4] = {
  172. video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
  173. video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
  174. video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
  175. video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
  176. };
  177. const f32 rx = cloud_size / 2.0f;
  178. // if clouds are flat, the top layer should be at the given height
  179. const f32 ry = m_enable_3d ? m_params.thickness * BS : 0.0f;
  180. const f32 rz = cloud_size / 2;
  181. for(int i=0; i<num_faces_to_draw; i++)
  182. {
  183. switch(i)
  184. {
  185. case 0: // top
  186. for (video::S3DVertex &vertex : v) {
  187. vertex.Normal.set(0,1,0);
  188. }
  189. v[0].Pos.set(-rx, ry,-rz);
  190. v[1].Pos.set(-rx, ry, rz);
  191. v[2].Pos.set( rx, ry, rz);
  192. v[3].Pos.set( rx, ry,-rz);
  193. break;
  194. case 1: // back
  195. if (INAREA(xi, zi - 1, m_cloud_radius_i)) {
  196. u32 j = GETINDEX(xi, zi - 1, m_cloud_radius_i);
  197. if(grid[j])
  198. continue;
  199. }
  200. for (video::S3DVertex &vertex : v) {
  201. vertex.Color = c_side_1;
  202. vertex.Normal.set(0,0,-1);
  203. }
  204. v[0].Pos.set(-rx, ry,-rz);
  205. v[1].Pos.set( rx, ry,-rz);
  206. v[2].Pos.set( rx, 0,-rz);
  207. v[3].Pos.set(-rx, 0,-rz);
  208. break;
  209. case 2: //right
  210. if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
  211. u32 j = GETINDEX(xi+1, zi, m_cloud_radius_i);
  212. if(grid[j])
  213. continue;
  214. }
  215. for (video::S3DVertex &vertex : v) {
  216. vertex.Color = c_side_2;
  217. vertex.Normal.set(1,0,0);
  218. }
  219. v[0].Pos.set( rx, ry,-rz);
  220. v[1].Pos.set( rx, ry, rz);
  221. v[2].Pos.set( rx, 0, rz);
  222. v[3].Pos.set( rx, 0,-rz);
  223. break;
  224. case 3: // front
  225. if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
  226. u32 j = GETINDEX(xi, zi + 1, m_cloud_radius_i);
  227. if(grid[j])
  228. continue;
  229. }
  230. for (video::S3DVertex &vertex : v) {
  231. vertex.Color = c_side_1;
  232. vertex.Normal.set(0,0,-1);
  233. }
  234. v[0].Pos.set( rx, ry, rz);
  235. v[1].Pos.set(-rx, ry, rz);
  236. v[2].Pos.set(-rx, 0, rz);
  237. v[3].Pos.set( rx, 0, rz);
  238. break;
  239. case 4: // left
  240. if (INAREA(xi-1, zi, m_cloud_radius_i)) {
  241. u32 j = GETINDEX(xi-1, zi, m_cloud_radius_i);
  242. if(grid[j])
  243. continue;
  244. }
  245. for (video::S3DVertex &vertex : v) {
  246. vertex.Color = c_side_2;
  247. vertex.Normal.set(-1,0,0);
  248. }
  249. v[0].Pos.set(-rx, ry, rz);
  250. v[1].Pos.set(-rx, ry,-rz);
  251. v[2].Pos.set(-rx, 0,-rz);
  252. v[3].Pos.set(-rx, 0, rz);
  253. break;
  254. case 5: // bottom
  255. for (video::S3DVertex &vertex : v) {
  256. vertex.Color = c_bottom;
  257. vertex.Normal.set(0,-1,0);
  258. }
  259. v[0].Pos.set( rx, 0, rz);
  260. v[1].Pos.set(-rx, 0, rz);
  261. v[2].Pos.set(-rx, 0,-rz);
  262. v[3].Pos.set( rx, 0,-rz);
  263. break;
  264. }
  265. v3f pos(p0.X, m_params.height * BS, p0.Y);
  266. pos -= intToFloat(m_camera_offset, BS);
  267. for (video::S3DVertex &vertex : v) {
  268. vertex.Pos += pos;
  269. vertices.push_back(vertex);
  270. }
  271. }
  272. }
  273. int quad_count = vertices.size() / 4;
  274. std::vector<u16> indices;
  275. indices.reserve(quad_count * 6);
  276. for (int k = 0; k < quad_count; k++) {
  277. indices.push_back(4 * k + 0);
  278. indices.push_back(4 * k + 1);
  279. indices.push_back(4 * k + 2);
  280. indices.push_back(4 * k + 2);
  281. indices.push_back(4 * k + 3);
  282. indices.push_back(4 * k + 0);
  283. }
  284. driver->drawVertexPrimitiveList(vertices.data(), vertices.size(), indices.data(), 2 * quad_count,
  285. video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
  286. // Restore fog settings
  287. driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
  288. fog_pixelfog, fog_rangefog);
  289. }
  290. void Clouds::step(float dtime)
  291. {
  292. m_origin = m_origin + dtime * BS * m_params.speed;
  293. }
  294. void Clouds::update(const v3f &camera_p, const video::SColorf &color_diffuse)
  295. {
  296. video::SColorf ambient(m_params.color_ambient);
  297. video::SColorf bright(m_params.color_bright);
  298. m_camera_pos = camera_p;
  299. m_color.r = core::clamp(color_diffuse.r * bright.r, ambient.r, 1.0f);
  300. m_color.g = core::clamp(color_diffuse.g * bright.g, ambient.g, 1.0f);
  301. m_color.b = core::clamp(color_diffuse.b * bright.b, ambient.b, 1.0f);
  302. m_color.a = bright.a;
  303. // is the camera inside the cloud mesh?
  304. m_camera_inside_cloud = false; // default
  305. if (m_enable_3d) {
  306. float camera_height = camera_p.Y - BS * m_camera_offset.Y;
  307. if (camera_height >= m_box.MinEdge.Y &&
  308. camera_height <= m_box.MaxEdge.Y) {
  309. v2f camera_in_noise;
  310. camera_in_noise.X = floor((camera_p.X - m_origin.X) / cloud_size + 0.5);
  311. camera_in_noise.Y = floor((camera_p.Z - m_origin.Y) / cloud_size + 0.5);
  312. bool filled = gridFilled(camera_in_noise.X, camera_in_noise.Y);
  313. m_camera_inside_cloud = filled;
  314. }
  315. }
  316. }
  317. void Clouds::readSettings()
  318. {
  319. // Upper limit was chosen due to posible render bugs
  320. m_cloud_radius_i = rangelim(g_settings->getU16("cloud_radius"), 1, 62);
  321. m_enable_3d = g_settings->getBool("enable_3d_clouds");
  322. }
  323. bool Clouds::gridFilled(int x, int y) const
  324. {
  325. float cloud_size_noise = cloud_size / (BS * 200.f);
  326. float noise = noise2d_perlin(
  327. (float)x * cloud_size_noise,
  328. (float)y * cloud_size_noise,
  329. m_seed, 3, 0.5);
  330. // normalize to 0..1 (given 3 octaves)
  331. static constexpr const float noise_bound = 1.0f + 0.5f + 0.25f;
  332. float density = noise / noise_bound * 0.5f + 0.5f;
  333. return (density < m_params.density);
  334. }