clouds.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. irr::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. bool *grid = new bool[m_cloud_radius_i * 2 * m_cloud_radius_i * 2];
  142. for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
  143. u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
  144. for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
  145. u32 i = si + xi;
  146. grid[i] = gridFilled(
  147. xi + center_of_drawing_in_noise_i.X,
  148. zi + center_of_drawing_in_noise_i.Y
  149. );
  150. }
  151. }
  152. #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
  153. #define INAREA(x, z, radius) \
  154. ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
  155. for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
  156. for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
  157. {
  158. s16 zi = zi0;
  159. s16 xi = xi0;
  160. // Draw from front to back (needed for transparency)
  161. /*if(zi <= 0)
  162. zi = -m_cloud_radius_i - zi;
  163. if(xi <= 0)
  164. xi = -m_cloud_radius_i - xi;*/
  165. // Draw from back to front
  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. /*if(zi <= 0 && xi <= 0){
  181. v[0].Color.setBlue(255);
  182. v[1].Color.setBlue(255);
  183. v[2].Color.setBlue(255);
  184. v[3].Color.setBlue(255);
  185. }*/
  186. f32 rx = cloud_size / 2.0f;
  187. // if clouds are flat, the top layer should be at the given height
  188. f32 ry = m_enable_3d ? m_params.thickness * BS : 0.0f;
  189. f32 rz = cloud_size / 2;
  190. for(int i=0; i<num_faces_to_draw; i++)
  191. {
  192. switch(i)
  193. {
  194. case 0: // top
  195. for (video::S3DVertex &vertex : v) {
  196. vertex.Normal.set(0,1,0);
  197. }
  198. v[0].Pos.set(-rx, ry,-rz);
  199. v[1].Pos.set(-rx, ry, rz);
  200. v[2].Pos.set( rx, ry, rz);
  201. v[3].Pos.set( rx, ry,-rz);
  202. break;
  203. case 1: // back
  204. if (INAREA(xi, zi - 1, m_cloud_radius_i)) {
  205. u32 j = GETINDEX(xi, zi - 1, m_cloud_radius_i);
  206. if(grid[j])
  207. continue;
  208. }
  209. for (video::S3DVertex &vertex : v) {
  210. vertex.Color = c_side_1;
  211. vertex.Normal.set(0,0,-1);
  212. }
  213. v[0].Pos.set(-rx, ry,-rz);
  214. v[1].Pos.set( rx, ry,-rz);
  215. v[2].Pos.set( rx, 0,-rz);
  216. v[3].Pos.set(-rx, 0,-rz);
  217. break;
  218. case 2: //right
  219. if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
  220. u32 j = GETINDEX(xi+1, zi, m_cloud_radius_i);
  221. if(grid[j])
  222. continue;
  223. }
  224. for (video::S3DVertex &vertex : v) {
  225. vertex.Color = c_side_2;
  226. vertex.Normal.set(1,0,0);
  227. }
  228. v[0].Pos.set( rx, ry,-rz);
  229. v[1].Pos.set( rx, ry, rz);
  230. v[2].Pos.set( rx, 0, rz);
  231. v[3].Pos.set( rx, 0,-rz);
  232. break;
  233. case 3: // front
  234. if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
  235. u32 j = GETINDEX(xi, zi + 1, m_cloud_radius_i);
  236. if(grid[j])
  237. continue;
  238. }
  239. for (video::S3DVertex &vertex : v) {
  240. vertex.Color = c_side_1;
  241. vertex.Normal.set(0,0,-1);
  242. }
  243. v[0].Pos.set( rx, ry, rz);
  244. v[1].Pos.set(-rx, ry, rz);
  245. v[2].Pos.set(-rx, 0, rz);
  246. v[3].Pos.set( rx, 0, rz);
  247. break;
  248. case 4: // left
  249. if (INAREA(xi-1, zi, m_cloud_radius_i)) {
  250. u32 j = GETINDEX(xi-1, zi, m_cloud_radius_i);
  251. if(grid[j])
  252. continue;
  253. }
  254. for (video::S3DVertex &vertex : v) {
  255. vertex.Color = c_side_2;
  256. vertex.Normal.set(-1,0,0);
  257. }
  258. v[0].Pos.set(-rx, ry, rz);
  259. v[1].Pos.set(-rx, ry,-rz);
  260. v[2].Pos.set(-rx, 0,-rz);
  261. v[3].Pos.set(-rx, 0, rz);
  262. break;
  263. case 5: // bottom
  264. for (video::S3DVertex &vertex : v) {
  265. vertex.Color = c_bottom;
  266. vertex.Normal.set(0,-1,0);
  267. }
  268. v[0].Pos.set( rx, 0, rz);
  269. v[1].Pos.set(-rx, 0, rz);
  270. v[2].Pos.set(-rx, 0,-rz);
  271. v[3].Pos.set( rx, 0,-rz);
  272. break;
  273. }
  274. v3f pos(p0.X, m_params.height * BS, p0.Y);
  275. pos -= intToFloat(m_camera_offset, BS);
  276. for (video::S3DVertex &vertex : v)
  277. vertex.Pos += pos;
  278. u16 indices[] = {0,1,2,2,3,0};
  279. driver->drawVertexPrimitiveList(v, 4, indices, 2,
  280. video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
  281. }
  282. }
  283. delete[] grid;
  284. // Restore fog settings
  285. driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
  286. fog_pixelfog, fog_rangefog);
  287. }
  288. void Clouds::step(float dtime)
  289. {
  290. m_origin = m_origin + dtime * BS * m_params.speed;
  291. }
  292. void Clouds::update(const v3f &camera_p, const video::SColorf &color_diffuse)
  293. {
  294. m_camera_pos = camera_p;
  295. m_color.r = MYMIN(MYMAX(color_diffuse.r * m_params.color_bright.getRed(),
  296. m_params.color_ambient.getRed()), 255) / 255.0f;
  297. m_color.g = MYMIN(MYMAX(color_diffuse.g * m_params.color_bright.getGreen(),
  298. m_params.color_ambient.getGreen()), 255) / 255.0f;
  299. m_color.b = MYMIN(MYMAX(color_diffuse.b * m_params.color_bright.getBlue(),
  300. m_params.color_ambient.getBlue()), 255) / 255.0f;
  301. m_color.a = m_params.color_bright.getAlpha() / 255.0f;
  302. // is the camera inside the cloud mesh?
  303. m_camera_inside_cloud = false; // default
  304. if (m_enable_3d) {
  305. float camera_height = camera_p.Y;
  306. if (camera_height >= m_box.MinEdge.Y &&
  307. camera_height <= m_box.MaxEdge.Y) {
  308. v2f camera_in_noise;
  309. camera_in_noise.X = floor((camera_p.X - m_origin.X) / cloud_size + 0.5);
  310. camera_in_noise.Y = floor((camera_p.Z - m_origin.Y) / cloud_size + 0.5);
  311. bool filled = gridFilled(camera_in_noise.X, camera_in_noise.Y);
  312. m_camera_inside_cloud = filled;
  313. }
  314. }
  315. }
  316. void Clouds::readSettings()
  317. {
  318. m_cloud_radius_i = g_settings->getU16("cloud_radius");
  319. m_enable_3d = g_settings->getBool("enable_3d_clouds");
  320. }
  321. bool Clouds::gridFilled(int x, int y) const
  322. {
  323. float cloud_size_noise = cloud_size / (BS * 200.f);
  324. float noise = noise2d_perlin(
  325. (float)x * cloud_size_noise,
  326. (float)y * cloud_size_noise,
  327. m_seed, 3, 0.5);
  328. // normalize to 0..1 (given 3 octaves)
  329. static constexpr const float noise_bound = 1.0f + 0.5f + 0.25f;
  330. float density = noise / noise_bound * 0.5f + 0.5f;
  331. return (density < m_params.density);
  332. }