Mercurial > games > semicongine
annotate src/semicongine/scene.nim @ 321:30117d8f0052
did next step in renderpipeline-refactoring, using shaderconfiguration objects instead for less ambigious shader-pipeline configuration
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 15 Aug 2023 23:51:37 +0700 |
parents | b145a05c2459 |
children | 6dab370d1758 |
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 |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
2 import std/sequtils |
247 | 3 import std/strutils |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
4 import std/tables |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
5 import std/hashes |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
6 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
|
7 |
206
7f921d7d0a2b
did: small refactoring of module structure
Sam <sam@basx.dev>
parents:
201
diff
changeset
|
8 import ./core |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
9 import ./material |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
10 import ./animation |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 type |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
13 Scene* = object |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
14 name*: string |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
15 root*: Entity |
229
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
16 shaderGlobals*: Table[string, DataList] |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
17 materials: OrderedTable[string, Material] |
321
30117d8f0052
did next step in renderpipeline-refactoring, using shaderconfiguration objects instead for less ambigious shader-pipeline configuration
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
18 transformAttribute*: string = "transform" |
30117d8f0052
did next step in renderpipeline-refactoring, using shaderconfiguration objects instead for less ambigious shader-pipeline configuration
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
19 materialIndexAttribute*: string = "materialIndex" |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
20 |
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
21 Component* = ref object of RootObj |
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
22 entity*: Entity |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
23 |
108 | 24 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
|
25 name*: string |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
26 internal_transform: Mat4 # todo: cache transform + only update VBO when transform changed |
305
44ecc0a01a9f
fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
27 parent: Entity |
44ecc0a01a9f
fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
28 children: seq[Entity] |
44ecc0a01a9f
fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
29 components: Table[string, Component] |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
30 |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
31 EntityAnimation* = ref object of Component |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
32 player: AnimationPlayer[Mat4] |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
33 |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
34 func newEntityAnimation*(animation: Animation[Mat4]): EntityAnimation = |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
35 result = EntityAnimation(player: newAnimator(animation)) |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
36 result.player.currentValue = Unit4 |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
37 |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
38 func setAnimation*(entityAnimation: EntityAnimation, animation: Animation[Mat4]) = |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
39 entityAnimation.player.animation = animation |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
40 entityAnimation.player.resetPlayer() |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
41 |
312 | 42 func start*(animation: EntityAnimation) = |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
43 animation.player.start() |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
44 |
312 | 45 func stop*(animation: EntityAnimation) = |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
46 animation.player.stop() |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
47 |
313 | 48 func reset*(animation: EntityAnimation) = |
49 animation.player.stop() | |
50 animation.player.resetPlayer() | |
51 | |
314 | 52 func playing*(animation: EntityAnimation): bool = |
53 animation.player.playing | |
54 | |
312 | 55 func update*(animation: EntityAnimation, dt: float32) = |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
56 animation.player.advance(dt) |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
57 |
305
44ecc0a01a9f
fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
58 func parent(entity: Entity): Entity = |
44ecc0a01a9f
fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
59 entity.parent |
44ecc0a01a9f
fix: some issues with new scene/api/component api
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
60 |
314 | 61 # TODO: this is wrong: transfrom setter + getter are not "symetric" |
290 | 62 func transform*(entity: Entity): Mat4 = |
63 result = entity.internal_transform | |
302 | 64 for component in entity.components.mvalues: |
292
d4b4ad203578
fix: do not apply last frame value after animation has stopped
Sam <sam@basx.dev>
parents:
290
diff
changeset
|
65 if component of EntityAnimation and EntityAnimation(component).player.playing: |
d4b4ad203578
fix: do not apply last frame value after animation has stopped
Sam <sam@basx.dev>
parents:
290
diff
changeset
|
66 result = result * EntityAnimation(component).player.currentValue |
290 | 67 |
314 | 68 func `transform=`*(entity: Entity, value: Mat4) = |
69 entity.internal_transform = value | |
70 | |
308
c73224f9d38f
add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents:
306
diff
changeset
|
71 # TODO: position-setter |
c73224f9d38f
add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents:
306
diff
changeset
|
72 func position*(entity: Entity): Vec3f = |
c73224f9d38f
add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents:
306
diff
changeset
|
73 return entity.transform.col(3) |
c73224f9d38f
add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents:
306
diff
changeset
|
74 |
290 | 75 func originalTransform*(entity: Entity): Mat4 = |
76 entity.internal_transform | |
77 | |
78 func getModelTransform*(entity: Entity): Mat4 = | |
79 result = entity.transform | |
80 if not entity.parent.isNil: | |
81 result = entity.transform * entity.parent.getModelTransform() | |
82 | |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
83 func addShaderGlobal*[T](scene: var Scene, name: string, data: T) = |
229
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
84 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]()) |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
85 setValues(scene.shaderGlobals[name], @[data]) |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
86 |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
87 func addShaderGlobalArray*[T](scene: var Scene, name: string, data: seq[T]) = |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
88 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]()) |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
89 setValues(scene.shaderGlobals[name], data) |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
90 |
190
8f2eaf0d2808
add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
Sam <sam@basx.dev>
parents:
189
diff
changeset
|
91 func getShaderGlobal*[T](scene: Scene, name: string): T = |
229
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
92 getValues[T](scene.shaderGlobals[name])[0] |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
93 |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
94 func getShaderGlobalArray*[T](scene: Scene, name: string): seq[T] = |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
95 getValues[T](scene.shaderGlobals[name]) |
190
8f2eaf0d2808
add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
Sam <sam@basx.dev>
parents:
189
diff
changeset
|
96 |
8f2eaf0d2808
add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
Sam <sam@basx.dev>
parents:
189
diff
changeset
|
97 func setShaderGlobal*[T](scene: var Scene, name: string, value: T) = |
229
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
98 setValues[T](scene.shaderGlobals[name], @[value]) |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
99 |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
100 func setShaderGlobalArray*[T](scene: var Scene, name: string, value: seq[T]) = |
7741bca03e7c
add: support for struct members to be array
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
101 setValues[T](scene.shaderGlobals[name], value) |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
102 |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
103 func appendShaderGlobalArray*[T](scene: var Scene, name: string, value: seq[T]) = |
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
104 appendValues[T](scene.shaderGlobals[name], value) |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
105 |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
106 func newScene*(name: string, root: Entity): Scene = |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
107 Scene(name: name, root: root) |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
108 |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
109 func addMaterial*(scene: var Scene, material: Material) = |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
110 assert not scene.materials.contains(material.name), &"Material with name '{material.name}' already exists in scene" |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
111 for name, value in material.constants.pairs: |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
112 scene.shaderGlobals[name] = newDataList(thetype=value.thetype) |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
113 |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
114 for name, value in material.constants.pairs: |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
115 scene.shaderGlobals[name].appendValue(value) |
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
116 |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
117 scene.materials[material.name] = material |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
118 |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
119 func materialIndex*(scene: Scene, materialName: string): int = |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
120 for name in scene.materials.keys: |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
121 if name == materialName: |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
122 return result |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
123 inc result |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
124 return -1 |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
125 |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
126 func materials*(scene: Scene): auto = |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
314
diff
changeset
|
127 scene.materials.values.toSeq |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
128 |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
129 func hash*(scene: Scene): Hash = |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
130 hash(scene.name) |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
131 |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
132 func `==`*(a, b: Scene): bool = |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
142
diff
changeset
|
133 a.name == b.name |
127 | 134 |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
135 func hash*(entity: Entity): Hash = |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
136 hash(cast[pointer](entity)) |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
137 |
142
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
128
diff
changeset
|
138 func hash*(component: Component): Hash = |
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
128
diff
changeset
|
139 hash(cast[pointer](component)) |
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
128
diff
changeset
|
140 |
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
|
141 method `$`*(entity: Entity): string {.base.} = entity.name |
122
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
142 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
|
143 "Unknown Component" |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
144 method `$`*(animation: EntityAnimation): string = |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
145 &"Entity animation: {animation.player.animation}" |
247 | 146 |
109 | 147 proc add*(entity: Entity, child: Entity) = |
148 child.parent = entity | |
149 entity.children.add child | |
312 | 150 proc `[]=`*[T](entity: Entity, index: int, child: var T) = |
302 | 151 child.parent = entity |
152 entity.children[index] = child | |
153 proc `[]`*(entity: Entity, index: int): Entity = | |
154 entity.children[index] | |
155 | |
306
046f7e2b1e13
add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents:
305
diff
changeset
|
156 proc `[]=`*[T](entity: Entity, name: string, component: T) = |
122
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
157 component.entity = entity |
302 | 158 entity.components[name] = component |
306
046f7e2b1e13
add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents:
305
diff
changeset
|
159 proc `[]`*[T](entity: Entity, name: string, component: T): T = |
046f7e2b1e13
add: nicer api to prevent need of component casting
Sam <sam@basx.dev>
parents:
305
diff
changeset
|
160 T(entity.components[name]) |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
161 |
302 | 162 func newEntity*(name: string, components: openArray[(string, Component)] = [], children: varargs[Entity]): Entity = |
108 | 163 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
|
164 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
|
165 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
|
166 result.name = name |
302 | 167 for (name, comp) in components: |
168 result[name] = comp | |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
169 if result.name == "": |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
315
diff
changeset
|
170 result.name = &"Entity[{$(cast[uint](result))}]" |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
171 result.internal_transform = Unit4 |
28 | 172 |
263
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
254
diff
changeset
|
173 iterator allEntitiesOfType*[T: Entity](root: Entity): T = |
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
254
diff
changeset
|
174 var queue = @[root] |
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
254
diff
changeset
|
175 while queue.len > 0: |
270
563ca4a82931
did: overhaul some of the mesh-data uploading and transformation handling, added: text/font rendering
Sam <sam@basx.dev>
parents:
266
diff
changeset
|
176 var entity = queue.pop |
263
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
254
diff
changeset
|
177 if entity of T: |
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
254
diff
changeset
|
178 yield T(entity) |
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
254
diff
changeset
|
179 for i in countdown(entity.children.len - 1, 0): |
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
254
diff
changeset
|
180 queue.add entity.children[i] |
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
254
diff
changeset
|
181 |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
182 iterator allComponentsOfType*[T: Component](root: Entity): var T = |
19
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
183 var queue = @[root] |
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
Sam <sam@basx.dev>
parents:
diff
changeset
|
184 while queue.len > 0: |
109 | 185 let entity = queue.pop |
302 | 186 for component in entity.components.mvalues: |
122
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
187 if component of T: |
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
188 yield T(component) |
109 | 189 for i in countdown(entity.children.len - 1, 0): |
190 queue.add entity.children[i] | |
28 | 191 |
108 | 192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 return child |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
199 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
|
200 |
254 | 201 func `[]`*(scene: Scene, name: string): Entity = |
202 return scene.root.firstWithName(name) | |
203 | |
122
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
204 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 if child.name == name: |
122
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
210 for component in child.components: |
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
211 if component of T: |
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
212 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
|
213 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
|
214 |
108 | 215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 |
122
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 if child.name == name: |
122
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
230 for component in child.components: |
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
231 if component of T: |
506090173619
did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents:
121
diff
changeset
|
232 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
|
233 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
|
234 |
109 | 235 iterator allEntities*(root: Entity): Entity = |
28 | 236 var queue = @[root] |
237 while queue.len > 0: | |
238 let next = queue.pop | |
239 for child in next.children: | |
240 queue.add child | |
241 yield next | |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
242 |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
243 proc prettyRecursive*(entity: Entity): seq[string] = |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
244 var compList: seq[string] |
302 | 245 for (name, comp) in entity.components.pairs: |
246 compList.add name & ": " & $comp | |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
247 |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
248 var trans = entity.transform.col(3) |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
249 var pos = entity.getModelTransform().col(3) |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
250 result.add "- " & $entity & " [" & $trans.x & ", " & $trans.y & ", " & $trans.z & "] -> [" & $pos.x & ", " & $pos.y & ", " & $pos.z & "]" |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
251 if compList.len > 0: |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
252 result.add " [" & compList.join(", ") & "]" |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
253 |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
254 for child in entity.children: |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
255 for childLine in child.prettyRecursive: |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
256 result.add " " & childLine |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
257 |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
258 proc pretty*(entity: Entity): string = |
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
259 entity.prettyRecursive.join("\n") |