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