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
|
#include "glm_bindings.h"
int honey_glm_vec3_mt_ref = LUA_NOREF;
int honey_glm_vec4_mt_ref = LUA_NOREF;
int honey_glm_mat3_mt_ref = LUA_NOREF;
int honey_glm_mat4_mt_ref = LUA_NOREF;
void honey_setup_glm(lua_State* L)
{
/* vec3 metatable */
honey_lua_create_table
(L, 2,
HONEY_TABLE, "__index", 21,
HONEY_FUNCTION, "copyTo", honey_glm_vec3_copy,
HONEY_FUNCTION, "zero", honey_glm_vec3_zero,
HONEY_FUNCTION, "eye", honey_glm_vec3_eye,
HONEY_FUNCTION, "dot", honey_glm_vec3_dot,
HONEY_FUNCTION, "cross", honey_glm_vec3_cross,
HONEY_FUNCTION, "crossn", honey_glm_vec3_crossn,
HONEY_FUNCTION, "norm2", honey_glm_vec3_norm2,
HONEY_FUNCTION, "norm", honey_glm_vec3_norm,
HONEY_FUNCTION, "add", honey_glm_vec3_add,
HONEY_FUNCTION, "adds", honey_glm_vec3_adds,
HONEY_FUNCTION, "sub", honey_glm_vec3_sub,
HONEY_FUNCTION, "subs", honey_glm_vec3_subs,
HONEY_FUNCTION, "mul", honey_glm_vec3_mul,
HONEY_FUNCTION, "scale", honey_glm_vec3_scale,
HONEY_FUNCTION, "scaleAs", honey_glm_vec3_scale_as,
HONEY_FUNCTION, "div", honey_glm_vec3_div,
HONEY_FUNCTION, "negate", honey_glm_vec3_negate,
HONEY_FUNCTION, "normalize", honey_glm_vec3_normalize,
HONEY_FUNCTION, "angleTo", honey_glm_vec3_angle,
HONEY_FUNCTION, "clamp", honey_glm_vec3_clamp,
HONEY_FUNCTION, "lerpTo", honey_glm_vec3_lerp,
HONEY_FUNCTION, "__gc", honey_glm_array_destroy);
honey_glm_vec3_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX);
/* vec4 metatable */
honey_lua_create_table
(L, 2,
HONEY_TABLE, "__index", 18,
HONEY_FUNCTION, "copyTo", honey_glm_vec4_copy,
HONEY_FUNCTION, "zero", honey_glm_vec4_zero,
HONEY_FUNCTION, "eye", honey_glm_vec4_eye,
HONEY_FUNCTION, "dot", honey_glm_vec4_dot,
HONEY_FUNCTION, "norm2", honey_glm_vec4_norm2,
HONEY_FUNCTION, "norm", honey_glm_vec4_norm,
HONEY_FUNCTION, "add", honey_glm_vec4_add,
HONEY_FUNCTION, "adds", honey_glm_vec4_adds,
HONEY_FUNCTION, "sub", honey_glm_vec4_sub,
HONEY_FUNCTION, "subs", honey_glm_vec4_subs,
HONEY_FUNCTION, "mul", honey_glm_vec4_mul,
HONEY_FUNCTION, "scale", honey_glm_vec4_scale,
HONEY_FUNCTION, "scaleAs", honey_glm_vec4_scale_as,
HONEY_FUNCTION, "div", honey_glm_vec4_div,
HONEY_FUNCTION, "negate", honey_glm_vec4_negate,
HONEY_FUNCTION, "normalize", honey_glm_vec4_normalize,
HONEY_FUNCTION, "clamp", honey_glm_vec4_clamp,
HONEY_FUNCTION, "lerpTo", honey_glm_vec4_lerp,
HONEY_FUNCTION, "__gc", honey_glm_array_destroy);
honey_glm_vec4_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX);
/* mat3 metatable */
honey_lua_create_table
(L, 2,
HONEY_TABLE, "__index", 9,
HONEY_FUNCTION, "copyTo", honey_glm_mat3_copy,
HONEY_FUNCTION, "eye", honey_glm_mat3_eye,
HONEY_FUNCTION, "zero", honey_glm_mat3_zero,
HONEY_FUNCTION, "mul", honey_glm_mat3_mul,
HONEY_FUNCTION, "transpose", honey_glm_mat3_transpose,
HONEY_FUNCTION, "mulv", honey_glm_mat3_mulv,
HONEY_FUNCTION, "scale", honey_glm_mat3_scale,
HONEY_FUNCTION, "det", honey_glm_mat3_det,
HONEY_FUNCTION, "inv", honey_glm_mat3_inv,
HONEY_FUNCTION, "__gc", honey_glm_array_destroy);
honey_glm_mat3_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX);
/* mat4 metatable */
honey_lua_create_table
(L, 2,
HONEY_TABLE, "__index", 22,
HONEY_FUNCTION, "copy", honey_glm_mat4_copy,
HONEY_FUNCTION, "eye", honey_glm_mat4_eye,
HONEY_FUNCTION, "zero", honey_glm_mat4_zero,
HONEY_FUNCTION, "mul", honey_glm_mat4_mul,
HONEY_FUNCTION, "transpose", honey_glm_mat4_transpose,
HONEY_FUNCTION, "mulv", honey_glm_mat4_mulv,
HONEY_FUNCTION, "scale", honey_glm_mat4_scale,
HONEY_FUNCTION, "det", honey_glm_mat4_det,
HONEY_FUNCTION, "inv", honey_glm_mat4_inv,
HONEY_FUNCTION, "translate", honey_glm_translate,
HONEY_FUNCTION, "translateX", honey_glm_translate_x,
HONEY_FUNCTION, "translateY", honey_glm_translate_y,
HONEY_FUNCTION, "translateZ", honey_glm_translate_z,
HONEY_FUNCTION, "scalev", honey_glm_scalev,
HONEY_FUNCTION, "rotateX", honey_glm_rotate_x,
HONEY_FUNCTION, "rotateY", honey_glm_rotate_y,
HONEY_FUNCTION, "rotateZ", honey_glm_rotate_z,
HONEY_FUNCTION, "rotate", honey_glm_rotate,
HONEY_FUNCTION, "perspective", honey_glm_perspective,
HONEY_FUNCTION, "perspectiveResize", honey_glm_perspective_resize,
HONEY_FUNCTION, "lookAt", honey_glm_lookat,
HONEY_FUNCTION, "look", honey_glm_look,
HONEY_FUNCTION, "__gc", honey_glm_array_destroy);
honey_glm_mat4_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX);
/* glm table */
honey_lua_create_table
(L, 4,
HONEY_FUNCTION, "vec3", honey_glm_new_vec3,
HONEY_FUNCTION, "vec4", honey_glm_new_vec4,
HONEY_FUNCTION, "mat3", honey_glm_new_mat3,
HONEY_FUNCTION, "mat4", honey_glm_new_mat4);
lua_setfield(L, -2, "glm");
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* GLM Array Basic Functions
*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
static void setup_new_array(lua_State* L,
honey_glm_array* array,
honey_glm_array_type type)
{
unsigned int size;
switch(type) {
case VEC3:
size = 3;
break;
case VEC4:
size = 4;
break;
case MAT3:
size = 9;
break;
case MAT4:
size = 16;
break;
default:
honey_lua_throw_error(L,
"unknown array type: %d",
type);
break;
}
array->type = type;
array->size = size;
array->data = calloc(size, sizeof(float));
if (array->data == NULL)
honey_lua_throw_error(L,
"failed to allocate memory for array of type %d",
type);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_glm_new_vec3(lua_State* L)
{
honey_glm_array* vec3 = lua_newuserdata(L, sizeof(honey_glm_array));
setup_new_array(L, vec3, VEC3);
lua_rawgeti(L, LUA_REGISTRYINDEX, honey_glm_vec3_mt_ref);
lua_setmetatable(L, -2);
return 1;
}
int honey_glm_new_vec4(lua_State* L)
{
honey_glm_array* vec4 = lua_newuserdata(L, sizeof(honey_glm_array));
setup_new_array(L, vec4, VEC4);
lua_rawgeti(L, LUA_REGISTRYINDEX, honey_glm_vec4_mt_ref);
lua_setmetatable(L, -2);
return 1;
}
int honey_glm_new_mat3(lua_State* L)
{
honey_glm_array* mat3 = lua_newuserdata(L, sizeof(honey_glm_array));
setup_new_array(L, mat3, MAT3);
lua_rawgeti(L, LUA_REGISTRYINDEX, honey_glm_mat3_mt_ref);
lua_setmetatable(L, -2);
return 1;
}
int honey_glm_new_mat4(lua_State* L)
{
honey_glm_array* mat4 = lua_newuserdata(L, sizeof(honey_glm_array));
setup_new_array(L, mat4, MAT4);
lua_rawgeti(L, LUA_REGISTRYINDEX, honey_glm_mat4_mt_ref);
lua_setmetatable(L, -2);
return 1;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int honey_glm_array_destroy(lua_State* L)
{
honey_glm_array* array;
honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &array);
free(array->data);
return 0;
}
|