tuned lava/universal damage code
[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*48;
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.95)
127                         continue;
128
129                 float b = m_brightness;
130                 video::SColor c_top(128,b*240,b*240,b*255);
131                 video::SColor c_side_1(128,b*230,b*230,b*255);
132                 video::SColor c_side_2(128,b*220,b*220,b*245);
133                 video::SColor c_bottom(128,b*205,b*205,b*230);
134
135                 video::S3DVertex v[4] =
136                 {
137                         video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
138                         video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
139                         video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
140                         video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
141                 };
142
143                 f32 rx = cloud_size;
144                 f32 ry = 8*BS;
145                 f32 rz = cloud_size;
146
147                 for(int i=0;i<6;i++)
148                 {
149                         switch(i)
150                         {
151                                 case 0: // top
152                                         v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
153                                         v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
154                                         v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
155                                         v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
156                                         break;
157                                 case 1: // back
158                                         for(int j=0;j<4;j++)
159                                                 v[j].Color=c_side_1;
160                                         v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
161                                         v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
162                                         v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
163                                         v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
164                                         break;
165                                 case 2: //right
166                                         for(int j=0;j<4;j++)
167                                                 v[j].Color=c_side_2;
168                                         v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
169                                         v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
170                                         v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
171                                         v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
172                                         break;
173                                 case 3: // front
174                                         for(int j=0;j<4;j++)
175                                                 v[j].Color=c_side_1;
176                                         v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
177                                         v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
178                                         v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
179                                         v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
180                                         break;
181                                 case 4: // left
182                                         for(int j=0;j<4;j++)
183                                                 v[j].Color=c_side_2;
184                                         v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
185                                         v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
186                                         v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
187                                         v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
188                                         break;
189                                 case 5: // bottom
190                                         for(int j=0;j<4;j++)
191                                                 v[j].Color=c_bottom;
192                                         v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
193                                         v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
194                                         v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
195                                         v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
196                                         break;
197                         }
198
199                         v3f pos = v3f(p0.X,m_cloud_y,p0.Y);
200
201                         for(u16 i=0; i<4; i++)
202                                 v[i].Pos += pos;
203                         u16 indices[] = {0,1,2,2,3,0};
204                         driver->drawVertexPrimitiveList(v, 4, indices, 2,
205                                         video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
206                 }
207
208         }
209 }
210
211 void Clouds::step(float dtime)
212 {
213         m_time += dtime;
214 }
215
216 void Clouds::update(v2f camera_p, float brightness)
217 {
218         m_camera_pos = camera_p;
219         m_brightness = brightness;
220 }
221