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