diff options
Diffstat (limited to 'src/farmesh.cpp')
-rw-r--r-- | src/farmesh.cpp | 228 |
1 files changed, 191 insertions, 37 deletions
diff --git a/src/farmesh.cpp b/src/farmesh.cpp index e4847966f..3faac163d 100644 --- a/src/farmesh.cpp +++ b/src/farmesh.cpp @@ -27,26 +27,52 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "debug.h" #include "noise.h" #include "map.h" +#include "client.h" + +/* + Temporarily exposed map generator stuff + Should only be used for testing +*/ +extern double base_rock_level_2d(u64 seed, v2s16 p); +extern double get_mud_add_amount(u64 seed, v2s16 p); +extern bool get_have_sand(u64 seed, v2s16 p2d); +extern double tree_amount_2d(u64 seed, v2s16 p); + FarMesh::FarMesh( scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id, - u64 seed + u64 seed, + Client *client ): scene::ISceneNode(parent, mgr, id), m_seed(seed), m_camera_pos(0,0), - m_time(0) + m_time(0), + m_client(client) { dstream<<__FUNCTION_NAME<<std::endl; + + video::IVideoDriver* driver = mgr->getVideoDriver(); - m_material.setFlag(video::EMF_LIGHTING, false); - m_material.setFlag(video::EMF_BACK_FACE_CULLING, true); - m_material.setFlag(video::EMF_BILINEAR_FILTER, false); - m_material.setFlag(video::EMF_FOG_ENABLE, false); - //m_material.setFlag(video::EMF_ANTI_ALIASING, true); - //m_material.MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA; + m_materials[0].setFlag(video::EMF_LIGHTING, false); + m_materials[0].setFlag(video::EMF_BACK_FACE_CULLING, true); + //m_materials[0].setFlag(video::EMF_BACK_FACE_CULLING, false); + m_materials[0].setFlag(video::EMF_BILINEAR_FILTER, false); + m_materials[0].setFlag(video::EMF_FOG_ENABLE, false); + //m_materials[0].setFlag(video::EMF_ANTI_ALIASING, true); + //m_materials[0].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA; + m_materials[0].setFlag(video::EMF_FOG_ENABLE, true); + + m_materials[1].setFlag(video::EMF_LIGHTING, false); + m_materials[1].setFlag(video::EMF_BACK_FACE_CULLING, false); + m_materials[1].setFlag(video::EMF_BILINEAR_FILTER, false); + m_materials[1].setFlag(video::EMF_FOG_ENABLE, false); + m_materials[1].setTexture + (0, driver->getTexture(getTexturePath("treeprop.png").c_str())); + m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; + m_materials[1].setFlag(video::EMF_FOG_ENABLE, true); m_box = core::aabbox3d<f32>(-BS*1000000,-BS*31000,-BS*1000000, BS*1000000,BS*31000,BS*1000000); @@ -58,13 +84,24 @@ FarMesh::~FarMesh() dstream<<__FUNCTION_NAME<<std::endl; } +u32 FarMesh::getMaterialCount() const +{ + return FARMESH_MATERIAL_COUNT; +} + +video::SMaterial& FarMesh::getMaterial(u32 i) +{ + return m_materials[i]; +} + + void FarMesh::OnRegisterSceneNode() { if(IsVisible) { //SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT); - //SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID); - SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX); + SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID); + //SceneManager->registerNodeForRendering(this, scene::ESNRP_SKY_BOX); } ISceneNode::OnRegisterSceneNode(); @@ -73,18 +110,34 @@ void FarMesh::OnRegisterSceneNode() #define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1) // Temporary hack -core::map<v2s16, float> g_heights; +struct HeightPoint +{ + float gh; // ground height + float ma; // mud amount + float have_sand; + float tree_amount; +}; +core::map<v2s16, HeightPoint> g_heights; -float ground_height(u64 seed, v2s16 p2d) +HeightPoint ground_height(u64 seed, v2s16 p2d) { - core::map<v2s16, float>::Node *n = g_heights.find(p2d); + core::map<v2s16, HeightPoint>::Node *n = g_heights.find(p2d); if(n) return n->getValue(); - float avg_mud_amount = 4; - float gh = BS*base_rock_level_2d(seed, p2d) + avg_mud_amount*BS; - //gh *= 10; - g_heights[p2d] = gh; - return gh; + HeightPoint hp; + hp.gh = BS*base_rock_level_2d(seed, p2d); + hp.gh -= BS*2; // Lower a bit so that it is not that much in the way + hp.ma = BS*get_mud_add_amount(seed, p2d); + hp.have_sand = get_have_sand(seed, p2d); + if(hp.gh > BS*WATER_LEVEL) + hp.tree_amount = tree_amount_2d(seed, p2d); + else + hp.tree_amount = 0; + // No mud has been added if mud amount is less than 2 + if(hp.ma < 2.0*BS) + hp.ma = 0.0; + g_heights[p2d] = hp; + return hp; } void FarMesh::render() @@ -93,16 +146,17 @@ void FarMesh::render() /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT) return;*/ - /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID) - return;*/ - if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SKY_BOX) + if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID) return; + /*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SKY_BOX) + return;*/ driver->setTransform(video::ETS_WORLD, AbsoluteTransformation); - driver->setMaterial(m_material); - const s16 grid_radius_i = 12; - const float grid_size = BS*50; + //const s16 grid_radius_i = 12; + //const float grid_size = BS*50; + const s16 grid_radius_i = 20; + const float grid_size = BS*MAP_BLOCKSIZE; const v2f grid_speed(-BS*0, 0); // Position of grid noise origin in world coordinates @@ -125,10 +179,19 @@ void FarMesh::render() for(s16 zi=-grid_radius_i; zi<grid_radius_i; zi++) for(s16 xi=-grid_radius_i; xi<grid_radius_i; xi++) { + /*// Don't draw very close to player + s16 dd = 3; + if(zi > -dd && zi < dd && xi > -dd && xi < dd) + continue;*/ + v2s16 p_in_noise_i( xi+center_of_drawing_in_noise_i.X, zi+center_of_drawing_in_noise_i.Y ); + + // If sector was drawn, don't draw it this way + if(m_client->m_env.getClientMap().sectorWasDrawn(p_in_noise_i)) + continue; /*if((p_in_noise_i.X + p_in_noise_i.Y)%2==0) continue;*/ @@ -159,32 +222,50 @@ void FarMesh::render() (float)(p_in_noise_i.Y+0)*grid_size/BS/100, m_seed, 3, 0.5);*/ - float noise[4]; - noise[0] = ground_height(m_seed, v2s16( + HeightPoint hps[5]; + hps[0] = ground_height(m_seed, v2s16( (p_in_noise_i.X+0)*grid_size/BS, (p_in_noise_i.Y+0)*grid_size/BS)); - noise[1] = ground_height(m_seed, v2s16( + hps[1] = ground_height(m_seed, v2s16( (p_in_noise_i.X+0)*grid_size/BS, (p_in_noise_i.Y+1)*grid_size/BS)); - noise[2] = ground_height(m_seed, v2s16( + hps[2] = ground_height(m_seed, v2s16( (p_in_noise_i.X+1)*grid_size/BS, (p_in_noise_i.Y+1)*grid_size/BS)); - noise[3] = ground_height(m_seed, v2s16( + hps[3] = ground_height(m_seed, v2s16( (p_in_noise_i.X+1)*grid_size/BS, (p_in_noise_i.Y+0)*grid_size/BS)); - + v2s16 centerpoint( + (p_in_noise_i.X+0)*grid_size/BS+MAP_BLOCKSIZE/2, + (p_in_noise_i.Y+0)*grid_size/BS+MAP_BLOCKSIZE/2); + hps[4] = ground_height(m_seed, centerpoint); + + float noise[5]; float h_min = BS*65535; float h_max = -BS*65536; - for(u32 i=0; i<4; i++) + float ma_avg = 0; + float h_avg = 0; + u32 have_sand_count = 0; + float tree_amount_avg = 0; + for(u32 i=0; i<5; i++) { + noise[i] = hps[i].gh + hps[i].ma; if(noise[i] < h_min) h_min = noise[i]; if(noise[i] > h_max) h_max = noise[i]; + ma_avg += hps[i].ma; + h_avg += noise[i]; + if(hps[i].have_sand) + have_sand_count++; + tree_amount_avg += hps[i].tree_amount; } + ma_avg /= 5.0; + h_avg /= 5.0; + tree_amount_avg /= 5.0; + float steepness = (h_max - h_min)/grid_size; - float h_avg = (noise[0]+noise[1]+noise[2]+noise[3])/4.0; float light_f = noise[0]+noise[1]-noise[2]-noise[3]; light_f /= 100; if(light_f < -1.0) light_f = -1.0; @@ -198,7 +279,9 @@ void FarMesh::render() // Detect water if(h_avg < WATER_LEVEL*BS && h_max < (WATER_LEVEL+5)*BS) { - c = video::SColor(128,59,86,146); + //c = video::SColor(255,59,86,146); + c = video::SColor(255,82,120,204); + /*// Set to water level for(u32 i=0; i<4; i++) { @@ -207,10 +290,33 @@ void FarMesh::render() }*/ light_f = 0; } + // Steep cliffs else if(steepness > 2.0) - c = video::SColor(128,128,128,128); + { + c = video::SColor(255,128,128,128); + } + // Basic ground else - c = video::SColor(128,107,134,51); + { + if(ma_avg < 2.0*BS) + { + c = video::SColor(255,128,128,128); + } + else + { + if(h_avg <= 2.5*BS && have_sand_count >= 2) + c = video::SColor(255,210,194,156); + else + { + /*// Trees if there are over 0.01 trees per MapNode + if(tree_amount_avg > 0.01) + c = video::SColor(255,50,128,50); + else + c = video::SColor(255,107,134,51);*/ + c = video::SColor(255,107,134,51); + } + } + } // Set to water level for(u32 i=0; i<4; i++) @@ -223,7 +329,9 @@ void FarMesh::render() if(b < 0) b = 0; if(b > 2) b = 2; - c = video::SColor(128, b*c.getRed(), b*c.getGreen(), b*c.getBlue()); + c = video::SColor(255, b*c.getRed(), b*c.getGreen(), b*c.getBlue()); + + driver->setMaterial(m_materials[0]); video::S3DVertex vertices[4] = { @@ -235,9 +343,54 @@ void FarMesh::render() u16 indices[] = {0,1,2,2,3,0}; driver->drawVertexPrimitiveList(vertices, 4, indices, 2, video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT); + + // Add some trees if appropriate + if(tree_amount_avg > 0.006 && steepness < 1.0 + && ma_avg >= 2.0*BS && have_sand_count <= 2) + { + driver->setMaterial(m_materials[1]); + + float b = m_brightness; + c = video::SColor(255, b*255, b*255, b*255); + + { + video::S3DVertex vertices[4] = + { + video::S3DVertex(p0.X,noise[0],p0.Y, + 0,0,0, c, 0,1), + video::S3DVertex(p0.X,noise[1]+BS*MAP_BLOCKSIZE,p0.Y, + 0,0,0, c, 0,0), + video::S3DVertex(p1.X,noise[2]+BS*MAP_BLOCKSIZE,p1.Y, + 0,0,0, c, 1,0), + video::S3DVertex(p1.X,noise[3],p1.Y, + 0,0,0, c, 1,1), + }; + u16 indices[] = {0,1,2,2,3,0}; + driver->drawVertexPrimitiveList(vertices, 4, indices, 2, + video::EVT_STANDARD, scene::EPT_TRIANGLES, + video::EIT_16BIT); + } + { + video::S3DVertex vertices[4] = + { + video::S3DVertex(p1.X,noise[0],p0.Y, + 0,0,0, c, 0,1), + video::S3DVertex(p1.X,noise[1]+BS*MAP_BLOCKSIZE,p0.Y, + 0,0,0, c, 0,0), + video::S3DVertex(p0.X,noise[2]+BS*MAP_BLOCKSIZE,p1.Y, + 0,0,0, c, 1,0), + video::S3DVertex(p0.X,noise[3],p1.Y, + 0,0,0, c, 1,1), + }; + u16 indices[] = {0,1,2,2,3,0}; + driver->drawVertexPrimitiveList(vertices, 4, indices, 2, + video::EVT_STANDARD, scene::EPT_TRIANGLES, + video::EIT_16BIT); + } + } } - driver->clearZBuffer(); + //driver->clearZBuffer(); } void FarMesh::step(float dtime) @@ -251,3 +404,4 @@ void FarMesh::update(v2f camera_p, float brightness) m_brightness = brightness; } + |