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