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