Mercurial > games > semicongine
annotate src/semicongine/scene.nim @ 306:046f7e2b1e13
add: nicer api to prevent need of component casting
| author | Sam <sam@basx.dev> | 
|---|---|
| date | Wed, 28 Jun 2023 00:35:09 +0700 | 
| parents | 44ecc0a01a9f | 
| children | c73224f9d38f | 
| rev | line source | 
|---|---|
| 60 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 1 import std/strformat | 
| 266 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 2 import std/sequtils | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 3 import std/algorithm | 
| 247 | 4 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: 
142diff
changeset | 5 import std/tables | 
| 128 
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
 Sam <sam@basx.dev> parents: 
127diff
changeset | 6 import std/hashes | 
| 60 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 7 import std/typetraits | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 8 | 
| 206 
7f921d7d0a2b
did: small refactoring of module structure
 Sam <sam@basx.dev> parents: 
201diff
changeset | 9 import ./core | 
| 288 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
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: 
142diff
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: 
142diff
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: 
142diff
changeset | 15 root*: Entity | 
| 229 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 16 shaderGlobals*: Table[string, DataList] | 
| 266 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 17 materials: seq[Material] | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 18 | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 19 Material* = object | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 20 name*: string | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 21 textures*: Table[string, Texture] | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 22 data*: Table[string, DataValue] | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 23 | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 24 Component* = ref object of RootObj | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 25 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: 
142diff
changeset | 26 | 
| 108 | 27 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: 
40diff
changeset | 28 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: 
270diff
changeset | 29 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: 
302diff
changeset | 30 parent: Entity | 
| 
44ecc0a01a9f
fix: some issues with new scene/api/component api
 Sam <sam@basx.dev> parents: 
302diff
changeset | 31 children: seq[Entity] | 
| 
44ecc0a01a9f
fix: some issues with new scene/api/component api
 Sam <sam@basx.dev> parents: 
302diff
changeset | 32 components: Table[string, Component] | 
| 60 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 33 | 
| 288 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 34 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: 
270diff
changeset | 35 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: 
270diff
changeset | 36 | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 37 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: 
270diff
changeset | 38 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: 
270diff
changeset | 39 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: 
270diff
changeset | 40 | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 41 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: 
270diff
changeset | 42 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: 
270diff
changeset | 43 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: 
270diff
changeset | 44 | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 45 func start*(animation: var EntityAnimation) = | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 46 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: 
270diff
changeset | 47 | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 48 func stop*(animation: var EntityAnimation) = | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 49 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: 
270diff
changeset | 50 | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 51 func update*(animation: var EntityAnimation, dt: float32) = | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 52 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: 
270diff
changeset | 53 | 
| 305 
44ecc0a01a9f
fix: some issues with new scene/api/component api
 Sam <sam@basx.dev> parents: 
302diff
changeset | 54 func parent(entity: Entity): Entity = | 
| 
44ecc0a01a9f
fix: some issues with new scene/api/component api
 Sam <sam@basx.dev> parents: 
302diff
changeset | 55 entity.parent | 
| 
44ecc0a01a9f
fix: some issues with new scene/api/component api
 Sam <sam@basx.dev> parents: 
302diff
changeset | 56 | 
| 290 | 57 func transform*(entity: Entity): Mat4 = | 
| 58 result = entity.internal_transform | |
| 302 | 59 for component in entity.components.mvalues: | 
| 292 
d4b4ad203578
fix: do not apply last frame value after animation has stopped
 Sam <sam@basx.dev> parents: 
290diff
changeset | 60 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: 
290diff
changeset | 61 result = result * EntityAnimation(component).player.currentValue | 
| 290 | 62 | 
| 63 func originalTransform*(entity: Entity): Mat4 = | |
| 64 entity.internal_transform | |
| 65 | |
| 66 func `transform=`*(entity: Entity, value: Mat4) = | |
| 67 entity.internal_transform = value | |
| 68 | |
| 69 func getModelTransform*(entity: Entity): Mat4 = | |
| 70 result = entity.transform | |
| 71 if not entity.parent.isNil: | |
| 72 result = entity.transform * entity.parent.getModelTransform() | |
| 73 | |
| 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: 
142diff
changeset | 74 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: 
211diff
changeset | 75 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]()) | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 76 setValues(scene.shaderGlobals[name], @[data]) | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 77 | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 78 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: 
211diff
changeset | 79 scene.shaderGlobals[name] = newDataList(thetype=getDataType[T]()) | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 80 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: 
142diff
changeset | 81 | 
| 190 
8f2eaf0d2808
add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
 Sam <sam@basx.dev> parents: 
189diff
changeset | 82 func getShaderGlobal*[T](scene: Scene, name: string): T = | 
| 229 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 83 getValues[T](scene.shaderGlobals[name])[0] | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 84 | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 85 func getShaderGlobalArray*[T](scene: Scene, name: string): seq[T] = | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 86 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: 
189diff
changeset | 87 | 
| 
8f2eaf0d2808
add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
 Sam <sam@basx.dev> parents: 
189diff
changeset | 88 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: 
211diff
changeset | 89 setValues[T](scene.shaderGlobals[name], @[value]) | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 90 | 
| 
7741bca03e7c
add: support for struct members to be array
 Sam <sam@basx.dev> parents: 
211diff
changeset | 91 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: 
211diff
changeset | 92 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: 
142diff
changeset | 93 | 
| 266 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 94 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: 
263diff
changeset | 95 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: 
142diff
changeset | 96 | 
| 
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: 
142diff
changeset | 97 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: 
142diff
changeset | 98 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: 
142diff
changeset | 99 | 
| 266 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 100 func getMaterials*(scene: Scene): seq[Material] = scene.materials | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 101 | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 102 func addMaterial*(scene: var Scene, material: Material) = | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 103 if scene.materials.len > 0: | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 104 assert material.data.keys.toSeq.sorted() == scene.materials[0].data.keys.toSeq.sorted(), &"{material.data.keys.toSeq.sorted()} == {scene.materials[0].data.keys.toSeq.sorted()}" | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 105 else: | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 106 for name, value in material.data.pairs: | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 107 scene.shaderGlobals[name] = newDataList(thetype=value.thetype) | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 108 | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 109 for name, value in material.data.pairs: | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 110 scene.shaderGlobals[name].appendValue(value) | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 111 | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 112 scene.materials.add material | 
| 
fd1a95f433d1
add: better material loading system, still far from great
 Sam <sam@basx.dev> parents: 
263diff
changeset | 113 | 
| 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: 
142diff
changeset | 114 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: 
142diff
changeset | 115 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: 
142diff
changeset | 116 | 
| 
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: 
142diff
changeset | 117 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: 
142diff
changeset | 118 a.name == b.name | 
| 127 | 119 | 
| 128 
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
 Sam <sam@basx.dev> parents: 
127diff
changeset | 120 func hash*(entity: Entity): Hash = | 
| 
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
 Sam <sam@basx.dev> parents: 
127diff
changeset | 121 hash(cast[pointer](entity)) | 
| 60 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 122 | 
| 142 
9c0d7839dc91
add: correct mesh buffer data updates to GPU
 Sam <sam@basx.dev> parents: 
128diff
changeset | 123 func hash*(component: Component): Hash = | 
| 
9c0d7839dc91
add: correct mesh buffer data updates to GPU
 Sam <sam@basx.dev> parents: 
128diff
changeset | 124 hash(cast[pointer](component)) | 
| 
9c0d7839dc91
add: correct mesh buffer data updates to GPU
 Sam <sam@basx.dev> parents: 
128diff
changeset | 125 | 
| 112 
0c5a74885796
did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
 Sam <sam@basx.dev> parents: 
109diff
changeset | 126 method `$`*(entity: Entity): string {.base.} = entity.name | 
| 122 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 127 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: 
109diff
changeset | 128 "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: 
270diff
changeset | 129 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: 
270diff
changeset | 130 &"Entity animation: {animation.player.animation}" | 
| 247 | 131 | 
| 109 | 132 proc add*(entity: Entity, child: Entity) = | 
| 133 child.parent = entity | |
| 134 entity.children.add child | |
| 306 
046f7e2b1e13
add: nicer api to prevent need of component casting
 Sam <sam@basx.dev> parents: 
305diff
changeset | 135 proc `[]=`*[T](entity: var Entity, index: int, child: var T) = | 
| 302 | 136 child.parent = entity | 
| 137 entity.children[index] = child | |
| 138 proc `[]`*(entity: Entity, index: int): Entity = | |
| 139 entity.children[index] | |
| 140 | |
| 306 
046f7e2b1e13
add: nicer api to prevent need of component casting
 Sam <sam@basx.dev> parents: 
305diff
changeset | 141 proc `[]=`*[T](entity: Entity, name: string, component: T) = | 
| 122 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 142 component.entity = entity | 
| 302 | 143 entity.components[name] = component | 
| 306 
046f7e2b1e13
add: nicer api to prevent need of component casting
 Sam <sam@basx.dev> parents: 
305diff
changeset | 144 proc `[]`*[T](entity: Entity, name: string, component: T): T = | 
| 
046f7e2b1e13
add: nicer api to prevent need of component casting
 Sam <sam@basx.dev> parents: 
305diff
changeset | 145 T(entity.components[name]) | 
| 19 
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
 Sam <sam@basx.dev> parents: diff
changeset | 146 | 
| 302 | 147 func newEntity*(name: string, components: openArray[(string, Component)] = [], children: varargs[Entity]): Entity = | 
| 108 | 148 result = new Entity | 
| 60 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 149 for child in children: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 150 result.add child | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 151 result.name = name | 
| 302 | 152 for (name, comp) in components: | 
| 153 result[name] = comp | |
| 60 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 154 if result.name == "": | 
| 108 | 155 result.name = &"Entity[{$(cast[ByteAddress](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: 
270diff
changeset | 156 result.internal_transform = Unit4 | 
| 28 | 157 | 
| 263 
d1ee2a815fa1
add: some api improvments, preparing for font-loading
 Sam <sam@basx.dev> parents: 
254diff
changeset | 158 iterator allEntitiesOfType*[T: Entity](root: Entity): T = | 
| 
d1ee2a815fa1
add: some api improvments, preparing for font-loading
 Sam <sam@basx.dev> parents: 
254diff
changeset | 159 var queue = @[root] | 
| 
d1ee2a815fa1
add: some api improvments, preparing for font-loading
 Sam <sam@basx.dev> parents: 
254diff
changeset | 160 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: 
266diff
changeset | 161 var entity = queue.pop | 
| 263 
d1ee2a815fa1
add: some api improvments, preparing for font-loading
 Sam <sam@basx.dev> parents: 
254diff
changeset | 162 if entity of T: | 
| 
d1ee2a815fa1
add: some api improvments, preparing for font-loading
 Sam <sam@basx.dev> parents: 
254diff
changeset | 163 yield T(entity) | 
| 
d1ee2a815fa1
add: some api improvments, preparing for font-loading
 Sam <sam@basx.dev> parents: 
254diff
changeset | 164 for i in countdown(entity.children.len - 1, 0): | 
| 
d1ee2a815fa1
add: some api improvments, preparing for font-loading
 Sam <sam@basx.dev> parents: 
254diff
changeset | 165 queue.add entity.children[i] | 
| 
d1ee2a815fa1
add: some api improvments, preparing for font-loading
 Sam <sam@basx.dev> parents: 
254diff
changeset | 166 | 
| 128 
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
 Sam <sam@basx.dev> parents: 
127diff
changeset | 167 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 | 168 var queue = @[root] | 
| 
b55d6ecde79d
did: introduce scene graph, meshs and generic vertex buffers
 Sam <sam@basx.dev> parents: diff
changeset | 169 while queue.len > 0: | 
| 109 | 170 let entity = queue.pop | 
| 302 | 171 for component in entity.components.mvalues: | 
| 122 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 172 if component of T: | 
| 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 173 yield T(component) | 
| 109 | 174 for i in countdown(entity.children.len - 1, 0): | 
| 175 queue.add entity.children[i] | |
| 28 | 176 | 
| 108 | 177 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: 
40diff
changeset | 178 var queue = @[root] | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 179 while queue.len > 0: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 180 let next = queue.pop | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 181 for child in next.children: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 182 if child.name == name: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 183 return child | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 184 queue.add child | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 185 | 
| 254 | 186 func `[]`*(scene: Scene, name: string): Entity = | 
| 187 return scene.root.firstWithName(name) | |
| 188 | |
| 122 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 189 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: 
40diff
changeset | 190 var queue = @[root] | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 191 while queue.len > 0: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 192 let next = queue.pop | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 193 for child in next.children: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 194 if child.name == name: | 
| 122 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 195 for component in child.components: | 
| 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 196 if component of T: | 
| 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 197 return T(component) | 
| 60 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 198 queue.add child | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 199 | 
| 108 | 200 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: 
40diff
changeset | 201 var queue = @[root] | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 202 while queue.len > 0: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 203 let next = queue.pop | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 204 for child in next.children: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 205 if child.name == name: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 206 result.add child | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 207 queue.add child | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 208 | 
| 122 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 209 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: 
40diff
changeset | 210 var queue = @[root] | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 211 while queue.len > 0: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 212 let next = queue.pop | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 213 for child in next.children: | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 214 if child.name == name: | 
| 122 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 215 for component in child.components: | 
| 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 216 if component of T: | 
| 
506090173619
did: implement uniforms, some refactoring
 Sam <sam@basx.dev> parents: 
121diff
changeset | 217 result.add T(component) | 
| 60 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 218 queue.add child | 
| 
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
 Sam <sam@basx.dev> parents: 
40diff
changeset | 219 | 
| 109 | 220 iterator allEntities*(root: Entity): Entity = | 
| 28 | 221 var queue = @[root] | 
| 222 while queue.len > 0: | |
| 223 let next = queue.pop | |
| 224 for child in next.children: | |
| 225 queue.add child | |
| 226 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: 
270diff
changeset | 227 | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 228 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: 
270diff
changeset | 229 var compList: seq[string] | 
| 302 | 230 for (name, comp) in entity.components.pairs: | 
| 231 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: 
270diff
changeset | 232 | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 233 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: 
270diff
changeset | 234 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: 
270diff
changeset | 235 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: 
270diff
changeset | 236 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: 
270diff
changeset | 237 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: 
270diff
changeset | 238 | 
| 
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
 Sam <sam@basx.dev> parents: 
270diff
changeset | 239 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: 
270diff
changeset | 240 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: 
270diff
changeset | 241 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: 
270diff
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: 
270diff
changeset | 243 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: 
270diff
changeset | 244 entity.prettyRecursive.join("\n") | 
