3 Copyright (C) 2010-2011 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"
24 #include "main.h" // For g_profiler and g_settings
29 scene::ISceneNode* parent,
30 scene::ISceneManager* mgr,
34 scene::ISceneNode(parent, mgr, id),
39 m_material.setFlag(video::EMF_LIGHTING, false);
40 //m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
41 m_material.setFlag(video::EMF_BACK_FACE_CULLING, true);
42 m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
43 m_material.setFlag(video::EMF_FOG_ENABLE, true);
44 m_material.setFlag(video::EMF_ANTI_ALIASING, true);
45 //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
46 m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
51 m_box = core::aabbox3d<f32>(-BS*1000000,m_cloud_y-BS,-BS*1000000,
52 BS*1000000,m_cloud_y+BS,BS*1000000);
60 void Clouds::OnRegisterSceneNode()
64 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
65 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
68 ISceneNode::OnRegisterSceneNode();
71 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
75 video::IVideoDriver* driver = SceneManager->getVideoDriver();
77 if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
78 //if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
81 ScopeProfiler sp(g_profiler, "Rendering of clouds, avg", SPT_AVG);
83 bool enable_3d = g_settings->getBool("enable_3d_clouds");
84 int num_faces_to_draw = enable_3d ? 6 : 1;
86 m_material.setFlag(video::EMF_BACK_FACE_CULLING, enable_3d);
88 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
89 driver->setMaterial(m_material);
92 Clouds move from X+ towards X-
95 const s16 cloud_radius_i = 12;
96 const float cloud_size = BS*64;
97 const v2f cloud_speed(0, -BS*2);
99 const float cloud_full_radius = cloud_size * cloud_radius_i;
101 // Position of cloud noise origin in world coordinates
102 v2f world_cloud_origin_pos_f = m_time*cloud_speed;
103 // Position of cloud noise origin from the camera
104 v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
105 // The center point of drawing in the noise
106 v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
107 // The integer center point of drawing in the noise
108 v2s16 center_of_drawing_in_noise_i(
109 MYROUND(center_of_drawing_in_noise_f.X / cloud_size),
110 MYROUND(center_of_drawing_in_noise_f.Y / cloud_size)
112 // The world position of the integer center point of drawing in the noise
113 v2f world_center_of_drawing_in_noise_f = v2f(
114 center_of_drawing_in_noise_i.X * cloud_size,
115 center_of_drawing_in_noise_i.Y * cloud_size
116 ) + world_cloud_origin_pos_f;
118 /*video::SColor c_top(128,b*240,b*240,b*255);
119 video::SColor c_side_1(128,b*230,b*230,b*255);
120 video::SColor c_side_2(128,b*220,b*220,b*245);
121 video::SColor c_bottom(128,b*205,b*205,b*230);*/
122 video::SColorf c_top_f(m_color);
123 video::SColorf c_side_1_f(m_color);
124 video::SColorf c_side_2_f(m_color);
125 video::SColorf c_bottom_f(m_color);
126 c_side_1_f.r *= 0.95;
127 c_side_1_f.g *= 0.95;
128 c_side_1_f.b *= 0.95;
129 c_side_2_f.r *= 0.90;
130 c_side_2_f.g *= 0.90;
131 c_side_2_f.b *= 0.90;
132 c_bottom_f.r *= 0.80;
133 c_bottom_f.g *= 0.80;
134 c_bottom_f.b *= 0.80;
139 video::SColor c_top = c_top_f.toSColor();
140 video::SColor c_side_1 = c_side_1_f.toSColor();
141 video::SColor c_side_2 = c_side_2_f.toSColor();
142 video::SColor c_bottom = c_bottom_f.toSColor();
144 // Get fog parameters for setting them back later
145 video::SColor fog_color(0,0,0,0);
146 video::E_FOG_TYPE fog_type = video::EFT_FOG_LINEAR;
150 bool fog_pixelfog = false;
151 bool fog_rangefog = false;
152 driver->getFog(fog_color, fog_type, fog_start, fog_end, fog_density,
153 fog_pixelfog, fog_rangefog);
156 driver->setFog(fog_color, fog_type, cloud_full_radius * 0.5,
157 cloud_full_radius*1.2, fog_density, fog_pixelfog, fog_rangefog);
161 bool *grid = new bool[cloud_radius_i*2*cloud_radius_i*2];
163 for(s16 zi=-cloud_radius_i; zi<cloud_radius_i; zi++)
164 for(s16 xi=-cloud_radius_i; xi<cloud_radius_i; xi++)
166 u32 i = (zi+cloud_radius_i)*cloud_radius_i*2 + xi+cloud_radius_i;
169 xi+center_of_drawing_in_noise_i.X,
170 zi+center_of_drawing_in_noise_i.Y
174 double noise = noise2d_perlin_abs(
175 (float)p_in_noise_i.X*cloud_size/BS/200,
176 (float)p_in_noise_i.Y*cloud_size/BS/200,
178 grid[i] = (noise >= 0.80);
181 double noise = noise2d_perlin(
182 (float)p_in_noise_i.X*cloud_size/BS/200,
183 (float)p_in_noise_i.Y*cloud_size/BS/200,
185 grid[i] = (noise >= 0.4);
189 #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
190 #define CONTAINS(x, z, radius) \
191 ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))
193 for(s16 zi0=-cloud_radius_i; zi0<cloud_radius_i; zi0++)
194 for(s16 xi0=-cloud_radius_i; xi0<cloud_radius_i; xi0++)
198 // Draw from front to back (needed for transparency)
200 zi = -cloud_radius_i - zi;
202 xi = -cloud_radius_i - xi;*/
203 // Draw from back to front
205 zi = cloud_radius_i - zi - 1;
207 xi = cloud_radius_i - xi - 1;
209 u32 i = GETINDEX(xi, zi, cloud_radius_i);
214 v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
216 video::S3DVertex v[4] = {
217 video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
218 video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
219 video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
220 video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
223 /*if(zi <= 0 && xi <= 0){
224 v[0].Color.setBlue(255);
225 v[1].Color.setBlue(255);
226 v[2].Color.setBlue(255);
227 v[3].Color.setBlue(255);
230 f32 rx = cloud_size/2;
232 f32 rz = cloud_size/2;
234 for(int i=0; i<num_faces_to_draw; i++)
239 for(int j=0;j<4;j++){
240 v[j].Normal.set(0,1,0);
242 v[0].Pos.set(-rx, ry,-rz);
243 v[1].Pos.set(-rx, ry, rz);
244 v[2].Pos.set( rx, ry, rz);
245 v[3].Pos.set( rx, ry,-rz);
248 if(CONTAINS(xi, zi-1, cloud_radius_i)){
249 u32 j = GETINDEX(xi, zi-1, cloud_radius_i);
253 for(int j=0;j<4;j++){
254 v[j].Color = c_side_1;
255 v[j].Normal.set(0,0,-1);
257 v[0].Pos.set(-rx, ry,-rz);
258 v[1].Pos.set( rx, ry,-rz);
259 v[2].Pos.set( rx,-ry,-rz);
260 v[3].Pos.set(-rx,-ry,-rz);
263 if(CONTAINS(xi+1, zi, cloud_radius_i)){
264 u32 j = GETINDEX(xi+1, zi, cloud_radius_i);
268 for(int j=0;j<4;j++){
269 v[j].Color = c_side_2;
270 v[j].Normal.set(1,0,0);
272 v[0].Pos.set( rx, ry,-rz);
273 v[1].Pos.set( rx, ry, rz);
274 v[2].Pos.set( rx,-ry, rz);
275 v[3].Pos.set( rx,-ry,-rz);
278 if(CONTAINS(xi, zi+1, cloud_radius_i)){
279 u32 j = GETINDEX(xi, zi+1, cloud_radius_i);
283 for(int j=0;j<4;j++){
284 v[j].Color = c_side_1;
285 v[j].Normal.set(0,0,-1);
287 v[0].Pos.set( rx, ry, rz);
288 v[1].Pos.set(-rx, ry, rz);
289 v[2].Pos.set(-rx,-ry, rz);
290 v[3].Pos.set( rx,-ry, rz);
293 if(CONTAINS(xi-1, zi, cloud_radius_i)){
294 u32 j = GETINDEX(xi-1, zi, cloud_radius_i);
298 for(int j=0;j<4;j++){
299 v[j].Color = c_side_2;
300 v[j].Normal.set(-1,0,0);
302 v[0].Pos.set(-rx, ry, rz);
303 v[1].Pos.set(-rx, ry,-rz);
304 v[2].Pos.set(-rx,-ry,-rz);
305 v[3].Pos.set(-rx,-ry, rz);
308 for(int j=0;j<4;j++){
309 v[j].Color = c_bottom;
310 v[j].Normal.set(0,-1,0);
312 v[0].Pos.set( rx,-ry, rz);
313 v[1].Pos.set(-rx,-ry, rz);
314 v[2].Pos.set(-rx,-ry,-rz);
315 v[3].Pos.set( rx,-ry,-rz);
319 v3f pos(p0.X, m_cloud_y, p0.Y);
321 for(u16 i=0; i<4; i++)
323 u16 indices[] = {0,1,2,2,3,0};
324 driver->drawVertexPrimitiveList(v, 4, indices, 2,
325 video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
331 // Restore fog settings
332 driver->setFog(fog_color, fog_type, fog_start, fog_end, fog_density,
333 fog_pixelfog, fog_rangefog);
336 void Clouds::step(float dtime)
341 void Clouds::update(v2f camera_p, video::SColorf color)
343 m_camera_pos = camera_p;
345 //m_brightness = brightness;
346 //dstream<<"m_brightness="<<m_brightness<<std::endl;