3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 A quick messy implementation of terrain rendering for a long
22 distance according to map seed
26 #include "constants.h"
33 Temporarily exposed map generator stuff
34 Should only be used for testing
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);
43 scene::ISceneNode* parent,
44 scene::ISceneManager* mgr,
49 scene::ISceneNode(parent, mgr, id),
54 m_render_range(20*MAP_BLOCKSIZE)
56 dstream<<__FUNCTION_NAME<<std::endl;
58 video::IVideoDriver* driver = mgr->getVideoDriver();
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);
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);
78 m_box = core::aabbox3d<f32>(-BS*1000000,-BS*31000,-BS*1000000,
79 BS*1000000,BS*31000,BS*1000000);
85 dstream<<__FUNCTION_NAME<<std::endl;
88 u32 FarMesh::getMaterialCount() const
90 return FARMESH_MATERIAL_COUNT;
93 video::SMaterial& FarMesh::getMaterial(u32 i)
95 return m_materials[i];
99 void FarMesh::OnRegisterSceneNode()
103 //SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
104 SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
105 //SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX);
108 ISceneNode::OnRegisterSceneNode();
111 #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)
116 float gh; // ground height
117 float ma; // mud amount
121 core::map<v2s16, HeightPoint> g_heights;
123 HeightPoint ground_height(u64 seed, v2s16 p2d)
125 core::map<v2s16, HeightPoint>::Node *n = g_heights.find(p2d);
127 return n->getValue();
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);
136 // No mud has been added if mud amount is less than 1
139 hp.gh -= BS*3; // Lower a bit so that it is not that much in the way
144 void FarMesh::render()
146 video::IVideoDriver* driver = SceneManager->getVideoDriver();
148 /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
150 if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
152 /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SKY_BOX)
155 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
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);
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)
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;
180 for(s16 zi=-grid_radius_i; zi<grid_radius_i; zi++)
181 for(s16 xi=-grid_radius_i; xi<grid_radius_i; xi++)
183 /*// Don't draw very close to player
185 if(zi > -dd && zi < dd && xi > -dd && xi < dd)
189 xi+center_of_drawing_in_noise_i.X,
190 zi+center_of_drawing_in_noise_i.Y
193 // If sector was drawn, don't draw it this way
194 if(m_client->m_env.getClientMap().sectorWasDrawn(p_in_noise_i))
197 /*if((p_in_noise_i.X + p_in_noise_i.Y)%2==0)
199 /*if((p_in_noise_i.X/2 + p_in_noise_i.Y/2)%2==0)
202 v2f p0 = v2f(xi,zi)*grid_size + world_center_of_drawing_in_noise_f;
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,
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,
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,
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,
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));
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);
245 float h_min = BS*65535;
246 float h_max = -BS*65536;
249 u32 have_sand_count = 0;
250 float tree_amount_avg = 0;
251 for(u32 i=0; i<5; i++)
253 noise[i] = hps[i].gh + hps[i].ma;
262 tree_amount_avg += hps[i].tree_amount;
266 tree_amount_avg /= 5.0;
268 float steepness = (h_max - h_min)/grid_size;
270 float light_f = noise[0]+noise[1]-noise[2]-noise[3];
272 if(light_f < -1.0) light_f = -1.0;
273 if(light_f > 1.0) light_f = 1.0;
277 v2f p1 = p0 + v2f(1,1)*grid_size;
279 bool ground_is_sand = false;
280 bool ground_is_rock = false;
281 bool ground_is_mud = false;
284 if(h_avg < WATER_LEVEL*BS && h_max < (WATER_LEVEL+5)*BS)
286 //c = video::SColor(255,59,86,146);
287 c = video::SColor(255,82,120,204);
289 /*// Set to water level
290 for(u32 i=0; i<4; i++)
292 if(noise[i] < BS*WATER_LEVEL)
293 noise[i] = BS*WATER_LEVEL;
298 else if(steepness > 2.0)
300 c = video::SColor(255,128,128,128);
301 ground_is_rock = true;
308 c = video::SColor(255,128,128,128);
309 ground_is_rock = true;
313 if(h_avg <= 2.5*BS && have_sand_count >= 2)
315 c = video::SColor(255,210,194,156);
316 ground_is_sand = true;
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);
324 c = video::SColor(255,107,134,51);*/
325 c = video::SColor(255,107,134,51);
326 ground_is_mud = true;
331 // Set to water level
332 for(u32 i=0; i<4; i++)
334 if(noise[i] < BS*WATER_LEVEL)
335 noise[i] = BS*WATER_LEVEL;
338 float b = m_brightness + light_f*0.1*m_brightness;
342 c = video::SColor(255, b*c.getRed(), b*c.getGreen(), b*c.getBlue());
344 driver->setMaterial(m_materials[0]);
346 video::S3DVertex vertices[4] =
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),
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);
357 // Add some trees if appropriate
358 if(tree_amount_avg >= 0.0065 && steepness < 1.4
359 && ground_is_mud == true)
361 driver->setMaterial(m_materials[1]);
363 float b = m_brightness;
364 c = video::SColor(255, b*255, b*255, b*255);
367 video::S3DVertex vertices[4] =
369 video::S3DVertex(p0.X,noise[0],p0.Y,
371 video::S3DVertex(p0.X,noise[0]+BS*MAP_BLOCKSIZE,p0.Y,
373 video::S3DVertex(p1.X,noise[2]+BS*MAP_BLOCKSIZE,p1.Y,
375 video::S3DVertex(p1.X,noise[2],p1.Y,
378 u16 indices[] = {0,1,2,2,3,0};
379 driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
380 video::EVT_STANDARD, scene::EPT_TRIANGLES,
384 video::S3DVertex vertices[4] =
386 video::S3DVertex(p1.X,noise[3],p0.Y,
388 video::S3DVertex(p1.X,noise[3]+BS*MAP_BLOCKSIZE,p0.Y,
390 video::S3DVertex(p0.X,noise[1]+BS*MAP_BLOCKSIZE,p1.Y,
392 video::S3DVertex(p0.X,noise[1],p1.Y,
395 u16 indices[] = {0,1,2,2,3,0};
396 driver->drawVertexPrimitiveList(vertices, 4, indices, 2,
397 video::EVT_STANDARD, scene::EPT_TRIANGLES,
403 //driver->clearZBuffer();
406 void FarMesh::step(float dtime)
411 void FarMesh::update(v2f camera_p, float brightness, s16 render_range)
413 m_camera_pos = camera_p;
414 m_brightness = brightness;
415 m_render_range = render_range;