summaryrefslogtreecommitdiff
path: root/src/import/import.c
blob: a4fc55224e773f4752649e0ce38fcce814201669 (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
#include <lua.h>
#include <honeysuckle.h>
#include <assimp/scene.h>
#include "import.h"


void push_vector(lua_State *L, struct aiVector3D vec)
{
	hs_create_table(L,
		hs_str_num("x", vec.x),
		hs_str_num("y", vec.y),
		hs_str_num("z", vec.z),
	);
}


void push_face(lua_State *L, struct aiFace face)
{
	lua_createtable(L, face.mNumIndices, 0);
	int tbl = lua_gettop(L);

	for (int i=0; i<face.mNumIndices; i++) {
		lua_pushinteger(L, face.mIndices[i]);
		lua_rawseti(L, tbl, i+1);
	}
}


void push_aistring(lua_State *L, struct aiString str)
{
	lua_pushstring(L, str.data);
}


void push_mesh(lua_State *L, struct aiMesh mesh)
{
	int count = mesh.mNumVertices;

	/* create tables */
	lua_createtable(L, count, 0);
	int vertices = lua_gettop(L);

	/* populate */
	for (int i=0; i<count; i++) {
		/* positions */
		push_vector(L, mesh.mVertices[i]);
		lua_rawseti(L, vertices, i+1);
	}

	/* create mesh table */
	lua_createtable(L, 0, 1);
	int meshtbl = lua_gettop(L);

	lua_pushvalue(L, vertices);
	lua_setfield(L, meshtbl, "vertices");

	/* clean up */
	lua_remove(L, vertices);
}