-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_tree.odin
More file actions
380 lines (317 loc) · 8.1 KB
/
transform_tree.odin
File metadata and controls
380 lines (317 loc) · 8.1 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package transform_tree
import "core:math"
// The float type to use. Default is `f32`
float :: f32
Transform :: struct
{
id: u32,
}
Transform_Data :: struct
{
using _: struct #raw_union
{
position: [2]float,
pos: [2]float,
},
using _: struct #raw_union
{
scale: [2]float,
scl: [2]float,
},
using _: struct #raw_union
{
rotation: float,
rot: float,
},
_prev_free: ^Transform_Data,
_ref: Transform,
_parent_ref: Transform,
}
Transform_Tree :: struct
{
data: [dynamic]Transform_Data,
last_free: ^Transform_Data,
}
@(thread_local)
global_tree: ^Transform_Tree
@(require_results)
create_tree :: proc(n: int, allocator := context.allocator) -> Transform_Tree
{
result: Transform_Tree
result.data = make([dynamic]Transform_Data, 1, n+1, allocator)
return result
}
copy_tree :: proc(dst, src: ^Transform_Tree)
{
temp := dst.data
dst^ = src^
dst.data = temp
resize(&dst.data, len(src.data))
copy(dst.data[:], src.data[:])
shrink(&dst.data)
curr: ^^Transform_Data
for curr = &dst.last_free; curr^ != nil;
{
prev := &(curr^)._prev_free
curr^ = &dst.data[(curr^)._ref.id]
curr = prev
}
}
clear_tree :: proc(tree: ^Transform_Tree)
{
for &xform in tree.data
{
xform = {}
}
clear(&tree.data)
append(&tree.data, Transform_Data{})
tree.last_free = nil
}
destroy_tree :: proc(tree: ^Transform_Tree)
{
delete(tree.data)
tree^ = {}
}
alloc_transform :: proc
{
alloc_transform_parent,
alloc_transform_no_parent,
}
@(require_results)
alloc_transform_parent :: proc(tree: ^Transform_Tree, parent: Transform) -> Transform
{
result: Transform
if tree.last_free != nil
{
result = tree.last_free._ref
tree.data[result.id]._ref = result
tree.data[result.id]._parent_ref = parent
tree.data[result.id].scale = {1, 1}
tree.last_free = tree.last_free._prev_free
}
else
{
append(&tree.data, Transform_Data{})
idx := len(tree.data) - 1
result = Transform{u32(idx)}
tree.data[idx]._ref = result
tree.data[idx]._parent_ref = parent
tree.data[idx].scale = {1, 1}
}
return result
}
@(require_results)
alloc_transform_no_parent :: #force_inline proc(tree: ^Transform_Tree) -> Transform
{
return alloc_transform_parent(tree, Transform{})
}
free_transform :: proc(tree: ^Transform_Tree, xform: Transform)
{
tree.data[xform.id]._prev_free = tree.last_free
tree.last_free = &tree.data[xform.id]
tree.data[xform.id] = {
_ref = tree.data[xform.id]._ref,
_prev_free = tree.data[xform.id]._prev_free,
}
}
set_parent :: #force_inline proc(child, parent: Transform, tree := global_tree)
{
if tree == nil do return
tree.data[child.id]._parent_ref = parent
}
attach_child :: #force_inline proc(parent, child: Transform, tree := global_tree)
{
if tree == nil do return
set_parent(child, parent, tree)
}
/*
Returns a pointer to the transform data. The fields `pos`, `scl`, and `rot` give the
local values and are safe to mutate.
*/
@(require_results)
local :: proc(xform: Transform, tree := global_tree) -> ^Transform_Data
{
if tree == nil do return {}
return &tree.data[xform.id]
}
local_pos :: local_position
local_scl :: local_scale
local_rot :: local_rotation
@(require_results)
local_position :: proc(xform: Transform, tree := global_tree) -> [2]float
{
if tree == nil do return {0, 0}
return tree.data[xform.id].pos
}
@(require_results)
local_scale :: proc(xform: Transform, tree := global_tree) -> [2]float
{
if tree == nil do return {0, 0}
return tree.data[xform.id].scl
}
@(require_results)
local_rotation :: proc(xform: Transform, tree := global_tree) -> float
{
if tree == nil do return 0
return tree.data[xform.id].rot
}
set_local_pos :: set_local_position
set_local_scl :: set_local_scale
set_local_rot :: set_local_rotation
set_local_position :: proc(xform: Transform, pos: [2]f32, tree := global_tree)
{
if tree == nil do return
tree.data[xform.id].pos = pos
}
set_local_scale :: proc(xform: Transform, scl: [2]f32, tree := global_tree)
{
if tree == nil do return
tree.data[xform.id].scl = scl
}
set_local_rotation :: proc(xform: Transform, rot: f32, tree := global_tree)
{
if tree == nil do return
tree.data[xform.id].rot = rot
}
global_pos :: global_position
global_scl :: global_scale
global_rot :: global_rotation
@(require_results)
global_position :: proc(xform: Transform, tree := global_tree) -> [2]float
{
if tree == nil do return {0, 0}
result: matrix[3,3]float = ident_3x3f(float(1))
curr_xform := local(xform, tree)
for curr_xform._ref != {}
{
result = model_matrix(curr_xform._ref, tree) * result
curr_xform = local(curr_xform._parent_ref, tree)
}
return {result[0,2], result[1,2]}
}
@(require_results)
global_scale :: proc(xform: Transform, tree := global_tree) -> [2]float
{
if tree == nil do return {0, 0}
result: [2]float = {1, 1}
curr_xform := local(xform, tree)
for curr_xform._ref != {}
{
result *= curr_xform.scl
curr_xform = local(curr_xform._parent_ref, tree)
}
return result
}
@(require_results)
global_rotation :: proc(xform: Transform, tree := global_tree) -> float
{
if tree == nil do return 0
result: float
curr_xform := local(xform, tree)
for curr_xform._ref != {}
{
result += curr_xform.rot
curr_xform = local(curr_xform._parent_ref, tree)
}
return result
}
set_global_pos :: set_global_position
set_global_scl :: set_global_scale
set_global_rot :: set_global_rotation
set_global_position :: proc(xform: Transform, pos: [2]float, tree := global_tree)
{
if tree == nil do return
curr_global_pos := global_pos(xform, tree)
if curr_global_pos.x < pos.x
{
local(xform, tree).pos.x += abs(curr_global_pos.x - pos.x)
}
else if curr_global_pos.x > pos.x
{
local(xform, tree).pos.x -= abs(curr_global_pos.x - pos.x)
}
if curr_global_pos.y < pos.y
{
local(xform, tree).pos.y += abs(curr_global_pos.y - pos.y)
}
else if curr_global_pos.y > pos.y
{
local(xform, tree).pos.y -= abs(curr_global_pos.y - pos.y)
}
}
set_global_scale :: proc(xform: Transform, scl: [2]float, tree := global_tree)
{
if tree == nil do return
curr_global_scl := global_scl(xform, tree)
curr_local_scl := local(xform, tree).scl
local(xform, tree).scl.x *= curr_local_scl.x / curr_global_scl.x * scl.x
local(xform, tree).scl.y *= curr_local_scl.y / curr_global_scl.y * scl.y
}
set_global_rotation :: proc(xform: Transform, rot: float, tree := global_tree)
{
if tree == nil do return
curr_global_rot := global_rot(xform, tree)
if curr_global_rot < rot
{
local(xform, tree).rot += abs(curr_global_rot - rot)
}
else if curr_global_rot > rot
{
local(xform, tree).rot -= abs(curr_global_rot - rot)
}
}
@(require_results)
model_matrix :: proc(xform: Transform, tree := global_tree) -> matrix[3,3]float
{
if tree == nil do return {}
xform := tree.data[xform.id]
result := ident_3x3f(1)
result = scale_3x3f(xform.scl) * result
result = rotation_3x3f(xform.rot) * result
result = translation_3x3f(xform.pos) * result
return result
}
// 2D Matrix ///////////////////////////////////////////////////////////////////////////
@(require_results, private)
ident_3x3f :: #force_inline proc(val: float) -> matrix[3,3]float
{
return {
val, 0, 0,
0, val, 0,
0, 0, val,
}
}
@(require_results, private)
translation_3x3f :: proc(v: [2]float) -> matrix[3,3]float
{
result: matrix[3,3]float = ident_3x3f(float(1))
result[0,2] = v.x
result[1,2] = v.y
return result
}
@(require_results, private)
scale_3x3f :: proc(v: [2]$float) -> matrix[3,3]float
{
result: matrix[3,3]float = ident_3x3f(float(1))
result[0,0] = v.x
result[1,1] = v.y
return result
}
@(require_results, private)
shear_3x3f :: proc(v: [2]float) -> matrix[3,3]float
{
result: matrix[3,3]float = ident_3x3f(float(1))
result[0,1] = v.x
result[1,0] = v.y
return result
}
@(require_results, private)
rotation_3x3f :: proc(rads: float) -> matrix[3,3]float
{
result: matrix[3,3]float = ident_3x3f(float(1))
result[0,0] = math.cos(rads)
result[0,1] = -math.sin(rads)
result[1,0] = math.sin(rads)
result[1,1] = math.cos(rads)
return result
}