Esempio n. 1
0
void FarMesh::render()
{
	video::IVideoDriver* driver = SceneManager->getVideoDriver();

	/*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_TRANSPARENT)
		return;*/
	if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SOLID)
		return;
	/*if(SceneManager->getSceneNodeRenderPass() != scene::ESNRP_SKY_BOX)
		return;*/

	driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
	
	//const s16 grid_radius_i = 12;
	//const float grid_size = BS*50;
	const s16 grid_radius_i = m_render_range/MAP_BLOCKSIZE;
	const float grid_size = BS*MAP_BLOCKSIZE;
	const v2f grid_speed(-BS*0, 0);
	
	// Position of grid noise origin in world coordinates
	v2f world_grid_origin_pos_f(0,0);
	// Position of grid noise origin from the camera
	v2f grid_origin_from_camera_f = world_grid_origin_pos_f - m_camera_pos;
	// The center point of drawing in the noise
	v2f center_of_drawing_in_noise_f = -grid_origin_from_camera_f;
	// The integer center point of drawing in the noise
	v2s16 center_of_drawing_in_noise_i(
		MYROUND(center_of_drawing_in_noise_f.X / grid_size),
		MYROUND(center_of_drawing_in_noise_f.Y / grid_size)
	);
	// The world position of the integer center point of drawing in the noise
	v2f world_center_of_drawing_in_noise_f = v2f(
		center_of_drawing_in_noise_i.X * grid_size,
		center_of_drawing_in_noise_i.Y * grid_size
	) + world_grid_origin_pos_f;

	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;*/
		/*if((p_in_noise_i.X/2 + p_in_noise_i.Y/2)%2==0)
			continue;*/

		v2f p0 = v2f(xi,zi)*grid_size + world_center_of_drawing_in_noise_f;
		
		/*double noise[4];
		double d = 100*BS;
		noise[0] = d*noise2d_perlin(
				(float)(p_in_noise_i.X+0)*grid_size/BS/100,
				(float)(p_in_noise_i.Y+0)*grid_size/BS/100,
				m_seed, 3, 0.5);
		
		noise[1] = d*noise2d_perlin(
				(float)(p_in_noise_i.X+0)*grid_size/BS/100,
				(float)(p_in_noise_i.Y+1)*grid_size/BS/100,
				m_seed, 3, 0.5);
		
		noise[2] = d*noise2d_perlin(
				(float)(p_in_noise_i.X+1)*grid_size/BS/100,
				(float)(p_in_noise_i.Y+1)*grid_size/BS/100,
				m_seed, 3, 0.5);
		
		noise[3] = d*noise2d_perlin(
				(float)(p_in_noise_i.X+1)*grid_size/BS/100,
				(float)(p_in_noise_i.Y+0)*grid_size/BS/100,
				m_seed, 3, 0.5);*/
		
		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));
		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));
		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));
		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;
		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 light_f = noise[0]+noise[1]-noise[2]-noise[3];
		light_f /= 100;
		if(light_f < -1.0) light_f = -1.0;
		if(light_f > 1.0) light_f = 1.0;
		//light_f += 1.0;
		//light_f /= 2.0;
		
		v2f p1 = p0 + v2f(1,1)*grid_size;
		
		bool ground_is_sand = false;
		bool ground_is_rock = false;
		bool ground_is_mud = false;
		video::SColor c;
		// Detect water
		if(h_avg < WATER_LEVEL*BS && h_max < (WATER_LEVEL+5)*BS)
		{
			//c = video::SColor(255,59,86,146);
			//c = video::SColor(255,82,120,204);
			c = video::SColor(255,74,105,170);

			/*// Set to water level
			for(u32 i=0; i<4; i++)
			{
				if(noise[i] < BS*WATER_LEVEL)
					noise[i] = BS*WATER_LEVEL;
			}*/
			light_f = 0;
		}
		// Steep cliffs
		else if(steepness > 2.0)
		{
			c = video::SColor(255,128,128,128);
			ground_is_rock = true;
		}
		// Basic ground
		else
		{
			if(ma_avg < 2.0*BS)
			{
				c = video::SColor(255,128,128,128);
				ground_is_rock = true;
			}
			else
			{
				if(h_avg <= 2.5*BS && have_sand_count >= 2)
				{
					c = video::SColor(255,210,194,156);
					ground_is_sand = true;
				}
				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);
					ground_is_mud = true;
				}
			}
		}
		
		// Set to water level
		for(u32 i=0; i<4; i++)
		{
			if(noise[i] < BS*WATER_LEVEL)
				noise[i] = BS*WATER_LEVEL;
		}

		float b = m_brightness + light_f*0.1*m_brightness;
		if(b < 0) b = 0;
		if(b > 2) b = 2;
		
		c = video::SColor(255, b*c.getRed(), b*c.getGreen(), b*c.getBlue());
		
		driver->setMaterial(m_materials[0]);

		video::S3DVertex vertices[4] =
		{
			video::S3DVertex(p0.X,noise[0],p0.Y, 0,0,0, c, 0,1),
			video::S3DVertex(p0.X,noise[1],p1.Y, 0,0,0, c, 1,1),
			video::S3DVertex(p1.X,noise[2],p1.Y, 0,0,0, c, 1,0),
			video::S3DVertex(p1.X,noise[3],p0.Y, 0,0,0, c, 0,0),
		};
		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.0065 && steepness < 1.4
				&& ground_is_mud == true)
		{
			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[0]+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[2],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[3],p0.Y,
							0,0,0, c, 0,1),
					video::S3DVertex(p1.X,noise[3]+BS*MAP_BLOCKSIZE,p0.Y,
							0,0,0, c, 0,0),
					video::S3DVertex(p0.X,noise[1]+BS*MAP_BLOCKSIZE,p1.Y,
							0,0,0, c, 1,0),
					video::S3DVertex(p0.X,noise[1],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();
}
Esempio n. 2
0
	bool CloudParticle::idle(const Uint64 delta_t)
	{
		if (effect->recall)
			return false;

		if (base->particles.size() > 1)
		{
			if ((pos - base->center).magnitude_squared()
				> MAX_DRAW_DISTANCE_SQUARED)
			{
				//Find everything that sees this as a neighbor and delete the references to this.
				for (std::vector<CloudParticle*>::iterator iter =
					incoming_neighbors.begin(); iter
					!= incoming_neighbors.end(); iter++)
					(*iter)->remove_neighbor(this);
				for (std::vector<CloudParticle*>::iterator iter =
					neighbors.begin(); iter != neighbors.end(); iter++)
					(*iter)->remove_incoming_neighbor(this);
				return false;
			}
		}

		Vec3 velocity_shift;
		velocity_shift.randomize();
		velocity_shift.y /= 3;
		velocity_shift.normalize(0.00002 * fastsqrt(delta_t));
		velocity += velocity_shift;
		const coord_t magnitude = velocity.magnitude();
		if (magnitude > 0.15)
			velocity /= (magnitude / 0.15);

		if (fabs(velocity.y) > 0.1)
			velocity.y *= math_cache.powf_05_close(delta_t / 300000.0);

		if (pos.y - size / 40 < min_height)
			velocity.y += delta_t / 500000.0;

		if (pos.y + size / 40 > max_height)
			velocity.y -= delta_t / 2500000.0;

		if (effect->particles.size() <= 1)
			return true;

		if (!neighbors.size())
		{
			std::map<Particle*, bool>::iterator next_iter;
			int offset;
			CloudParticle* next;
			while (true)
			{
				next_iter = effect->particles.begin();
				offset = randint((int)effect->particles.size());
				for (int j = 0; j < offset; j++)
					next_iter++;
				next = (CloudParticle*)next_iter->first;
				if (next != this)
					break;
			}
			neighbors.push_back(next);
			next->add_incoming_neighbor(this);
		}

		// Adjust our neighbors -- try a few points to see if they're closer.
		// First, create the map

		CloudEffect* eff = (CloudEffect*)effect;
		std::map<coord_t, CloudParticle*> neighbors_map;
		for (int i = 0; i < (int)neighbors.size(); i++)
		{
			const coord_t distsquared = (neighbors[i]->pos - pos).magnitude_squared();
			neighbors_map[distsquared] = neighbors[i];
		}

		// Now, try to replace elements.
		coord_t maxdist = neighbors_map.rbegin()->first;
		for (int i = 0; (i < 1) || ((neighbors_map.size() < 20) && (i < 40)); i++)
		{
			std::map<Particle*, bool>::iterator iter;
			int offset;
			CloudParticle* neighbor;
			while (true)
			{
				iter = eff->particles.begin();
				offset = randint((int)eff->particles.size());
				for (int j = 0; j < offset; j++)
					iter++;
				neighbor = (CloudParticle*)iter->first;
				if (neighbor != this)
					break;
			}
			const coord_t distsquared = (neighbor->pos - pos).magnitude_squared();
			if (neighbors_map.size() >= 20)
			{
				if (distsquared > maxdist)
					continue;
				if (neighbors_map.count(distsquared))
					continue;
			}
			if (neighbors_map.size() >= 20)
			{
				std::map<coord_t, CloudParticle*>::iterator iter =
					neighbors_map.begin();
				for (int j = 0; j < (int)neighbors_map.size() - 1; j++)
					iter++;
				neighbors_map.erase(iter);
			}
			neighbors_map[distsquared] = neighbor;
			maxdist = neighbors_map.rbegin()->first;
		}

		// Set our color based on how deep into the cloud we are, based on our neighbors.  Also rebuild the neighbors vector.
		coord_t distsquaredsum = 0;
		Vec3 centerpoint(0.0, 0.0, 0.0);
		for (std::vector<CloudParticle*>::iterator iter = neighbors.begin(); iter
			!= neighbors.end(); iter++)
			(*iter)->remove_incoming_neighbor(this);
		neighbors.clear();

		for (std::map<coord_t, CloudParticle*>::iterator iter =
			neighbors_map.begin(); iter != neighbors_map.end(); iter++)
		{
			distsquaredsum += iter->first;
			centerpoint += iter->second->pos; //Should really be (pos - iter->second->pos); will correct this below for speed.
			neighbors.push_back(iter->second);
			iter->second->add_incoming_neighbor(this);
		}
		centerpoint = (pos * 20) - centerpoint;
		Vec3 new_normal = centerpoint;
		const coord_t magnitude_squared = centerpoint.magnitude_squared();
		const coord_t scale= fastsqrt(magnitude_squared);
		new_normal /= scale; // Normalize
		//  light_t new_brightness = 1.0 - (25.0 / (scale + 50.0));
		//  new_normal.x = (new_normal.x < 0 ? -1 : 1) * math_cache.powf_0_1_rough_close(fabs(new_normal.x), new_brightness * 2.0 - 1.0);
		//  new_normal.y = (new_normal.y < 0 ? -1 : 1) * math_cache.powf_0_1_rough_close(fabs(new_normal.y), new_brightness * 2.0 - 1.0);
		//  new_normal.z = (new_normal.z < 0 ? -1 : 1) * math_cache.powf_0_1_rough_close(fabs(new_normal.z), new_brightness * 2.0 - 1.0);
		const percent_t change_rate = math_cache.powf_05_close(delta_t
			/ 2000000.0);
		normal = normal * change_rate + new_normal * (1.0 - change_rate);
		normal.normalize();
		//  color[0] = color[0] * change_rate + new_brightness * (1.0 - change_rate);
		//  color[1] = color[0];
		//  color[2] = color[0];
		//  std::cout << "  " << centerpoint << std::endl;
		//  std::cout << "  " << normal << std::endl;
		//  std::cout << "  " << brightness << std::endl;

		return true;
	}