comment update
[oweals/minetest.git] / src / clouds.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
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 General Public License for more details.
14
15 You should have received a copy of the GNU 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.
18 */
19
20 #include "clouds.h"
21 #include "noise.h"
22 #include "constants.h"
23 #include "debug.h"
24
25 Clouds::Clouds(
26                 scene::ISceneNode* parent,
27                 scene::ISceneManager* mgr,
28                 s32 id,
29                 float cloud_y,
30                 u32 seed
31 ):
32         scene::ISceneNode(parent, mgr, id),
33         m_cloud_y(cloud_y),
34         m_seed(seed),
35         m_camera_pos(0,0),
36         m_time(0)
37 {
38         dstream<<__FUNCTION_NAME<<std::endl;
39
40         m_material.setFlag(video::EMF_LIGHTING, false);
41         m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
42         m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
43         m_material.setFlag(video::EMF_FOG_ENABLE, false);
44         //m_material.setFlag(video::EMF_ANTI_ALIASING, true);
45         //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
46
47         m_box = core::aabbox3d<f32>(-BS*1000000,cloud_y-BS,-BS*1000000,
48                         BS*1000000,cloud_y+BS,BS*1000000);
49
50 }
51
52 Clouds::~Clouds()
53 {
54         dstream<<__FUNCTION_NAME<<std::endl;
55 }
56
57 void Clouds::OnRegisterSceneNode()
58 {
59         if(IsVisible)
60         {
61                 //SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
62                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
63         }
64
65         ISceneNode::OnRegisterSceneNode();
66 }
67
68 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
69
70 void Clouds::render()
71 {
72         video::IVideoDriver* driver = SceneManager->getVideoDriver();
73
74         /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
75                 return;*/
76         if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
77                 return;
78
79         driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
80         driver->setMaterial(m_material);
81         
82         /*
83                 Clouds move from X+ towards X-
84         */
85
86         const s16 cloud_radius_i = 12;
87         const float cloud_size = BS*50;
88         const v2f cloud_speed(-BS*2, 0);
89         
90         // Position of cloud noise origin in world coordinates
91         v2f world_cloud_origin_pos_f = m_time*cloud_speed;
92         // Position of cloud noise origin from the camera
93         v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
94         // The center point of drawing in the noise
95         v2f center_of_drawing_in_noise_f = -cloud_origin_from_camera_f;
96         // The integer center point of drawing in the noise
97         v2s16 center_of_drawing_in_noise_i(
98                 MYROUND(center_of_drawing_in_noise_f.X / cloud_size),
99                 MYROUND(center_of_drawing_in_noise_f.Y / cloud_size)
100         );
101         // The world position of the integer center point of drawing in the noise
102         v2f world_center_of_drawing_in_noise_f = v2f(
103                 center_of_drawing_in_noise_i.X * cloud_size,
104                 center_of_drawing_in_noise_i.Y * cloud_size
105         ) + world_cloud_origin_pos_f;
106
107         for(s16 zi=-cloud_radius_i; zi<cloud_radius_i; zi++)
108         for(s16 xi=-cloud_radius_i; xi<cloud_radius_i; xi++)
109         {
110                 v2s16 p_in_noise_i(
111                         xi+center_of_drawing_in_noise_i.X,
112                         zi+center_of_drawing_in_noise_i.Y
113                 );
114
115                 /*if((p_in_noise_i.X + p_in_noise_i.Y)%2==0)
116                         continue;*/
117                 /*if((p_in_noise_i.X/2 + p_in_noise_i.Y/2)%2==0)
118                         continue;*/
119
120                 v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;
121                 
122                 double noise = noise2d_perlin_abs(
123                                 (float)p_in_noise_i.X*cloud_size/BS/200,
124                                 (float)p_in_noise_i.Y*cloud_size/BS/200,
125                                 m_seed, 3, 0.4);
126                 if(noise < 0.8)
127                         continue;
128                 
129                 v2f p1 = p0 + v2f(1,1)*cloud_size;
130
131                 //video::SColor c(128,255,255,255);
132                 float b = m_brightness;
133                 video::SColor c(128,b*230,b*230,b*255);
134                 video::S3DVertex vertices[4] =
135                 {
136                         video::S3DVertex(p0.X,m_cloud_y,p0.Y, 0,0,0, c, 0,1),
137                         video::S3DVertex(p0.X,m_cloud_y,p1.Y, 0,0,0, c, 1,1),
138                         video::S3DVertex(p1.X,m_cloud_y,p1.Y, 0,0,0, c, 1,0),
139                         video::S3DVertex(p1.X,m_cloud_y,p0.Y, 0,0,0, c, 0,0),
140                 };
141                 u16 indices[] = {0,1,2,2,3,0};
142                 driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
143                                 video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
144         }
145 }
146
147 void Clouds::step(float dtime)
148 {
149         m_time += dtime;
150 }
151
152 void Clouds::update(v2f camera_p, float brightness)
153 {
154         m_camera_pos = camera_p;
155         m_brightness = brightness;
156 }
157