comparison examples/E03_hello_cube.nim @ 1027:d6c27f0ed3e4

fix: examples not compiling
author sam <sam@basx.dev>
date Wed, 22 May 2024 03:45:16 +0700
parents c66503386e8b
children 71315636ba82
comparison
equal deleted inserted replaced
1026:f7802c5069ce 1027:d6c27f0ed3e4
1 #
2 # TODO: Needs Depth-Buffer first!
3 #
4 #
5 #
6 #
7 #
8 #
9
10
11 import std/tables 1 import std/tables
12 import std/times 2 import std/times
13 3
14 import ../src/semicongine 4 import ../semicongine
15 5
16 const 6 const
17 TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32) 7 TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32)
18 TopRightFront = newVec3f(0.5'f32, -0.5'f32, -0.5'f32) 8 TopRightFront = newVec3f(0.5'f32, -0.5'f32, -0.5'f32)
19 BottomRightFront = newVec3f(0.5'f32, 0.5'f32, -0.5'f32) 9 BottomRightFront = newVec3f(0.5'f32, 0.5'f32, -0.5'f32)
22 TopRightBack = newVec3f(-0.5'f32, -0.5'f32, 0.5'f32) 12 TopRightBack = newVec3f(-0.5'f32, -0.5'f32, 0.5'f32)
23 BottomRightBack = newVec3f(-0.5'f32, 0.5'f32, 0.5'f32) 13 BottomRightBack = newVec3f(-0.5'f32, 0.5'f32, 0.5'f32)
24 BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32) 14 BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32)
25 const 15 const
26 cube_pos = @[ 16 cube_pos = @[
27 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front 17 TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front
28 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back 18 TopLeftBack, TopRightBack, BottomRightBack, BottomLeftBack, # back
29 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left 19 TopLeftBack, TopLeftFront, BottomLeftFront, BottomLeftBack, # left
30 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right 20 TopRightBack, TopRightFront, BottomRightFront, BottomRightBack, # right
31 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top 21 TopLeftBack, TopRightBack, TopRightFront, TopLeftFront, # top
32 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom 22 BottomLeftFront, BottomRightFront, BottomRightBack, BottomLeftBack, # bottom
33 ] 23 ]
34 R = newVec4f(1, 0, 0, 1) 24 R = newVec4f(1, 0, 0, 1)
35 G = newVec4f(0, 1, 0, 1) 25 G = newVec4f(0, 1, 0, 1)
36 B = newVec4f(0, 0, 1, 1) 26 B = newVec4f(0, 0, 1, 1)
52 when isMainModule: 42 when isMainModule:
53 var myengine = initEngine("Hello cube") 43 var myengine = initEngine("Hello cube")
54 44
55 const 45 const
56 shaderConfiguration = createShaderConfiguration( 46 shaderConfiguration = createShaderConfiguration(
57 inputs=[ 47 name = "default shader",
48 inputs = [
58 attr[Vec3f]("position"), 49 attr[Vec3f]("position"),
59 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), 50 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
60 ], 51 ],
61 intermediates=[attr[Vec4f]("outcolor")], 52 intermediates = [attr[Vec4f]("outcolor")],
62 uniforms=[ 53 uniforms = [
63 attr[Mat4]("projection"), 54 attr[Mat4]("projection"),
64 attr[Mat4]("view"), 55 attr[Mat4]("view"),
65 attr[Mat4]("model"), 56 attr[Mat4]("model"),
66 ], 57 ],
67 outputs=[attr[Vec4f]("color")], 58 outputs = [attr[Vec4f]("color")],
68 vertexCode="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""", 59 vertexCode = """outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""",
69 fragmentCode="color = outcolor;", 60 fragmentCode = "color = outcolor;",
70 ) 61 )
71 myengine.initRenderer({"default": shaderConfiguration}.toTable) 62 var matDef = MaterialType(name: "default material", vertexAttributes: {"position": Vec3F32, "color": Vec4F32}.toTable)
72 var cube = Scene(name: "scene", meshes: @[newMesh(positions=cube_pos, indices=tris, colors=cube_color, material=Material(name: "default"))]) 63 var cube = Scene(name: "scene", meshes: @[newMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.initMaterialData(name = "default"))])
73 cube.addShaderGlobal("projection", Unit4f32) 64 cube.addShaderGlobal("projection", Unit4f32)
74 cube.addShaderGlobal("view", Unit4f32) 65 cube.addShaderGlobal("view", Unit4f32)
75 cube.addShaderGlobal("model", Unit4f32) 66 cube.addShaderGlobal("model", Unit4f32)
76 myengine.addScene(cube) 67 myengine.initRenderer({matDef: shaderConfiguration})
68 myengine.loadScene(cube)
77 69
78 var t: float32 = cpuTime() 70 var t: float32 = cpuTime()
79 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): 71 while myengine.UpdateInputs() and not KeyWasPressed(Escape):
80 setShaderGlobal(cube, "model", translate(0'f32, 0'f32, 10'f32) * rotate(t, Yf32)) 72 setShaderGlobal(cube, "model", translate(0'f32, 0'f32, 10'f32) * rotate(t, Yf32))
81 setShaderGlobal(cube, "projection", 73 setShaderGlobal(cube, "projection",
82 perspective( 74 perspective(
83 float32(PI / 4), 75 float32(PI / 4),
84 float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]), 76 float32(myengine.GetWindow().size[0]) / float32(myengine.GetWindow().size[1]),
85 0.1'f32, 77 0.1'f32,
86 100'f32 78 100'f32
87 ) 79 )
88 ) 80 )
89 t = cpuTime() 81 t = cpuTime()