comparison examples/E02_squares.nim @ 316:b145a05c2459

add: changing rendering system, not finished yet, also upgrading to Nim 2
author Sam <sam@basx.dev>
date Mon, 07 Aug 2023 00:23:00 +0700
parents da0bd61abe91
children 887ddc8d45fd
comparison
equal deleted inserted replaced
315:4921ec86dcb4 316:b145a05c2459
40 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)] 40 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex + 1), uint16(vertIndex + 2)]
41 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)] 41 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex + 3), uint16(vertIndex + 0)]
42 42
43 43
44 const 44 const
45 vertexInput = @[ 45 inputs = @[
46 attr[Vec3f]("position"), 46 attr[Vec3f]("position"),
47 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), 47 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
48 attr[uint32]("index"), 48 attr[uint32]("index"),
49 ] 49 ]
50 vertexOutput = @[attr[Vec4f]("outcolor")] 50 intermediate = @[attr[Vec4f]("outcolor")]
51 uniforms = @[attr[float32]("time")] 51 uniforms = @[attr[float32]("time")]
52 fragOutput = @[attr[Vec4f]("color")] 52 outputs = @[attr[Vec4f]("color")]
53 vertexCode = compileGlslShader( 53 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
54 stage=VK_SHADER_STAGE_VERTEX_BIT, 54 inputs=inputs,
55 inputs=vertexInput, 55 intermediate=intermediate,
56 outputs=outputs,
56 uniforms=uniforms, 57 uniforms=uniforms,
57 outputs=vertexOutput, 58 vertexCode="""
58 main="""
59 float pos_weight = index / 100.0; // add some gamma correction? 59 float pos_weight = index / 100.0; // add some gamma correction?
60 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5; 60 float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5;
61 float v = min(1, max(0, pow(pos_weight - t, 2))); 61 float v = min(1, max(0, pow(pos_weight - t, 2)));
62 v = pow(1 - v, 3000); 62 v = pow(1 - v, 3000);
63 outcolor = vec4(color.r, color.g, v * 0.5, 1); 63 outcolor = vec4(color.r, color.g, v * 0.5, 1);
64 gl_Position = vec4(position, 1.0); 64 gl_Position = vec4(position, 1.0);
65 """ 65 """,
66 ) 66 fragmentCode="color = outcolor;",
67 fragmentCode = compileGlslShader(
68 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
69 inputs=vertexOutput,
70 uniforms=uniforms,
71 outputs=fragOutput,
72 main="color = outcolor;"
73 ) 67 )
74 var squaremesh = newMesh( 68 var squaremesh = newMesh(
75 positions=vertices, 69 positions=vertices,
76 indices=indices, 70 indices=indices,
77 colors=colors, 71 colors=colors,
80 74
81 var myengine = initEngine("Squares") 75 var myengine = initEngine("Squares")
82 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 76 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
83 77
84 var scene = newScene("scene", newEntity("scene", [], newEntity("squares", {"mesh": Component(squaremesh)}))) 78 var scene = newScene("scene", newEntity("scene", [], newEntity("squares", {"mesh": Component(squaremesh)})))
85 myengine.addScene(scene, vertexInput, @[], transformAttribute="") 79 myengine.addScene(scene, inputs, @[], transformAttribute="")
86 scene.addShaderGlobal("time", 0.0'f32) 80 scene.addShaderGlobal("time", 0.0'f32)
87 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): 81 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
88 setShaderGlobal(scene, "time", getShaderGlobal[float32](scene, "time") + 0.0005'f) 82 setShaderGlobal(scene, "time", getShaderGlobal[float32](scene, "time") + 0.0005'f)
89 myengine.renderScene(scene) 83 myengine.renderScene(scene)
90 84