comparison tests/test_mesh.nim @ 1021:73b572f82a1f

add: bases for a better input-system
author sam <sam@basx.dev>
date Thu, 09 May 2024 23:02:35 +0700
parents 6406766a222d
children 74957cbf589b
comparison
equal deleted inserted replaced
1020:355ef428b5f4 1021:73b572f82a1f
1 import std/strformat 1 import std/strformat
2 import semicongine 2 import semicongine
3 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
4 proc main() = 14 proc main() =
5 var scenes = [ 15 var scenes = [
6 Scene(name: "Donut", meshes: loadMeshes("donut.glb", COLORED_SINGLE_TEXTURE_MATERIAL)[0].toSeq), 16 Scene(name: "Donut", meshes: loadMeshes("donut.glb", MeshMaterial)[0].toSeq),
7 ] 17 ]
8 18
9 var engine = initEngine("Test meshes") 19 var engine = initEngine("Test meshes")
10 const 20 const
11 shaderConfiguration = createShaderConfiguration( 21 shaderConfiguration = createShaderConfiguration(
22 name = "default shader",
12 inputs = [ 23 inputs = [
13 attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead), 24 attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead),
14 attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true), 25 attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true),
15 attr[Vec2f]("texcoord_0", memoryPerformanceHint = PreferFastRead), 26 attr[Vec2f]("texcoord_0", memoryPerformanceHint = PreferFastRead),
16 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), 27 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true),
26 attr[Mat4]("view"), 37 attr[Mat4]("view"),
27 attr[Vec4f]("color", arrayCount = 4), 38 attr[Vec4f]("color", arrayCount = 4),
28 ], 39 ],
29 samplers = [attr[Texture]("baseTexture", arrayCount = 4)], 40 samplers = [attr[Texture]("baseTexture", arrayCount = 4)],
30 vertexCode = &""" 41 vertexCode = &"""
31 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); 42 gl_Position = vec4(position, 1.0) * (transform * (Uniforms.view * Uniforms.projection));
32 vertexColor = Uniforms.color[{MATERIALINDEX_ATTRIBUTE}]; 43 vertexColor = Uniforms.color[{MATERIALINDEX_ATTRIBUTE}];
33 colorTexCoord = texcoord_0; 44 colorTexCoord = texcoord_0;
34 materialIndexOut = {MATERIALINDEX_ATTRIBUTE}; 45 materialIndexOut = {MATERIALINDEX_ATTRIBUTE};
35 """, 46 """,
36 fragmentCode = "color = texture(baseTexture[materialIndexOut], colorTexCoord) * vertexColor;" 47 fragmentCode = "color = texture(baseTexture[materialIndexOut], colorTexCoord) * vertexColor;"
37 ) 48 )
38 engine.initRenderer({COLORED_SINGLE_TEXTURE_MATERIAL: shaderConfiguration}) 49 engine.initRenderer({MeshMaterial: shaderConfiguration})
39 50
40 for scene in scenes.mitems: 51 for scene in scenes.mitems:
41 scene.addShaderGlobal("projection", Unit4F32) 52 scene.addShaderGlobal("projection", Unit4F32)
42 scene.addShaderGlobal("view", Unit4F32) 53 scene.addShaderGlobal("view", Unit4F32)
43 engine.loadScene(scene) 54 engine.loadScene(scene)
46 size = 1'f32 57 size = 1'f32
47 elevation = 0'f32 58 elevation = 0'f32
48 azimut = 0'f32 59 azimut = 0'f32
49 currentScene = 0 60 currentScene = 0
50 61
51 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 62 while engine.updateInputs() and not keyIsDown(Escape):
52 if engine.keyWasPressed(`1`): 63 if keyWasPressed(`1`):
53 currentScene = 0 64 currentScene = 0
54 elif engine.keyWasPressed(`2`): 65 elif keyWasPressed(`2`):
55 currentScene = 1 66 currentScene = 1
56 elif engine.keyWasPressed(`3`): 67 elif keyWasPressed(`3`):
57 currentScene = 2 68 currentScene = 2
58 elif engine.keyWasPressed(`4`): 69 elif keyWasPressed(`4`):
59 currentScene = 3 70 currentScene = 3
60 elif engine.keyWasPressed(`5`): 71 elif keyWasPressed(`5`):
61 currentScene = 4 72 currentScene = 4
62 elif engine.keyWasPressed(`6`): 73 elif keyWasPressed(`6`):
63 currentScene = 5 74 currentScene = 5
64 75
65 if engine.keyWasPressed(NumberRowExtra3): 76 if keyWasPressed(NumberRowExtra3):
66 size = 0.3'f32 77 size = 0.3'f32
67 elevation = 0'f32 78 elevation = 0'f32
68 azimut = 0'f32 79 azimut = 0'f32
69 80
70 let ratio = engine.getWindow().size[0] / engine.getWindow().size[1] 81 let ratio = engine.getWindow().size[0] / engine.getWindow().size[1]
71 size *= 1'f32 + engine.mouseWheel() * 0.05 82 size *= 1'f32 + mouseWheel() * 0.05
72 azimut += engine.mouseMove().x / 180'f32 83 azimut += mouseMove().x / 180'f32
73 elevation -= engine.mouseMove().y / 180'f32 84 elevation -= mouseMove().y / 180'f32
74 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1)) 85 scenes[currentScene].setShaderGlobal("projection", perspective(PI / 2, ratio, -0.5, 1))
75 scenes[currentScene].setShaderGlobal( 86 scenes[currentScene].setShaderGlobal(
76 "view", 87 "view",
77 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32) 88 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32)
78 ) 89 )
79 engine.renderScene(scenes[currentScene]) 90 engine.renderScene(scenes[currentScene])