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