Mercurial > games > semicongine
annotate examples/E03_hello_cube.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 |
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 |
271 | 13 import ../src/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 ] |
210 | 33 R = newVec4f(1, 0, 0, 1) |
34 G = newVec4f(0, 1, 0, 1) | |
35 B = newVec4f(0, 0, 1, 1) | |
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
|
36 cube_color = @[ |
210 | 37 R, R, R, R, |
38 R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, | |
39 G, G, G, G, | |
40 G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, | |
41 B, B, B, B, | |
42 B * 0.5'f32, B * 0.5'f32, B * 0.5'f32, B * 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
|
43 ] |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 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
|
45 tris: seq[array[3, uint16]] |
39 | 46 for i in 0'u16 ..< 6'u16: |
47 let off = i * 4 | |
48 tris.add [off + 0'u16, off + 1'u16, off + 2'u16] | |
49 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
|
50 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 when isMainModule: |
141 | 52 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
|
53 |
141 | 54 const |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
55 inputs = @[ |
163 | 56 attr[Vec3f]("position"), |
210 | 57 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
141 | 58 ] |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
59 intermediate = @[attr[Vec4f]("outcolor")] |
141 | 60 uniforms = @[ |
61 attr[Mat4]("projection"), | |
62 attr[Mat4]("view"), | |
63 attr[Mat4]("model"), | |
64 ] | |
65 fragOutput = @[attr[Vec4f]("color")] | |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
66 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( |
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
67 inputs=inputs, |
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
68 intermediate=intermediate, |
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
69 outputs=fragOutput, |
141 | 70 uniforms=uniforms, |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
71 vertexCode="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""", |
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
72 fragmentCode="color = outcolor;", |
141 | 73 ) |
74 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) | |
302 | 75 var cube = newScene("scene", newEntity("cube", {"mesh": Component(newMesh(positions=cube_pos, indices=tris, colors=cube_color))})) |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
76 cube.addShaderGlobal("projection", Unit4f32) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
77 cube.addShaderGlobal("view", Unit4f32) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
78 cube.addShaderGlobal("model", Unit4f32) |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
79 myengine.addScene(cube, inputs, @[], transformAttribute="") |
141 | 80 |
81 var t: float32 = cpuTime() | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
141
diff
changeset
|
82 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
|
83 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
|
84 setShaderGlobal(cube, "projection", |
141 | 85 perspective( |
86 float32(PI / 4), | |
87 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]), | |
88 0.1'f32, | |
89 100'f32 | |
90 ) | |
91 ) | |
92 t = cpuTime() | |
93 | |
94 myengine.renderScene(cube) | |
95 | |
96 myengine.destroy() |