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.
22 #include "constants.h"
28 // Menu clouds are created later
30 Clouds *g_menuclouds = NULL;
31 irr::scene::ISceneManager *g_menucloudsmgr = NULL;
33 static void cloud_3d_setting_changed(const std::string &settingname, void *data)
35 ((Clouds *)data)->readSettings();
39 scene::ISceneNode* parent,
40 scene::ISceneManager* mgr,
45 scene::ISceneNode(parent, mgr, id),
49 m_camera_offset(0,0,0)
51 m_material.setFlag(video::EMF_LIGHTING, false);
52 //m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
53 m_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
54 m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
55 m_material.setFlag(video::EMF_FOG_ENABLE, true);
56 m_material.setFlag(video::EMF_ANTI_ALIASING, true);
57 //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
58 m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
60 m_passed_cloud_y = cloudheight;
62 g_settings->registerChangedCallback("enable_3d_clouds",
63 &cloud_3d_setting_changed, this);
65 m_box = core::aabbox3d<f32>(-BS*1000000,m_cloud_y-BS,-BS*1000000,
66 BS*1000000,m_cloud_y+BS,BS*1000000);
72 g_settings->deregisterChangedCallback("enable_3d_clouds",
73 &cloud_3d_setting_changed, this);
76 void Clouds::OnRegisterSceneNode()
80 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
81 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
84 ISceneNode::OnRegisterSceneNode();
87 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
91 video::IVideoDriver* driver = SceneManager->getVideoDriver();
93 if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
94 //if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
97 ScopeProfiler sp(g_profiler, "Rendering of clouds, avg", SPT_AVG);
99 int num_faces_to_draw = m_enable_3d ? 6 : 1;
101 m_material.setFlag(video::EMF_BACK_FACE_CULLING, m_enable_3d);
103 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
104 driver->setMaterial(m_material);
107 Clouds move from Z+ towards Z-
110 const float cloud_size = BS * 64;
111 const v2f cloud_speed(0, -BS * 2);
113 const float cloud_full_radius = cloud_size * m_cloud_radius_i;
115 // Position of cloud noise origin in world coordinates
116 v2f world_cloud_origin_pos_f = m_time * cloud_speed;
117 // Position of cloud noise origin from the camera
118 v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
119 // The center point of drawing in the noise
120 v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
121 // The integer center point of drawing in the noise
122 v2s16 center_of_drawing_in_noise_i(
123 MYROUND(center_of_drawing_in_noise_f.X / cloud_size),
124 MYROUND(center_of_drawing_in_noise_f.Y / cloud_size)
126 // The world position of the integer center point of drawing in the noise
127 v2f world_center_of_drawing_in_noise_f = v2f(
128 center_of_drawing_in_noise_i.X * cloud_size,
129 center_of_drawing_in_noise_i.Y * cloud_size
130 ) + world_cloud_origin_pos_f;
132 /*video::SColor c_top(128,b*240,b*240,b*255);
133 video::SColor c_side_1(128,b*230,b*230,b*255);
134 video::SColor c_side_2(128,b*220,b*220,b*245);
135 video::SColor c_bottom(128,b*205,b*205,b*230);*/
136 video::SColorf c_top_f(m_color);
137 video::SColorf c_side_1_f(m_color);
138 video::SColorf c_side_2_f(m_color);
139 video::SColorf c_bottom_f(m_color);
140 c_side_1_f.r *= 0.95;
141 c_side_1_f.g *= 0.95;
142 c_side_1_f.b *= 0.95;
143 c_side_2_f.r *= 0.90;
144 c_side_2_f.g *= 0.90;
145 c_side_2_f.b *= 0.90;
146 c_bottom_f.r *= 0.80;
147 c_bottom_f.g *= 0.80;
148 c_bottom_f.b *= 0.80;
153 video::SColor c_top = c_top_f.toSColor();
154 video::SColor c_side_1 = c_side_1_f.toSColor();
155 video::SColor c_side_2 = c_side_2_f.toSColor();
156 video::SColor c_bottom = c_bottom_f.toSColor();
158 // Get fog parameters for setting them back later
159 video::SColor fog_color(0,0,0,0);
160 video::E_FOG_TYPE fog_type = video::EFT_FOG_LINEAR;
164 bool fog_pixelfog = false;
165 bool fog_rangefog = false;
166 driver->getFog(fog_color, fog_type, fog_start, fog_end, fog_density,
167 fog_pixelfog, fog_rangefog);
170 driver->setFog(fog_color, fog_type, cloud_full_radius * 0.5,
171 cloud_full_radius*1.2, fog_density, fog_pixelfog, fog_rangefog);
175 bool *grid = new bool[m_cloud_radius_i * 2 * m_cloud_radius_i * 2];
177 float cloud_size_noise = cloud_size / BS / 200;
179 for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
180 u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
182 for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
186 xi + center_of_drawing_in_noise_i.X,
187 zi + center_of_drawing_in_noise_i.Y
190 double noise = noise2d_perlin(
191 (float)p_in_noise_i.X * cloud_size_noise,
192 (float)p_in_noise_i.Y * cloud_size_noise,
194 grid[i] = (noise >= 0.4);
198 #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
199 #define INAREA(x, z, radius) \
200 ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
202 for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
203 for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
207 // Draw from front to back (needed for transparency)
209 zi = -m_cloud_radius_i - zi;
211 xi = -m_cloud_radius_i - xi;*/
212 // Draw from back to front
214 zi = m_cloud_radius_i - zi - 1;
216 xi = m_cloud_radius_i - xi - 1;
218 u32 i = GETINDEX(xi, zi, m_cloud_radius_i);
223 v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
225 video::S3DVertex v[4] = {
226 video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
227 video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
228 video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
229 video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
232 /*if(zi <= 0 && xi <= 0){
233 v[0].Color.setBlue(255);
234 v[1].Color.setBlue(255);
235 v[2].Color.setBlue(255);
236 v[3].Color.setBlue(255);
239 f32 rx = cloud_size/2;
241 f32 rz = cloud_size / 2;
243 for(int i=0; i<num_faces_to_draw; i++)
248 for(int j=0;j<4;j++){
249 v[j].Normal.set(0,1,0);
251 v[0].Pos.set(-rx, ry,-rz);
252 v[1].Pos.set(-rx, ry, rz);
253 v[2].Pos.set( rx, ry, rz);
254 v[3].Pos.set( rx, ry,-rz);
257 if (INAREA(xi, zi - 1, m_cloud_radius_i)) {
258 u32 j = GETINDEX(xi, zi - 1, m_cloud_radius_i);
262 for(int j=0;j<4;j++){
263 v[j].Color = c_side_1;
264 v[j].Normal.set(0,0,-1);
266 v[0].Pos.set(-rx, ry,-rz);
267 v[1].Pos.set( rx, ry,-rz);
268 v[2].Pos.set( rx,-ry,-rz);
269 v[3].Pos.set(-rx,-ry,-rz);
272 if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
273 u32 j = GETINDEX(xi+1, zi, m_cloud_radius_i);
277 for(int j=0;j<4;j++){
278 v[j].Color = c_side_2;
279 v[j].Normal.set(1,0,0);
281 v[0].Pos.set( rx, ry,-rz);
282 v[1].Pos.set( rx, ry, rz);
283 v[2].Pos.set( rx,-ry, rz);
284 v[3].Pos.set( rx,-ry,-rz);
287 if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
288 u32 j = GETINDEX(xi, zi + 1, m_cloud_radius_i);
292 for(int j=0;j<4;j++){
293 v[j].Color = c_side_1;
294 v[j].Normal.set(0,0,-1);
296 v[0].Pos.set( rx, ry, rz);
297 v[1].Pos.set(-rx, ry, rz);
298 v[2].Pos.set(-rx,-ry, rz);
299 v[3].Pos.set( rx,-ry, rz);
302 if (INAREA(xi-1, zi, m_cloud_radius_i)) {
303 u32 j = GETINDEX(xi-1, zi, m_cloud_radius_i);
307 for(int j=0;j<4;j++){
308 v[j].Color = c_side_2;
309 v[j].Normal.set(-1,0,0);
311 v[0].Pos.set(-rx, ry, rz);
312 v[1].Pos.set(-rx, ry,-rz);
313 v[2].Pos.set(-rx,-ry,-rz);
314 v[3].Pos.set(-rx,-ry, rz);
317 for(int j=0;j<4;j++){
318 v[j].Color = c_bottom;
319 v[j].Normal.set(0,-1,0);
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 v3f pos(p0.X, m_cloud_y, p0.Y);
329 pos -= intToFloat(m_camera_offset, BS);
331 for(u16 i=0; i<4; i++)
333 u16 indices[] = {0,1,2,2,3,0};
334 driver->drawVertexPrimitiveList(v, 4, indices, 2,
335 video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
341 // Restore fog settings
342 driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
343 fog_pixelfog, fog_rangefog);
346 void Clouds::step(float dtime)
351 void Clouds::update(v2f camera_p, video::SColorf color)
353 m_camera_pos = camera_p;
355 //m_brightness = brightness;
356 //dstream<<"m_brightness="<<m_brightness<<std::endl;
359 void Clouds::readSettings()
361 m_cloud_y = BS * (m_passed_cloud_y ? m_passed_cloud_y :
362 g_settings->getS16("cloud_height"));
363 m_cloud_radius_i = g_settings->getU16("cloud_radius");
364 m_enable_3d = g_settings->getBool("enable_3d_clouds");