comparison examples/E02_squares.nim @ 1027:d6c27f0ed3e4

fix: examples not compiling
author sam <sam@basx.dev>
date Wed, 22 May 2024 03:45:16 +0700
parents c66503386e8b
children 71315636ba82
comparison
equal deleted inserted replaced
1026:f7802c5069ce 1027:d6c27f0ed3e4
1 import std/sequtils 1 import std/sequtils
2 import std/tables 2 import std/tables
3 import std/random 3 import std/random
4 4
5 import ../src/semicongine 5 import ../semicongine
6 6
7 7
8 when isMainModule: 8 when isMainModule:
9 randomize() 9 randomize()
10 const 10 const
42 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)] 42 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)]
43 43
44 44
45 const 45 const
46 shaderConfiguration = createShaderConfiguration( 46 shaderConfiguration = createShaderConfiguration(
47 inputs=[ 47 name = "default shader",
48 inputs = [
48 attr[Vec3f]("position"), 49 attr[Vec3f]("position"),
49 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), 50 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
50 attr[uint32]("index"), 51 attr[uint32]("index"),
51 ], 52 ],
52 intermediates=[attr[Vec4f]("outcolor")], 53 intermediates = [attr[Vec4f]("outcolor")],
53 uniforms=[attr[float32]("time")], 54 uniforms = [attr[float32]("time")],
54 outputs=[attr[Vec4f]("color")], 55 outputs = [attr[Vec4f]("color")],
55 vertexCode=""" 56 vertexCode = """
56 float pos_weight = index / 100.0; // add some gamma correction? 57 float pos_weight = index / 100.0; // add some gamma correction?
57 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5; 58 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
58 float v = min(1, max(0, pow(pos_weight - t, 2))); 59 float v = min(1, max(0, pow(pos_weight - t, 2)));
59 v = pow(1 - v, 3000); 60 v = pow(1 - v, 3000);
60 outcolor = vec4(color.r, color.g, v * 0.5, 1); 61 outcolor = vec4(color.r, color.g, v * 0.5, 1);
61 gl_Position = vec4(position, 1.0); 62 gl_Position = vec4(position, 1.0);
62 """, 63 """,
63 fragmentCode="color = outcolor;", 64 fragmentCode = "color = outcolor;",
64 ) 65 )
66 let matDef = MaterialType(name: "default", vertexAttributes: {
67 "position": Vec3F32,
68 "color": Vec4F32,
69 "index": UInt32,
70 }.toTable)
65 var squaremesh = newMesh( 71 var squaremesh = newMesh(
66 positions=vertices, 72 positions = vertices,
67 indices=indices, 73 indices = indices,
68 colors=colors, 74 colors = colors,
69 material=Material(name: "default")
70 ) 75 )
71 squaremesh[].initVertexAttribute("index", iValues.toSeq) 76 squaremesh[].initVertexAttribute("index", iValues.toSeq)
77 squaremesh.material = matDef.initMaterialData(name = "default")
72 78
73 var myengine = initEngine("Squares") 79 var myengine = initEngine("Squares")
74 myengine.initRenderer({"default": shaderConfiguration}.toTable) 80 myengine.initRenderer({matDef: shaderConfiguration})
75 81
76 var scene = Scene(name: "scene", meshes: @[squaremesh]) 82 var scene = Scene(name: "scene", meshes: @[squaremesh])
77 scene.addShaderGlobal("time", 0.0'f32) 83 scene.addShaderGlobal("time", 0.0'f32)
78 myengine.addScene(scene) 84 myengine.loadScene(scene)
79 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): 85 while myengine.UpdateInputs() and not KeyWasPressed(Escape):
80 scene.setShaderGlobal("time", getShaderGlobal[float32](scene, "time") + 0.0005'f) 86 scene.setShaderGlobal("time", getShaderGlobal[float32](scene, "time") + 0.0005'f)
81 myengine.renderScene(scene) 87 myengine.renderScene(scene)
82 88
83 myengine.destroy() 89 myengine.destroy()