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
|
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
local Matrix = {}
local metatable = {
__index = Matrix,
__tostring = function( m )
return "{\n"
.." {"..m[1][1]..", "..m[1][2]..", "..m[1][3]..", "..m[1][4].."},\n"
.." {"..m[2][1]..", "..m[2][2]..", "..m[2][3]..", "..m[2][4].."},\n"
.." {"..m[3][1]..", "..m[3][2]..", "..m[3][3]..", "..m[3][4].."},\n"
.." {"..m[4][1]..", "..m[4][2]..", "..m[4][3]..", "..m[4][4].."},\n"
.."}"
end,
__add = function( m1, m2 )
return Matrix:new( RL.MatrixAdd( m1, m2 ) )
end,
__sub = function( m1, m2 )
return Matrix:new( RL.MatrixSubtract( m1, m2 ) )
end,
__mul = function( m1, m2 )
return Matrix:new( RL.MatrixMultiply( m1, m2 ) )
end,
__concat = function( a, b )
return tostring( a )..tostring( b )
end,
}
function Matrix:copyMatrix( orig )
if orig ~= nil then
for y = 1, 4 do
for x = 1, 4 do
if orig[x][y] ~= nil then
self[x][y] = orig[x][y]
end
end
end
end
end
function Matrix:new( m )
local object = setmetatable( {}, metatable )
-- Raylib style transposed matrix.
for x = 1, 4 do
object[x] = {}
for y = 1, 4 do
table.insert( object[x], m[x][y] )
end
end
return object
end
function Matrix:set( m )
self:copyMatrix( m )
end
function Matrix:serialize()
local str = { "Matrix:new({" }
for i, row in ipairs( self ) do
table.insert( str, "{" )
for c, v in ipairs( row ) do
table.insert( str, v )
if c < 4 then
table.insert( str, "," )
end
end
table.insert( str, "}" )
if i < 4 then
table.insert( str, "," )
end
end
table.insert( str, "})" )
return table.concat( str )
end
function Matrix:clone()
return Matrix:new( self )
end
function Matrix:determinant()
return RL.MatrixDeterminant( self )
end
function Matrix:trace()
return RL.MatrixTranspose( self )
end
function Matrix:transpose()
return Matrix:new( RL.MatrixTranspose( self ) )
end
function Matrix:multiply( m2 )
return Matrix:new( RL.MatrixMultiply( self, m2 ) )
end
function Matrix:invert()
return Matrix:new( RL.MatrixInvert( self ) )
end
function Matrix:identity()
return Matrix:new( RL.MatrixIdentity() )
end
function Matrix:translate( translate )
return Matrix:new( RL.MatrixTranslate( translate ) )
end
function Matrix:rotate( axis, angle )
return Matrix:new( RL.MatrixRotate( axis, angle ) )
end
function Matrix:rotateX( angle )
return Matrix:new( RL.MatrixRotateX( angle ) )
end
function Matrix:rotateY( angle )
return Matrix:new( RL.MatrixRotateY( angle ) )
end
function Matrix:rotateZ( angle )
return Matrix:new( RL.MatrixRotateZ( angle ) )
end
function Matrix:rotateXYZ( angles )
return Matrix:new( RL.MatrixRotateXYZ( angles ) )
end
function Matrix:rotateZYX( angles )
return Matrix:new( RL.MatrixRotateZYX( angles ) )
end
function Matrix:scale( scale )
return Matrix:new( RL.MatrixScale( scale ) )
end
function Matrix:frustrum( left, right, bottom, top, near, far )
return Matrix:new( RL.MatrixFrustum( left, right, bottom, top, near, far ) )
end
function Matrix:perspective( fovy, aspect, near, far )
return Matrix:new( RL.MatrixPerspective( fovy, aspect, near, far ) )
end
function Matrix:ortho( left, right, bottom, top, near, far )
return Matrix:new( RL.MatrixOrtho( left, right, bottom, top, near, far ) )
end
function Matrix:lookAt( eye, target, up )
return Matrix:new( RL.MatrixLookAt( eye, target, up ) )
end
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100
local tempPool = {}
local curTemp = 1
for _ = 1, TEMP_COUNT do
table.insert( tempPool, Matrix:new( RL.MatrixIdentity() ) )
end
function Matrix:temp( m )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object:copyMatrix( m )
return object
end
function Matrix:getTempId()
return curTemp
end
return Matrix
|