annotate semiconginev2/core/matrix.nim @ 1234:841e12f33c47

add: text & font rendering, not tested yet
author sam <sam@basx.dev>
date Sat, 20 Jul 2024 00:03:57 +0700
parents 56781cc0fc7c
children c4f98eb4bb05
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
1 export math
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
3 type
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
4 # layout is row-first
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
5 # having an object instead of directly aliasing the array seems a bit ugly at
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
6 # first, but is necessary to be able to work correctly with distinguished
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
7 # types (i.e. TMat23 and TMat32 would be an alias for the same type array[6, T]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
8 # which prevents the type system from identifying the correct type at times)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
9 #
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
10 # Though, great news is that objects have zero overhead!
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
11 TMat2*[T: SomeNumber] = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
12 data*: array[4, T]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
13 TMat23*[T: SomeNumber] = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
14 data*: array[6, T]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
15 TMat32*[T: SomeNumber] = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
16 data*: array[6, T]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
17 TMat3*[T: SomeNumber] = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
18 data*: array[9, T]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
19 TMat34*[T: SomeNumber] = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
20 data*: array[12, T]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
21 TMat43*[T: SomeNumber] = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
22 data*: array[12, T]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
23 TMat4*[T: SomeNumber] = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
24 data*: array[16, T]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25 TMat* = TMat2|TMat3|TMat4|TMat23|TMat32|TMat34|TMat43
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
26 Mat2* = TMat2[float32]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
27 Mat23* = TMat23[float32]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
28 Mat32* = TMat32[float32]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
29 Mat3* = TMat3[float32]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
30 Mat34* = TMat34[float32]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
31 Mat43* = TMat43[float32]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
32 Mat4* = TMat4[float32]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
33
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
34 func MakeUnit2*[T: SomeNumber](): auto {.compiletime.} = TMat2[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
35 T(1), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
36 T(0), T(1),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
37 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
38 func MakeUnit3*[T: SomeNumber](): auto {.compiletime.} = TMat3[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
39 T(1), T(0), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
40 T(0), T(1), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
41 T(0), T(0), T(1),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
42 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
43 func MakeUnit4*[T: SomeNumber](): auto {.compiletime.} = TMat4[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
44 T(1), T(0), T(0), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
45 T(0), T(1), T(0), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
46 T(0), T(0), T(1), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
47 T(0), T(0), T(0), T(1),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
48 ])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
49
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
50 # generates constants: Unit
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
51 # Also for Y, Z, R, G, B
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
52 # not sure if this is necessary or even a good idea...
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
53 macro generateAllMatrixConsts() =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
54 result = newStmtList()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
55 for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
56 var typename = theType[0 .. 0]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
57 if theType[^2].isDigit:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
58 typename = typename & theType[^2]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
59 if theType[^1].isDigit:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
60 typename = typename & theType[^1]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
61 result.add(newConstStmt(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
62 postfix(ident("Unit2" & typename), "*"),
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
63 newCall(nnkBracketExpr.newTree(ident("MakeUnit2"), ident(theType)))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
64 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
65 result.add(newConstStmt(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
66 postfix(ident("Unit3" & typename), "*"),
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
67 newCall(nnkBracketExpr.newTree(ident("MakeUnit3"), ident(theType)))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
68 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
69 result.add(newConstStmt(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
70 postfix(ident("Unit4" & typename), "*"),
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
71 newCall(nnkBracketExpr.newTree(ident("MakeUnit4"), ident(theType)))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
72 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
73
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
74 generateAllMatrixConsts()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
75
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
76 const Unit2* = MakeUnit2[float32]()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
77 const Unit3* = MakeUnit3[float32]()
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
78 const Unit4* = MakeUnit4[float32]()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
79
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
80 template RowCount*(m: typedesc): int =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
81 when m is TMat2: 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
82 elif m is TMat23: 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
83 elif m is TMat32: 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
84 elif m is TMat3: 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
85 elif m is TMat34: 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
86 elif m is TMat43: 4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
87 elif m is TMat4: 4
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
88 template ColumnCount*(m: typedesc): int =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
89 when m is TMat2: 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
90 elif m is TMat23: 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
91 elif m is TMat32: 2
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
92 elif m is TMat3: 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
93 elif m is TMat34: 4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
94 elif m is TMat43: 3
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
95 elif m is TMat4: 4
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
96 template matlen(m: typedesc): int =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
97 when m is TMat2: 4
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
98 elif m is TMat23: 6
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
99 elif m is TMat32: 6
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
100 elif m is TMat3: 9
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
101 elif m is TMat34: 12
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
102 elif m is TMat43: 12
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
103 elif m is TMat4: 16
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
104
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
105
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
106 func toString[T: TMat](value: T): string =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
107 var
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
108 strvalues: seq[string]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
109 maxwidth = 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
110
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
111 for n in value.data:
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
112 let strval = &"{float(n):.4f}"
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
113 strvalues.add(strval)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
114 if strval.len > maxwidth:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
115 maxwidth = strval.len
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
116
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
117 for i in 0 ..< strvalues.len:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
118 let filler = " ".repeat(maxwidth - strvalues[i].len)
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
119 if i mod T.ColumnCount == T.ColumnCount - 1:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
120 result &= filler & strvalues[i] & "\n"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
121 else:
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
122 if i mod T.ColumnCount == 0:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
123 result &= " "
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
124 result &= filler & strvalues[i] & " "
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
125
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
126 func `$`*(v: TMat2[SomeNumber]): string = toString[TMat2[SomeNumber]](v)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
127 func `$`*(v: TMat23[SomeNumber]): string = toString[TMat23[SomeNumber]](v)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
128 func `$`*(v: TMat32[SomeNumber]): string = toString[TMat32[SomeNumber]](v)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
129 func `$`*(v: TMat3[SomeNumber]): string = toString[TMat3[SomeNumber]](v)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
130 func `$`*(v: TMat34[SomeNumber]): string = toString[TMat34[SomeNumber]](v)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
131 func `$`*(v: TMat43[SomeNumber]): string = toString[TMat43[SomeNumber]](v)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
132 func `$`*(v: TMat4[SomeNumber]): string = toString[TMat4[SomeNumber]](v)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
133
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
134 func `[]`*[T: TMat](m: T, row, col: int): auto = m.data[col + row * T.ColumnCount]
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
135 func `[]=`*[T: TMat, U](m: var T, row, col: int, value: U) = m.data[col + row * T.ColumnCount] = value
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
136 func `[]`*[T: TMat](m: T, i: int): auto = m.data[i]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
137 func `[]=`*[T: TMat, U](m: var T, i: int, value: U) = m.data[i] = value
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
138
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
139 func Row*[T: TMat2](m: T, i: 0..1): auto = TVec2([m[i, 0], m[i, 1]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
140 func Row*[T: TMat32](m: T, i: 0..2): auto = TVec2([m[i, 0], m[i, 1]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
141 func Row*[T: TMat23](m: T, i: 0..1): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
142 func Row*[T: TMat3](m: T, i: 0..2): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
143 func Row*[T: TMat43](m: T, i: 0..3): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
144 func Row*[T: TMat34](m: T, i: 0..2): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
145 func Row*[T: TMat4](m: T, i: 0..3): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]])
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
146
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
147 func Col*[T: TMat2](m: T, i: 0..1): auto = TVec2([m[0, i], m[1, i]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
148 func Col*[T: TMat23](m: T, i: 0..2): auto = TVec2([m[0, i], m[1, i]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
149 func Col*[T: TMat32](m: T, i: 0..1): auto = TVec3([m[0, i], m[1, i], m[2, i]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
150 func Col*[T: TMat3](m: T, i: 0..2): auto = TVec3([m[0, i], m[1, i], m[2, i]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
151 func Col*[T: TMat34](m: T, i: 0..3): auto = TVec3([m[0, i], m[1, i], m[2, i]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
152 func Col*[T: TMat43](m: T, i: 0..2): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
153 func Col*[T: TMat4](m: T, i: 0..3): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]])
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
154
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
155 proc createMatMatMultiplicationOperator(leftType: typedesc, rightType: typedesc, outType: typedesc): NimNode =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
156 var data = nnkBracket.newTree()
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
157 for i in 0 ..< RowCount(leftType):
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
158 for j in 0 ..< rightType.ColumnCount:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
159 data.add(newCall(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
160 ident("sum"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
161 infix(
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
162 newCall(newDotExpr(ident("a"), ident("Row")), newLit(i)),
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
163 "*",
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
164 newCall(newDotExpr(ident("b"), ident("Col")), newLit(j))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
165 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
166 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
167
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
168 return newProc(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
169 postfix(nnkAccQuoted.newTree(ident("*")), "*"),
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
170 params = [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
171 ident("auto"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
172 newIdentDefs(ident("a"), ident(leftType.name)),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
173 newIdentDefs(ident("b"), ident(rightType.name))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
174 ],
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
175 body = nnkObjConstr.newTree(ident(outType.name), nnkExprColonExpr.newTree(ident("data"), data)),
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
176 procType = nnkFuncDef,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
177 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
178
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
179 proc createMatMatAdditionOperator(theType: typedesc): NimNode =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
180 var data = nnkBracket.newTree()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
181 for i in 0 ..< matlen(theType):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
182 data.add(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
183 infix(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
184 nnkBracketExpr.newTree(ident("a"), newLit(i)),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
185 "+",
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
186 nnkBracketExpr.newTree(ident("b"), newLit(i)),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
187 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
188
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
189 return newProc(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
190 postfix(nnkAccQuoted.newTree(ident("+")), "*"),
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
191 params = [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
192 ident("auto"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
193 newIdentDefs(ident("a"), ident(theType.name)),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
194 newIdentDefs(ident("b"), ident(theType.name))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
195 ],
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
196 body = nnkObjConstr.newTree(ident(theType.name), nnkExprColonExpr.newTree(ident("data"), data)),
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
197 procType = nnkFuncDef,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
198 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
199
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
200 proc createVecMatMultiplicationOperator(matType: typedesc, vecType: typedesc): NimNode =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
201 var data = nnkBracket.newTree()
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
202 for i in 0 ..< matType.RowCount:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
203 data.add(newCall(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
204 ident("sum"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
205 infix(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
206 ident("v"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
207 "*",
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
208 newCall(newDotExpr(ident("m"), ident("Row")), newLit(i))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
209 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
210 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
211
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
212 let resultVec = newCall(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
213 nnkBracketExpr.newTree(ident(vecType.name), ident("T")),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
214 data,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
215 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
216 let name = postfix(nnkAccQuoted.newTree(ident("*")), "*")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
217 let genericParams = nnkGenericParams.newTree(nnkIdentDefs.newTree(ident("T"), ident("SomeNumber"), newEmptyNode()))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
218 let formalParams = nnkFormalParams.newTree(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
219 ident("auto"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
220 newIdentDefs(ident("m"), nnkBracketExpr.newTree(ident(matType.name), ident("T"))),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
221 newIdentDefs(ident("v"), nnkBracketExpr.newTree(ident(vecType.name), ident("T"))),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
222 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
223
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
224 return nnkFuncDef.newTree(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
225 name,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
226 newEmptyNode(),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
227 genericParams,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
228 formalParams,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
229 newEmptyNode(),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
230 newEmptyNode(),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
231 resultVec
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
232 )
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
233
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
234
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
235 proc createMatScalarOperator(matType: typedesc, op: string): NimNode =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
236 result = newStmtList()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
237
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
238 var data = nnkBracket.newTree()
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
239 for i in 0 ..< matType.RowCount * matType.ColumnCount:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
240 data.add(infix(nnkBracketExpr.newTree(newDotExpr(ident("a"), ident("data")), newLit(i)), op, ident("b")))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
241 result.add(newProc(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
242 postfix(nnkAccQuoted.newTree(ident(op)), "*"),
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
243 params = [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
244 ident("auto"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
245 newIdentDefs(ident("a"), ident(matType.name)),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
246 newIdentDefs(ident("b"), ident("SomeNumber")),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
247 ],
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
248 body = nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data)),
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
249 procType = nnkFuncDef,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
250 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
251 result.add(newProc(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
252 postfix(nnkAccQuoted.newTree(ident(op)), "*"),
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
253 params = [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
254 ident("auto"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
255 newIdentDefs(ident("b"), ident("SomeNumber")),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
256 newIdentDefs(ident("a"), ident(matType.name)),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
257 ],
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
258 body = nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data)),
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
259 procType = nnkFuncDef,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
260 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
261 if op == "-":
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
262 var data2 = nnkBracket.newTree()
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
263 for i in 0 ..< matType.RowCount * matType.ColumnCount:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
264 data2.add(prefix(nnkBracketExpr.newTree(newDotExpr(ident("a"), ident("data")), newLit(i)), op))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
265 result.add(newProc(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
266 postfix(nnkAccQuoted.newTree(ident(op)), "*"),
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
267 params = [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
268 ident("auto"),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
269 newIdentDefs(ident("a"), ident(matType.name)),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
270 ],
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
271 body = nnkObjConstr.newTree(ident(matType.name), nnkExprColonExpr.newTree(ident("data"), data2)),
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
272 procType = nnkFuncDef,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
273 ))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
274
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
275 macro createAllMultiplicationOperators() =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
276 result = newStmtList()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
277
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
278 for op in ["+", "-", "*", "/"]:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
279 result.add(createMatScalarOperator(TMat2, op))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
280 result.add(createMatScalarOperator(TMat23, op))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
281 result.add(createMatScalarOperator(TMat32, op))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
282 result.add(createMatScalarOperator(TMat3, op))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
283 result.add(createMatScalarOperator(TMat34, op))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
284 result.add(createMatScalarOperator(TMat43, op))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
285 result.add(createMatScalarOperator(TMat4, op))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
286
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
287 result.add(createMatMatMultiplicationOperator(TMat2, TMat2, TMat2))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
288 result.add(createMatMatMultiplicationOperator(TMat2, TMat23, TMat23))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
289 result.add(createMatMatMultiplicationOperator(TMat23, TMat32, TMat2))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
290 result.add(createMatMatMultiplicationOperator(TMat23, TMat3, TMat23))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
291 result.add(createMatMatMultiplicationOperator(TMat32, TMat2, TMat32))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
292 result.add(createMatMatMultiplicationOperator(TMat32, TMat23, TMat3))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
293 result.add(createMatMatMultiplicationOperator(TMat3, TMat32, TMat32))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
294 result.add(createMatMatMultiplicationOperator(TMat3, TMat3, TMat3))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
295 result.add(createMatMatMultiplicationOperator(TMat3, TMat34, TMat34))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
296 result.add(createMatMatMultiplicationOperator(TMat43, TMat3, TMat43))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
297 result.add(createMatMatMultiplicationOperator(TMat43, TMat34, TMat4))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
298 result.add(createMatMatMultiplicationOperator(TMat4, TMat43, TMat43))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
299 result.add(createMatMatMultiplicationOperator(TMat4, TMat4, TMat4))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
300
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
301 result.add(createMatMatAdditionOperator(TMat2))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
302 result.add(createMatMatAdditionOperator(TMat23))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
303 result.add(createMatMatAdditionOperator(TMat32))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
304 result.add(createMatMatAdditionOperator(TMat3))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
305 result.add(createMatMatAdditionOperator(TMat34))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
306 result.add(createMatMatAdditionOperator(TMat43))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
307 result.add(createMatMatAdditionOperator(TMat4))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
308
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
309 result.add(createVecMatMultiplicationOperator(TMat2, TVec2))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
310 result.add(createVecMatMultiplicationOperator(TMat3, TVec3))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
311 result.add(createVecMatMultiplicationOperator(TMat4, TVec4))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
312
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
313 createAllMultiplicationOperators()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
314
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
315 func `*`*(mat: Mat4, vec: Vec3f): Vec3f =
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
316 (mat * vec.ToVec4(1)).ToVec3
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
317
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
318 func Transposed*[T](m: TMat2[T]): TMat2[T] = TMat2[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
319 m[0, 0], m[1, 0],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
320 m[0, 1], m[1, 1],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
321 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
322 func Transposed*[T](m: TMat23[T]): TMat32[T] = TMat32[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
323 m[0, 0], m[1, 0],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
324 m[0, 1], m[1, 1],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
325 m[0, 2], m[1, 2],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
326 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
327 func Transposed*[T](m: TMat32[T]): TMat23[T] = TMat23[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
328 m[0, 0], m[1, 0], m[2, 0],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
329 m[0, 1], m[1, 1], m[2, 1],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
330 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
331 func Transposed*[T](m: TMat3[T]): TMat3[T] = TMat3[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
332 m[0, 0], m[1, 0], m[2, 0],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
333 m[0, 1], m[1, 1], m[2, 1],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
334 m[0, 2], m[1, 2], m[2, 2],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
335 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
336 func Transposed*[T](m: TMat43[T]): TMat34[T] = TMat34[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
337 m[0, 0], m[1, 0], m[2, 0], m[3, 0],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
338 m[0, 1], m[1, 1], m[2, 1], m[3, 1],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
339 m[0, 2], m[1, 2], m[2, 2], m[3, 2],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
340 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
341 func Transposed*[T](m: TMat34[T]): TMat43[T] = TMat43[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
342 m[0, 0], m[1, 0], m[2, 0],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
343 m[0, 1], m[1, 1], m[2, 1],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
344 m[0, 2], m[1, 2], m[2, 2],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
345 m[0, 3], m[1, 3], m[2, 3],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
346 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
347 func Transposed*[T](m: TMat4[T]): TMat4[T] = TMat4[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
348 m[0, 0], m[1, 0], m[2, 0], m[3, 0],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
349 m[0, 1], m[1, 1], m[2, 1], m[3, 1],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
350 m[0, 2], m[1, 2], m[2, 2], m[3, 2],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
351 m[0, 3], m[1, 3], m[2, 3], m[3, 3],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
352 ])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
353
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
354 func Translate2d*[T](x, y: T): TMat3[T] = TMat3[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
355 T(1), T(0), x,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
356 T(0), T(1), y,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
357 T(0), T(0), T(1),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
358 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
359 func Scale2d*[T](sx, sy: T): TMat3[T] = TMat3[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
360 sx, T(0), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
361 T(0), sy, T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
362 T(0), T(0), T(1),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
363 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
364 func Rotate2d*[T](angle: T): TMat3[T] = TMat3[T](data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
365 cos(angle), -sin(angle), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
366 sin(angle), cos(angle), T(0),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
367 T(0), T(0), T(1),
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
368 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
369 func Translate*(x = 0'f32, y = 0'f32, z = 0'f32): TMat4[float32] = Mat4(data: [
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
370 1'f32, 0'f32, 0'f32, x,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
371 0'f32, 1'f32, 0'f32, y,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
372 0'f32, 0'f32, 1'f32, z,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
373 0'f32, 0'f32, 0'f32, 1'f32,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
374 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
375 func Translate*[T: TVec3](v: T): TMat4[float32] = Translate(v[0], v[1], v[2])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
376 func Scale*(x = 1'f32, y = 1'f32, z = 1'f32): Mat4 = Mat4(data: [
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
377 x, 0'f32, 0'f32, 0'f32,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
378 0'f32, y, 0'f32, 0'f32,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
379 0'f32, 0'f32, z, 0'f32,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
380 0'f32, 0'f32, 0'f32, 1'f32,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
381 ])
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
382 func Scale*[T: TVec3](v: T): TMat4[float32] = Scale(v[0], v[1], v[2])
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
383 func Rotate*(angle: float32, a: Vec3f): Mat4 =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
384 let
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
385 cosa = cos(angle)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
386 sina = sin(angle)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
387 x = a[0]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
388 y = a[1]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
389 z = a[2]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
390 Mat4(data: [
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
391 x * x * (1 - cosa) + cosa, y * x * (1 - cosa) - z * sina, z * x * (1 - cosa) + y * sina, 0'f32,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
392 x * y * (1 - cosa) + z * sina, y * y * (1 - cosa) + cosa, z * y * (1 - cosa) - x * sina, 0'f32,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
393 x * z * (1 - cosa) - y * sina, y * z * (1 - cosa) + x * sina, z * z * (1 - cosa) + cosa, 0'f32,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
394 0'f32, 0'f32, 0'f32, 1'f32,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
395 ])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
396
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
397 func asMat3(m: Mat4): auto =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
398 Mat3(data: [
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
399 m[0, 0], m[0, 1], m[0, 2],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
400 m[1, 0], m[1, 1], m[1, 2],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
401 m[2, 0], m[2, 1], m[2, 2],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
402 ])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
403
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
404
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
405 func Inversed*(m: Mat4): Mat4 =
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
406 var m3 = m.asMat3.Transposed
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
407 m3[0, 0] = 1'f32 / m3[0, 0]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
408 m3[1, 1] = 1'f32 / m3[1, 1]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
409 m3[2, 2] = 1'f32 / m3[2, 2]
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
410 let col3 = -(m3 * m.Col(3).xyz)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
411 return Mat4(data: [
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
412 m3[0, 0], m3[0, 1], m3[0, 2], col3.x,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
413 m3[1, 0], m3[1, 1], m3[1, 2], col3.y,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
414 m3[2, 0], m3[2, 1], m3[2, 2], col3.z,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
415 0, 0, 0, 1,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
416 ])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
417
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
418
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
419 # call e.g. TMat32[int]().randomized() to get a random matrix
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
420 template makeRandomMatrixInit(mattype: typedesc) =
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
421 proc Randomized*[T: SomeInteger](m: mattype[T]): mattype[T] =
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
422 for i in 0 ..< result.data.len:
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
423 result.data[i] = rand(low(typeof(m.data[0])) .. high(typeof(m.data[0])))
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
424 proc Randomized*[T: SomeFloat](m: mattype[T]): mattype[T] =
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
425 for i in 0 ..< result.data.len:
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
426 result.data[i] = rand(T(1.0))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
427
1190
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
428 makeRandomMatrixInit(TMat2)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
429 makeRandomMatrixInit(TMat23)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
430 makeRandomMatrixInit(TMat32)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
431 makeRandomMatrixInit(TMat3)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
432 makeRandomMatrixInit(TMat34)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
433 makeRandomMatrixInit(TMat43)
a3eb305bcac2 start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents: 1139
diff changeset
434 makeRandomMatrixInit(TMat4)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
435
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
436 func Perspective*(fovy, aspect, zNear, zFar: float32): Mat4 =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
437 let tanHalfFovy = tan(fovy / 2)
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
438 return Mat4(data: [
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
439 1 / (aspect * tanHalfFovy), 0, 0, 0,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
440 0, 1 / tanHalfFovy, 0, 0,
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
441 0, 0, zFar / (zFar - zNear), -(zFar * zNear) / (zFar - zNear),
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
442 0, 0, 1, 1,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
443 ])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
444
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
445 func Ortho*(left, right, top, bottom, zNear, zFar: float32): Mat4 =
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
446 Mat4(data: [
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
447 2 / (right - left), 0, 0, -(right + left) / (right - left),
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
448 0, 2 / (bottom - top), 0, -(bottom + top) / (bottom - top),
997
fe48fb1bdfda fix: ortho projection matrix (hopefully), better string output for matrix and vector
sam <sam@basx.dev>
parents: 920
diff changeset
449 0, 0, 1 / (zFar - zNear), zNear / (zFar - zNear),
fe48fb1bdfda fix: ortho projection matrix (hopefully), better string output for matrix and vector
sam <sam@basx.dev>
parents: 920
diff changeset
450 0, 0, 0, 1,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
451 ])
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
452
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
453 # create an orthographic perspective that will map from -1 .. 1 on all axis and keep a 1:1 aspect ratio
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
454 # the smaller dimension (width or height) will always be 1 and the larger dimension will be larger, to keep the ratio
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
455 func OrthoWindowAspect*(windowAspect: float32): Mat4 =
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
456 if windowAspect < 1:
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
457 let space = 2 * (1 / windowAspect - 1) / 2
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
458 Ortho(-1, 1, -1 - space, 1 + space, 0, 1)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
459 else:
901
7f430850e587 add: helpers for screen-aspect/perspective matrix generating, auto-formatting was done
Sam <sam@basx.dev>
parents: 840
diff changeset
460 let space = 2 * (windowAspect - 1) / 2
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
461 Ortho(-1 - space, 1 + space, -1, 1, 0, 1)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
462
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
463 func Position*(mat: Mat4): Vec3f {.deprecated.} =
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
464 mat.Col(3).ToVec3
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
465
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
466 func Scaling*(mat: Mat4): Vec3f {.deprecated.} =
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 997
diff changeset
467 NewVec4f(mat[0, 0], mat[1, 1], mat[2, 2])