v2d & aabbox3d<f32> & sky cleanups
[oweals/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         ((Clouds *)data)->readSettings();
36 }
37
38 Clouds::Clouds(
39                 scene::ISceneNode* parent,
40                 scene::ISceneManager* mgr,
41                 s32 id,
42                 u32 seed,
43                 s16 cloudheight
44 ):
45         scene::ISceneNode(parent, mgr, id),
46         m_seed(seed),
47         m_camera_pos(0,0),
48         m_time(0),
49         m_camera_offset(0,0,0)
50 {
51         m_material.setFlag(video::EMF_LIGHTING, false);
52         //m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
53         m_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
54         m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
55         m_material.setFlag(video::EMF_FOG_ENABLE, true);
56         m_material.setFlag(video::EMF_ANTI_ALIASING, true);
57         //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
58         m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
59
60         m_passed_cloud_y = cloudheight;
61         readSettings();
62         g_settings->registerChangedCallback("enable_3d_clouds",
63                 &cloud_3d_setting_changed, this);
64
65         m_box = aabb3f(-BS*1000000,m_cloud_y-BS,-BS*1000000,
66                         BS*1000000,m_cloud_y+BS,BS*1000000);
67
68 }
69
70 Clouds::~Clouds()
71 {
72         g_settings->deregisterChangedCallback("enable_3d_clouds",
73                 &cloud_3d_setting_changed, this);
74 }
75
76 void Clouds::OnRegisterSceneNode()
77 {
78         if(IsVisible)
79         {
80                 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
81                 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
82         }
83
84         ISceneNode::OnRegisterSceneNode();
85 }
86
87 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
88
89 void Clouds::render()
90 {
91         video::IVideoDriver* driver = SceneManager->getVideoDriver();
92
93         if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
94         //if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
95                 return;
96
97         ScopeProfiler sp(g_profiler, "Rendering of clouds, avg", SPT_AVG);
98         
99         int num_faces_to_draw = m_enable_3d ? 6 : 1;
100         
101         m_material.setFlag(video::EMF_BACK_FACE_CULLING, m_enable_3d);
102
103         driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
104         driver->setMaterial(m_material);
105         
106         /*
107                 Clouds move from Z+ towards Z-
108         */
109
110         const float cloud_size = BS * 64;
111         const v2f cloud_speed(0, -BS * 2);
112         
113         const float cloud_full_radius = cloud_size * m_cloud_radius_i;
114         
115         // Position of cloud noise origin in world coordinates
116         v2f world_cloud_origin_pos_f = m_time * cloud_speed;
117         // Position of cloud noise origin from the camera
118         v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
119         // The center point of drawing in the noise
120         v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
121         // The integer center point of drawing in the noise
122         v2s16 center_of_drawing_in_noise_i(
123                 MYROUND(center_of_drawing_in_noise_f.X / cloud_size),
124                 MYROUND(center_of_drawing_in_noise_f.Y / cloud_size)
125         );
126         // The world position of the integer center point of drawing in the noise
127         v2f world_center_of_drawing_in_noise_f = v2f(
128                 center_of_drawing_in_noise_i.X * cloud_size,
129                 center_of_drawing_in_noise_i.Y * cloud_size
130         ) + world_cloud_origin_pos_f;
131
132         /*video::SColor c_top(128,b*240,b*240,b*255);
133         video::SColor c_side_1(128,b*230,b*230,b*255);
134         video::SColor c_side_2(128,b*220,b*220,b*245);
135         video::SColor c_bottom(128,b*205,b*205,b*230);*/
136         video::SColorf c_top_f(m_color);
137         video::SColorf c_side_1_f(m_color);
138         video::SColorf c_side_2_f(m_color);
139         video::SColorf c_bottom_f(m_color);
140         c_side_1_f.r *= 0.95;
141         c_side_1_f.g *= 0.95;
142         c_side_1_f.b *= 0.95;
143         c_side_2_f.r *= 0.90;
144         c_side_2_f.g *= 0.90;
145         c_side_2_f.b *= 0.90;
146         c_bottom_f.r *= 0.80;
147         c_bottom_f.g *= 0.80;
148         c_bottom_f.b *= 0.80;
149         c_top_f.a = 0.9;
150         c_side_1_f.a = 0.9;
151         c_side_2_f.a = 0.9;
152         c_bottom_f.a = 0.9;
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         float cloud_size_noise = cloud_size / BS / 200;
178
179         for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
180                 u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
181
182                 for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
183                         u32 i = si + xi;
184
185                         v2s16 p_in_noise_i(
186                                 xi + center_of_drawing_in_noise_i.X,
187                                 zi + center_of_drawing_in_noise_i.Y
188                         );
189
190                         double noise = noise2d_perlin(
191                                         (float)p_in_noise_i.X * cloud_size_noise,
192                                         (float)p_in_noise_i.Y * cloud_size_noise,
193                                         m_seed, 3, 0.5);
194                         grid[i] = (noise >= 0.4);
195                 }
196         }
197
198 #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
199 #define INAREA(x, z, radius) \
200         ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
201
202         for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
203         for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
204         {
205                 s16 zi = zi0;
206                 s16 xi = xi0;
207                 // Draw from front to back (needed for transparency)
208                 /*if(zi <= 0)
209                         zi = -m_cloud_radius_i - zi;
210                 if(xi <= 0)
211                         xi = -m_cloud_radius_i - xi;*/
212                 // Draw from back to front
213                 if(zi >= 0)
214                         zi = m_cloud_radius_i - zi - 1;
215                 if(xi >= 0)
216                         xi = m_cloud_radius_i - xi - 1;
217
218                 u32 i = GETINDEX(xi, zi, m_cloud_radius_i);
219
220                 if(grid[i] == false)
221                         continue;
222
223                 v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
224
225                 video::S3DVertex v[4] = {
226                         video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
227                         video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
228                         video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
229                         video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
230                 };
231
232                 /*if(zi <= 0 && xi <= 0){
233                         v[0].Color.setBlue(255);
234                         v[1].Color.setBlue(255);
235                         v[2].Color.setBlue(255);
236                         v[3].Color.setBlue(255);
237                 }*/
238
239                 f32 rx = cloud_size/2;
240                 f32 ry = 8 * BS;
241                 f32 rz = cloud_size / 2;
242
243                 for(int i=0; i<num_faces_to_draw; i++)
244                 {
245                         switch(i)
246                         {
247                         case 0: // top
248                                 for(int j=0;j<4;j++){
249                                         v[j].Normal.set(0,1,0);
250                                 }
251                                 v[0].Pos.set(-rx, ry,-rz);
252                                 v[1].Pos.set(-rx, ry, rz);
253                                 v[2].Pos.set( rx, ry, rz);
254                                 v[3].Pos.set( rx, ry,-rz);
255                                 break;
256                         case 1: // back
257                                 if (INAREA(xi, zi - 1, m_cloud_radius_i)) {
258                                         u32 j = GETINDEX(xi, zi - 1, m_cloud_radius_i);
259                                         if(grid[j])
260                                                 continue;
261                                 }
262                                 for(int j=0;j<4;j++){
263                                         v[j].Color = c_side_1;
264                                         v[j].Normal.set(0,0,-1);
265                                 }
266                                 v[0].Pos.set(-rx, ry,-rz);
267                                 v[1].Pos.set( rx, ry,-rz);
268                                 v[2].Pos.set( rx,-ry,-rz);
269                                 v[3].Pos.set(-rx,-ry,-rz);
270                                 break;
271                         case 2: //right
272                                 if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
273                                         u32 j = GETINDEX(xi+1, zi, m_cloud_radius_i);
274                                         if(grid[j])
275                                                 continue;
276                                 }
277                                 for(int j=0;j<4;j++){
278                                         v[j].Color = c_side_2;
279                                         v[j].Normal.set(1,0,0);
280                                 }
281                                 v[0].Pos.set( rx, ry,-rz);
282                                 v[1].Pos.set( rx, ry, rz);
283                                 v[2].Pos.set( rx,-ry, rz);
284                                 v[3].Pos.set( rx,-ry,-rz);
285                                 break;
286                         case 3: // front
287                                 if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
288                                         u32 j = GETINDEX(xi, zi + 1, m_cloud_radius_i);
289                                         if(grid[j])
290                                                 continue;
291                                 }
292                                 for(int j=0;j<4;j++){
293                                         v[j].Color = c_side_1;
294                                         v[j].Normal.set(0,0,-1);
295                                 }
296                                 v[0].Pos.set( rx, ry, rz);
297                                 v[1].Pos.set(-rx, ry, rz);
298                                 v[2].Pos.set(-rx,-ry, rz);
299                                 v[3].Pos.set( rx,-ry, rz);
300                                 break;
301                         case 4: // left
302                                 if (INAREA(xi-1, zi, m_cloud_radius_i)) {
303                                         u32 j = GETINDEX(xi-1, zi, m_cloud_radius_i);
304                                         if(grid[j])
305                                                 continue;
306                                 }
307                                 for(int j=0;j<4;j++){
308                                         v[j].Color = c_side_2;
309                                         v[j].Normal.set(-1,0,0);
310                                 }
311                                 v[0].Pos.set(-rx, ry, rz);
312                                 v[1].Pos.set(-rx, ry,-rz);
313                                 v[2].Pos.set(-rx,-ry,-rz);
314                                 v[3].Pos.set(-rx,-ry, rz);
315                                 break;
316                         case 5: // bottom
317                                 for(int j=0;j<4;j++){
318                                         v[j].Color = c_bottom;
319                                         v[j].Normal.set(0,-1,0);
320                                 }
321                                 v[0].Pos.set( rx,-ry, rz);
322                                 v[1].Pos.set(-rx,-ry, rz);
323                                 v[2].Pos.set(-rx,-ry,-rz);
324                                 v[3].Pos.set( rx,-ry,-rz);
325                                 break;
326                         }
327
328                         v3f pos(p0.X, m_cloud_y, p0.Y);
329                         pos -= intToFloat(m_camera_offset, BS);
330
331                         for(u16 i=0; i<4; i++)
332                                 v[i].Pos += pos;
333                         u16 indices[] = {0,1,2,2,3,0};
334                         driver->drawVertexPrimitiveList(v, 4, indices, 2,
335                                         video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
336                 }
337         }
338
339         delete[] grid;
340         
341         // Restore fog settings
342         driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
343                         fog_pixelfog, fog_rangefog);
344 }
345
346 void Clouds::step(float dtime)
347 {
348         m_time += dtime;
349 }
350
351 void Clouds::update(v2f camera_p, video::SColorf color)
352 {
353         m_camera_pos = camera_p;
354         m_color = color;
355         //m_brightness = brightness;
356         //dstream<<"m_brightness="<<m_brightness<<std::endl;
357 }
358
359 void Clouds::readSettings()
360 {
361         m_cloud_y = BS * (m_passed_cloud_y ? m_passed_cloud_y :
362                 g_settings->getS16("cloud_height"));
363         m_cloud_radius_i = g_settings->getU16("cloud_radius");
364         m_enable_3d = g_settings->getBool("enable_3d_clouds");
365 }
366