Repository navigation
Expand file tree
/
Copy pathscene_lua.cpp
More file actions
426 lines (327 loc) · 11.5 KB
/
Copy pathscene_lua.cpp
File metadata and controls
426 lines (327 loc) · 11.5 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//
// CS488 - Introduction to Computer Graphics
//
// scene_lua.cpp
//
// Everything that's needed to parse a scene file using Lua.
// You don't necessarily have to understand exactly everything that
// goes on here, although it will be useful to have a reasonable idea
// if you wish to add new commands to the scene format.
//
// Lua interfaces with C/C++ using a special stack. Everytime you want
// to get something from lua, or pass something back to lua (e.g. a
// return value), you need to use this stack. Thus, most of the lua_
// and luaL_ functions actually manipulate the stack. All the
// functions beginning with "lua_" are part of the Lua C API itself,
// whereas the "luaL_" functions belong to a library of useful
// functions on top of that called lauxlib.
//
// This file consists of a bunch of C function declarations which
// implement functions callable from Lua. There are also two tables
// used to set up the interface between Lua and these functions, and
// the main "driver" function, import_lua, which calls the lua
// interpreter and sets up all the state.
//
// Note that each of the function declarations follow the same format:
// they take as their only argument the current state of the lua
// interpreter, and return the number of values returned back to lua.
//
// For more information see the book "Programming In Lua," available
// online at http://www.lua.org/pil/, and of course the Lua reference
// manual at http://www.lua.org/manual/5.0/.
//
// http://lua-users.org/wiki/LauxLibDocumentation provides a useful
// documentation of the "lauxlib" functions (beginning with luaL_).
//
// -- University of Waterloo Computer Graphics Lab 2005
#include "scene_lua.hpp"
#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdio>
#include "lua488.hpp"
#include "JointNode.hpp"
#include "GeometryNode.hpp"
// Uncomment the following line to enable debugging messages
//#define GRLUA_ENABLE_DEBUG
#ifdef GRLUA_ENABLE_DEBUG
# define GRLUA_DEBUG(x) do { std::cerr << x << std::endl; } while (0)
# define GRLUA_DEBUG_CALL do { std::cerr << __FUNCTION__ << std::endl; } while (0)
#else
# define GRLUA_DEBUG(x) do { } while (0)
# define GRLUA_DEBUG_CALL do { } while (0)
#endif
// You may wonder, for the following types, why we use special "_ud"
// types instead of, for example, just allocating SceneNodes directly
// from lua. Part of the answer is that Lua is a C api. It doesn't
// call any constructors or destructors for you, so it's easier if we
// let it just allocate a pointer for the node, and handle
// allocation/deallocation of the node itself. Another (perhaps more
// important) reason is that we will want SceneNodes to stick around
// even after lua is done with them, after all, we want to pass them
// back to the program. If we let Lua allocate SceneNodes directly,
// we'd lose them all when we are done parsing the script. This way,
// we can easily keep around the data, all we lose is the extra
// pointers to it.
// The "userdata" type for a node. Objects of this type will be
// allocated by Lua to represent nodes.
struct gr_node_ud {
SceneNode* node;
};
// The "userdata" type for a material. Objects of this type will be
// allocated by Lua to represent materials.
struct gr_material_ud {
Material* material;
};
// Create a node
extern "C"
int gr_node_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* data = (gr_node_ud*)lua_newuserdata(L, sizeof(gr_node_ud));
data->node = 0;
const char* name = luaL_checkstring(L, 1);
data->node = new SceneNode(name);
luaL_getmetatable(L, "gr.node");
lua_setmetatable(L, -2);
return 1;
}
// Create a joint node
extern "C"
int gr_joint_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* data = (gr_node_ud*)lua_newuserdata(L, sizeof(gr_node_ud));
data->node = 0;
const char* name = luaL_checkstring(L, 1);
JointNode* node = new JointNode(name);
luaL_checktype(L, 2, LUA_TTABLE);
luaL_argcheck(L, luaL_len(L, 2) == 3, 2, "Three-tuple expected");
luaL_checktype(L, 3, LUA_TTABLE);
luaL_argcheck(L, luaL_len(L, 3) == 3, 3, "Three-tuple expected");
double x[3], y[3];
for (int i = 1; i <= 3; i++) {
lua_rawgeti(L, 2, i);
x[i - 1] = luaL_checknumber(L, -1);
lua_rawgeti(L, 3, i);
y[i - 1] = luaL_checknumber(L, -1);
lua_pop(L, 2);
}
node->set_joint_x(x[0], x[1], x[2]);
node->set_joint_y(y[0], y[1], y[2]);
data->node = node;
luaL_getmetatable(L, "gr.node");
lua_setmetatable(L, -2);
return 1;
}
extern "C"
int gr_mesh_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* data = (gr_node_ud*)lua_newuserdata(L, sizeof(gr_node_ud));
data->node = 0;
const char* meshId = luaL_checkstring(L, 1);
const char* name = luaL_checkstring(L, 2);
data->node = new GeometryNode(meshId, name);
luaL_getmetatable(L, "gr.node");
lua_setmetatable(L, -2);
return 1;
}
// Create a material
extern "C"
int gr_material_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_material_ud* data = (gr_material_ud*)lua_newuserdata(L, sizeof(gr_material_ud));
data->material = 0;
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argcheck(L, luaL_len(L, 1) == 3, 1, "Three-tuple expected");
luaL_checktype(L, 2, LUA_TTABLE);
luaL_argcheck(L, luaL_len(L, 2) == 3, 2, "Three-tuple expected");
luaL_checktype(L, 3, LUA_TNUMBER);
double kd[3], ks[3];
for (int i = 1; i <= 3; i++) {
lua_rawgeti(L, 1, i);
kd[i - 1] = luaL_checknumber(L, -1);
lua_rawgeti(L, 2, i);
ks[i - 1] = luaL_checknumber(L, -1);
lua_pop(L, 2);
}
double shininess = luaL_checknumber(L, 3);
data->material = new Material();
for(int i(0); i < 3; ++i) {
data->material->kd[i] = kd[i];
data->material->ks[i] = ks[i];
}
data->material->shininess = shininess;
luaL_newmetatable(L, "gr.material");
lua_setmetatable(L, -2);
return 1;
}
// Add a child to a node
extern "C"
int gr_node_add_child_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* selfdata = (gr_node_ud*)luaL_checkudata(L, 1, "gr.node");
luaL_argcheck(L, selfdata != 0, 1, "Node expected");
SceneNode* self = selfdata->node;
gr_node_ud* childdata = (gr_node_ud*)luaL_checkudata(L, 2, "gr.node");
luaL_argcheck(L, childdata != 0, 2, "Node expected");
SceneNode* child = childdata->node;
self->add_child(child);
return 0;
}
// Set a node's material
extern "C"
int gr_node_set_material_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* selfdata = (gr_node_ud*)luaL_checkudata(L, 1, "gr.node");
luaL_argcheck(L, selfdata != 0, 1, "Node expected");
GeometryNode* self = dynamic_cast<GeometryNode*>(selfdata->node);
luaL_argcheck(L, self != 0, 1, "Geometry node expected");
gr_material_ud* matdata = (gr_material_ud*)luaL_checkudata(L, 2, "gr.material");
luaL_argcheck(L, matdata != 0, 2, "Material expected");
Material * material = matdata->material;
self->material.kd = material->kd;
self->material.ks = material->ks;
self->material.shininess = material->shininess;
return 0;
}
// Add a scaling transformation to a node.
extern "C"
int gr_node_scale_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* selfdata = (gr_node_ud*)luaL_checkudata(L, 1, "gr.node");
luaL_argcheck(L, selfdata != 0, 1, "Node expected");
SceneNode* self = selfdata->node;
double values[3];
for (int i = 0; i < 3; i++) {
values[i] = luaL_checknumber(L, i + 2);
}
self->scale(glm::vec3(values[0], values[1], values[2]));
return 0;
}
// Add a translation to a node.
extern "C"
int gr_node_translate_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* selfdata = (gr_node_ud*)luaL_checkudata(L, 1, "gr.node");
luaL_argcheck(L, selfdata != 0, 1, "Node expected");
SceneNode* self = selfdata->node;
double values[3];
for (int i = 0; i < 3; i++) {
values[i] = luaL_checknumber(L, i + 2);
}
self->translate(glm::vec3(values[0], values[1], values[2]));
return 0;
}
// Rotate a node.
extern "C"
int gr_node_rotate_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* selfdata = (gr_node_ud*)luaL_checkudata(L, 1, "gr.node");
luaL_argcheck(L, selfdata != 0, 1, "Node expected");
SceneNode* self = selfdata->node;
const char* axis_string = luaL_checkstring(L, 2);
luaL_argcheck(L, axis_string
&& std::strlen(axis_string) == 1, 2, "Single character expected");
char axis = std::tolower(axis_string[0]);
luaL_argcheck(L, axis >= 'x' && axis <= 'z', 2, "Axis must be x, y or z");
double angle = luaL_checknumber(L, 3);
self->rotate(axis, angle);
return 0;
}
// Garbage collection function for lua.
extern "C"
int gr_node_gc_cmd(lua_State* L)
{
GRLUA_DEBUG_CALL;
gr_node_ud* data = (gr_node_ud*)luaL_checkudata(L, 1, "gr.node");
luaL_argcheck(L, data != 0, 1, "Node expected");
// Note that we don't delete the node here. This is because we still
// want the scene to be around when we close the lua interpreter,
// but at that point everything will be garbage collected.
//
// If data->node happened to be a reference-counted pointer, this
// will in fact just decrease lua's reference to it, so it's not a
// bad thing to include this line.
data->node = 0;
return 0;
}
// This is where all the "global" functions in our library are
// declared.
// If you want to add a new non-member function, add it here.
static const luaL_Reg grlib_functions[] = {
{"node", gr_node_cmd},
{"joint", gr_joint_cmd},
{"mesh", gr_mesh_cmd},
{"material", gr_material_cmd},
{0, 0}
};
// This is where all the member functions for "gr.node" objects are
// declared. Since all the other objects (e.g. materials) are so
// simple, we only really need to make member functions for nodes.
//
// If you want to add a new member function for gr.node, add it
// here.
//
// We could have used inheritance in lua to match the inheritance
// between different node types, but it's easier to just give all
// nodes the same Lua type and then do any additional type checking in
// the appropriate member functions (e.g. gr_node_set_material_cmd
// ensures that the node is a GeometryNode, see above).
static const luaL_Reg grlib_node_methods[] = {
{"__gc", gr_node_gc_cmd},
{"add_child", gr_node_add_child_cmd},
{"set_material", gr_node_set_material_cmd},
{"scale", gr_node_scale_cmd},
{"rotate", gr_node_rotate_cmd},
{"translate", gr_node_translate_cmd},
{0, 0}
};
// This function calls the lua interpreter to do the actual importing
SceneNode* import_lua(const std::string& filename)
{
GRLUA_DEBUG("Importing scene from " << filename);
// Start a lua interpreter
lua_State* L = luaL_newstate();
GRLUA_DEBUG("Loading base libraries");
// Load some base library
luaL_openlibs(L);
GRLUA_DEBUG("Setting up our functions");
// Set up the metatable for gr.node
luaL_newmetatable(L, "gr.node");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2);
lua_settable(L, -3);
// Load the gr.node methods
luaL_setfuncs(L, grlib_node_methods, 0);
// Load the gr functions
luaL_setfuncs(L, grlib_functions, 0);
lua_setglobal(L, "gr");
GRLUA_DEBUG("Parsing the scene");
// Now parse the actual scene
if (luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 1, 0)) {
std::cerr << "Error loading " << filename << ": " << lua_tostring(L, -1) << std::endl;
return 0;
}
GRLUA_DEBUG("Getting back the node");
// Pull the returned node off the stack
gr_node_ud* data = (gr_node_ud*)luaL_checkudata(L, -1, "gr.node");
if (!data) {
std::cerr << "Error loading " << filename << ": Must return the root node." << std::endl;
return 0;
}
// Store it
SceneNode* node = data->node;
GRLUA_DEBUG("Closing the interpreter");
// Close the interpreter, free up any resources not needed
lua_close(L);
// And return the node
return node;
}