annotate tests/test_collision.nim @ 1137:a4aa9f374d44

did: more renaming
author sam <sam@basx.dev>
date Tue, 04 Jun 2024 20:51:22 +0700
parents 71315636ba82
children 02e1d2658ff5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
1 import semicongine
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
2
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
3 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
4 var scene = Scene(name: "main")
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
5
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 371
diff changeset
6 scene.add rect(color = "f00f")
331
05fb85ba97dd 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: 329
diff changeset
7 scene.add rect()
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 371
diff changeset
8 scene.add circle(color = "0f0f")
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 352
diff changeset
9 scene.meshes[0].material = VERTEX_COLORED_MATERIAL.initMaterialData()
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 352
diff changeset
10 scene.meshes[1].material = VERTEX_COLORED_MATERIAL.initMaterialData()
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 352
diff changeset
11 scene.meshes[2].material = VERTEX_COLORED_MATERIAL.initMaterialData()
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
12 scene.meshes[1].transform = Scale(0.8, 0.8)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
13 scene.meshes[2].transform = Scale(0.1, 0.1)
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
14 scene.addShaderGlobal("perspective", Unit4F32)
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
15
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
16 const
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
17 shaderConfiguration = createShaderConfiguration(
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 371
diff changeset
18 name = "default shader",
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 371
diff changeset
19 inputs = [
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
20 Attr[Mat4]("transform", memoryPerformanceHint = PreferFastRead, perInstance = true),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
21 Attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
22 Attr[Vec4f]("color", memoryPerformanceHint = PreferFastRead),
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
23 ],
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
24 intermediates = [Attr[Vec4f]("colorout")],
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
25 uniforms = [Attr[Mat4]("perspective")],
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
26 outputs = [Attr[Vec4f]("fragcolor")],
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 371
diff changeset
27 vertexCode = """gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 371
diff changeset
28 fragmentCode = """fragcolor = colorout;""",
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
29 )
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
30
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
31 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
32
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 352
diff changeset
33 engine.initRenderer({VERTEX_COLORED_MATERIAL: shaderConfiguration})
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 352
diff changeset
34 engine.loadScene(scene)
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
35
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
36 while engine.UpdateInputs() and not KeyIsDown(Escape):
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
37 if WindowWasResized():
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
38 var winSize = engine.GetWindow().size
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
39 scene.setShaderGlobal("perspective", OrthoWindowAspect(winSize[0] / winSize[1]))
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
40 if KeyIsDown(A): scene.meshes[0].transform = scene.meshes[0].transform * Translate(-0.001, 0, 0)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
41 if KeyIsDown(D): scene.meshes[0].transform = scene.meshes[0].transform * Translate(0.001, 0, 0)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
42 if KeyIsDown(W): scene.meshes[0].transform = scene.meshes[0].transform * Translate(0, -0.001, 0)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
43 if KeyIsDown(S): scene.meshes[0].transform = scene.meshes[0].transform * Translate(0, 0.001, 0)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
44 if KeyIsDown(Q): scene.meshes[0].transform = scene.meshes[0].transform * Rotate(-0.001, Z)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
45 if 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
46
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
47 if KeyIsDown(Key.Z): scene.meshes[1].transform = scene.meshes[1].transform * Rotate(-0.001, Z)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
48 if KeyIsDown(Key.X): scene.meshes[1].transform = scene.meshes[1].transform * Rotate(0.001, Z)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
49 if KeyIsDown(Key.C): scene.meshes[1].transform = scene.meshes[1].transform * Translate(0, -0.001, 0)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
50 if KeyIsDown(Key.V): scene.meshes[1].transform = scene.meshes[1].transform * Translate(0, 0.001, 0)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
51 let hitbox = Collider(theType: Box, transform: scene.meshes[0].transform * Translate(-0.5, -0.5))
329
69e18f69713b add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents: 320
diff changeset
52 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
53 echo intersects(hitbox, hitsphere)
277
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
54 engine.renderScene(scene)
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
55 engine.destroy()
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
56
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
57
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
58 when isMainModule:
1fbef3a2d769 add: collision tests
Sam <sam@basx.dev>
parents:
diff changeset
59 main()