aboutsummaryrefslogtreecommitdiff
path: root/src/client/lua_api/scene/imesh.cpp
blob: b5c8939bd4494ca47bbd7d502c8d6c94d7dfda82 (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
#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 "imesh.hpp"
#include "igeneric.hpp"
#include <shared/lua_api/common.hpp>

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

extern IrrlichtDevice* device;

//static LISceneNode* checkismesh(lua_State* L, int index){
  //void* ud = luaL_checkudata(L,index,"scene.imesh");
  //luaL_argcheck(L,ud != NULL, index, "'scene.imesh' expected");
  //return (LISceneNode*) ud;
//}

/*
static LISceneNode* checkismesh(lua_State* L){
  return checkismesh(L,1);
}
*/

//{} :: scene.newmesh("/path/to/model")
static int newiscenemesh(lua_State* L){//"path/to"

	printf("Createing mesh!\n");
	int nargs = lua_gettop(L);
	if(nargs != 1){
		lua_pushfstring(L,"scene.newmesh got %d arguments, expecting 1",nargs);
		lua_error(L);
	}
	//The model for the mesh
	const char* modelpath = luaL_optstring(L,1,"error");//"path/to"
	lua_pop(L,1);//

	//Create the mesh
	ISceneManager* smgr = device->getSceneManager();
	IAnimatedMesh* amesh = smgr->getMesh(modelpath);
	IAnimatedMeshSceneNode* mesh = smgr->addAnimatedMeshSceneNode( amesh );
	mesh->setMaterialFlag(EMF_GOURAUD_SHADING,true);
	printf("Registered the mesh!\n");

	lua_newtable(L);//{}
	luaL_getmetatable(L,"scene.imesh");//{},scene.imesh
	lua_setmetatable(L,-2);//{}

	lua_pushlightuserdata(L,mesh);//{},ud_mesh
	lua_setfield(L,-2,"node");//{}

	return 1;
}

// ud_node :: ({v3 size}, {v3 origin})
void makenewiscenecube(lua_State* L){
	double x,y,z;
	popvector3d(L,&x,&y,&z);//{v3 size}
	double sx,sy,sz;
	popvector3d(L,&sx,&sy,&sz);//{}
	IMeshSceneNode* n = device->getSceneManager()->addCubeSceneNode(1,0,-1,core::vector3df(x,y,z),core::vector3df(0,0,0),core::vector3df(sx,sy,sz));
	lua_pushlightuserdata(L,n);
}

// {} :: scene.newcube({v3 size}, {v3 origin})
int newiscenecube(lua_State* L){//{v3 size}, {v3 origin}
	makenewiscenecube(L);//ud_node
	ISceneNode* n = (ISceneNode*)lua_touserdata(L,-1);//ud_node
	lua_pop(L,1);//
	lua_newtable(L);//{}
	luaL_getmetatable(L,"scene.icube");//{},scene.icube
	lua_setmetatable(L,-2);//{}

	lua_pushlightuserdata(L,n);//{},ud_mesh
	lua_setfield(L,-2,"node");//{}

	return 1;
}

// self:setMaterial("path/to/material")
//int iscenesetmaterial(lua_State* L){//self,"path/to"
	//ISceneNode* node = (IMeshSceneNode*)lua_touserdata(L,-2);
	//const char* s = lua_tostring(L,-1);
	////ISceneNode* i = toiscenenode(L,1)->n;
	////const char* s = luaL_optstring(L,2,"error.png");
	////printf("Setting material to %s",s);

	//IVideoDriver* driver = device->getVideoDriver();
	//node->setMaterialTexture(0, driver->getTexture(s));

	//lua_pop(L,2);
	//return 0;
//}

static const luaL_reg imesh_m[] = {
	{"setMaterial",          iscenesetmaterial},
	{"getpos",               iscenegetpos},
	{"setpos",               iscenesetpos},
	//  {"remove",        removeiguielement},
	{0, 0},
};

static const luaL_reg icube_m[] = {
	{0,0},
};

void imesh_register(lua_State* L){

	luaL_newmetatable(L, "scene.imesh");//scene.icamera
	lua_newtable(L);//scene.icamera,{}
	luaL_register(L,NULL,imesh_m);//scene.icamera,{}
	lua_setfield(L,-2,"__index");//scene.icamera
	lua_pop(L,1);//
	
	luaL_newmetatable(L,"scene.icube");//scene.icube
	lua_newtable(L);//scene.icube, {}
	luaL_register(L,NULL,imesh_m);//scene.icube,{}
	luaL_register(L,NULL,icube_m);//scene.icube,{}
	lua_setfield(L,-2,"__index");//scene.icube
	lua_pop(L,1);//

	lua_getglobal(L,"scene");
	lua_pushcfunction(L,newiscenemesh);
	lua_setfield(L,-2,"newmesh");
	lua_pushcfunction(L,newiscenecube);
	lua_setfield(L,-2,"newcube");

}