Move ClientMap to clientmap.{h,cpp}
[oweals/minetest.git] / src / farmesh.cpp
1 /*
2 Part of Minetest-c55
3 Copyright (C) 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 /*
21         A quick messy implementation of terrain rendering for a long
22         distance according to map seed
23 */
24
25 #include "farmesh.h"
26
27 #include "constants.h"
28 #include "debug.h"
29 #include "noise.h"
30 #include "map.h"
31 #include "client.h"
32 #include "tile.h" // ITextureSource
33 #include "clientmap.h"
34
35 #include "mapgen.h" // Shouldn't really be done this way
36
37 FarMesh::FarMesh(
38                 scene::ISceneNode* parent,
39                 scene::ISceneManager* mgr,
40                 s32 id,
41                 u64 seed,
42                 Client *client
43 ):
44         scene::ISceneNode(parent, mgr, id),
45         m_seed(seed),
46         m_camera_pos(0,0),
47         m_time(0),
48         m_client(client),
49         m_render_range(20*MAP_BLOCKSIZE)
50 {
51         dstream<<__FUNCTION_NAME<<std::endl;
52         
53         //video::IVideoDriver* driver = mgr->getVideoDriver();
54
55         m_materials[0].setFlag(video::EMF_LIGHTING, false);
56         m_materials[0].setFlag(video::EMF_BACK_FACE_CULLING, true);
57         //m_materials[0].setFlag(video::EMF_BACK_FACE_CULLING, false);
58         m_materials[0].setFlag(video::EMF_BILINEAR_FILTER, false);
59         m_materials[0].setFlag(video::EMF_FOG_ENABLE, false);
60         //m_materials[0].setFlag(video::EMF_ANTI_ALIASING, true);
61         //m_materials[0].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
62         m_materials[0].setFlag(video::EMF_FOG_ENABLE, true);
63         
64         m_materials[1].setFlag(video::EMF_LIGHTING, false);
65         m_materials[1].setFlag(video::EMF_BACK_FACE_CULLING, false);
66         m_materials[1].setFlag(video::EMF_BILINEAR_FILTER, false);
67         m_materials[1].setFlag(video::EMF_FOG_ENABLE, false);
68         m_materials[1].setTexture(0, client->tsrc()->getTextureRaw("treeprop.png"));
69         m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
70         m_materials[1].setFlag(video::EMF_FOG_ENABLE, true);
71
72         m_box = core::aabbox3d<f32>(-BS*1000000,-BS*31000,-BS*1000000,
73                         BS*1000000,BS*31000,BS*1000000);
74
75 }
76
77 FarMesh::~FarMesh()
78 {
79         dstream<<__FUNCTION_NAME<<std::endl;
80 }
81
82 u32 FarMesh::getMaterialCount() const
83 {
84         return FARMESH_MATERIAL_COUNT;
85 }
86
87 video::SMaterial& FarMesh::getMaterial(u32 i)
88 {
89         return m_materials[i];
90 }
91         
92
93 void FarMesh::OnRegisterSceneNode()
94 {
95         if(IsVisible)
96         {
97                 //SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
98                 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
99                 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
100         }
101
102         ISceneNode::OnRegisterSceneNode();
103 }
104
105 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
106
107 // Temporary hack
108 struct HeightPoint
109 {
110         float gh; // ground height
111         float ma; // mud amount
112         float have_sand;
113         float tree_amount;
114 };
115 core::map<v2s16, HeightPoint> g_heights;
116
117 HeightPoint ground_height(u64 seed, v2s16 p2d)
118 {
119         core::map<v2s16, HeightPoint>::Node *n = g_heights.find(p2d);
120         if(n)
121                 return n->getValue();
122         HeightPoint hp;
123         s16 level = mapgen::find_ground_level_from_noise(seed, p2d, 3);
124         hp.gh = (level-4)*BS;
125         hp.ma = (4)*BS;
126         /*hp.gh = BS*base_rock_level_2d(seed, p2d);
127         hp.ma = BS*get_mud_add_amount(seed, p2d);*/
128         hp.have_sand = mapgen::get_have_sand(seed, p2d);
129         if(hp.gh > BS*WATER_LEVEL)
130                 hp.tree_amount = mapgen::tree_amount_2d(seed, p2d);
131         else
132                 hp.tree_amount = 0;
133         // No mud has been added if mud amount is less than 1
134         if(hp.ma < 1.0*BS)
135                 hp.ma = 0.0;
136         //hp.gh -= BS*3; // Lower a bit so that it is not that much in the way
137         g_heights[p2d] = hp;
138         return hp;
139 }
140
141 void FarMesh::render()
142 {
143         video::IVideoDriver* driver = SceneManager->getVideoDriver();
144
145         /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
146                 return;*/
147         if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
148                 return;
149         /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SKY_BOX)
150                 return;*/
151
152         driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
153         
154         //const s16 grid_radius_i = 12;
155         //const float grid_size = BS*50;
156         const s16 grid_radius_i = m_render_range/MAP_BLOCKSIZE;
157         const float grid_size = BS*MAP_BLOCKSIZE;
158         const v2f grid_speed(-BS*0, 0);
159         
160         // Position of grid noise origin in world coordinates
161         v2f world_grid_origin_pos_f(0,0);
162         // Position of grid noise origin from the camera
163         v2f grid_origin_from_camera_f = world_grid_origin_pos_f - m_camera_pos;
164         // The center point of drawing in the noise
165         v2f center_of_drawing_in_noise_f = -grid_origin_from_camera_f;
166         // The integer center point of drawing in the noise
167         v2s16 center_of_drawing_in_noise_i(
168                 MYROUND(center_of_drawing_in_noise_f.X / grid_size),
169                 MYROUND(center_of_drawing_in_noise_f.Y / grid_size)
170         );
171         // The world position of the integer center point of drawing in the noise
172         v2f world_center_of_drawing_in_noise_f = v2f(
173                 center_of_drawing_in_noise_i.X * grid_size,
174                 center_of_drawing_in_noise_i.Y * grid_size
175         ) + world_grid_origin_pos_f;
176
177         for(s16 zi=-grid_radius_i; zi<grid_radius_i; zi++)
178         for(s16 xi=-grid_radius_i; xi<grid_radius_i; xi++)
179         {
180                 /*// Don't draw very close to player
181                 s16 dd = 3;
182                 if(zi > -dd && zi < dd && xi > -dd && xi < dd)
183                         continue;*/
184
185                 v2s16 p_in_noise_i(
186                         xi+center_of_drawing_in_noise_i.X,
187                         zi+center_of_drawing_in_noise_i.Y
188                 );
189                 
190                 // If sector was drawn, don't draw it this way
191                 if(m_client->m_env.getClientMap().sectorWasDrawn(p_in_noise_i))
192                         continue;
193
194                 /*if((p_in_noise_i.X + p_in_noise_i.Y)%2==0)
195                         continue;*/
196                 /*if((p_in_noise_i.X/2 + p_in_noise_i.Y/2)%2==0)
197                         continue;*/
198
199                 v2f p0 = v2f(xi,zi)*grid_size + world_center_of_drawing_in_noise_f;
200                 
201                 /*double noise[4];
202                 double d = 100*BS;
203                 noise[0] = d*noise2d_perlin(
204                                 (float)(p_in_noise_i.X+0)*grid_size/BS/100,
205                                 (float)(p_in_noise_i.Y+0)*grid_size/BS/100,
206                                 m_seed, 3, 0.5);
207                 
208                 noise[1] = d*noise2d_perlin(
209                                 (float)(p_in_noise_i.X+0)*grid_size/BS/100,
210                                 (float)(p_in_noise_i.Y+1)*grid_size/BS/100,
211                                 m_seed, 3, 0.5);
212                 
213                 noise[2] = d*noise2d_perlin(
214                                 (float)(p_in_noise_i.X+1)*grid_size/BS/100,
215                                 (float)(p_in_noise_i.Y+1)*grid_size/BS/100,
216                                 m_seed, 3, 0.5);
217                 
218                 noise[3] = d*noise2d_perlin(
219                                 (float)(p_in_noise_i.X+1)*grid_size/BS/100,
220                                 (float)(p_in_noise_i.Y+0)*grid_size/BS/100,
221                                 m_seed, 3, 0.5);*/
222                 
223                 HeightPoint hps[5];
224                 hps[0] = ground_height(m_seed, v2s16(
225                                 (p_in_noise_i.X+0)*grid_size/BS,
226                                 (p_in_noise_i.Y+0)*grid_size/BS));
227                 hps[1] = ground_height(m_seed, v2s16(
228                                 (p_in_noise_i.X+0)*grid_size/BS,
229                                 (p_in_noise_i.Y+1)*grid_size/BS));
230                 hps[2] = ground_height(m_seed, v2s16(
231                                 (p_in_noise_i.X+1)*grid_size/BS,
232                                 (p_in_noise_i.Y+1)*grid_size/BS));
233                 hps[3] = ground_height(m_seed, v2s16(
234                                 (p_in_noise_i.X+1)*grid_size/BS,
235                                 (p_in_noise_i.Y+0)*grid_size/BS));
236                 v2s16 centerpoint(
237                                 (p_in_noise_i.X+0)*grid_size/BS+MAP_BLOCKSIZE/2,
238                                 (p_in_noise_i.Y+0)*grid_size/BS+MAP_BLOCKSIZE/2);
239                 hps[4] = ground_height(m_seed, centerpoint);
240                 
241                 float noise[5];
242                 float h_min = BS*65535;
243                 float h_max = -BS*65536;
244                 float ma_avg = 0;
245                 float h_avg = 0;
246                 u32 have_sand_count = 0;
247                 float tree_amount_avg = 0;
248                 for(u32 i=0; i<5; i++)
249                 {
250                         noise[i] = hps[i].gh + hps[i].ma;
251                         if(noise[i] < h_min)
252                                 h_min = noise[i];
253                         if(noise[i] > h_max)
254                                 h_max = noise[i];
255                         ma_avg += hps[i].ma;
256                         h_avg += noise[i];
257                         if(hps[i].have_sand)
258                                 have_sand_count++;
259                         tree_amount_avg += hps[i].tree_amount;
260                 }
261                 ma_avg /= 5.0;
262                 h_avg /= 5.0;
263                 tree_amount_avg /= 5.0;
264
265                 float steepness = (h_max - h_min)/grid_size;
266                 
267                 float light_f = noise[0]+noise[1]-noise[2]-noise[3];
268                 light_f /= 100;
269                 if(light_f < -1.0) light_f = -1.0;
270                 if(light_f > 1.0) light_f = 1.0;
271                 //light_f += 1.0;
272                 //light_f /= 2.0;
273                 
274                 v2f p1 = p0 + v2f(1,1)*grid_size;
275                 
276                 bool ground_is_sand = false;
277                 bool ground_is_rock = false;
278                 bool ground_is_mud = false;
279                 video::SColor c;
280                 // Detect water
281                 if(h_avg < WATER_LEVEL*BS && h_max < (WATER_LEVEL+5)*BS)
282                 {
283                         //c = video::SColor(255,59,86,146);
284                         //c = video::SColor(255,82,120,204);
285                         c = video::SColor(255,74,105,170);
286
287                         /*// Set to water level
288                         for(u32 i=0; i<4; i++)
289                         {
290                                 if(noise[i] < BS*WATER_LEVEL)
291                                         noise[i] = BS*WATER_LEVEL;
292                         }*/
293                         light_f = 0;
294                 }
295                 // Steep cliffs
296                 else if(steepness > 2.0)
297                 {
298                         c = video::SColor(255,128,128,128);
299                         ground_is_rock = true;
300                 }
301                 // Basic ground
302                 else
303                 {
304                         if(ma_avg < 2.0*BS)
305                         {
306                                 c = video::SColor(255,128,128,128);
307                                 ground_is_rock = true;
308                         }
309                         else
310                         {
311                                 if(h_avg <= 2.5*BS && have_sand_count >= 2)
312                                 {
313                                         c = video::SColor(255,210,194,156);
314                                         ground_is_sand = true;
315                                 }
316                                 else
317                                 {
318                                         /*// Trees if there are over 0.01 trees per MapNode
319                                         if(tree_amount_avg > 0.01)
320                                                 c = video::SColor(255,50,128,50);
321                                         else
322                                                 c = video::SColor(255,107,134,51);*/
323                                         c = video::SColor(255,107,134,51);
324                                         ground_is_mud = true;
325                                 }
326                         }
327                 }
328                 
329                 // Set to water level
330                 for(u32 i=0; i<4; i++)
331                 {
332                         if(noise[i] < BS*WATER_LEVEL)
333                                 noise[i] = BS*WATER_LEVEL;
334                 }
335
336                 float b = m_brightness + light_f*0.1*m_brightness;
337                 if(b < 0) b = 0;
338                 if(b > 2) b = 2;
339                 
340                 c = video::SColor(255, b*c.getRed(), b*c.getGreen(), b*c.getBlue());
341                 
342                 driver->setMaterial(m_materials[0]);
343
344                 video::S3DVertex vertices[4] =
345                 {
346                         video::S3DVertex(p0.X,noise[0],p0.Y, 0,0,0, c, 0,1),
347                         video::S3DVertex(p0.X,noise[1],p1.Y, 0,0,0, c, 1,1),
348                         video::S3DVertex(p1.X,noise[2],p1.Y, 0,0,0, c, 1,0),
349                         video::S3DVertex(p1.X,noise[3],p0.Y, 0,0,0, c, 0,0),
350                 };
351                 u16 indices[] = {0,1,2,2,3,0};
352                 driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
353                                 video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
354
355                 // Add some trees if appropriate
356                 if(tree_amount_avg >= 0.0065 && steepness < 1.4
357                                 && ground_is_mud == true)
358                 {
359                         driver->setMaterial(m_materials[1]);
360                         
361                         float b = m_brightness;
362                         c = video::SColor(255, b*255, b*255, b*255);
363                         
364                         {
365                                 video::S3DVertex vertices[4] =
366                                 {
367                                         video::S3DVertex(p0.X,noise[0],p0.Y,
368                                                         0,0,0, c, 0,1),
369                                         video::S3DVertex(p0.X,noise[0]+BS*MAP_BLOCKSIZE,p0.Y,
370                                                         0,0,0, c, 0,0),
371                                         video::S3DVertex(p1.X,noise[2]+BS*MAP_BLOCKSIZE,p1.Y,
372                                                         0,0,0, c, 1,0),
373                                         video::S3DVertex(p1.X,noise[2],p1.Y,
374                                                         0,0,0, c, 1,1),
375                                 };
376                                 u16 indices[] = {0,1,2,2,3,0};
377                                 driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
378                                                 video::EVT_STANDARD, scene::EPT_TRIANGLES,
379                                                 video::EIT_16BIT);
380                         }
381                         {
382                                 video::S3DVertex vertices[4] =
383                                 {
384                                         video::S3DVertex(p1.X,noise[3],p0.Y,
385                                                         0,0,0, c, 0,1),
386                                         video::S3DVertex(p1.X,noise[3]+BS*MAP_BLOCKSIZE,p0.Y,
387                                                         0,0,0, c, 0,0),
388                                         video::S3DVertex(p0.X,noise[1]+BS*MAP_BLOCKSIZE,p1.Y,
389                                                         0,0,0, c, 1,0),
390                                         video::S3DVertex(p0.X,noise[1],p1.Y,
391                                                         0,0,0, c, 1,1),
392                                 };
393                                 u16 indices[] = {0,1,2,2,3,0};
394                                 driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
395                                                 video::EVT_STANDARD, scene::EPT_TRIANGLES,
396                                                 video::EIT_16BIT);
397                         }
398                 }
399         }
400
401         //driver->clearZBuffer();
402 }
403
404 void FarMesh::step(float dtime)
405 {
406         m_time += dtime;
407 }
408
409 void FarMesh::update(v2f camera_p, float brightness, s16 render_range)
410 {
411         m_camera_pos = camera_p;
412         m_brightness = brightness;
413         m_render_range = render_range;
414 }
415
416