annotate tests/test_collision.nim @ 329:69e18f69713b

add: scene/shader compatability check, fix collision code to work with new APIs
author Sam <sam@basx.dev>
date Tue, 29 Aug 2023 00:01:13 +0700
parents 89800309ab09
children 05fb85ba97dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
1 import std/tables
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
2
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
4
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
5 proc main() =
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
6 var scene = Scene(name: "main")
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
7
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
8 scene.meshes.add rect(color="f00f")
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
9 scene.meshes.add rect()
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
10 scene.meshes.add circle(color="0f0f")
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
11 scene.meshes[1].transform = scale(0.8, 0.8)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
12 scene.meshes[2].transform = scale(0.1, 0.1)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
13 scene.addShaderGlobal("perspective", Unit4F32)
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
14
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
15 const
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
16 shaderConfiguration = createShaderConfiguration(
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
17 inputs=[
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
18 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
19 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
20 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead),
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
21 ],
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
22 intermediates=[attr[Vec4f]("colorout")],
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
23 uniforms=[attr[Mat4]("perspective")],
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
24 outputs=[attr[Vec4f]("fragcolor")],
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
25 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
26 fragmentCode="""fragcolor = colorout;""",
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
27 )
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
28
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
29 var engine = initEngine("Test collisions")
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
30
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
31 engine.initRenderer({"": shaderConfiguration}.toTable)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
32 engine.addScene(scene)
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
33
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
34 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
35 if engine.windowWasResized():
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
36 var winSize = engine.getWindow().size
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
37 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
38 if engine.keyIsDown(A): scene.meshes[0].transform = scene.meshes[0].transform * translate(-0.001, 0, 0)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
39 if engine.keyIsDown(D): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0.001, 0, 0)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
40 if engine.keyIsDown(W): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0, -0.001, 0)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
41 if engine.keyIsDown(S): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0, 0.001, 0)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
42 if engine.keyIsDown(Q): scene.meshes[0].transform = scene.meshes[0].transform * rotate(-0.001, Z)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
43 if engine.keyIsDown(Key.E): scene.meshes[0].transform = scene.meshes[0].transform * rotate( 0.001, Z)
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
44
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
45 if engine.keyIsDown(Key.Z): scene.meshes[1].transform = scene.meshes[1].transform * rotate(-0.001, Z)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
46 if engine.keyIsDown(Key.X): scene.meshes[1].transform = scene.meshes[1].transform * rotate( 0.001, Z)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
47 if engine.keyIsDown(Key.C): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, -0.001, 0)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
48 if engine.keyIsDown(Key.V): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, 0.001, 0)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
49 let hitbox = Collider(theType: Box, transform: scene.meshes[0].transform * translate(-0.5, -0.5))
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
50 let hitsphere = Collider(theType: Sphere, transform: scene.meshes[2].transform, radius: 0.5)
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
51 echo intersects(hitbox, hitsphere)
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
52 engine.renderScene(scene)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
53 engine.destroy()
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
54
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
55
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
56 when isMainModule:
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
57 main()