]> git.lizzy.rs Git - minetest.git/blob - src/clouds.cpp
Fog effect when camera is inside cloud
[minetest.git] / src / clouds.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "client/renderingengine.h"
21 #include "clouds.h"
22 #include "noise.h"
23 #include "constants.h"
24 #include "debug.h"
25 #include "profiler.h"
26 #include "settings.h"
27 #include <cmath>
28
29
30 // Menu clouds are created later
31 class Clouds;
32 Clouds *g_menuclouds = NULL;
33 irr::scene::ISceneManager *g_menucloudsmgr = NULL;
34
35 // Constant for now
36 static constexpr const float cloud_size = BS * 64.0f;
37
38 static void cloud_3d_setting_changed(const std::string &settingname, void *data)
39 {
40         // TODO: only re-read cloud settings, not height or radius
41         ((Clouds *)data)->readSettings();
42 }
43
44 Clouds::Clouds(scene::ISceneManager* mgr,
45                 s32 id,
46                 u32 seed,
47                 s16 cloudheight
48 ):
49         scene::ISceneNode(mgr->getRootSceneNode(), mgr, id),
50         m_seed(seed)
51 {
52         m_material.setFlag(video::EMF_LIGHTING, false);
53         //m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
54         m_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
55         m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
56         m_material.setFlag(video::EMF_FOG_ENABLE, true);
57         m_material.setFlag(video::EMF_ANTI_ALIASING, true);
58         //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
59         m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
60
61         m_params.density       = 0.4f;
62         m_params.thickness     = 16.0f;
63         m_params.color_bright  = video::SColor(229, 240, 240, 255);
64         m_params.color_ambient = video::SColor(255, 0, 0, 0);
65         m_params.speed         = v2f(0.0f, -2.0f);
66
67         m_passed_cloud_y = cloudheight;
68         readSettings();
69         g_settings->registerChangedCallback("enable_3d_clouds",
70                 &cloud_3d_setting_changed, this);
71
72         updateBox();
73 }
74
75 Clouds::~Clouds()
76 {
77         g_settings->deregisterChangedCallback("enable_3d_clouds",
78                 &cloud_3d_setting_changed, this);
79 }
80
81 void Clouds::OnRegisterSceneNode()
82 {
83         if(IsVisible)
84         {
85                 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
86                 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
87         }
88
89         ISceneNode::OnRegisterSceneNode();
90 }
91
92 void Clouds::render()
93 {
94
95         if (m_params.density <= 0.0f)
96                 return; // no need to do anything
97
98         video::IVideoDriver* driver = SceneManager->getVideoDriver();
99
100         if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
101         //if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
102                 return;
103
104         ScopeProfiler sp(g_profiler, "Rendering of clouds, avg", SPT_AVG);
105
106         int num_faces_to_draw = m_enable_3d ? 6 : 1;
107
108         m_material.setFlag(video::EMF_BACK_FACE_CULLING, m_enable_3d);
109
110         driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
111         driver->setMaterial(m_material);
112
113         /*
114                 Clouds move from Z+ towards Z-
115         */
116
117         const float cloud_full_radius = cloud_size * m_cloud_radius_i;
118
119         v2f camera_pos_2d(m_camera_pos.X, m_camera_pos.Z);
120         // Position of cloud noise origin from the camera
121         v2f cloud_origin_from_camera_f = m_origin - camera_pos_2d;
122         // The center point of drawing in the noise
123         v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
124         // The integer center point of drawing in the noise
125         v2s16 center_of_drawing_in_noise_i(
126                 std::floor(center_of_drawing_in_noise_f.X / cloud_size),
127                 std::floor(center_of_drawing_in_noise_f.Y / cloud_size)
128         );
129
130         // The world position of the integer center point of drawing in the noise
131         v2f world_center_of_drawing_in_noise_f = v2f(
132                 center_of_drawing_in_noise_i.X * cloud_size,
133                 center_of_drawing_in_noise_i.Y * cloud_size
134         ) + m_origin;
135
136         /*video::SColor c_top(128,b*240,b*240,b*255);
137         video::SColor c_side_1(128,b*230,b*230,b*255);
138         video::SColor c_side_2(128,b*220,b*220,b*245);
139         video::SColor c_bottom(128,b*205,b*205,b*230);*/
140         video::SColorf c_top_f(m_color);
141         video::SColorf c_side_1_f(m_color);
142         video::SColorf c_side_2_f(m_color);
143         video::SColorf c_bottom_f(m_color);
144         c_side_1_f.r *= 0.95;
145         c_side_1_f.g *= 0.95;
146         c_side_1_f.b *= 0.95;
147         c_side_2_f.r *= 0.90;
148         c_side_2_f.g *= 0.90;
149         c_side_2_f.b *= 0.90;
150         c_bottom_f.r *= 0.80;
151         c_bottom_f.g *= 0.80;
152         c_bottom_f.b *= 0.80;
153         video::SColor c_top = c_top_f.toSColor();
154         video::SColor c_side_1 = c_side_1_f.toSColor();
155         video::SColor c_side_2 = c_side_2_f.toSColor();
156         video::SColor c_bottom = c_bottom_f.toSColor();
157
158         // Get fog parameters for setting them back later
159         video::SColor fog_color(0,0,0,0);
160         video::E_FOG_TYPE fog_type = video::EFT_FOG_LINEAR;
161         f32 fog_start = 0;
162         f32 fog_end = 0;
163         f32 fog_density = 0;
164         bool fog_pixelfog = false;
165         bool fog_rangefog = false;
166         driver->getFog(fog_color, fog_type, fog_start, fog_end, fog_density,
167                         fog_pixelfog, fog_rangefog);
168
169         // Set our own fog
170         driver->setFog(fog_color, fog_type, cloud_full_radius * 0.5,
171                         cloud_full_radius*1.2, fog_density, fog_pixelfog, fog_rangefog);
172
173         // Read noise
174
175         bool *grid = new bool[m_cloud_radius_i * 2 * m_cloud_radius_i * 2];
176
177
178         for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
179                 u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
180
181                 for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
182                         u32 i = si + xi;
183
184                         grid[i] = gridFilled(
185                                 xi + center_of_drawing_in_noise_i.X,
186                                 zi + center_of_drawing_in_noise_i.Y
187                         );
188                 }
189         }
190
191 #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
192 #define INAREA(x, z, radius) \
193         ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
194
195         for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
196         for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
197         {
198                 s16 zi = zi0;
199                 s16 xi = xi0;
200                 // Draw from front to back (needed for transparency)
201                 /*if(zi <= 0)
202                         zi = -m_cloud_radius_i - zi;
203                 if(xi <= 0)
204                         xi = -m_cloud_radius_i - xi;*/
205                 // Draw from back to front
206                 if(zi >= 0)
207                         zi = m_cloud_radius_i - zi - 1;
208                 if(xi >= 0)
209                         xi = m_cloud_radius_i - xi - 1;
210
211                 u32 i = GETINDEX(xi, zi, m_cloud_radius_i);
212
213                 if(grid[i] == false)
214                         continue;
215
216                 v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
217
218                 video::S3DVertex v[4] = {
219                         video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
220                         video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
221                         video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
222                         video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
223                 };
224
225                 /*if(zi <= 0 && xi <= 0){
226                         v[0].Color.setBlue(255);
227                         v[1].Color.setBlue(255);
228                         v[2].Color.setBlue(255);
229                         v[3].Color.setBlue(255);
230                 }*/
231
232                 f32 rx = cloud_size / 2.0f;
233                 // if clouds are flat, the top layer should be at the given height
234                 f32 ry = m_enable_3d ? m_params.thickness * BS : 0.0f;
235                 f32 rz = cloud_size / 2;
236
237                 for(int i=0; i<num_faces_to_draw; i++)
238                 {
239                         switch(i)
240                         {
241                         case 0: // top
242                                 for(int j=0;j<4;j++){
243                                         v[j].Normal.set(0,1,0);
244                                 }
245                                 v[0].Pos.set(-rx, ry,-rz);
246                                 v[1].Pos.set(-rx, ry, rz);
247                                 v[2].Pos.set( rx, ry, rz);
248                                 v[3].Pos.set( rx, ry,-rz);
249                                 break;
250                         case 1: // back
251                                 if (INAREA(xi, zi - 1, m_cloud_radius_i)) {
252                                         u32 j = GETINDEX(xi, zi - 1, m_cloud_radius_i);
253                                         if(grid[j])
254                                                 continue;
255                                 }
256                                 for(int j=0;j<4;j++){
257                                         v[j].Color = c_side_1;
258                                         v[j].Normal.set(0,0,-1);
259                                 }
260                                 v[0].Pos.set(-rx, ry,-rz);
261                                 v[1].Pos.set( rx, ry,-rz);
262                                 v[2].Pos.set( rx,  0,-rz);
263                                 v[3].Pos.set(-rx,  0,-rz);
264                                 break;
265                         case 2: //right
266                                 if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
267                                         u32 j = GETINDEX(xi+1, zi, m_cloud_radius_i);
268                                         if(grid[j])
269                                                 continue;
270                                 }
271                                 for(int j=0;j<4;j++){
272                                         v[j].Color = c_side_2;
273                                         v[j].Normal.set(1,0,0);
274                                 }
275                                 v[0].Pos.set( rx, ry,-rz);
276                                 v[1].Pos.set( rx, ry, rz);
277                                 v[2].Pos.set( rx,  0, rz);
278                                 v[3].Pos.set( rx,  0,-rz);
279                                 break;
280                         case 3: // front
281                                 if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
282                                         u32 j = GETINDEX(xi, zi + 1, m_cloud_radius_i);
283                                         if(grid[j])
284                                                 continue;
285                                 }
286                                 for(int j=0;j<4;j++){
287                                         v[j].Color = c_side_1;
288                                         v[j].Normal.set(0,0,-1);
289                                 }
290                                 v[0].Pos.set( rx, ry, rz);
291                                 v[1].Pos.set(-rx, ry, rz);
292                                 v[2].Pos.set(-rx,  0, rz);
293                                 v[3].Pos.set( rx,  0, rz);
294                                 break;
295                         case 4: // left
296                                 if (INAREA(xi-1, zi, m_cloud_radius_i)) {
297                                         u32 j = GETINDEX(xi-1, zi, m_cloud_radius_i);
298                                         if(grid[j])
299                                                 continue;
300                                 }
301                                 for(int j=0;j<4;j++){
302                                         v[j].Color = c_side_2;
303                                         v[j].Normal.set(-1,0,0);
304                                 }
305                                 v[0].Pos.set(-rx, ry, rz);
306                                 v[1].Pos.set(-rx, ry,-rz);
307                                 v[2].Pos.set(-rx,  0,-rz);
308                                 v[3].Pos.set(-rx,  0, rz);
309                                 break;
310                         case 5: // bottom
311                                 for(int j=0;j<4;j++){
312                                         v[j].Color = c_bottom;
313                                         v[j].Normal.set(0,-1,0);
314                                 }
315                                 v[0].Pos.set( rx,  0, rz);
316                                 v[1].Pos.set(-rx,  0, rz);
317                                 v[2].Pos.set(-rx,  0,-rz);
318                                 v[3].Pos.set( rx,  0,-rz);
319                                 break;
320                         }
321
322                         v3f pos(p0.X, m_params.height * BS, p0.Y);
323                         pos -= intToFloat(m_camera_offset, BS);
324
325                         for(u16 i=0; i<4; i++)
326                                 v[i].Pos += pos;
327                         u16 indices[] = {0,1,2,2,3,0};
328                         driver->drawVertexPrimitiveList(v, 4, indices, 2,
329                                         video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
330                 }
331         }
332
333         delete[] grid;
334
335         // Restore fog settings
336         driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
337                         fog_pixelfog, fog_rangefog);
338 }
339
340 void Clouds::step(float dtime)
341 {
342         m_origin = m_origin + dtime * BS * m_params.speed;
343 }
344
345 void Clouds::update(const v3f &camera_p, const video::SColorf &color_diffuse)
346 {
347         m_camera_pos = camera_p;
348         m_color.r = MYMIN(MYMAX(color_diffuse.r * m_params.color_bright.getRed(),
349                         m_params.color_ambient.getRed()), 255) / 255.0f;
350         m_color.g = MYMIN(MYMAX(color_diffuse.g * m_params.color_bright.getGreen(),
351                         m_params.color_ambient.getGreen()), 255) / 255.0f;
352         m_color.b = MYMIN(MYMAX(color_diffuse.b * m_params.color_bright.getBlue(),
353                         m_params.color_ambient.getBlue()), 255) / 255.0f;
354         m_color.a = m_params.color_bright.getAlpha() / 255.0f;
355
356         // is the camera inside the cloud mesh?
357         m_camera_inside_cloud = false; // default
358         if (m_enable_3d) {
359                 float camera_height = camera_p.Y;
360                 if (camera_height >= m_box.MinEdge.Y &&
361                                 camera_height <= m_box.MaxEdge.Y) {
362                         v2f camera_in_noise;
363                         camera_in_noise.X = floor((camera_p.X - m_origin.X) / cloud_size + 0.5);
364                         camera_in_noise.Y = floor((camera_p.Z - m_origin.Y) / cloud_size + 0.5);
365                         bool filled = gridFilled(camera_in_noise.X, camera_in_noise.Y);
366                         m_camera_inside_cloud = filled;
367                 }
368         }
369 }
370
371 void Clouds::readSettings()
372 {
373         m_params.height = (m_passed_cloud_y ? m_passed_cloud_y :
374                 g_settings->getS16("cloud_height"));
375         m_cloud_radius_i = g_settings->getU16("cloud_radius");
376         m_enable_3d = g_settings->getBool("enable_3d_clouds");
377 }
378
379 bool Clouds::gridFilled(int x, int y) const
380 {
381         float cloud_size_noise = cloud_size / (BS * 200.f);
382         float noise = noise2d_perlin(
383                         (float)x * cloud_size_noise,
384                         (float)y * cloud_size_noise,
385                         m_seed, 3, 0.5);
386         // normalize to 0..1 (given 3 octaves)
387         static constexpr const float noise_bound = 1.0f + 0.5f + 0.25f;
388         float density = noise / noise_bound * 0.5f + 0.5f;
389         return (density < m_params.density);
390 }