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