aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/scene/ilight.cpp
blob: e405443c004b2f556a81cf4d4352e1b1f3a18ff0 (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
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <memory>
#include <map>
#include <functional>
extern "C" {
  #include <lua.h>
  #include <lauxlib.h>
  #include <lualib.h>
}
#include <irrlicht.h>
#include "../gameparts.hpp"
#include "ilight.hpp"
#include "igeneric.hpp"
#include <shared/lua_api/common.hpp>

using namespace irr;
using namespace video;
using namespace scene;
using namespace core;

extern IrrlichtDevice* device;

/***
Create a light
Creates a light that illuminates the surrounding objects dynamically.
@function scene.newlight(number radius, vector3d position)
@tparam number radius The radius that the light should shine
@tparam vector3d position The position to create the light at
@treturn iscenelight The created light
*/
/***
A light.
Lights illuminate the scene nodes arounded them based on mesh vertexes. You
probably want to bake the lightmap instead of using light nodes.
@class iscenelight
@inherits iscenenode
*/
//{} :: scene.newlight(radius, {v3 position})
static int newiscenelight(lua_State* L){
	printf("Createing light!\n");
	double x,y,z;
	popvector3d(L,&x,&y,&z);//radius
	double radius = lua_tonumber(L,-1);
    	lua_pop(L,1);//

	//Create the mesh
	ISceneManager* smgr = device->getSceneManager();
	ILightSceneNode* light = smgr->addLightSceneNode(0,vector3df(x,y,z),video::SColor(1.0f,1.0f,1.0f,1.0f),(f32)radius,(s32)-1);

	//Create it's lua representation
	lua_newtable(L);//{}
	lua_pushlightuserdata(L,light);
	lua_setfield(L,-2,"node");

	//Set it's metatable
	luaL_getmetatable(L, "scene.imesh");
	lua_setmetatable(L, -2);

	return 1;

}

/***
Sets the light type
Different light types illuminate the surrounding meshes in different ways.
@function iscenelight:settype(number type)
@tparam number type The type the light should be
*/
//settype(self,const)
int settype(lua_State *L){
	video::E_LIGHT_TYPE type = (video::E_LIGHT_TYPE)lua_tonumber(L,-1);//self,type
	lua_pop(L,1);//self
	lua_getfield(L,-1,"node");//self,ud_ILightSceneNode
	ILightSceneNode *light = (ILightSceneNode*)lua_touserdata(L,-1);
	lua_pop(L,2);//
	light->setLightType(type);
	return 0;
}

static const luaL_reg ilight_m[] = {
	{"settype",       settype},
	{0, 0},
};

void ilight_register(lua_State* L){

	lua_getglobal(L,"scene");//{scene}
	lua_pushcfunction(L,newiscenelight);//{scene},newiscenelight()
	lua_setfield(L,-2,"newlight");//{scene}

/***
A point light
@field scene.ELT_POINT
*/
	set_const(L,ELT_POINT);
/***
A spot light
@field scene.ELT_SPOT
*/
	set_const(L,ELT_SPOT);
/***
A sun light
@field scene.ELT_DIRECTIONAL
*/
	set_const(L,ELT_DIRECTIONAL);

	lua_pop(L,1);//

	luaL_newmetatable(L,"scene.ilight");//scene.ilight
	lua_newtable(L);//scene.ilight,{}
	luaL_register(L, NULL, ilight_m);//scene.ilight,{}
	luaL_register(L, NULL, igeneric_m);
	lua_setfield(L,-2,"__index");//scene.ilight

	lua_pop(L,1);
}