comparison old_tests/test_mesh.nim @ 1203:6360c8d17ce0 compiletime-tests

did: preprations to add rendering tests
author sam <sam@basx.dev>
date Mon, 15 Jul 2024 20:06:42 +0700
parents tests/test_mesh.nim@114f395b9144
children
comparison
equal deleted inserted replaced
1202:a8864fe6fe6e 1203:6360c8d17ce0
1 import std/strformat
2 import semicongine
3
4 const
5 MeshMaterial* = MaterialType(
6 name: "colored single texture material",
7 vertexAttributes: {
8 "position": Vec3F32,
9 "texcoord_0": Vec2F32,
10 }.toTable,
11 attributes: {"baseTexture": TextureType, "color": Vec4F32}.toTable
12 )
13
14 proc main() =
15 var scenes = [
16 Scene(name: "Donut", meshes: LoadMeshes("donut.glb", MeshMaterial)[0].toSeq),
17 ]
18
19 var engine = InitEngine("Test meshes")
20 const
21 shaderConfiguration = CreateShaderConfiguration(
22 name = "default shader",
23 inputs = [
24 Attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead),
25 Attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true),
26 Attr[Vec2f]("texcoord_0", memoryPerformanceHint = PreferFastRead),
27 Attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true),
28 ],
29 intermediates = [
30 Attr[Vec4f]("vertexColor"),
31 Attr[Vec2f]("colorTexCoord"),
32 Attr[uint16]("materialIndexOut", noInterpolation = true)
33 ],
34 outputs = [Attr[Vec4f]("color")],
35 uniforms = [
36 Attr[Mat4]("projection"),
37 Attr[Mat4]("view"),
38 Attr[Vec4f]("color", arrayCount = 4),
39 ],
40 samplers = [Attr[Texture]("baseTexture", arrayCount = 4)],
41 vertexCode = &"""
42 gl_Position = vec4(position, 1.0) * (transform * (Uniforms.view * Uniforms.projection));
43 vertexColor = Uniforms.color[{MATERIALINDEX_ATTRIBUTE}];
44 colorTexCoord = texcoord_0;
45 materialIndexOut = {MATERIALINDEX_ATTRIBUTE};
46 """,
47 fragmentCode = "color = texture(baseTexture[materialIndexOut], colorTexCoord) * vertexColor;"
48 )
49 engine.InitRenderer({MeshMaterial: shaderConfiguration})
50
51 for scene in scenes.mitems:
52 scene.AddShaderGlobal("projection", Unit4F32)
53 scene.AddShaderGlobal("view", Unit4F32)
54 engine.LoadScene(scene)
55
56 var
57 size = 1'f32
58 elevation = 0'f32
59 azimut = 0'f32
60 currentScene = 0
61
62 while engine.UpdateInputs() and not KeyIsDown(Escape):
63 if KeyWasPressed(`1`):
64 currentScene = 0
65 elif KeyWasPressed(`2`):
66 currentScene = 1
67 elif KeyWasPressed(`3`):
68 currentScene = 2
69 elif KeyWasPressed(`4`):
70 currentScene = 3
71 elif KeyWasPressed(`5`):
72 currentScene = 4
73 elif KeyWasPressed(`6`):
74 currentScene = 5
75
76 if KeyWasPressed(NumberRowExtra3):
77 size = 0.3'f32
78 elevation = 0'f32
79 azimut = 0'f32
80
81 let ratio = engine.GetWindow().Size[0] / engine.GetWindow().Size[1]
82 size *= 1'f32 + MouseWheel() * 0.05
83 azimut += MouseMove().x / 180'f32
84 elevation -= MouseMove().y / 180'f32
85 scenes[currentScene].SetShaderGlobal("projection", Perspective(PI / 2, ratio, -0.5, 1))
86 scenes[currentScene].SetShaderGlobal(
87 "view",
88 Scale(size, size, size) * Rotate(elevation, NewVec3f(1, 0, 0)) * Rotate(azimut, Yf32)
89 )
90 engine.RenderScene(scenes[currentScene])
91 engine.Destroy()
92
93 when isMainModule:
94 main()