Mercurial > games > semicongine
annotate examples/E03_hello_cube.nim @ 364:eef6cc3e1104
add anykey-pressed api, extend default charset when loading fonts
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 02 Oct 2023 10:15:09 +0700 |
parents | 887ddc8d45fd |
children | c66503386e8b |
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 |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
11 import std/tables |
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
|
12 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
|
13 |
271 | 14 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
|
15 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 const |
141 | 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) | |
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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 ] |
210 | 34 R = newVec4f(1, 0, 0, 1) |
35 G = newVec4f(0, 1, 0, 1) | |
36 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
|
37 cube_color = @[ |
210 | 38 R, R, R, R, |
39 R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, | |
40 G, G, G, G, | |
41 G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, | |
42 B, B, B, B, | |
43 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
|
44 ] |
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 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
|
46 tris: seq[array[3, uint16]] |
39 | 47 for i in 0'u16 ..< 6'u16: |
48 let off = i * 4 | |
49 tris.add [off + 0'u16, off + 1'u16, off + 2'u16] | |
50 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
|
51 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 when isMainModule: |
141 | 53 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
|
54 |
141 | 55 const |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
56 shaderConfiguration = createShaderConfiguration( |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
57 inputs=[ |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
58 attr[Vec3f]("position"), |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
59 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
60 ], |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
61 intermediates=[attr[Vec4f]("outcolor")], |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
62 uniforms=[ |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
63 attr[Mat4]("projection"), |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
64 attr[Mat4]("view"), |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
65 attr[Mat4]("model"), |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
66 ], |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
67 outputs=[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
|
68 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
|
69 fragmentCode="color = outcolor;", |
141 | 70 ) |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
71 myengine.initRenderer({"default": shaderConfiguration}.toTable) |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
72 var cube = Scene(name: "scene", meshes: @[newMesh(positions=cube_pos, indices=tris, colors=cube_color, material=Material(name: "default"))]) |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
73 cube.addShaderGlobal("projection", Unit4f32) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
74 cube.addShaderGlobal("view", Unit4f32) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
75 cube.addShaderGlobal("model", Unit4f32) |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
76 myengine.addScene(cube) |
141 | 77 |
78 var t: float32 = cpuTime() | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
141
diff
changeset
|
79 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
80 setShaderGlobal(cube, "model", translate(0'f32, 0'f32, 10'f32) * rotate(t, Yf32)) |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
81 setShaderGlobal(cube, "projection", |
141 | 82 perspective( |
83 float32(PI / 4), | |
84 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]), | |
85 0.1'f32, | |
86 100'f32 | |
87 ) | |
88 ) | |
89 t = cpuTime() | |
90 myengine.renderScene(cube) | |
91 | |
92 myengine.destroy() |