Skip to content

Commit

Permalink
Add clouds API
Browse files Browse the repository at this point in the history
  • Loading branch information
bendeutsch authored and paramat committed Apr 29, 2017
1 parent 95409da commit f1d7a26
Show file tree
Hide file tree
Showing 17 changed files with 357 additions and 50 deletions.
9 changes: 9 additions & 0 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,15 @@ This is basically a reference to a C++ `ServerActiveObject`
* `"skybox"`: Uses 6 textures, `bgcolor` used
* `"plain"`: Uses 0 textures, `bgcolor` used
* `get_sky()`: returns bgcolor, type and a table with the textures
* `set_clouds(parameters)`: set cloud parameters
* `parameters` is a table with the following optional fields:
* `density`: from `0` (no clouds) to `1` (full clouds) (default `0.4`)
* `color`: basic cloud color, with alpha channel (default `#fff0f0e5`)
* `ambient`: cloud color lower bound, use for a "glow at night" effect (default `#000000`)
* `height`: cloud height, i.e. y of cloud base (default per conf, usually `120`)
* `thickness`: cloud thickness in nodes (default `16`)
* `speed`: 2D cloud speed + direction in nodes per second (default `{x=0, y=-2}`)
* `get_clouds()`: returns a table with the current cloud parameters as in `set_clouds`
* `override_day_night_ratio(ratio or nil)`
* `0`...`1`: Overrides day-night ratio, controlling sunlight to a specific amount
* `nil`: Disables override, defaulting to sunlight based on day-night cycle
Expand Down
11 changes: 11 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ enum ClientEventType
CE_HUDCHANGE,
CE_SET_SKY,
CE_OVERRIDE_DAY_NIGHT_RATIO,
CE_CLOUD_PARAMS,
};

struct ClientEvent
Expand Down Expand Up @@ -178,6 +179,15 @@ struct ClientEvent
bool do_override;
float ratio_f;
} override_day_night_ratio;
struct {
f32 density;
u32 color_bright;
u32 color_ambient;
f32 height;
f32 thickness;
f32 speed_x;
f32 speed_y;
} cloud_params;
};
};

Expand Down Expand Up @@ -331,6 +341,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
void handleCommand_HudSetFlags(NetworkPacket* pkt);
void handleCommand_HudSetParam(NetworkPacket* pkt);
void handleCommand_HudSetSky(NetworkPacket* pkt);
void handleCommand_CloudParams(NetworkPacket* pkt);
void handleCommand_OverrideDayNightRatio(NetworkPacket* pkt);
void handleCommand_LocalPlayerAnimations(NetworkPacket* pkt);
void handleCommand_EyeOffset(NetworkPacket* pkt);
Expand Down
33 changes: 33 additions & 0 deletions src/cloudparams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Minetest
Copyright (C) 2017 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef CLOUDPARAMS_HEADER
#define CLOUDPARAMS_HEADER

struct CloudParams
{
float density;
video::SColor color_bright;
video::SColor color_ambient;
float thickness;
float height;
v2f speed;
};

#endif
90 changes: 50 additions & 40 deletions src/clouds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ irr::scene::ISceneManager *g_menucloudsmgr = NULL;

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

Expand All @@ -44,9 +45,10 @@ Clouds::Clouds(
):
scene::ISceneNode(parent, mgr, id),
m_seed(seed),
m_camera_pos(0,0),
m_time(0),
m_camera_offset(0,0,0)
m_camera_pos(0.0f, 0.0f),
m_origin(0.0f, 0.0f),
m_camera_offset(0.0f, 0.0f, 0.0f),
m_color(1.0f, 1.0f, 1.0f, 1.0f)
{
m_material.setFlag(video::EMF_LIGHTING, false);
//m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
Expand All @@ -57,14 +59,18 @@ Clouds::Clouds(
//m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;

m_params.density = 0.4f;
m_params.thickness = 16.0f;
m_params.color_bright = video::SColor(229, 255, 240, 240);
m_params.color_ambient = video::SColor(255, 0, 0, 0);
m_params.speed = v2f(0.0f, -2.0f);

m_passed_cloud_y = cloudheight;
readSettings();
g_settings->registerChangedCallback("enable_3d_clouds",
&cloud_3d_setting_changed, this);

m_box = aabb3f(-BS*1000000,m_cloud_y-BS,-BS*1000000,
BS*1000000,m_cloud_y+BS,BS*1000000);

updateBox();
}

Clouds::~Clouds()
Expand All @@ -88,6 +94,10 @@ void Clouds::OnRegisterSceneNode()

void Clouds::render()
{

if (m_params.density <= 0.0f)
return; // no need to do anything

video::IVideoDriver* driver = SceneManager->getVideoDriver();

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

const float cloud_size = BS * 64;
const v2f cloud_speed(0, -BS * 2);
static const float cloud_size = BS * 64.0f;

const float cloud_full_radius = cloud_size * m_cloud_radius_i;

// Position of cloud noise origin in world coordinates
v2f world_cloud_origin_pos_f = m_time * cloud_speed;
// Position of cloud noise origin from the camera
v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
v2f cloud_origin_from_camera_f = m_origin - m_camera_pos;
// The center point of drawing in the noise
v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
// The integer center point of drawing in the noise
Expand All @@ -127,7 +134,7 @@ void Clouds::render()
v2f world_center_of_drawing_in_noise_f = v2f(
center_of_drawing_in_noise_i.X * cloud_size,
center_of_drawing_in_noise_i.Y * cloud_size
) + world_cloud_origin_pos_f;
) + m_origin;

/*video::SColor c_top(128,b*240,b*240,b*255);
video::SColor c_side_1(128,b*230,b*230,b*255);
Expand All @@ -146,10 +153,6 @@ void Clouds::render()
c_bottom_f.r *= 0.80;
c_bottom_f.g *= 0.80;
c_bottom_f.b *= 0.80;
c_top_f.a = 0.9;
c_side_1_f.a = 0.9;
c_side_2_f.a = 0.9;
c_bottom_f.a = 0.9;
video::SColor c_top = c_top_f.toSColor();
video::SColor c_side_1 = c_side_1_f.toSColor();
video::SColor c_side_2 = c_side_2_f.toSColor();
Expand Down Expand Up @@ -187,11 +190,14 @@ void Clouds::render()
zi + center_of_drawing_in_noise_i.Y
);

double noise = noise2d_perlin(
float noise = noise2d_perlin(
(float)p_in_noise_i.X * cloud_size_noise,
(float)p_in_noise_i.Y * cloud_size_noise,
m_seed, 3, 0.5);
grid[i] = (noise >= 0.4);
// normalize to 0..1 (given 3 octaves)
static const float noise_bound = 1.0f + 0.5f + 0.25f;
float density = noise / noise_bound * 0.5f + 0.5f;
grid[i] = (density < m_params.density);
}
}

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

f32 rx = cloud_size/2;
f32 ry = 8 * BS;
f32 rx = cloud_size / 2.0f;
// if clouds are flat, the top layer should be at the given height
f32 ry = m_enable_3d ? m_params.thickness * BS : 0.0f;
f32 rz = cloud_size / 2;

for(int i=0; i<num_faces_to_draw; i++)
Expand Down Expand Up @@ -265,8 +272,8 @@ void Clouds::render()
}
v[0].Pos.set(-rx, ry,-rz);
v[1].Pos.set( rx, ry,-rz);
v[2].Pos.set( rx,-ry,-rz);
v[3].Pos.set(-rx,-ry,-rz);
v[2].Pos.set( rx, 0,-rz);
v[3].Pos.set(-rx, 0,-rz);
break;
case 2: //right
if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
Expand All @@ -280,8 +287,8 @@ void Clouds::render()
}
v[0].Pos.set( rx, ry,-rz);
v[1].Pos.set( rx, ry, rz);
v[2].Pos.set( rx,-ry, rz);
v[3].Pos.set( rx,-ry,-rz);
v[2].Pos.set( rx, 0, rz);
v[3].Pos.set( rx, 0,-rz);
break;
case 3: // front
if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
Expand All @@ -295,8 +302,8 @@ void Clouds::render()
}
v[0].Pos.set( rx, ry, rz);
v[1].Pos.set(-rx, ry, rz);
v[2].Pos.set(-rx,-ry, rz);
v[3].Pos.set( rx,-ry, rz);
v[2].Pos.set(-rx, 0, rz);
v[3].Pos.set( rx, 0, rz);
break;
case 4: // left
if (INAREA(xi-1, zi, m_cloud_radius_i)) {
Expand All @@ -310,22 +317,22 @@ void Clouds::render()
}
v[0].Pos.set(-rx, ry, rz);
v[1].Pos.set(-rx, ry,-rz);
v[2].Pos.set(-rx,-ry,-rz);
v[3].Pos.set(-rx,-ry, rz);
v[2].Pos.set(-rx, 0,-rz);
v[3].Pos.set(-rx, 0, rz);
break;
case 5: // bottom
for(int j=0;j<4;j++){
v[j].Color = c_bottom;
v[j].Normal.set(0,-1,0);
}
v[0].Pos.set( rx,-ry, rz);
v[1].Pos.set(-rx,-ry, rz);
v[2].Pos.set(-rx,-ry,-rz);
v[3].Pos.set( rx,-ry,-rz);
v[0].Pos.set( rx, 0, rz);
v[1].Pos.set(-rx, 0, rz);
v[2].Pos.set(-rx, 0,-rz);
v[3].Pos.set( rx, 0,-rz);
break;
}

v3f pos(p0.X, m_cloud_y, p0.Y);
v3f pos(p0.X, m_params.height * BS, p0.Y);
pos -= intToFloat(m_camera_offset, BS);

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

void Clouds::step(float dtime)
{
m_time += dtime;
m_origin = m_origin + dtime * BS * m_params.speed;
}

void Clouds::update(v2f camera_p, video::SColorf color)
void Clouds::update(v2f camera_p, video::SColorf color_diffuse)
{
m_camera_pos = camera_p;
m_color = color;
//m_brightness = brightness;
//dstream<<"m_brightness="<<m_brightness<<std::endl;
m_color.r = MYMIN(MYMAX(color_diffuse.r * m_params.color_bright.getRed(),
m_params.color_ambient.getRed()), 255) / 255.0f;
m_color.g = MYMIN(MYMAX(color_diffuse.r * m_params.color_bright.getGreen(),
m_params.color_ambient.getGreen()), 255) / 255.0f;
m_color.b = MYMIN(MYMAX(color_diffuse.b * m_params.color_bright.getBlue(),
m_params.color_ambient.getBlue()), 255) / 255.0f;
m_color.a = m_params.color_bright.getAlpha() / 255.0f;
}

void Clouds::readSettings()
{
m_cloud_y = BS * (m_passed_cloud_y ? m_passed_cloud_y :
m_params.height = (m_passed_cloud_y ? m_passed_cloud_y :
g_settings->getS16("cloud_height"));
m_cloud_radius_i = g_settings->getU16("cloud_radius");
m_enable_3d = g_settings->getBool("enable_3d_clouds");
}

54 changes: 48 additions & 6 deletions src/clouds.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_extrabloated.h"
#include <iostream>
#include "constants.h"
#include "cloudparams.h"

// Menu clouds
class Clouds;
Expand Down Expand Up @@ -79,27 +80,68 @@ class Clouds : public scene::ISceneNode
void updateCameraOffset(v3s16 camera_offset)
{
m_camera_offset = camera_offset;
m_box = aabb3f(-BS * 1000000, m_cloud_y - BS - BS * camera_offset.Y, -BS * 1000000,
BS * 1000000, m_cloud_y + BS - BS * camera_offset.Y, BS * 1000000);
updateBox();
}

void readSettings();

void setDensity(float density)
{
m_params.density = density;
// currently does not need bounding
}

void setColorBright(const video::SColor &color_bright)
{
m_params.color_bright = color_bright;
}

void setColorAmbient(const video::SColor &color_ambient)
{
m_params.color_ambient = color_ambient;
}

void setHeight(float height)
{
m_params.height = height; // add bounding when necessary
updateBox();
}

void setSpeed(v2f speed)
{
m_params.speed = speed;
}

void setThickness(float thickness)
{
m_params.thickness = thickness;
updateBox();
}

private:
void updateBox()
{
float height_bs = m_params.height * BS;
float thickness_bs = m_params.thickness * BS;
m_box = aabb3f(-BS * 1000000.0f, height_bs - BS * m_camera_offset.Y, -BS * 1000000.0f,
BS * 1000000.0f, height_bs + thickness_bs - BS * m_camera_offset.Y, BS * 1000000.0f);
}

video::SMaterial m_material;
aabb3f m_box;
s16 m_passed_cloud_y;
float m_cloud_y;
u16 m_cloud_radius_i;
bool m_enable_3d;
video::SColorf m_color;
u32 m_seed;
v2f m_camera_pos;
float m_time;
v2f m_origin;
v2f m_speed;
v3s16 m_camera_offset;
video::SColorf m_color;
CloudParams m_params;

};



#endif

Loading

0 comments on commit f1d7a26

Please sign in to comment.