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