Mercurial > games > semicongine
annotate examples/E03_hello_cube.nim @ 1139:114f395b9144
did: finish refactoring and updated all tests accordingly
author | sam <sam@basx.dev> |
---|---|
date | Sat, 08 Jun 2024 14:58:25 +0700 |
parents | 71315636ba82 |
children | 5934c5615f13 |
rev | line source |
---|---|
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
1 import std/tables |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/times |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 |
1027 | 4 import ../semicongine |
522
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 const |
1136 | 7 TopLeftFront = NewVec3f(-0.5'f32, -0.5'f32, -0.5'f32) |
8 TopRightFront = NewVec3f(0.5'f32, -0.5'f32, -0.5'f32) | |
9 BottomRightFront = NewVec3f(0.5'f32, 0.5'f32, -0.5'f32) | |
10 BottomLeftFront = NewVec3f(-0.5'f32, 0.5'f32, -0.5'f32) | |
11 TopLeftBack = NewVec3f(0.5'f32, -0.5'f32, 0.5'f32) | |
12 TopRightBack = NewVec3f(-0.5'f32, -0.5'f32, 0.5'f32) | |
13 BottomRightBack = NewVec3f(-0.5'f32, 0.5'f32, 0.5'f32) | |
14 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
|
15 const |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 cube_pos = @[ |
1027 | 17 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front |
18 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back | |
19 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left | |
20 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right | |
21 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 ] |
1136 | 24 R = NewVec4f(1, 0, 0, 1) |
25 G = NewVec4f(0, 1, 0, 1) | |
26 B = NewVec4f(0, 0, 1, 1) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 cube_color = @[ |
671 | 28 R, R, R, R, |
29 R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, R * 0.5'f32, | |
30 G, G, G, G, | |
31 G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, G * 0.5'f32, | |
32 B, B, B, B, | |
33 B * 0.5'f32, B * 0.5'f32, B * 0.5'f32, B * 0.5'f32, | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 ] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 var |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 tris: seq[array[3, uint16]] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 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
|
38 let off = i * 4 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 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
|
40 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
|
41 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 when isMainModule: |
602 | 43 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
|
44 |
602 | 45 const |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
46 shaderConfiguration = createShaderConfiguration( |
1027 | 47 name = "default shader", |
48 inputs = [ | |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
49 attr[Vec3f]("position"), |
1027 | 50 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
51 ], |
1027 | 52 intermediates = [attr[Vec4f]("outcolor")], |
53 uniforms = [ | |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
54 attr[Mat4]("projection"), |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
55 attr[Mat4]("view"), |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
56 attr[Mat4]("model"), |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
57 ], |
1027 | 58 outputs = [attr[Vec4f]("color")], |
59 vertexCode = """outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""", | |
60 fragmentCode = "color = outcolor;", | |
602 | 61 ) |
1027 | 62 var matDef = MaterialType(name: "default material", vertexAttributes: {"position": Vec3F32, "color": Vec4F32}.toTable) |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
63 var cube = Scene(name: "scene", meshes: @[newMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.InitMaterialData(name = "default"))]) |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
64 cube.addShaderGlobal("projection", Unit4f32) |
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
65 cube.addShaderGlobal("view", Unit4f32) |
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
66 cube.addShaderGlobal("model", Unit4f32) |
1027 | 67 myengine.initRenderer({matDef: shaderConfiguration}) |
68 myengine.loadScene(cube) | |
602 | 69 |
70 var t: float32 = cpuTime() | |
1027 | 71 while myengine.UpdateInputs() and not KeyWasPressed(Escape): |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
72 setShaderGlobal(cube, "model", translate(0'f32, 0'f32, 10'f32) * rotate(t, Yf32)) |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
73 setShaderGlobal(cube, "projection", |
602 | 74 perspective( |
75 float32(PI / 4), | |
1027 | 76 float32(myengine.GetWindow().size[0]) / float32(myengine.GetWindow().size[1]), |
602 | 77 0.1'f32, |
78 100'f32 | |
79 ) | |
80 ) | |
81 t = cpuTime() | |
82 myengine.renderScene(cube) | |
83 | |
84 myengine.destroy() |