-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdmath_mat4_lua.c
More file actions
178 lines (148 loc) · 3.86 KB
/
Copy pathstdmath_mat4_lua.c
File metadata and controls
178 lines (148 loc) · 3.86 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
/*
* Copyright (c) 2015 Milovann Yanatchkov
*
* This file is part of Mud, a free software
* licensed under the GNU General Public License v2
* see /LICENSE for more information
*
*/
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#ifdef HAVE_LUA_5_1
#include "compat-5.3.h"
#endif
#include "lua_util.h"
#include <stdlib.h>
#include <string.h>
#include "stdmath.h"
#include "stdmath_lua.h"
#include "blenlib.h"
#include "stdvec.h"
// CHECK
t_lua_mat4 *lua_mat4_check( lua_State *L, int index)
{
t_lua_mat4 *m;
luaL_checktype( L, index, LUA_TUSERDATA);
m = ( t_lua_mat4 *) luaL_checkudata( L, index, L_MAT4);
if ( m == NULL) luaL_error( L, "Mat4 type error");
return m;
}
static int lua_mat4_new( lua_State *L)
{
float mat4[4][4] = { { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}};
t_mn_mat4 *mat = mn_mat4_new( mat4);
t_lua_mat4 *lua_mat = ( t_lua_mat4 *) lua_newuserdata( L, sizeof( t_lua_mat4));
lua_mat->m = mat;
luaL_setmetatable( L, L_MAT4);
return 1;
}
static int lua_mat4_set( lua_State *L)
{
t_lua_mat4 *matrix = lua_mat4_check( L, 1);
float a = luaL_checknumber( L, 2);
float b = luaL_checknumber( L, 3);
float c = luaL_checknumber( L, 4);
float d = luaL_checknumber( L, 5);
float e = luaL_checknumber( L, 6);
float f = luaL_checknumber( L, 7);
float g = luaL_checknumber( L, 8);
float h = luaL_checknumber( L, 9);
float i = luaL_checknumber( L, 10);
float j = luaL_checknumber( L, 11);
float k = luaL_checknumber( L, 12);
float l = luaL_checknumber( L, 13);
float m = luaL_checknumber( L, 14);
float n = luaL_checknumber( L, 15);
float o = luaL_checknumber( L, 16);
float p = luaL_checknumber( L, 17);
float mat[4][4] = { { a, b, c, d}, { e, f, g, h}, { i, j, k, l}, { m, n, o, p}};
mn_mat4_set( matrix->m, mat);
return 0;
}
static int lua_mat4_print( lua_State *L)
{
t_lua_mat4 *mat = lua_mat4_check( L, 1);
mn_mat4_print( mat->m);
return 0;
}
static int lua_mat4_rotate( lua_State *L)
{
t_lua_mat4 *lua_mat = lua_mat4_check( L, 1);
const char *axis = luaL_checkstring( L, 2);
float angle = luaL_checknumber( L, 3);
float eul[3]={0,0,0};
t_mn_mat4 *mat = lua_mat->m;
rotate_eul( eul, axis[0], deg_to_rad(angle));
eul_to_mat4( mat->m, eul);
return 0;
}
static int lua_mat4_translate( lua_State *L)
{
t_lua_mat4 *lua_mat = lua_mat4_check( L, 1);
float x = luaL_checknumber( L, 2);
float y = luaL_checknumber( L, 3);
float z = luaL_checknumber( L, 4);
t_mn_mat4 *mat = lua_mat->m;
translate_m4( mat->m, x, y, z);
return 0;
}
static int lua_mat4_identity( lua_State *L)
{
t_lua_mat4 *lua_mat = lua_mat4_check( L, 1);
unit_m4( lua_mat->m->m);
return 0;
}
static int lua_mat4_multiply( lua_State *L)
{
t_lua_mat4 *lua_mat_1 = lua_mat4_check( L, 1);
t_lua_mat4 *lua_mat_2 = lua_mat4_check( L, 2);
t_mn_mat4 *mat1 = lua_mat_1->m;
t_mn_mat4 *mat2 = lua_mat_2->m;
mul_m4_m4m4( mat1->m, mat1->m, mat2->m);
return 0;
}
static int lua_mat4_add( lua_State *L)
{
t_lua_mat4 *lua_mat_1 = lua_mat4_check( L, 1);
t_lua_mat4 *lua_mat_2 = lua_mat4_check( L, 2);
t_mn_mat4 *mat1 = lua_mat_1->m;
t_mn_mat4 *mat2 = lua_mat_2->m;
add_m4_m4m4( mat1->m, mat1->m, mat2->m);
return 0;
}
static const struct luaL_Reg mat4_methods[] =
{
{"multiply", lua_mat4_multiply},
{"add", lua_mat4_add},
{"rotate", lua_mat4_rotate},
{"translate", lua_mat4_translate},
{"print", lua_mat4_print},
{"identity", lua_mat4_identity},
{"set", lua_mat4_set},
{ NULL, NULL}
};
static const struct luaL_Reg mat4[] =
{
{"new", lua_mat4_new},
{ NULL, NULL}
};
void lua_make_table_mat4( lua_State *L)
{
luaL_newmetatable( L, L_MAT4);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, mat4_methods, 0);
}
void lua_mat4_register( lua_State *L)
{
lua_make_table_mat4( L);
luaL_newlib( L, mat4);
lua_setglobal( L, "mat4");
}
LUALIB_API int luaopen_mat4( lua_State *L)
{
lua_make_table_mat4( L);
luaL_newlib( L, mat4);
return 1;
}