annotate src/semicongine/entity.nim @ 122:506090173619

did: implement uniforms, some refactoring
author Sam <sam@basx.dev>
date Sun, 09 Apr 2023 01:04:54 +0700
parents dfaddaf96f09
children 5871acc2977e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
1 import std/strformat
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
2 import std/typetraits
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
3
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
4 import ./math/matrix
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
5
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
6 type
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
7 Component* = ref object of RootObj
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
8 entity*: Entity
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
9
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
10 Entity* = ref object of RootObj
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
11 name*: string
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
12 transform*: Mat4 # todo: cache transform + only update VBO when transform changed
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
13 parent*: Entity
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
14 children*: seq[Entity]
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
15 components*: seq[Component]
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
16
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
17
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 109
diff changeset
18 method `$`*(entity: Entity): string {.base.} = entity.name
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
19 method `$`*(component: Component): string {.base.} =
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 109
diff changeset
20 "Unknown Component"
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
21
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
22 proc add*(entity: Entity, child: Entity) =
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
23 child.parent = entity
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
24 entity.children.add child
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
25 proc add*(entity: Entity, component: Component) =
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
26 component.entity = entity
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
27 entity.components.add component
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
28 proc add*(entity: Entity, children: seq[Entity]) =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
29 for child in children:
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
30 child.parent = entity
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
31 entity.children.add child
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
32 proc add*(entity: Entity, components: seq[Component]) =
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
33 for component in components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
34 component.entity = entity
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
35 entity.components.add component
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
36
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
37 func newEntity*(name: string = ""): Entity =
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
38 result = new Entity
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
39 result.name = name
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
40 result.transform = Unit4
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
41 if result.name == "":
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
42 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
43
112
0c5a74885796 did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 109
diff changeset
44 func newEntity*(name: string, firstChild: Entity, children: varargs[Entity]): Entity =
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
45 result = new Entity
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
46 result.add firstChild
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
47 for child in children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
48 result.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
49 result.name = name
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
50 result.transform = Unit4
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
51 if result.name == "":
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
52 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
53
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
54 proc newEntity*(name: string, firstComponent: Component, components: varargs[Component]): Entity =
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
55 result = new Entity
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
56 result.name = name
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
57 result.add firstComponent
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
58 for component in components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
59 result.add component
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
60 if result.name == "":
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
61 result.name = &"Entity[{$(cast[ByteAddress](result))}]"
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
62 result.transform = Unit4
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
63
121
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
64 func getModelTransform*(entity: Entity): Mat4 =
dfaddaf96f09 did: refactor GPU data types, more generic, prepare to use for decriptors/uniforms
Sam <sam@basx.dev>
parents: 112
diff changeset
65 result = Unit4
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
66 var currentEntity = entity
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
67 while currentEntity != nil:
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
68 result = currentEntity.transform * result
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
69 currentEntity = currentEntity.parent
61
0f04ba283558 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 60
diff changeset
70
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
71 iterator allComponentsOfType*[T: Component](root: Entity): T =
19
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
72 var queue = @[root]
b55d6ecde79d did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff changeset
73 while queue.len > 0:
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
74 let entity = queue.pop
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
75 for component in entity.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
76 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
77 yield T(component)
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
78 for i in countdown(entity.children.len - 1, 0):
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
79 queue.add entity.children[i]
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
80
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
81 func firstWithName*(root: Entity, name: string): Entity =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
82 var queue = @[root]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
83 while queue.len > 0:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
84 let next = queue.pop
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
85 for child in next.children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
86 if child.name == name:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
87 return child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
88 queue.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
89
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
90 func firstComponentWithName*[T: Component](root: Entity, name: string): T =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
91 var queue = @[root]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
92 while queue.len > 0:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
93 let next = queue.pop
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
94 for child in next.children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
95 if child.name == name:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
96 for component in child.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
97 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
98 return T(component)
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
99 queue.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
100
108
70f92081d2c1 did: reorganize code
Sam <sam@basx.dev>
parents: 72
diff changeset
101 func allWithName*(root: Entity, name: string): seq[Entity] =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
102 var queue = @[root]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
103 while queue.len > 0:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
104 let next = queue.pop
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
105 for child in next.children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
106 if child.name == name:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
107 result.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
108 queue.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
109
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
110 func allComponentsWithName*[T: Component](root: Entity, name: string): seq[T] =
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
111 var queue = @[root]
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
112 while queue.len > 0:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
113 let next = queue.pop
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
114 for child in next.children:
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
115 if child.name == name:
122
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
116 for component in child.components:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
117 if component of T:
506090173619 did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 121
diff changeset
118 result.add T(component)
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
119 queue.add child
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 40
diff changeset
120
109
8d24727c9795 did: refactor rendering/scene concept
Sam <sam@basx.dev>
parents: 108
diff changeset
121 iterator allEntities*(root: Entity): Entity =
28
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
122 var queue = @[root]
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
123 while queue.len > 0:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
124 let next = queue.pop
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
125 for child in next.children:
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
126 queue.add child
b1b05d4efb52 big refactoring, part1
Sam <sam@basx.dev>
parents: 19
diff changeset
127 yield next