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