3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "client/renderingengine.h"
23 #include "constants.h"
29 // Menu clouds are created later
31 Clouds *g_menuclouds = NULL;
32 irr::scene::ISceneManager *g_menucloudsmgr = NULL;
34 static void cloud_3d_setting_changed(const std::string &settingname, void *data)
36 // TODO: only re-read cloud settings, not height or radius
37 ((Clouds *)data)->readSettings();
40 Clouds::Clouds(scene::ISceneManager* mgr,
45 scene::ISceneNode(mgr->getRootSceneNode(), mgr, id),
48 m_material.setFlag(video::EMF_LIGHTING, false);
49 //m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
50 m_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
51 m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
52 m_material.setFlag(video::EMF_FOG_ENABLE, true);
53 m_material.setFlag(video::EMF_ANTI_ALIASING, true);
54 //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
55 m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
57 m_params.density = 0.4f;
58 m_params.thickness = 16.0f;
59 m_params.color_bright = video::SColor(229, 240, 240, 255);
60 m_params.color_ambient = video::SColor(255, 0, 0, 0);
61 m_params.speed = v2f(0.0f, -2.0f);
63 m_passed_cloud_y = cloudheight;
65 g_settings->registerChangedCallback("enable_3d_clouds",
66 &cloud_3d_setting_changed, this);
73 g_settings->deregisterChangedCallback("enable_3d_clouds",
74 &cloud_3d_setting_changed, this);
77 void Clouds::OnRegisterSceneNode()
81 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
82 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
85 ISceneNode::OnRegisterSceneNode();
88 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
93 if (m_params.density <= 0.0f)
94 return; // no need to do anything
96 video::IVideoDriver* driver = SceneManager->getVideoDriver();
98 if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
99 //if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
102 ScopeProfiler sp(g_profiler, "Rendering of clouds, avg", SPT_AVG);
104 int num_faces_to_draw = m_enable_3d ? 6 : 1;
106 m_material.setFlag(video::EMF_BACK_FACE_CULLING, m_enable_3d);
108 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
109 driver->setMaterial(m_material);
112 Clouds move from Z+ towards Z-
115 static const float cloud_size = BS * 64.0f;
117 const float cloud_full_radius = cloud_size * m_cloud_radius_i;
119 // Position of cloud noise origin from the camera
120 v2f cloud_origin_from_camera_f = m_origin - m_camera_pos;
121 // The center point of drawing in the noise
122 v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
123 // The integer center point of drawing in the noise
124 v2s16 center_of_drawing_in_noise_i(
125 MYROUND(center_of_drawing_in_noise_f.X / cloud_size),
126 MYROUND(center_of_drawing_in_noise_f.Y / cloud_size)
128 // The world position of the integer center point of drawing in the noise
129 v2f world_center_of_drawing_in_noise_f = v2f(
130 center_of_drawing_in_noise_i.X * cloud_size,
131 center_of_drawing_in_noise_i.Y * cloud_size
134 /*video::SColor c_top(128,b*240,b*240,b*255);
135 video::SColor c_side_1(128,b*230,b*230,b*255);
136 video::SColor c_side_2(128,b*220,b*220,b*245);
137 video::SColor c_bottom(128,b*205,b*205,b*230);*/
138 video::SColorf c_top_f(m_color);
139 video::SColorf c_side_1_f(m_color);
140 video::SColorf c_side_2_f(m_color);
141 video::SColorf c_bottom_f(m_color);
142 c_side_1_f.r *= 0.95;
143 c_side_1_f.g *= 0.95;
144 c_side_1_f.b *= 0.95;
145 c_side_2_f.r *= 0.90;
146 c_side_2_f.g *= 0.90;
147 c_side_2_f.b *= 0.90;
148 c_bottom_f.r *= 0.80;
149 c_bottom_f.g *= 0.80;
150 c_bottom_f.b *= 0.80;
151 video::SColor c_top = c_top_f.toSColor();
152 video::SColor c_side_1 = c_side_1_f.toSColor();
153 video::SColor c_side_2 = c_side_2_f.toSColor();
154 video::SColor c_bottom = c_bottom_f.toSColor();
156 // Get fog parameters for setting them back later
157 video::SColor fog_color(0,0,0,0);
158 video::E_FOG_TYPE fog_type = video::EFT_FOG_LINEAR;
162 bool fog_pixelfog = false;
163 bool fog_rangefog = false;
164 driver->getFog(fog_color, fog_type, fog_start, fog_end, fog_density,
165 fog_pixelfog, fog_rangefog);
168 driver->setFog(fog_color, fog_type, cloud_full_radius * 0.5,
169 cloud_full_radius*1.2, fog_density, fog_pixelfog, fog_rangefog);
173 bool *grid = new bool[m_cloud_radius_i * 2 * m_cloud_radius_i * 2];
175 float cloud_size_noise = cloud_size / BS / 200;
177 for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
178 u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
180 for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
184 xi + center_of_drawing_in_noise_i.X,
185 zi + center_of_drawing_in_noise_i.Y
188 float noise = noise2d_perlin(
189 (float)p_in_noise_i.X * cloud_size_noise,
190 (float)p_in_noise_i.Y * cloud_size_noise,
192 // normalize to 0..1 (given 3 octaves)
193 static const float noise_bound = 1.0f + 0.5f + 0.25f;
194 float density = noise / noise_bound * 0.5f + 0.5f;
195 grid[i] = (density < m_params.density);
199 #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
200 #define INAREA(x, z, radius) \
201 ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
203 for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
204 for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
208 // Draw from front to back (needed for transparency)
210 zi = -m_cloud_radius_i - zi;
212 xi = -m_cloud_radius_i - xi;*/
213 // Draw from back to front
215 zi = m_cloud_radius_i - zi - 1;
217 xi = m_cloud_radius_i - xi - 1;
219 u32 i = GETINDEX(xi, zi, m_cloud_radius_i);
224 v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
226 video::S3DVertex v[4] = {
227 video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
228 video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
229 video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
230 video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
233 /*if(zi <= 0 && xi <= 0){
234 v[0].Color.setBlue(255);
235 v[1].Color.setBlue(255);
236 v[2].Color.setBlue(255);
237 v[3].Color.setBlue(255);
240 f32 rx = cloud_size / 2.0f;
241 // if clouds are flat, the top layer should be at the given height
242 f32 ry = m_enable_3d ? m_params.thickness * BS : 0.0f;
243 f32 rz = cloud_size / 2;
245 for(int i=0; i<num_faces_to_draw; i++)
250 for(int j=0;j<4;j++){
251 v[j].Normal.set(0,1,0);
253 v[0].Pos.set(-rx, ry,-rz);
254 v[1].Pos.set(-rx, ry, rz);
255 v[2].Pos.set( rx, ry, rz);
256 v[3].Pos.set( rx, ry,-rz);
259 if (INAREA(xi, zi - 1, m_cloud_radius_i)) {
260 u32 j = GETINDEX(xi, zi - 1, m_cloud_radius_i);
264 for(int j=0;j<4;j++){
265 v[j].Color = c_side_1;
266 v[j].Normal.set(0,0,-1);
268 v[0].Pos.set(-rx, ry,-rz);
269 v[1].Pos.set( rx, ry,-rz);
270 v[2].Pos.set( rx, 0,-rz);
271 v[3].Pos.set(-rx, 0,-rz);
274 if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
275 u32 j = GETINDEX(xi+1, zi, m_cloud_radius_i);
279 for(int j=0;j<4;j++){
280 v[j].Color = c_side_2;
281 v[j].Normal.set(1,0,0);
283 v[0].Pos.set( rx, ry,-rz);
284 v[1].Pos.set( rx, ry, rz);
285 v[2].Pos.set( rx, 0, rz);
286 v[3].Pos.set( rx, 0,-rz);
289 if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
290 u32 j = GETINDEX(xi, zi + 1, m_cloud_radius_i);
294 for(int j=0;j<4;j++){
295 v[j].Color = c_side_1;
296 v[j].Normal.set(0,0,-1);
298 v[0].Pos.set( rx, ry, rz);
299 v[1].Pos.set(-rx, ry, rz);
300 v[2].Pos.set(-rx, 0, rz);
301 v[3].Pos.set( rx, 0, rz);
304 if (INAREA(xi-1, zi, m_cloud_radius_i)) {
305 u32 j = GETINDEX(xi-1, zi, m_cloud_radius_i);
309 for(int j=0;j<4;j++){
310 v[j].Color = c_side_2;
311 v[j].Normal.set(-1,0,0);
313 v[0].Pos.set(-rx, ry, rz);
314 v[1].Pos.set(-rx, ry,-rz);
315 v[2].Pos.set(-rx, 0,-rz);
316 v[3].Pos.set(-rx, 0, rz);
319 for(int j=0;j<4;j++){
320 v[j].Color = c_bottom;
321 v[j].Normal.set(0,-1,0);
323 v[0].Pos.set( rx, 0, rz);
324 v[1].Pos.set(-rx, 0, rz);
325 v[2].Pos.set(-rx, 0,-rz);
326 v[3].Pos.set( rx, 0,-rz);
330 v3f pos(p0.X, m_params.height * BS, p0.Y);
331 pos -= intToFloat(m_camera_offset, BS);
333 for(u16 i=0; i<4; i++)
335 u16 indices[] = {0,1,2,2,3,0};
336 driver->drawVertexPrimitiveList(v, 4, indices, 2,
337 video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
343 // Restore fog settings
344 driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
345 fog_pixelfog, fog_rangefog);
348 void Clouds::step(float dtime)
350 m_origin = m_origin + dtime * BS * m_params.speed;
353 void Clouds::update(v2f camera_p, video::SColorf color_diffuse)
355 m_camera_pos = camera_p;
356 m_color.r = MYMIN(MYMAX(color_diffuse.r * m_params.color_bright.getRed(),
357 m_params.color_ambient.getRed()), 255) / 255.0f;
358 m_color.g = MYMIN(MYMAX(color_diffuse.g * m_params.color_bright.getGreen(),
359 m_params.color_ambient.getGreen()), 255) / 255.0f;
360 m_color.b = MYMIN(MYMAX(color_diffuse.b * m_params.color_bright.getBlue(),
361 m_params.color_ambient.getBlue()), 255) / 255.0f;
362 m_color.a = m_params.color_bright.getAlpha() / 255.0f;
365 void Clouds::readSettings()
367 m_params.height = (m_passed_cloud_y ? m_passed_cloud_y :
368 g_settings->getS16("cloud_height"));
369 m_cloud_radius_i = g_settings->getU16("cloud_radius");
370 m_enable_3d = g_settings->getBool("enable_3d_clouds");