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;
34 scene::ISceneNode* parent,
35 scene::ISceneManager* mgr,
40 scene::ISceneNode(parent, mgr, id),
44 m_camera_offset(0,0,0)
46 m_material.setFlag(video::EMF_LIGHTING, false);
47 //m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
48 m_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
49 m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
50 m_material.setFlag(video::EMF_FOG_ENABLE, true);
51 m_material.setFlag(video::EMF_ANTI_ALIASING, true);
52 //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
53 m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
55 m_cloud_y = BS * (cloudheight ? cloudheight :
56 g_settings->getS16("cloud_height"));
58 m_cloud_radius_i = g_settings->getU16("cloud_radius");
60 m_enable_3d = g_settings->getBool("enable_3d_clouds");
62 m_box = core::aabbox3d<f32>(-BS*1000000,m_cloud_y-BS,-BS*1000000,
63 BS*1000000,m_cloud_y+BS,BS*1000000);
71 void Clouds::OnRegisterSceneNode()
75 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
76 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
79 ISceneNode::OnRegisterSceneNode();
82 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
86 video::IVideoDriver* driver = SceneManager->getVideoDriver();
88 if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
89 //if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
92 ScopeProfiler sp(g_profiler, "Rendering of clouds, avg", SPT_AVG);
94 int num_faces_to_draw = m_enable_3d ? 6 : 1;
96 m_material.setFlag(video::EMF_BACK_FACE_CULLING, m_enable_3d);
98 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
99 driver->setMaterial(m_material);
102 Clouds move from Z+ towards Z-
105 const float cloud_size = BS * 64;
106 const v2f cloud_speed(0, -BS * 2);
108 const float cloud_full_radius = cloud_size * m_cloud_radius_i;
110 // Position of cloud noise origin in world coordinates
111 v2f world_cloud_origin_pos_f = m_time * cloud_speed;
112 // Position of cloud noise origin from the camera
113 v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
114 // The center point of drawing in the noise
115 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 MYROUND(center_of_drawing_in_noise_f.X / cloud_size),
119 MYROUND(center_of_drawing_in_noise_f.Y / cloud_size)
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 center_of_drawing_in_noise_i.X * cloud_size,
124 center_of_drawing_in_noise_i.Y * cloud_size
125 ) + world_cloud_origin_pos_f;
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 video::SColorf c_top_f(m_color);
132 video::SColorf c_side_1_f(m_color);
133 video::SColorf c_side_2_f(m_color);
134 video::SColorf c_bottom_f(m_color);
135 c_side_1_f.r *= 0.95;
136 c_side_1_f.g *= 0.95;
137 c_side_1_f.b *= 0.95;
138 c_side_2_f.r *= 0.90;
139 c_side_2_f.g *= 0.90;
140 c_side_2_f.b *= 0.90;
141 c_bottom_f.r *= 0.80;
142 c_bottom_f.g *= 0.80;
143 c_bottom_f.b *= 0.80;
148 video::SColor c_top = c_top_f.toSColor();
149 video::SColor c_side_1 = c_side_1_f.toSColor();
150 video::SColor c_side_2 = c_side_2_f.toSColor();
151 video::SColor c_bottom = c_bottom_f.toSColor();
153 // Get fog parameters for setting them back later
154 video::SColor fog_color(0,0,0,0);
155 video::E_FOG_TYPE fog_type = video::EFT_FOG_LINEAR;
159 bool fog_pixelfog = false;
160 bool fog_rangefog = false;
161 driver->getFog(fog_color, fog_type, fog_start, fog_end, fog_density,
162 fog_pixelfog, fog_rangefog);
165 driver->setFog(fog_color, fog_type, cloud_full_radius * 0.5,
166 cloud_full_radius*1.2, fog_density, fog_pixelfog, fog_rangefog);
170 bool *grid = new bool[m_cloud_radius_i * 2 * m_cloud_radius_i * 2];
172 float cloud_size_noise = cloud_size / BS / 200;
174 for(s16 zi = -m_cloud_radius_i; zi < m_cloud_radius_i; zi++) {
175 u32 si = (zi + m_cloud_radius_i) * m_cloud_radius_i * 2 + m_cloud_radius_i;
177 for (s16 xi = -m_cloud_radius_i; xi < m_cloud_radius_i; xi++) {
181 xi + center_of_drawing_in_noise_i.X,
182 zi + center_of_drawing_in_noise_i.Y
185 double noise = noise2d_perlin(
186 (float)p_in_noise_i.X * cloud_size_noise,
187 (float)p_in_noise_i.Y * cloud_size_noise,
189 grid[i] = (noise >= 0.4);
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))
197 for (s16 zi0= -m_cloud_radius_i; zi0 < m_cloud_radius_i; zi0++)
198 for (s16 xi0= -m_cloud_radius_i; xi0 < m_cloud_radius_i; xi0++)
202 // Draw from front to back (needed for transparency)
204 zi = -m_cloud_radius_i - zi;
206 xi = -m_cloud_radius_i - xi;*/
207 // Draw from back to front
209 zi = m_cloud_radius_i - zi - 1;
211 xi = m_cloud_radius_i - xi - 1;
213 u32 i = GETINDEX(xi, zi, m_cloud_radius_i);
218 v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
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)
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);
234 f32 rx = cloud_size/2;
236 f32 rz = cloud_size / 2;
238 for(int i=0; i<num_faces_to_draw; i++)
243 for(int j=0;j<4;j++){
244 v[j].Normal.set(0,1,0);
246 v[0].Pos.set(-rx, ry,-rz);
247 v[1].Pos.set(-rx, ry, rz);
248 v[2].Pos.set( rx, ry, rz);
249 v[3].Pos.set( rx, ry,-rz);
252 if (INAREA(xi, zi - 1, m_cloud_radius_i)) {
253 u32 j = GETINDEX(xi, zi - 1, m_cloud_radius_i);
257 for(int j=0;j<4;j++){
258 v[j].Color = c_side_1;
259 v[j].Normal.set(0,0,-1);
261 v[0].Pos.set(-rx, ry,-rz);
262 v[1].Pos.set( rx, ry,-rz);
263 v[2].Pos.set( rx,-ry,-rz);
264 v[3].Pos.set(-rx,-ry,-rz);
267 if (INAREA(xi + 1, zi, m_cloud_radius_i)) {
268 u32 j = GETINDEX(xi+1, zi, m_cloud_radius_i);
272 for(int j=0;j<4;j++){
273 v[j].Color = c_side_2;
274 v[j].Normal.set(1,0,0);
276 v[0].Pos.set( rx, ry,-rz);
277 v[1].Pos.set( rx, ry, rz);
278 v[2].Pos.set( rx,-ry, rz);
279 v[3].Pos.set( rx,-ry,-rz);
282 if (INAREA(xi, zi + 1, m_cloud_radius_i)) {
283 u32 j = GETINDEX(xi, zi + 1, m_cloud_radius_i);
287 for(int j=0;j<4;j++){
288 v[j].Color = c_side_1;
289 v[j].Normal.set(0,0,-1);
291 v[0].Pos.set( rx, ry, rz);
292 v[1].Pos.set(-rx, ry, rz);
293 v[2].Pos.set(-rx,-ry, rz);
294 v[3].Pos.set( rx,-ry, rz);
297 if (INAREA(xi-1, zi, m_cloud_radius_i)) {
298 u32 j = GETINDEX(xi-1, zi, m_cloud_radius_i);
302 for(int j=0;j<4;j++){
303 v[j].Color = c_side_2;
304 v[j].Normal.set(-1,0,0);
306 v[0].Pos.set(-rx, ry, rz);
307 v[1].Pos.set(-rx, ry,-rz);
308 v[2].Pos.set(-rx,-ry,-rz);
309 v[3].Pos.set(-rx,-ry, rz);
312 for(int j=0;j<4;j++){
313 v[j].Color = c_bottom;
314 v[j].Normal.set(0,-1,0);
316 v[0].Pos.set( rx,-ry, rz);
317 v[1].Pos.set(-rx,-ry, rz);
318 v[2].Pos.set(-rx,-ry,-rz);
319 v[3].Pos.set( rx,-ry,-rz);
323 v3f pos(p0.X, m_cloud_y, p0.Y);
324 pos -= intToFloat(m_camera_offset, BS);
326 for(u16 i=0; i<4; i++)
328 u16 indices[] = {0,1,2,2,3,0};
329 driver->drawVertexPrimitiveList(v, 4, indices, 2,
330 video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
336 // Restore fog settings
337 driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
338 fog_pixelfog, fog_rangefog);
341 void Clouds::step(float dtime)
346 void Clouds::update(v2f camera_p, video::SColorf color)
348 m_camera_pos = camera_p;
350 //m_brightness = brightness;
351 //dstream<<"m_brightness="<<m_brightness<<std::endl;