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