Skip to content

Commit f1d7a26

Browse files
bendeutschparamat
authored andcommittedApr 29, 2017
Add clouds API
1 parent 95409da commit f1d7a26

17 files changed

+357
-50
lines changed
 

‎doc/lua_api.txt

+9
Original file line numberDiff line numberDiff line change
@@ -3077,6 +3077,15 @@ This is basically a reference to a C++ `ServerActiveObject`
30773077
* `"skybox"`: Uses 6 textures, `bgcolor` used
30783078
* `"plain"`: Uses 0 textures, `bgcolor` used
30793079
* `get_sky()`: returns bgcolor, type and a table with the textures
3080+
* `set_clouds(parameters)`: set cloud parameters
3081+
* `parameters` is a table with the following optional fields:
3082+
* `density`: from `0` (no clouds) to `1` (full clouds) (default `0.4`)
3083+
* `color`: basic cloud color, with alpha channel (default `#fff0f0e5`)
3084+
* `ambient`: cloud color lower bound, use for a "glow at night" effect (default `#000000`)
3085+
* `height`: cloud height, i.e. y of cloud base (default per conf, usually `120`)
3086+
* `thickness`: cloud thickness in nodes (default `16`)
3087+
* `speed`: 2D cloud speed + direction in nodes per second (default `{x=0, y=-2}`)
3088+
* `get_clouds()`: returns a table with the current cloud parameters as in `set_clouds`
30803089
* `override_day_night_ratio(ratio or nil)`
30813090
* `0`...`1`: Overrides day-night ratio, controlling sunlight to a specific amount
30823091
* `nil`: Disables override, defaulting to sunlight based on day-night cycle

‎src/client.h

+11
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ enum ClientEventType
7777
CE_HUDCHANGE,
7878
CE_SET_SKY,
7979
CE_OVERRIDE_DAY_NIGHT_RATIO,
80+
CE_CLOUD_PARAMS,
8081
};
8182

8283
struct ClientEvent
@@ -178,6 +179,15 @@ struct ClientEvent
178179
bool do_override;
179180
float ratio_f;
180181
} override_day_night_ratio;
182+
struct {
183+
f32 density;
184+
u32 color_bright;
185+
u32 color_ambient;
186+
f32 height;
187+
f32 thickness;
188+
f32 speed_x;
189+
f32 speed_y;
190+
} cloud_params;
181191
};
182192
};
183193

@@ -331,6 +341,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
331341
void handleCommand_HudSetFlags(NetworkPacket* pkt);
332342
void handleCommand_HudSetParam(NetworkPacket* pkt);
333343
void handleCommand_HudSetSky(NetworkPacket* pkt);
344+
void handleCommand_CloudParams(NetworkPacket* pkt);
334345
void handleCommand_OverrideDayNightRatio(NetworkPacket* pkt);
335346
void handleCommand_LocalPlayerAnimations(NetworkPacket* pkt);
336347
void handleCommand_EyeOffset(NetworkPacket* pkt);

‎src/cloudparams.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2017 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
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+
#ifndef CLOUDPARAMS_HEADER
21+
#define CLOUDPARAMS_HEADER
22+
23+
struct CloudParams
24+
{
25+
float density;
26+
video::SColor color_bright;
27+
video::SColor color_ambient;
28+
float thickness;
29+
float height;
30+
v2f speed;
31+
};
32+
33+
#endif

‎src/clouds.cpp

+50-40
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ irr::scene::ISceneManager *g_menucloudsmgr = NULL;
3232

3333
static void cloud_3d_setting_changed(const std::string &settingname, void *data)
3434
{
35+
// TODO: only re-read cloud settings, not height or radius
3536
((Clouds *)data)->readSettings();
3637
}
3738

@@ -44,9 +45,10 @@ Clouds::Clouds(
4445
):
4546
scene::ISceneNode(parent, mgr, id),
4647
m_seed(seed),
47-
m_camera_pos(0,0),
48-
m_time(0),
49-
m_camera_offset(0,0,0)
48+
m_camera_pos(0.0f, 0.0f),
49+
m_origin(0.0f, 0.0f),
50+
m_camera_offset(0.0f, 0.0f, 0.0f),
51+
m_color(1.0f, 1.0f, 1.0f, 1.0f)
5052
{
5153
m_material.setFlag(video::EMF_LIGHTING, false);
5254
//m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
@@ -57,14 +59,18 @@ Clouds::Clouds(
5759
//m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
5860
m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
5961

62+
m_params.density = 0.4f;
63+
m_params.thickness = 16.0f;
64+
m_params.color_bright = video::SColor(229, 255, 240, 240);
65+
m_params.color_ambient = video::SColor(255, 0, 0, 0);
66+
m_params.speed = v2f(0.0f, -2.0f);
67+
6068
m_passed_cloud_y = cloudheight;
6169
readSettings();
6270
g_settings->registerChangedCallback("enable_3d_clouds",
6371
&cloud_3d_setting_changed, this);
6472

65-
m_box = aabb3f(-BS*1000000,m_cloud_y-BS,-BS*1000000,
66-
BS*1000000,m_cloud_y+BS,BS*1000000);
67-
73+
updateBox();
6874
}
6975

7076
Clouds::~Clouds()
@@ -88,6 +94,10 @@ void Clouds::OnRegisterSceneNode()
8894

8995
void Clouds::render()
9096
{
97+
98+
if (m_params.density <= 0.0f)
99+
return; // no need to do anything
100+
91101
video::IVideoDriver* driver = SceneManager->getVideoDriver();
92102

93103
if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
@@ -107,15 +117,12 @@ void Clouds::render()
107117
Clouds move from Z+ towards Z-
108118
*/
109119

110-
const float cloud_size = BS * 64;
111-
const v2f cloud_speed(0, -BS * 2);
120+
static const float cloud_size = BS * 64.0f;
112121

113122
const float cloud_full_radius = cloud_size * m_cloud_radius_i;
114123

115-
// Position of cloud noise origin in world coordinates
116-
v2f world_cloud_origin_pos_f = m_time * cloud_speed;
117124
// Position of cloud noise origin from the camera
118-
v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
125+
v2f cloud_origin_from_camera_f = m_origin - m_camera_pos;
119126
// The center point of drawing in the noise
120127
v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
121128
// The integer center point of drawing in the noise
@@ -127,7 +134,7 @@ void Clouds::render()
127134
v2f world_center_of_drawing_in_noise_f = v2f(
128135
center_of_drawing_in_noise_i.X * cloud_size,
129136
center_of_drawing_in_noise_i.Y * cloud_size
130-
) + world_cloud_origin_pos_f;
137+
) + m_origin;
131138

132139
/*video::SColor c_top(128,b*240,b*240,b*255);
133140
video::SColor c_side_1(128,b*230,b*230,b*255);
@@ -146,10 +153,6 @@ void Clouds::render()
146153
c_bottom_f.r *= 0.80;
147154
c_bottom_f.g *= 0.80;
148155
c_bottom_f.b *= 0.80;
149-
c_top_f.a = 0.9;
150-
c_side_1_f.a = 0.9;
151-
c_side_2_f.a = 0.9;
152-
c_bottom_f.a = 0.9;
153156
video::SColor c_top = c_top_f.toSColor();
154157
video::SColor c_side_1 = c_side_1_f.toSColor();
155158
video::SColor c_side_2 = c_side_2_f.toSColor();
@@ -187,11 +190,14 @@ void Clouds::render()
187190
zi + center_of_drawing_in_noise_i.Y
188191
);
189192

190-
double noise = noise2d_perlin(
193+
float noise = noise2d_perlin(
191194
(float)p_in_noise_i.X * cloud_size_noise,
192195
(float)p_in_noise_i.Y * cloud_size_noise,
193196
m_seed, 3, 0.5);
194-
grid[i] = (noise >= 0.4);
197+
// normalize to 0..1 (given 3 octaves)
198+
static const float noise_bound = 1.0f + 0.5f + 0.25f;
199+
float density = noise / noise_bound * 0.5f + 0.5f;
200+
grid[i] = (density < m_params.density);
195201
}
196202
}
197203

@@ -236,8 +242,9 @@ void Clouds::render()
236242
v[3].Color.setBlue(255);
237243
}*/
238244

239-
f32 rx = cloud_size/2;
240-
f32 ry = 8 * BS;
245+
f32 rx = cloud_size / 2.0f;
246+
// if clouds are flat, the top layer should be at the given height
247+
f32 ry = m_enable_3d ? m_params.thickness * BS : 0.0f;
241248
f32 rz = cloud_size / 2;
242249

243250
for(int i=0; i<num_faces_to_draw; i++)
@@ -265,8 +272,8 @@ void Clouds::render()
265272
}
266273
v[0].Pos.set(-rx, ry,-rz);
267274
v[1].Pos.set( rx, ry,-rz);
268-
v[2].Pos.set( rx,-ry,-rz);
269-
v[3].Pos.set(-rx,-ry,-rz);
275+
v[2].Pos.set( rx, 0,-rz);
276+
v[3].Pos.set(-rx, 0,-rz);
270277
break;
271278
case 2: //right
272279
if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
@@ -280,8 +287,8 @@ void Clouds::render()
280287
}
281288
v[0].Pos.set( rx, ry,-rz);
282289
v[1].Pos.set( rx, ry, rz);
283-
v[2].Pos.set( rx,-ry, rz);
284-
v[3].Pos.set( rx,-ry,-rz);
290+
v[2].Pos.set( rx, 0, rz);
291+
v[3].Pos.set( rx, 0,-rz);
285292
break;
286293
case 3: // front
287294
if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
@@ -295,8 +302,8 @@ void Clouds::render()
295302
}
296303
v[0].Pos.set( rx, ry, rz);
297304
v[1].Pos.set(-rx, ry, rz);
298-
v[2].Pos.set(-rx,-ry, rz);
299-
v[3].Pos.set( rx,-ry, rz);
305+
v[2].Pos.set(-rx, 0, rz);
306+
v[3].Pos.set( rx, 0, rz);
300307
break;
301308
case 4: // left
302309
if (INAREA(xi-1, zi, m_cloud_radius_i)) {
@@ -310,22 +317,22 @@ void Clouds::render()
310317
}
311318
v[0].Pos.set(-rx, ry, rz);
312319
v[1].Pos.set(-rx, ry,-rz);
313-
v[2].Pos.set(-rx,-ry,-rz);
314-
v[3].Pos.set(-rx,-ry, rz);
320+
v[2].Pos.set(-rx, 0,-rz);
321+
v[3].Pos.set(-rx, 0, rz);
315322
break;
316323
case 5: // bottom
317324
for(int j=0;j<4;j++){
318325
v[j].Color = c_bottom;
319326
v[j].Normal.set(0,-1,0);
320327
}
321-
v[0].Pos.set( rx,-ry, rz);
322-
v[1].Pos.set(-rx,-ry, rz);
323-
v[2].Pos.set(-rx,-ry,-rz);
324-
v[3].Pos.set( rx,-ry,-rz);
328+
v[0].Pos.set( rx, 0, rz);
329+
v[1].Pos.set(-rx, 0, rz);
330+
v[2].Pos.set(-rx, 0,-rz);
331+
v[3].Pos.set( rx, 0,-rz);
325332
break;
326333
}
327334

328-
v3f pos(p0.X, m_cloud_y, p0.Y);
335+
v3f pos(p0.X, m_params.height * BS, p0.Y);
329336
pos -= intToFloat(m_camera_offset, BS);
330337

331338
for(u16 i=0; i<4; i++)
@@ -345,22 +352,25 @@ void Clouds::render()
345352

346353
void Clouds::step(float dtime)
347354
{
348-
m_time += dtime;
355+
m_origin = m_origin + dtime * BS * m_params.speed;
349356
}
350357

351-
void Clouds::update(v2f camera_p, video::SColorf color)
358+
void Clouds::update(v2f camera_p, video::SColorf color_diffuse)
352359
{
353360
m_camera_pos = camera_p;
354-
m_color = color;
355-
//m_brightness = brightness;
356-
//dstream<<"m_brightness="<<m_brightness<<std::endl;
361+
m_color.r = MYMIN(MYMAX(color_diffuse.r * m_params.color_bright.getRed(),
362+
m_params.color_ambient.getRed()), 255) / 255.0f;
363+
m_color.g = MYMIN(MYMAX(color_diffuse.r * m_params.color_bright.getGreen(),
364+
m_params.color_ambient.getGreen()), 255) / 255.0f;
365+
m_color.b = MYMIN(MYMAX(color_diffuse.b * m_params.color_bright.getBlue(),
366+
m_params.color_ambient.getBlue()), 255) / 255.0f;
367+
m_color.a = m_params.color_bright.getAlpha() / 255.0f;
357368
}
358369

359370
void Clouds::readSettings()
360371
{
361-
m_cloud_y = BS * (m_passed_cloud_y ? m_passed_cloud_y :
372+
m_params.height = (m_passed_cloud_y ? m_passed_cloud_y :
362373
g_settings->getS16("cloud_height"));
363374
m_cloud_radius_i = g_settings->getU16("cloud_radius");
364375
m_enable_3d = g_settings->getBool("enable_3d_clouds");
365376
}
366-

‎src/clouds.h

+48-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include "irrlichttypes_extrabloated.h"
2424
#include <iostream>
2525
#include "constants.h"
26+
#include "cloudparams.h"
2627

2728
// Menu clouds
2829
class Clouds;
@@ -79,27 +80,68 @@ class Clouds : public scene::ISceneNode
7980
void updateCameraOffset(v3s16 camera_offset)
8081
{
8182
m_camera_offset = camera_offset;
82-
m_box = aabb3f(-BS * 1000000, m_cloud_y - BS - BS * camera_offset.Y, -BS * 1000000,
83-
BS * 1000000, m_cloud_y + BS - BS * camera_offset.Y, BS * 1000000);
83+
updateBox();
8484
}
8585

8686
void readSettings();
8787

88+
void setDensity(float density)
89+
{
90+
m_params.density = density;
91+
// currently does not need bounding
92+
}
93+
94+
void setColorBright(const video::SColor &color_bright)
95+
{
96+
m_params.color_bright = color_bright;
97+
}
98+
99+
void setColorAmbient(const video::SColor &color_ambient)
100+
{
101+
m_params.color_ambient = color_ambient;
102+
}
103+
104+
void setHeight(float height)
105+
{
106+
m_params.height = height; // add bounding when necessary
107+
updateBox();
108+
}
109+
110+
void setSpeed(v2f speed)
111+
{
112+
m_params.speed = speed;
113+
}
114+
115+
void setThickness(float thickness)
116+
{
117+
m_params.thickness = thickness;
118+
updateBox();
119+
}
120+
88121
private:
122+
void updateBox()
123+
{
124+
float height_bs = m_params.height * BS;
125+
float thickness_bs = m_params.thickness * BS;
126+
m_box = aabb3f(-BS * 1000000.0f, height_bs - BS * m_camera_offset.Y, -BS * 1000000.0f,
127+
BS * 1000000.0f, height_bs + thickness_bs - BS * m_camera_offset.Y, BS * 1000000.0f);
128+
}
129+
89130
video::SMaterial m_material;
90131
aabb3f m_box;
91132
s16 m_passed_cloud_y;
92-
float m_cloud_y;
93133
u16 m_cloud_radius_i;
94134
bool m_enable_3d;
95-
video::SColorf m_color;
96135
u32 m_seed;
97136
v2f m_camera_pos;
98-
float m_time;
137+
v2f m_origin;
138+
v2f m_speed;
99139
v3s16 m_camera_offset;
140+
video::SColorf m_color;
141+
CloudParams m_params;
142+
100143
};
101144

102145

103146

104147
#endif
105-

0 commit comments

Comments
 (0)
Please sign in to comment.