Mercurial > games > semicongine
annotate examples/E03_hello_cube.nim @ 204:6e2017cb8b8b
del: unused imports
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 08 May 2023 21:23:29 +0700 |
parents | 84fd522fdf3f |
children | f3912838cd69 |
rev | line source |
---|---|
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 # |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 # TODO: Needs Depth-Buffer first! |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 # |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 # |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 # |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 # |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 # |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 # |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
9 |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
10 |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 import std/times |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 |
56
94d7eed3f118
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
13 import semicongine |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 const |
141 | 16 TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32) |
17 TopRightFront = newVec3f(0.5'f32, -0.5'f32, -0.5'f32) | |
18 BottomRightFront = newVec3f(0.5'f32, 0.5'f32, -0.5'f32) | |
19 BottomLeftFront = newVec3f(-0.5'f32, 0.5'f32, -0.5'f32) | |
20 TopLeftBack = newVec3f(0.5'f32, -0.5'f32, 0.5'f32) | |
21 TopRightBack = newVec3f(-0.5'f32, -0.5'f32, 0.5'f32) | |
22 BottomRightBack = newVec3f(-0.5'f32, 0.5'f32, 0.5'f32) | |
23 BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32) | |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 const |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 cube_pos = @[ |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 ] |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 cube_color = @[ |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 Rf32, Rf32, Rf32, Rf32, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, Rf32 * 0.5'f32, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 Gf32, Gf32, Gf32, Gf32, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, Gf32 * 0.5'f32, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 Bf32, Bf32, Bf32, Bf32, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, Bf32 * 0.5'f32, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 ] |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 var |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 tris: seq[array[3, uint16]] |
39 | 43 for i in 0'u16 ..< 6'u16: |
44 let off = i * 4 | |
45 tris.add [off + 0'u16, off + 1'u16, off + 2'u16] | |
46 tris.add [off + 2'u16, off + 3'u16, off + 0'u16] | |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 when isMainModule: |
141 | 49 var myengine = initEngine("Hello cube") |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 |
141 | 51 const |
52 vertexInput = @[ | |
163 | 53 attr[Vec3f]("position"), |
54 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite), | |
141 | 55 ] |
56 vertexOutput = @[attr[Vec3f]("outcolor")] | |
57 uniforms = @[ | |
58 attr[Mat4]("projection"), | |
59 attr[Mat4]("view"), | |
60 attr[Mat4]("model"), | |
61 ] | |
62 fragOutput = @[attr[Vec4f]("color")] | |
63 vertexCode = compileGlslShader( | |
64 stage=VK_SHADER_STAGE_VERTEX_BIT, | |
65 inputs=vertexInput, | |
66 uniforms=uniforms, | |
67 outputs=vertexOutput, | |
68 main="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""" | |
69 ) | |
70 fragmentCode = compileGlslShader( | |
71 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | |
72 inputs=vertexOutput, | |
73 uniforms=uniforms, | |
74 outputs=fragOutput, | |
75 main="color = vec4(outcolor, 1);" | |
76 ) | |
77 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) | |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
78 var cube = newScene("scene", newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color))) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
79 cube.addShaderGlobal("projection", Unit4f32) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
80 cube.addShaderGlobal("view", Unit4f32) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
81 cube.addShaderGlobal("model", Unit4f32) |
141 | 82 myengine.addScene(cube, vertexInput) |
83 | |
84 var t: float32 = cpuTime() | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
141
diff
changeset
|
85 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
86 setShaderGlobal(cube, "model", translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32)) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
87 setShaderGlobal(cube, "projection", |
141 | 88 perspective( |
89 float32(PI / 4), | |
90 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]), | |
91 0.1'f32, | |
92 100'f32 | |
93 ) | |
94 ) | |
95 t = cpuTime() | |
96 | |
97 myengine.renderScene(cube) | |
98 | |
99 myengine.destroy() |