summaryrefslogtreecommitdiff
path: root/src/import/import.c
blob: a68ac0533e4524047a16c336ab1709452c9f5734 (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
#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++) {
		/* add one to the index bc lua is 1-indexed */
		lua_pushinteger(L, face.mIndices[i]+1);
		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)
{
	/* create mesh table */
	lua_createtable(L, 0, 1);
	int meshtbl = lua_gettop(L);

	int count = mesh.mNumVertices;
	int pop_count = 0;

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

	int normals = 0;
	if (mesh.mNormals != NULL) {
		lua_createtable(L, count, 0);
		normals = lua_gettop(L);
		pop_count += 1;
	}

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

		/* normals */
		if (normals) {
			push_vector(L, mesh.mNormals[i]);
			lua_rawseti(L, normals, i+1);
		}
	}

	/* populate faces */
	int faces = 0;
	if (mesh.mNumFaces != 0) {
		lua_createtable(L, mesh.mNumFaces, 0);
		faces = lua_gettop(L);
		pop_count += 1;
		for (int i=0; i<mesh.mNumFaces; i++) {
			push_face(L, mesh.mFaces[i]);
			lua_rawseti(L, faces, i+1);
		}
	}

	/* assign values */
	lua_pushvalue(L, vertices);
	lua_setfield(L, meshtbl, "vertices");

	if (normals) {
		lua_pushvalue(L, normals);
		lua_setfield(L, meshtbl, "normals");
	}

	if (faces) {
		lua_pushvalue(L, faces);
		lua_setfield(L, meshtbl, "faces");
	}

	/* clean up */
	lua_pop(L, pop_count);
}