Mercurial > games > semicongine
annotate examples/E03_hello_cube.nim @ 1174:26e367076c15 compiletime-tests
de: test2
author | sam <sam@basx.dev> |
---|---|
date | Sat, 29 Jun 2024 11:12:48 +0700 |
parents | d3e014c3551c |
children |
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: |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
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 |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
46 shaderConfiguration = CreateShaderConfiguration( |
1027 | 47 name = "default shader", |
48 inputs = [ | |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
49 Attr[Vec3f]("position"), |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
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 ], |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
52 intermediates = [Attr[Vec4f]("outcolor")], |
1027 | 53 uniforms = [ |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
54 Attr[Mat4]("projection"), |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
55 Attr[Mat4]("view"), |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
56 Attr[Mat4]("model"), |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
57 ], |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
58 outputs = [Attr[Vec4f]("color")], |
1141 | 59 vertexCode = """ |
60 outcolor = color; | |
61 // gl_Position = vec4(position, 1) * (Uniforms.model * Uniforms.projection * Uniforms.view); | |
62 gl_Position = vec4(position, 1) * (Uniforms.model * Uniforms.projection * Uniforms.view); | |
63 """, | |
1027 | 64 fragmentCode = "color = outcolor;", |
602 | 65 ) |
1027 | 66 var matDef = MaterialType(name: "default material", vertexAttributes: {"position": Vec3F32, "color": Vec4F32}.toTable) |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
67 var cube = Scene(name: "scene", meshes: @[NewMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.InitMaterialData(name = "default"))]) |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
68 cube.AddShaderGlobal("projection", Unit4f32) |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
69 cube.AddShaderGlobal("view", Unit4f32) |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
70 cube.AddShaderGlobal("model", Unit4f32) |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
71 myengine.InitRenderer({matDef: shaderConfiguration}) |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
72 myengine.LoadScene(cube) |
602 | 73 |
74 var t: float32 = cpuTime() | |
1027 | 75 while myengine.UpdateInputs() and not KeyWasPressed(Escape): |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
76 SetShaderGlobal(cube, "model", Translate(0'f32, 0'f32, 10'f32) * Rotate(t, Yf32)) |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
77 SetShaderGlobal(cube, "projection", |
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
78 Perspective( |
602 | 79 float32(PI / 4), |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
80 float32(myengine.GetWindow().Size[0]) / float32(myengine.GetWindow().Size[1]), |
602 | 81 0.1'f32, |
82 100'f32 | |
83 ) | |
84 ) | |
85 t = cpuTime() | |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
86 myengine.RenderScene(cube) |
602 | 87 |
1140
5934c5615f13
did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
88 myengine.Destroy() |