comparison examples/E02_squares.nim @ 671:d84b2e88776a

fix: always use rgba
author Sam <sam@basx.dev>
date Tue, 09 May 2023 18:19:17 +0700
parents e85f54b88afb
children 5f7ec8d1bd33
comparison
equal deleted inserted replaced
670:881a5c9ea50f 671:d84b2e88776a
11 ROWS = 10 11 ROWS = 10
12 WIDTH = 2'f32 / COLUMNS 12 WIDTH = 2'f32 / COLUMNS
13 HEIGHT = 2'f32 / ROWS 13 HEIGHT = 2'f32 / ROWS
14 var 14 var
15 vertices: array[COLUMNS * ROWS * 4, Vec3f] 15 vertices: array[COLUMNS * ROWS * 4, Vec3f]
16 colors: array[COLUMNS * ROWS * 4, Vec3f] 16 colors: array[COLUMNS * ROWS * 4, Vec4f]
17 iValues: array[COLUMNS * ROWS * 4, uint32] 17 iValues: array[COLUMNS * ROWS * 4, uint32]
18 indices: array[COLUMNS * ROWS * 2, array[3, uint16]] 18 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
19 19
20 for row in 0 ..< ROWS: 20 for row in 0 ..< ROWS:
21 for col in 0 ..< COLUMNS: 21 for col in 0 ..< COLUMNS:
22 let 22 let
23 y: float32 = (row * 2 / COLUMNS) - 1 23 y: float32 = (row * 2 / COLUMNS) - 1
24 x: float32 = (col * 2 / ROWS) - 1 24 x: float32 = (col * 2 / ROWS) - 1
25 color = Vec3f([(x + 1) / 2, (y + 1) / 2, 0'f32]) 25 color = newVec4f((x + 1) / 2, (y + 1) / 2, 0, 1)
26 squareIndex = row * COLUMNS + col 26 squareIndex = row * COLUMNS + col
27 vertIndex = squareIndex * 4 27 vertIndex = squareIndex * 4
28 vertices[vertIndex + 0] = newVec3f(x, y) 28 vertices[vertIndex + 0] = newVec3f(x, y)
29 vertices[vertIndex + 1] = newVec3f(x + WIDTH, y) 29 vertices[vertIndex + 1] = newVec3f(x + WIDTH, y)
30 vertices[vertIndex + 2] = newVec3f(x + WIDTH, y + HEIGHT) 30 vertices[vertIndex + 2] = newVec3f(x + WIDTH, y + HEIGHT)
42 42
43 43
44 const 44 const
45 vertexInput = @[ 45 vertexInput = @[
46 attr[Vec3f]("position"), 46 attr[Vec3f]("position"),
47 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite), 47 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
48 attr[uint32]("index"), 48 attr[uint32]("index"),
49 ] 49 ]
50 vertexOutput = @[attr[Vec3f]("outcolor")] 50 vertexOutput = @[attr[Vec4f]("outcolor")]
51 uniforms = @[attr[float32]("time")] 51 uniforms = @[attr[float32]("time")]
52 fragOutput = @[attr[Vec4f]("color")] 52 fragOutput = @[attr[Vec4f]("color")]
53 vertexCode = compileGlslShader( 53 vertexCode = compileGlslShader(
54 stage=VK_SHADER_STAGE_VERTEX_BIT, 54 stage=VK_SHADER_STAGE_VERTEX_BIT,
55 inputs=vertexInput, 55 inputs=vertexInput,
58 main=""" 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 = vec3(color.r, color.g, v * 0.5); 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 )
67 fragmentCode = compileGlslShader( 67 fragmentCode = compileGlslShader(
68 stage=VK_SHADER_STAGE_FRAGMENT_BIT, 68 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
69 inputs=vertexOutput, 69 inputs=vertexOutput,
70 uniforms=uniforms, 70 uniforms=uniforms,
71 outputs=fragOutput, 71 outputs=fragOutput,
72 main="color = vec4(outcolor, 1);" 72 main="color = outcolor;"
73 ) 73 )
74 var squaremesh = newMesh( 74 var squaremesh = newMesh(
75 positions=vertices, 75 positions=vertices,
76 indices=indices, 76 indices=indices,
77 colors=colors, 77 colors=colors,