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