comparison examples/E03_hello_cube.nim @ 336:887ddc8d45fd

did: update examples to work with improved scenegraph/material api, notice removed complexity!
author Sam <sam@basx.dev>
date Tue, 05 Sep 2023 00:28:35 +0700
parents b145a05c2459
children c66503386e8b
comparison
equal deleted inserted replaced
335:f05b4bef44d1 336:887ddc8d45fd
6 # 6 #
7 # 7 #
8 # 8 #
9 9
10 10
11 import std/tables
11 import std/times 12 import std/times
12 13
13 import ../src/semicongine 14 import ../src/semicongine
14 15
15 const 16 const
50 51
51 when isMainModule: 52 when isMainModule:
52 var myengine = initEngine("Hello cube") 53 var myengine = initEngine("Hello cube")
53 54
54 const 55 const
55 inputs = @[ 56 shaderConfiguration = createShaderConfiguration(
56 attr[Vec3f]("position"), 57 inputs=[
57 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), 58 attr[Vec3f]("position"),
58 ] 59 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
59 intermediate = @[attr[Vec4f]("outcolor")] 60 ],
60 uniforms = @[ 61 intermediates=[attr[Vec4f]("outcolor")],
61 attr[Mat4]("projection"), 62 uniforms=[
62 attr[Mat4]("view"), 63 attr[Mat4]("projection"),
63 attr[Mat4]("model"), 64 attr[Mat4]("view"),
64 ] 65 attr[Mat4]("model"),
65 fragOutput = @[attr[Vec4f]("color")] 66 ],
66 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( 67 outputs=[attr[Vec4f]("color")],
67 inputs=inputs,
68 intermediate=intermediate,
69 outputs=fragOutput,
70 uniforms=uniforms,
71 vertexCode="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""", 68 vertexCode="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""",
72 fragmentCode="color = outcolor;", 69 fragmentCode="color = outcolor;",
73 ) 70 )
74 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 71 myengine.initRenderer({"default": shaderConfiguration}.toTable)
75 var cube = newScene("scene", newEntity("cube", {"mesh": Component(newMesh(positions=cube_pos, indices=tris, colors=cube_color))})) 72 var cube = Scene(name: "scene", meshes: @[newMesh(positions=cube_pos, indices=tris, colors=cube_color, material=Material(name: "default"))])
76 cube.addShaderGlobal("projection", Unit4f32) 73 cube.addShaderGlobal("projection", Unit4f32)
77 cube.addShaderGlobal("view", Unit4f32) 74 cube.addShaderGlobal("view", Unit4f32)
78 cube.addShaderGlobal("model", Unit4f32) 75 cube.addShaderGlobal("model", Unit4f32)
79 myengine.addScene(cube, inputs, @[], transformAttribute="") 76 myengine.addScene(cube)
80 77
81 var t: float32 = cpuTime() 78 var t: float32 = cpuTime()
82 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): 79 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
83 setShaderGlobal(cube, "model", translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32)) 80 setShaderGlobal(cube, "model", translate(0'f32, 0'f32, 10'f32) * rotate(t, Yf32))
84 setShaderGlobal(cube, "projection", 81 setShaderGlobal(cube, "projection",
85 perspective( 82 perspective(
86 float32(PI / 4), 83 float32(PI / 4),
87 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]), 84 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]),
88 0.1'f32, 85 0.1'f32,
89 100'f32 86 100'f32
90 ) 87 )
91 ) 88 )
92 t = cpuTime() 89 t = cpuTime()
93
94 myengine.renderScene(cube) 90 myengine.renderScene(cube)
95 91
96 myengine.destroy() 92 myengine.destroy()