comparison examples/E10_pong.nim @ 77:4b16d2af316a

add: color functions + gamma correction
author Sam <sam@basx.dev>
date Tue, 07 Feb 2023 12:20:04 +0700
parents 779607656b12
children 8bfcaed87cd6
comparison
equal deleted inserted replaced
76:e5be7a25634e 77:4b16d2af316a
7 transform: ModelTransformAttribute 7 transform: ModelTransformAttribute
8 Uniforms = object 8 Uniforms = object
9 view: ViewProjectionTransform 9 view: ViewProjectionTransform
10 10
11 const 11 const
12 barcolor = Vec3([1'f, 0'f, 0'f]) 12 barcolor = RGBfromHex("5A3F00").gamma(2.2)
13 barSize = 0.1'f 13 barSize = 0.1'f
14 barWidth = 0.01'f 14 barWidth = 0.01'f
15 ballcolor = Vec3([0'f, 0'f, 0'f]) 15 ballcolor = RGBfromHex("B17F08").gamma(2.2)
16 levelRatio = 1 16 levelRatio = 1
17 ballSize = 0.01'f 17 ballSize = 0.01'f
18 backgroundColor = RGBAfromHex("FAC034").gamma(2.2)
19 ballSpeed = 60'f
18 20
19 var 21 var
20 uniforms = Uniforms() 22 uniforms = Uniforms()
21 pipeline: RenderPipeline[Vertex, Uniforms] 23 pipeline: RenderPipeline[Vertex, Uniforms]
22 level: Thing 24 level: Thing
23 ballVelocity = Vec2([1'f, 1'f]).normalized * 30'f 25 ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed
24 26
25 proc globalUpdate(engine: var Engine; t, dt: float32) = 27 proc globalUpdate(engine: var Engine; t, dt: float32) =
26 var height = float32(engine.vulkan.frameSize.y) / float32( 28 var height = float32(engine.vulkan.frameSize.y) / float32(
27 engine.vulkan.frameSize.x) 29 engine.vulkan.frameSize.x)
28 var width = 1'f 30 var width = 1'f
42 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, 44 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt,
43 ballVelocity[1] * dt, 0'f) 45 ballVelocity[1] * dt, 0'f)
44 46
45 # loose 47 # loose
46 if ball.transform.col(3).x - ballSize/2 <= 0: 48 if ball.transform.col(3).x - ballSize/2 <= 0:
47 ballVelocity = Vec2([1'f, 1'f]).normalized * 30'f 49 ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed
48 ball.transform[0, 3] = 0.5 50 ball.transform[0, 3] = width / 2
49 ball.transform[1, 3] = 0.5 51 ball.transform[1, 3] = height / 2
50 52
51 # bounce level 53 # bounce level
52 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[ 54 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[
53 0] = -ballVelocity[0] 55 0] = -ballVelocity[0]
54 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] 56 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
66 ballVelocity[0] = abs(ballVelocity[0]) 68 ballVelocity[0] = abs(ballVelocity[0])
67 69
68 70
69 when isMainModule: 71 when isMainModule:
70 var myengine = igniteEngine("Pong") 72 var myengine = igniteEngine("Pong")
71 myengine.fps = 60 73 myengine.maxFPS = 61
72 level = newThing("Level") 74 level = newThing("Level")
73 var playerbarmesh = quad[Vertex, Vec2, float32]() 75 var playerbarmesh = quad[Vertex, Vec2, float32]()
74 playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor] 76 playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor]
75 playerbarmesh.vertexData.transform.data = @[Unit44] 77 playerbarmesh.vertexData.transform.data = @[Unit44]
76 var playerbar = newThing("playerbar", playerbarmesh) 78 var playerbar = newThing("playerbar", playerbarmesh)
97 myengine, 99 myengine,
98 level, 100 level,
99 vertexShader, 101 vertexShader,
100 fragmentShader 102 fragmentShader
101 ) 103 )
102 pipeline.clearColor = Vec4([1.0'f, 1.0'f, 1.0'f, 1.0'f]) 104 pipeline.clearColor = backgroundColor
103 # show something 105 # show something
104 myengine.run(pipeline, globalUpdate) 106 myengine.run(pipeline, globalUpdate)
105 pipeline.trash() 107 pipeline.trash()
106 myengine.trash() 108 myengine.trash()