Mercurial > games > semicongine
annotate examples/E03_hello_cube.nim @ 624:3f13de7d8ec4
fix: API change for shader attributes
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 29 Apr 2023 18:29:19 +0700 |
parents | f41c1b78cf5b |
children | c33c8e156e3e |
rev | line source |
---|---|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 # TODO: Needs Depth-Buffer first! |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 # |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 import std/times |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 import std/strutils |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 import semicongine |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 const |
602 | 17 TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32) |
18 TopRightFront = newVec3f(0.5'f32, -0.5'f32, -0.5'f32) | |
19 BottomRightFront = newVec3f(0.5'f32, 0.5'f32, -0.5'f32) | |
20 BottomLeftFront = newVec3f(-0.5'f32, 0.5'f32, -0.5'f32) | |
21 TopLeftBack = newVec3f(0.5'f32, -0.5'f32, 0.5'f32) | |
22 TopRightBack = newVec3f(-0.5'f32, -0.5'f32, 0.5'f32) | |
23 BottomRightBack = newVec3f(-0.5'f32, 0.5'f32, 0.5'f32) | |
24 BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 const |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 cube_pos = @[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 ] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 cube_color = @[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 Rf32, Rf32, Rf32, Rf32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 Gf32, Gf32, Gf32, Gf32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 Bf32, Bf32, Bf32, Bf32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 ] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 var |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 tris: seq[array[3, uint16]] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 for i in 0'u16 ..< 6'u16: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 let off = i * 4 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 tris.add [off + 0'u16, off + 1'u16, off + 2'u16] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 tris.add [off + 2'u16, off + 3'u16, off + 0'u16] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 when isMainModule: |
602 | 50 var myengine = initEngine("Hello cube") |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 |
602 | 52 const |
53 vertexInput = @[ | |
624 | 54 attr[Vec3f]("position"), |
55 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite), | |
602 | 56 ] |
57 vertexOutput = @[attr[Vec3f]("outcolor")] | |
58 uniforms = @[ | |
59 attr[Mat4]("projection"), | |
60 attr[Mat4]("view"), | |
61 attr[Mat4]("model"), | |
62 ] | |
63 fragOutput = @[attr[Vec4f]("color")] | |
64 vertexCode = compileGlslShader( | |
65 stage=VK_SHADER_STAGE_VERTEX_BIT, | |
66 inputs=vertexInput, | |
67 uniforms=uniforms, | |
68 outputs=vertexOutput, | |
69 main="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""" | |
70 ) | |
71 fragmentCode = compileGlslShader( | |
72 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | |
73 inputs=vertexOutput, | |
74 uniforms=uniforms, | |
75 outputs=fragOutput, | |
76 main="color = vec4(outcolor, 1);" | |
77 ) | |
78 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) | |
79 var | |
80 projection = initShaderGlobal("projection", Unit4f32) | |
81 view = initShaderGlobal("view", Unit4f32) | |
82 model = initShaderGlobal("model", Unit4f32) | |
83 cube = newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color)) | |
84 cube.components.add projection | |
85 cube.components.add view | |
86 cube.components.add model | |
87 myengine.addScene(cube, vertexInput) | |
88 | |
89 var t: float32 = cpuTime() | |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
602
diff
changeset
|
90 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): |
602 | 91 setValue[Mat4](model.value, translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32)) |
92 setValue[Mat4]( | |
93 projection.value, | |
94 perspective( | |
95 float32(PI / 4), | |
96 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]), | |
97 0.1'f32, | |
98 100'f32 | |
99 ) | |
100 ) | |
101 t = cpuTime() | |
102 | |
103 myengine.renderScene(cube) | |
104 | |
105 myengine.destroy() |