annotate tests/test_collision.nim @ 826:7df5eacf84a0

add: mesh name to drawable
author Sam <sam@basx.dev>
date Wed, 04 Oct 2023 22:02:23 +0700
parents fb22fd8142b9
children f054b8bacab8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
1 import semicongine
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
2
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
3 proc main() =
790
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
4 var scene = Scene(name: "main")
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
5
792
d65b62812d34 did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents: 790
diff changeset
6 scene.add rect(color="f00f")
d65b62812d34 did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents: 790
diff changeset
7 scene.add rect()
d65b62812d34 did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents: 790
diff changeset
8 scene.add circle(color="0f0f")
790
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
9 scene.meshes[1].transform = scale(0.8, 0.8)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
10 scene.meshes[2].transform = scale(0.1, 0.1)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
11 scene.addShaderGlobal("perspective", Unit4F32)
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
12
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
13 const
790
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
14 shaderConfiguration = createShaderConfiguration(
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
15 inputs=[
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
16 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
17 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
18 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
19 ],
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
20 intermediates=[attr[Vec4f]("colorout")],
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
21 uniforms=[attr[Mat4]("perspective")],
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
22 outputs=[attr[Vec4f]("fragcolor")],
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
23 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
24 fragmentCode="""fragcolor = colorout;""",
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
25 )
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
26
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
27 var engine = initEngine("Test collisions")
790
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
28
813
fb22fd8142b9 fix: tests
Sam <sam@basx.dev>
parents: 805
diff changeset
29 engine.initRenderer({"default material": shaderConfiguration})
790
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
30 engine.addScene(scene)
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
31
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
32 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
33 if engine.windowWasResized():
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
34 var winSize = engine.getWindow().size
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
35 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
790
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
36 if engine.keyIsDown(A): scene.meshes[0].transform = scene.meshes[0].transform * translate(-0.001, 0, 0)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
37 if engine.keyIsDown(D): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0.001, 0, 0)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
38 if engine.keyIsDown(W): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0, -0.001, 0)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
39 if engine.keyIsDown(S): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0, 0.001, 0)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
40 if engine.keyIsDown(Q): scene.meshes[0].transform = scene.meshes[0].transform * rotate(-0.001, Z)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
41 if engine.keyIsDown(Key.E): scene.meshes[0].transform = scene.meshes[0].transform * rotate( 0.001, Z)
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
42
790
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
43 if engine.keyIsDown(Key.Z): scene.meshes[1].transform = scene.meshes[1].transform * rotate(-0.001, Z)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
44 if engine.keyIsDown(Key.X): scene.meshes[1].transform = scene.meshes[1].transform * rotate( 0.001, Z)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
45 if engine.keyIsDown(Key.C): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, -0.001, 0)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
46 if engine.keyIsDown(Key.V): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, 0.001, 0)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
47 let hitbox = Collider(theType: Box, transform: scene.meshes[0].transform * translate(-0.5, -0.5))
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
48 let hitsphere = Collider(theType: Sphere, transform: scene.meshes[2].transform, radius: 0.5)
a979e5c581de add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 781
diff changeset
49 echo intersects(hitbox, hitsphere)
738
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
50 engine.renderScene(scene)
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
51 engine.destroy()
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
52
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
53
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
54 when isMainModule:
eb1a6ca20a84 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
55 main()