summaryrefslogtreecommitdiff
path: root/src/clouds.cpp
blob: 23243659707bb8c56ddc2a9ce72f920d57d5e4f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Minetest-c55
Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "clouds.h"
#include "noise.h"
#include "constants.h"
#include "debug.h"
#include "main.h" // For g_profiler and g_settings
#include "profiler.h"
#include "settings.h"

Clouds::Clouds(
		scene::ISceneNode* parent,
		scene::ISceneManager* mgr,
		s32 id,
		float cloud_y,
		u32 seed
):
	scene::ISceneNode(parent, mgr, id),
	m_cloud_y(cloud_y),
	m_seed(seed),
	m_camera_pos(0,0),
	m_time(0)
{
	dstream<<__FUNCTION_NAME<<std::endl;

	m_material.setFlag(video::EMF_LIGHTING, false);
	m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
	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_box = core::aabbox3d<f32>(-BS*1000000,cloud_y-BS,-BS*1000000,
			BS*1000000,cloud_y+BS,BS*1000000);

}

Clouds::~Clouds()
{
	dstream<<__FUNCTION_NAME<<std::endl;
}

void Clouds::OnRegisterSceneNode()
{
	if(IsVisible)
	{
		//SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT);
		SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID);
	}

	ISceneNode::OnRegisterSceneNode();
}

#define MYROUND(x) (x > 0.0 ? (int)x : (int)x - 1)

void Clouds::render()
{
	video::IVideoDriver* driver = SceneManager->getVideoDriver();

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

	ScopeProfiler sp(g_profiler, "Rendering of clouds, avg", SPT_AVG);

	int num_faces_to_draw = 1;
	if(g_settings->getBool("enable_3d_clouds"))
		num_faces_to_draw = 6;

	driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
	driver->setMaterial(m_material);
	
	/*
		Clouds move from X+ towards X-
	*/

	const s16 cloud_radius_i = 12;
	const float cloud_size = BS*48;
	const v2f cloud_speed(-BS*2, 0);
	
	// Position of cloud noise origin in world coordinates
	v2f world_cloud_origin_pos_f = m_time*cloud_speed;
	// Position of cloud noise origin from the camera
	v2f cloud_origin_from_camera_f = world_cloud_origin_pos_f - m_camera_pos;
	// The center point of drawing in the noise
	v2f center_of_drawing_in_noise_f = -cloud_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 / cloud_size),
		MYROUND(center_of_drawing_in_noise_f.Y / cloud_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 * cloud_size,
		center_of_drawing_in_noise_i.Y * cloud_size
	) + world_cloud_origin_pos_f;

	for(s16 zi=-cloud_radius_i; zi<cloud_radius_i; zi++)
	for(s16 xi=-cloud_radius_i; xi<cloud_radius_i; xi++)
	{
		v2s16 p_in_noise_i(
			xi+center_of_drawing_in_noise_i.X,
			zi+center_of_drawing_in_noise_i.Y
		);

		/*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)*cloud_size + world_center_of_drawing_in_noise_f;
		
		double noise = noise2d_perlin_abs(
				(float)p_in_noise_i.X*cloud_size/BS/200,
				(float)p_in_noise_i.Y*cloud_size/BS/200,
				m_seed, 3, 0.4);
		if(noise < 0.95)
			continue;

		float b = m_brightness;
		video::SColor c_top(128,b*240,b*240,b*255);
		video::SColor c_side_1(128,b*230,b*230,b*255);
		video::SColor c_side_2(128,b*220,b*220,b*245);
		video::SColor c_bottom(128,b*205,b*205,b*230);

		video::S3DVertex v[4] =
		{
			video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 1),
			video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 1),
			video::S3DVertex(0,0,0, 0,0,0, c_top, 1, 0),
			video::S3DVertex(0,0,0, 0,0,0, c_top, 0, 0)
		};

		f32 rx = cloud_size;
		f32 ry = 8*BS;
		f32 rz = cloud_size;

		for(int i=0; i<num_faces_to_draw; i++)
		{
			switch(i)
			{
				case 0:	// top
					v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
					v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
					v[2].Pos.X= rx; v[2].Pos.Y= ry; v[2].Pos.Z= rz;
					v[3].Pos.X= rx; v[3].Pos.Y= ry, v[3].Pos.Z=-rz;
					break;
				case 1: // back
					for(int j=0;j<4;j++)
						v[j].Color=c_side_1;
					v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
					v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
					v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
					v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
					break;
				case 2: //right
					for(int j=0;j<4;j++)
						v[j].Color=c_side_2;
					v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z=-rz;
					v[1].Pos.X= rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
					v[2].Pos.X= rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
					v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
					break;
				case 3: // front
					for(int j=0;j<4;j++)
						v[j].Color=c_side_1;
					v[0].Pos.X= rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
					v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z= rz;
					v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z= rz;
					v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
					break;
				case 4: // left
					for(int j=0;j<4;j++)
						v[j].Color=c_side_2;
					v[0].Pos.X=-rx; v[0].Pos.Y= ry; v[0].Pos.Z= rz;
					v[1].Pos.X=-rx; v[1].Pos.Y= ry; v[1].Pos.Z=-rz;
					v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
					v[3].Pos.X=-rx; v[3].Pos.Y=-ry, v[3].Pos.Z= rz;
					break;
				case 5: // bottom
					for(int j=0;j<4;j++)
						v[j].Color=c_bottom;
					v[0].Pos.X= rx; v[0].Pos.Y=-ry; v[0].Pos.Z= rz;
					v[1].Pos.X=-rx; v[1].Pos.Y=-ry; v[1].Pos.Z= rz;
					v[2].Pos.X=-rx; v[2].Pos.Y=-ry; v[2].Pos.Z=-rz;
					v[3].Pos.X= rx; v[3].Pos.Y=-ry, v[3].Pos.Z=-rz;
					break;
			}

			v3f pos = v3f(p0.X,m_cloud_y,p0.Y);

			for(u16 i=0; i<4; i++)
				v[i].Pos += pos;
			u16 indices[] = {0,1,2,2,3,0};
			driver->drawVertexPrimitiveList(v, 4, indices, 2,
					video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
		}

	}
}

void Clouds::step(float dtime)
{
	m_time += dtime;
}

void Clouds::update(v2f camera_p, float brightness)
{
	m_camera_pos = camera_p;
	m_brightness = brightness;
}

ailable. if not, skip step if not advtrains.get_or_create_path(self.train_id, gp) then self.object:setvelocity({x=0, y=0, z=0}) return end local index=advtrains.get_real_path_index(self:train(), self.pos_in_train) --print("trainindex "..gp.index.." wagonindex "..index) --position recalculation local first_pos=gp.path[math.floor(index)] local second_pos=gp.path[math.floor(index)+1] if not first_pos or not second_pos then --print("[advtrains] object "..self.unique_id.." path end reached!") self.object:setvelocity({x=0,y=0,z=0}) return end --checking for environment collisions(a 3x3 cube around the center) if not gp.recently_collided_with_env then local collides=false for x=-1,1 do for y=0,2 do for z=-1,1 do local node=minetest.get_node_or_nil(vector.add(first_pos, {x=x, y=y, z=z})) if (advtrains.train_collides(node)) then collides=true end end end end if collides then gp.recently_collided_with_env=true gp.velocity=-0.5*gp.velocity gp.tarvelocity=0 end end local velocity=gp.velocity/(gp.path_dist[math.floor(gp.index)] or 1) local factor=index-math.floor(index) local actual_pos={x=first_pos.x-(first_pos.x-second_pos.x)*factor, y=first_pos.y-(first_pos.y-second_pos.y)*factor, z=first_pos.z-(first_pos.z-second_pos.z)*factor,} local velocityvec={x=(first_pos.x-second_pos.x)*velocity*-1, z=(first_pos.z-second_pos.z)*velocity*-1, y=(first_pos.y-second_pos.y)*velocity*-1} --some additional positions to determine orientation local aposfwd=gp.path[math.floor(index+2)] local aposbwd=gp.path[math.floor(index-1)] local yaw if aposfwd and aposbwd then yaw=advtrains.get_wagon_yaw(aposfwd, second_pos, first_pos, aposbwd, factor)+math.pi--TODO remove when cleaning up else yaw=math.atan2((first_pos.x-second_pos.x), (second_pos.z-first_pos.z)) end if self.wagon_flipped then yaw=yaw+math.pi end self.updatepct_timer=(self.updatepct_timer or 0)-dtime if not self.old_velocity_vector or not vector.equals(velocityvec, self.old_velocity_vector) or self.old_yaw~=yaw or self.updatepct_timer<=0 then--only send update packet if something changed self.object:setpos(actual_pos) self.object:setvelocity(velocityvec) self.object:setyaw(yaw) self.updatepct_timer=2 if self.update_animation then self:update_animation(gp.velocity) end end self.old_velocity_vector=velocityvec self.old_yaw=yaw printbm("wagon step", t) end function advtrains.get_real_path_index(train, pit) local pos_in_train_left=pit local index=train.index if pos_in_train_left>(index-math.floor(index))*(train.path_dist[math.floor(index)] or 1) then pos_in_train_left=pos_in_train_left - (index-math.floor(index))*(train.path_dist[math.floor(index)] or 1) index=math.floor(index) while pos_in_train_left>(train.path_dist[index-1] or 1) do pos_in_train_left=pos_in_train_left - (train.path_dist[index-1] or 1) index=index-1 end index=index-(pos_in_train_left/(train.path_dist[index-1] or 1)) else index=index-(pos_in_train_left/(train.path_dist[math.floor(index-1)] or 1)) end return index end function advtrains.register_wagon(sysname, traintype, prototype, desc, inv_img) setmetatable(prototype, {__index=wagon}) minetest.register_entity("advtrains:"..sysname,prototype) minetest.register_craftitem("advtrains:"..sysname, { description = desc, inventory_image = inv_img, wield_image = inv_img, stack_max = 1, on_place = function(itemstack, placer, pointed_thing) if not pointed_thing.type == "node" then return end local le=minetest.env:add_entity(pointed_thing.under, "advtrains:"..sysname):get_luaentity() local node=minetest.env:get_node_or_nil(pointed_thing.under) if not node then print("[advtrains]Ignore at placer position") return itemstack end local nodename=node.name if(not advtrains.is_track_and_drives_on(nodename, advtrains.all_traintypes[traintype].drives_on)) then print("[advtrains]no trck here, not placing.") return itemstack end local conn1=advtrains.get_track_connections(node.name, node.param2) local id=advtrains.create_new_train_at(pointed_thing.under, advtrains.dirCoordSet(pointed_thing.under, conn1), traintype) advtrains.add_wagon_to_train(le, id) if not minetest.setting_getbool("creative_mode") then itemstack:take_item() end return itemstack end, }) end advtrains.register_train_type("steam", {"regular", "default"}) --[[advtrains.register_wagon("blackwagon", "steam",{textures = {"black.png"}}) advtrains.register_wagon("bluewagon", "steam",{textures = {"blue.png"}}) advtrains.register_wagon("greenwagon", "steam",{textures = {"green.png"}}) advtrains.register_wagon("redwagon", "steam",{textures = {"red.png"}}) advtrains.register_wagon("yellowwagon", "steam",{textures = {"yellow.png"}}) ]] --[[ wagons can define update_animation(self, velocity) if they have a speed-dependent animation this function will be called when the velocity vector changes or every 2 seconds. ]] advtrains.register_wagon("newlocomotive", "steam",{ mesh="newlocomotive.b3d", textures = {"advtrains_newlocomotive.png"}, is_locomotive=true, attach_offset={x=5, y=10, z=-10}, view_offset={x=0, y=6, z=0}, visual_size = {x=1, y=1}, wagon_span=1.85, collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0}, update_animation=function(self, velocity) if self.old_anim_velocity~=advtrains.abs_ceil(velocity) then self.object:set_animation({x=1,y=60}, math.floor(velocity)) self.old_anim_velocity=advtrains.abs_ceil(velocity) end end }, "Steam Engine", "advtrains_newlocomotive_inv.png") advtrains.register_wagon("wagon_default", "steam",{ mesh="wagon.b3d", textures = {"advtrains_wagon.png"}, attach_offset={x=0, y=10, z=0}, view_offset={x=0, y=6, z=0}, visual_size = {x=1, y=1}, wagon_span=1.8, collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0}, }, "Passenger Wagon", "advtrains_wagon_inv.png") advtrains.register_train_type("subway", {"regular", "default"}) advtrains.register_wagon("subway_wagon", "subway",{ mesh="advtrains_subway_train.b3d", textures = {"advtrains_subway_train.png"}, attach_offset={x=0, y=10, z=0}, view_offset={x=0, y=6, z=0}, visual_size = {x=1, y=1}, wagon_span=1.8, collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0}, is_locomotive=true, }, "Subway Passenger Wagon", "advtrains_subway_train_inv.png") --[[ advtrains.register_wagon("wagontype1",{on_rightclick=function(self, clicker) if clicker:get_player_control().sneak then advtrains.disconnect_train_before_wagon(self) return end --just debugging. look for first active wagon and attach to it. for _,v in pairs(minetest.luaentities) do if v.is_wagon and v.unique_id and v.unique_id~=self.unique_id then self.train_id=v.unique_id end end if not self.train_id then minetest.chat_send_all("not found") return end minetest.chat_send_all(self.train_id.." found and attached.") end}) ]]