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