aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/video/smaterial.cpp
blob: 4ed6a46903b2226629467b10b9d9bfafd0db6550 (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
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <irrlicht.h>
#include "smaterial.hpp"

using namespace irr::video;

/***
Creates a new material.
Creates a new material that can then be used on an @{iscenenode}
WARNING: this item is not currently garbage collected, be careful not to make
more smaterials than you need.
@function video.newsmaterial()
@treturn smaterial The new material
*/
/*TODO: This probably needs a _gc metamethod*/
//newsmaterial()
int newsmaterial(lua_State* L){

	SMaterial* mat = new SMaterial();
	lua_newtable(L);//{}
	lua_pushlightuserdata(L,mat);//{},ud_smaterial
	lua_setfield(L,-2,"smaterial");//{smaterial}

	luaL_getmetatable(L,"video.smaterial");//{smaterial},{m_smaterial}
	lua_setmetatable(L,-2);//{smaterial}

	return 1;
}

/***
Sets a texture for this material.
Sets a texture for the material on the given index. For more information on
what each texture of a material does, see the irrlicht documentation.
@function smaterial:settexture(index,texture)
@tparam number index The index of the texture in the material
@tparam itexture texture The texture to use
*/
//setTexture(self,int_num,{ITexture texture})
int setTexture(lua_State* L){
	lua_getfield(L,-1,"texture");
	ITexture* tex = (ITexture*)lua_touserdata(L,-1);
	lua_pop(L,2);
	double num = lua_tonumber(L,-1);
	lua_pop(L,1);
	lua_getfield(L,-1,"smaterial");
	SMaterial* self = (SMaterial*)lua_touserdata(L,-1);
	lua_pop(L,2);
	self->setTexture(num,tex);
	return 0;
}

/***
Sets a flag on this material.
Sets the given flag on the material
@function smaterial:setflag(flag,state)
@tparam number flag The flag to set, see @{video} for flags.
@tparam boolean state The state to set the flag in
*/
//{Material},flag,state
int setFlag(lua_State* L){
	int b = lua_toboolean(L,-1);//{material},flag,state
	lua_pop(L,1);//{material},flag
	int flag = lua_tonumber(L,-1);//{material},flag
	lua_pop(L,1);//{material}
	lua_getfield(L,-1,"smaterial");//{material},ud_material
	SMaterial* mat = (SMaterial*)lua_touserdata(L,-1);
	lua_pop(L,2);
	mat->setFlag((E_MATERIAL_FLAG)flag,b == 1 ? true : false);
	return 0;
}

static const luaL_reg smaterial_m[] = {
  {"settexture",        setTexture},
  {"setflag",           setFlag},
  {0,0},
};

#define set_const(l,x) lua_pushstring(l,#x);lua_pushinteger(l,x);lua_settable(l,-3);

void smaterial_register(lua_State* L){
	//Add globals dealing with material flags

	luaL_newmetatable(L,"video.smaterial");//{m_smaterial}
	
	lua_newtable(L);//{m_smaterial},{}
	luaL_register(L,NULL,smaterial_m);//{m_smaterial},{smaterial}

	lua_setfield(L,-2,"__index");//{m_smaterial}

	lua_pop(L,1);//

	lua_getglobal(L,"video");//{}

	/***
	A table of constants for the material class.
	@field EMF_WIREFRAME - enable wireframe
	@field EMF_POINTCLOUD - displays the texture as a pointcloud instead of a solid face
	@field EMF_GOURAUD_SHADING
	@field EMF_LIGHTING
	@field EMF_ZBUFFER
	@field EMF_ZWRITE_ENABLE
	@field EMF_BACK_FACE_CULLING
	@field EMF_FRONT_FACE_CULLING
	@field EMF_BILINEAR_FILTER
	@field EMF_TRILINEAR_FILTER
	@field EMF_ANISOTROPIC_FILTER
	@field EMF_FOG_ENABLE
	@field EMF_NORMALIZE_NORMALS
	@field EMF_TEXTURE_WRAP
	@field EMF_ANTI_ALIASING
	@field EMF_COLOR_MASK
	@field EMF_COLOR_MATERIAL
	@field EMF_USE_MIP_MAPS
	@field EMF_BLEND_OPERATION
	@field EMF_POLYGON_OFFSET
	@table .video.
	*/
	set_const(L,EMF_WIREFRAME);
	set_const(L,EMF_POINTCLOUD);
	set_const(L,EMF_GOURAUD_SHADING);
	set_const(L,EMF_LIGHTING);
	set_const(L,EMF_ZBUFFER);
	set_const(L,EMF_ZWRITE_ENABLE);
	set_const(L,EMF_BACK_FACE_CULLING);
	set_const(L,EMF_FRONT_FACE_CULLING);
	set_const(L,EMF_BILINEAR_FILTER);
	set_const(L,EMF_TRILINEAR_FILTER);
	set_const(L,EMF_ANISOTROPIC_FILTER);
	set_const(L,EMF_FOG_ENABLE);
	set_const(L,EMF_NORMALIZE_NORMALS);
	set_const(L,EMF_TEXTURE_WRAP);
	set_const(L,EMF_ANTI_ALIASING);
	set_const(L,EMF_COLOR_MASK);
	set_const(L,EMF_COLOR_MATERIAL);
	set_const(L,EMF_USE_MIP_MAPS);
	set_const(L,EMF_BLEND_OPERATION);
	set_const(L,EMF_POLYGON_OFFSET);

	lua_pushcfunction(L,newsmaterial);//{},newsmaterial
	lua_setfield(L,-2,"newsmaterial");//{}

	lua_pop(L,1);//

}