aboutsummaryrefslogtreecommitdiff
path: root/src/shader.h
blob: 811f33080d8453091da09889f1e8831730d813fc (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
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2013 Kahrl <kahrl@gmx.net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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.
*/

#ifndef SHADER_HEADER
#define SHADER_HEADER

#include "irrlichttypes_extrabloated.h"
#include "threads.h"
#include <string>

class IGameDef;

/*
	shader.{h,cpp}: Shader handling stuff.
*/

/*
	Gets the path to a shader by first checking if the file
	  name_of_shader/filename
	exists in shader_path and if not, using the data path.

	If not found, returns "".

	Utilizes a thread-safe cache.
*/
std::string getShaderPath(const std::string &name_of_shader,
		const std::string &filename);

struct ShaderInfo
{
	std::string name;
	video::E_MATERIAL_TYPE base_material;
	video::E_MATERIAL_TYPE material;
	u8 drawtype;
	u8 material_type;
	s32 user_data;

	ShaderInfo(): name(""), base_material(video::EMT_SOLID),
		material(video::EMT_SOLID),
		drawtype(0), material_type(0) {}
	virtual ~ShaderInfo() {}
};

/*
	Setter of constants for shaders
*/

namespace irr { namespace video {
	class IMaterialRendererServices;
} }

class IShaderConstantSetter
{
public:
	virtual ~IShaderConstantSetter(){};
	virtual void onSetConstants(video::IMaterialRendererServices *services,
			bool is_highlevel) = 0;
};

/*
	ShaderSource creates and caches shaders.
*/

class IShaderSource
{
public:
	IShaderSource(){}
	virtual ~IShaderSource(){}
	virtual u32 getShaderIdDirect(const std::string &name,
		const u8 material_type, const u8 drawtype){return 0;}
	virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
	virtual u32 getShader(const std::string &name,
		const u8 material_type, const u8 drawtype){return 0;}
};

class IWritableShaderSource : public IShaderSource
{
public:
	IWritableShaderSource(){}
	virtual ~IWritableShaderSource(){}
	virtual u32 getShaderIdDirect(const std::string &name,
		const u8 material_type, const u8 drawtype){return 0;}
	virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
	virtual u32 getShader(const std::string &name,
		const u8 material_type, const u8 drawtype){return 0;}

	virtual void processQueue()=0;
	virtual void insertSourceShader(const std::string &name_of_shader,
		const std::string &filename, const std::string &program)=0;
	virtual void rebuildShaders()=0;
	virtual void addGlobalConstantSetter(IShaderConstantSetter *setter)=0;
};

IWritableShaderSource* createShaderSource(IrrlichtDevice *device);

#endif
/a> 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
-- Minetest: builtin/item.lua

local builtin_shared = ...
local SCALE = 0.667

local facedir_to_euler = {
	{y = 0, x = 0, z = 0},
	{y = -math.pi/2, x = 0, z = 0},
	{y = math.pi, x = 0, z = 0},
	{y = math.pi/2, x = 0, z = 0},
	{y = math.pi/2, x = -math.pi/2, z = math.pi/2},
	{y = math.pi/2, x = math.pi, z = math.pi/2},
	{y = math.pi/2, x = math.pi/2, z = math.pi/2},
	{y = math.pi/2, x = 0, z = math.pi/2},
	{y = -math.pi/2, x = math.pi/2, z = math.pi/2},
	{y = -math.pi/2, x = 0, z = math.pi/2},
	{y = -math.pi/2, x = -math.pi/2, z = math.pi/2},
	{y = -math.pi/2, x = math.pi, z = math.pi/2},
	{y = 0, x = 0, z = math.pi/2},
	{y = 0, x = -math.pi/2, z = math.pi/2},
	{y = 0, x = math.pi, z = math.pi/2},
	{y = 0, x = math.pi/2, z = math.pi/2},
	{y = math.pi, x = math.pi, z = math.pi/2},
	{y = math.pi, x = math.pi/2, z = math.pi/2},
	{y = math.pi, x = 0, z = math.pi/2},
	{y = math.pi, x = -math.pi/2, z = math.pi/2},
	{y = math.pi, x = math.pi, z = 0},
	{y = -math.pi/2, x = math.pi, z = 0},
	{y = 0, x = math.pi, z = 0},
	{y = math.pi/2, x = math.pi, z = 0}
}

local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81

--
-- Falling stuff
--

core.register_entity(":__builtin:falling_node", {
	initial_properties = {
		visual = "item",
		visual_size = {x = SCALE, y = SCALE, z = SCALE},
		textures = {},
		physical = true,
		is_visible = false,
		collide_with_objects = true,
		collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
	},

	node = {},
	meta = {},
	floats = false,

	set_node = function(self, node, meta)
		self.node = node
		meta = meta or {}
		if type(meta.to_table) == "function" then
			meta = meta:to_table()
		end
		for _, list in pairs(meta.inventory or {}) do
			for i, stack in pairs(list) do
				if type(stack) == "userdata" then
					list[i] = stack:to_string()
				end
			end
		end
		local def = core.registered_nodes[node.name]
		if not def then
			-- Don't allow unknown nodes to fall
			core.log("info",
				"Unknown falling node removed at "..
				core.pos_to_string(self.object:get_pos()))
			self.object:remove()
			return
		end
		self.meta = meta

		-- Cache whether we're supposed to float on water
		self.floats = core.get_item_group(node.name, "float") ~= 0

		-- Set entity visuals
		if def.drawtype == "torchlike" or def.drawtype == "signlike" then
			local textures
			if def.tiles and def.tiles[1] then
				local tile = def.tiles[1]
				if type(tile) == "table" then
					tile = tile.name
				end
				if def.drawtype == "torchlike" then
					textures = { "("..tile..")^[transformFX", tile }
				else
					textures = { tile, "("..tile..")^[transformFX" }
				end
			end
			local vsize
			if def.visual_scale then
				local s = def.visual_scale
				vsize = {x = s, y = s, z = s}
			end
			self.object:set_properties({
				is_visible = true,
				visual = "upright_sprite",
				visual_size = vsize,
				textures = textures,
				glow = def.light_source,
			})
		elseif def.drawtype ~= "airlike" then
			local itemstring = node.name
			if core.is_colored_paramtype(def.paramtype2) then
				itemstring = core.itemstring_with_palette(itemstring, node.param2)
			end
			local vsize
			if def.visual_scale then
				local s = def.visual_scale * SCALE
				vsize = {x = s, y = s, z = s}
			end
			self.object:set_properties({
				is_visible = true,
				wield_item = itemstring,
				visual_size = vsize,
				glow = def.light_source,
			})
		end

		-- Rotate entity
		if def.drawtype == "torchlike" then
			self.object:set_yaw(math.pi*0.25)
		elseif (node.param2 ~= 0 and (def.wield_image == ""
				or def.wield_image == nil))
				or def.drawtype == "signlike"
				or def.drawtype == "mesh"
				or def.drawtype == "normal"
				or def.drawtype == "nodebox" then
			if (def.paramtype2 == "facedir" or def.paramtype2 == "colorfacedir") then
				local fdir = node.param2 % 32
				-- Get rotation from a precalculated lookup table
				local euler = facedir_to_euler[fdir + 1]
				if euler then
					self.object:set_rotation(euler)
				end
			elseif (def.paramtype2 == "wallmounted" or def.paramtype2 == "colorwallmounted") then
				local rot = node.param2 % 8
				local pitch, yaw, roll = 0, 0, 0
				if rot == 1 then
					pitch, yaw = math.pi, math.pi
				elseif rot == 2 then
					pitch, yaw = math.pi/2, math.pi/2
				elseif rot == 3 then
					pitch, yaw = math.pi/2, -math.pi/2
				elseif rot == 4 then
					pitch, yaw = math.pi/2, math.pi
				elseif rot == 5 then
					pitch, yaw = math.pi/2, 0
				end
				if def.drawtype == "signlike" then
					pitch = pitch - math.pi/2
					if rot == 0 then
						yaw = yaw + math.pi/2
					elseif rot == 1 then
						yaw = yaw - math.pi/2
					end
				elseif def.drawtype == "mesh" or def.drawtype == "normal" then
					if rot >= 0 and rot <= 1 then
						roll = roll + math.pi
					else
						yaw = yaw + math.pi
					end
				end
				self.object:set_rotation({x=pitch, y=yaw, z=roll})
			end
		end
	end,

	get_staticdata = function(self)
		local ds = {
			node = self.node,
			meta = self.meta,
		}
		return core.serialize(ds)
	end,

	on_activate = function(self, staticdata)
		self.object:set_armor_groups({immortal = 1})
		self.object:set_acceleration({x = 0, y = -gravity, z = 0})

		local ds = core.deserialize(staticdata)
		if ds and ds.node then
			self:set_node(ds.node, ds.meta)
		elseif ds then
			self:set_node(ds)
		elseif staticdata ~= "" then
			self:set_node({name = staticdata})
		end
	end,

	try_place = function(self, bcp, bcn)
		local bcd = core.registered_nodes[bcn.name]
		-- Add levels if dropped on same leveled node
		if bcd and bcd.leveled and
				bcn.name == self.node.name then
			local addlevel = self.node.level
			if not addlevel or addlevel <= 0 then
				addlevel = bcd.leveled
			end
			if core.add_node_level(bcp, addlevel) == 0 then
				return true
			end
		end

		-- Decide if we're replacing the node or placing on top
		local np = vector.new(bcp)
		if bcd and bcd.buildable_to and
				(not self.floats or bcd.liquidtype == "none") then
			core.remove_node(bcp)
		else
			np.y = np.y + 1
		end

		-- Check what's here
		local n2 = core.get_node(np)
		local nd = core.registered_nodes[n2.name]
		-- If it's not air or liquid, remove node and replace it with
		-- it's drops
		if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
			if nd and nd.buildable_to == false then
				nd.on_dig(np, n2, nil)
				-- If it's still there, it might be protected
				if core.get_node(np).name == n2.name then
					return false
				end
			else
				core.remove_node(np)
			end
		end

		-- Create node
		local def = core.registered_nodes[self.node.name]
		if def then
			core.add_node(np, self.node)
			if self.meta then
				core.get_meta(np):from_table(self.meta)
			end
			if def.sounds and def.sounds.place then
				core.sound_play(def.sounds.place, {pos = np}, true)
			end
		end
		core.check_for_falling(np)
		return true
	end,

	on_step = function(self, dtime, moveresult)
		-- Fallback code since collision detection can't tell us
		-- about liquids (which do not collide)
		if self.floats then
			local pos = self.object:get_pos()

			local bcp = vector.round({x = pos.x, y = pos.y - 0.7, z = pos.z})
			local bcn = core.get_node(bcp)

			local bcd = core.registered_nodes[bcn.name]
			if bcd and bcd.liquidtype ~= "none" then
				if self:try_place(bcp, bcn) then
					self.object:remove()
					return
				end
			end
		end

		assert(moveresult)
		if not moveresult.collides then
			return -- Nothing to do :)
		end

		local bcp, bcn
		local player_collision
		if moveresult.touching_ground then
			for _, info in ipairs(moveresult.collisions) do
				if info.type == "object" then
					if info.axis == "y" and info.object:is_player() then
						player_collision = info
					end
				elseif info.axis == "y" then
					bcp = info.node_pos
					bcn = core.get_node(bcp)
					break
				end
			end
		end

		if not bcp then
			-- We're colliding with something, but not the ground. Irrelevant to us.
			if player_collision then
				-- Continue falling through players by moving a little into
				-- their collision box
				-- TODO: this hack could be avoided in the future if objects
				--       could choose who to collide with
				local vel = self.object:get_velocity()
				self.object:set_velocity({
					x = vel.x,
					y = player_collision.old_velocity.y,
					z = vel.z
				})
				self.object:set_pos(vector.add(self.object:get_pos(),
					{x = 0, y = -0.2, z = 0}))
			end
			return
		elseif bcn.name == "ignore" then
			-- Delete on contact with ignore at world edges
			self.object:remove()
			return
		end

		local failure = false

		local pos = self.object:get_pos()
		local distance = vector.apply(vector.subtract(pos, bcp), math.abs)
		if distance.x >= 1 or distance.z >= 1 then
			-- We're colliding with some part of a node that's sticking out
			-- Since we don't want to visually teleport, drop as item
			failure = true
		elseif distance.y >= 2 then
			-- Doors consist of a hidden top node and a bottom node that is
			-- the actual door. Despite the top node being solid, the moveresult
			-- almost always indicates collision with the bottom node.
			-- Compensate for this by checking the top node
			bcp.y = bcp.y + 1
			bcn = core.get_node(bcp)