Mercurial > games > semicongine
annotate examples/E01_hello_triangle.nim @ 586:a136e3f43c2c
add: mesh primitives
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 11 Apr 2023 00:58:16 +0700 |
parents | 21c276c0a968 |
children | 512d33d314c4 |
rev | line source |
---|---|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/times |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/strutils |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/enumerate |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import semicongine |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 type |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 # define type of vertex |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 VertexDataA = object |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 position: PositionAttribute[Vec2] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 color: ColorAttribute[Vec3] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 var pipeline: RenderPipeline[VertexDataA, void] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 |
535 | 15 proc globalUpdate(engine: var Engine, t, dt: float32) = |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 discard |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 # vertex data (types must match the above VertexAttributes) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 const |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 triangle_pos = @[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 Vec2([0.0'f32, -0.5'f32]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 Vec2([0.5'f32, 0.5'f32]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 Vec2([-0.5'f32, 0.5'f32]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 ] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 triangle_color = @[ |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 Vec3([1.0'f32, 0.0'f32, 0.0'f32]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 Vec3([0.0'f32, 1.0'f32, 0.0'f32]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 Vec3([0.0'f32, 0.0'f32, 1.0'f32]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 ] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 when isMainModule: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 var myengine = igniteEngine("Hello triangle") |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 # build a mesh |
529 | 35 var trianglemesh = new Mesh[VertexDataA, uint16] |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 trianglemesh.vertexData = VertexDataA( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 position: PositionAttribute[Vec2](data: triangle_pos), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 color: ColorAttribute[Vec3](data: triangle_color), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 # build a single-object scene graph |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 var triangle = newThing("triangle", trianglemesh) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 # upload data, prepare shaders, etc |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 const vertexShader = generateVertexShaderCode[VertexDataA, void]() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
529 | 46 pipeline = setupPipeline[VertexDataA, void, uint16]( |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 myengine, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 triangle, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 vertexShader, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 fragmentShader |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 # show something |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 myengine.run(pipeline, globalUpdate) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 pipeline.trash() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 myengine.trash() |