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
|
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
local function deepCopy( orig )
local copy
if type( orig ) == "table" then
copy = {}
for origKey, origValue in next, orig, nil do
-- If object has clone method, use that.
copy[ deepCopy( origKey ) ] = deepCopy( origValue )
end
-- setmetatable( copy, utillib.deepCopy( getmetatable( orig ) ) )
else -- number, string, boolean, etc.
copy = orig
end
return copy
end
local Matrix = {}
Matrix.meta = {
__index = Matrix,
__tostring = function( m )
return "{\n"
.." {"..m.m[1][1]..", "..m.m[1][2]..", "..m.m[1][3]..", "..m.m[1][4].."},\n"
.." {"..m.m[2][1]..", "..m.m[2][2]..", "..m.m[2][3]..", "..m.m[2][4].."},\n"
.." {"..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].."},\n"
.." {"..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][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:new( m )
local object = setmetatable( {}, Matrix.meta )
object.m = deepCopy( m )
return object
end
function Matrix:set( m )
self.m = deepCopy( m )
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
return Matrix
|