Mercurial > games > semicongine
comparison tests/test_vulkan_wrapper.nim @ 125:6e2c48cb6f60
add: mesh primitives
| author | Sam <sam@basx.dev> |
|---|---|
| date | Tue, 11 Apr 2023 00:58:16 +0700 |
| parents | cb9e27a30165 |
| children | 81a8e62215db |
comparison
equal
deleted
inserted
replaced
| 124:cb9e27a30165 | 125:6e2c48cb6f60 |
|---|---|
| 87 ) | 87 ) |
| 88 var mymesh4 = newMesh( | 88 var mymesh4 = newMesh( |
| 89 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)], | 89 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)], |
| 90 colors=[newVec3f(0.0, 0.0, 1.0), newVec3f(0.0, 0.0, 1.0), newVec3f(0.0, 0.0, 1.0)], | 90 colors=[newVec3f(0.0, 0.0, 1.0), newVec3f(0.0, 0.0, 1.0), newVec3f(0.0, 0.0, 1.0)], |
| 91 indices=[[0'u16, 1'u16, 2'u16]], | 91 indices=[[0'u16, 1'u16, 2'u16]], |
| 92 ) | 92 instanceCount=2 |
| 93 setMeshData[Vec3f](mymesh1, "translate", @[newVec3f(0.3, 0.3)]) | 93 ) |
| 94 setMeshData[Vec3f](mymesh1, "translate", @[newVec3f(0.3, 0.0)]) | |
| 95 setMeshData[Vec3f](mymesh2, "translate", @[newVec3f(0.0, 0.3)]) | |
| 96 setMeshData[Vec3f](mymesh3, "translate", @[newVec3f(-0.3, 0.0)]) | |
| 97 setMeshData[Vec3f](mymesh4, "translate", @[newVec3f(0.0, -0.3), newVec3f(0.0, 0.5)]) | |
| 94 result = Scene( | 98 result = Scene( |
| 95 name: "main", | 99 name: "main", |
| 96 root: newEntity("root", newEntity("triangle", mymesh4, mymesh3, mymesh2, mymesh1),) | 100 root: newEntity("root", newEntity("triangle", mymesh4, mymesh3, mymesh2, mymesh1)) |
| 97 ) | 101 ) |
| 98 | 102 |
| 103 proc scene_primitives(): Scene = | |
| 104 var r = rect(color="ff0000") | |
| 105 var t = tri(color="0000ff") | |
| 106 var c = circle(color="00ff00") | |
| 107 setMeshData[Vec3f](r, "translate", @[newVec3f(0.5, -0.3)]) | |
| 108 setMeshData[Vec3f](t, "translate", @[newVec3f(0.3, 0.3)]) | |
| 109 setMeshData[Vec3f](c, "translate", @[newVec3f(-0.3, 0.1)]) | |
| 110 result = Scene( | |
| 111 name: "main", | |
| 112 root: newEntity("root", t, r, c) | |
| 113 ) | |
| 99 | 114 |
| 100 when isMainModule: | 115 when isMainModule: |
| 101 # INIT ENGINE: | 116 # INIT ENGINE: |
| 102 # create instance | 117 # create instance |
| 103 var thewindow = createWindow("Test") | 118 var thewindow = createWindow("Test") |
| 119 # INIT RENDERER: | 134 # INIT RENDERER: |
| 120 const | 135 const |
| 121 vertexInput = @[ | 136 vertexInput = @[ |
| 122 attr[Vec3f]("position", memoryLocation=VRAM), | 137 attr[Vec3f]("position", memoryLocation=VRAM), |
| 123 attr[Vec3f]("color", memoryLocation=VRAM), | 138 attr[Vec3f]("color", memoryLocation=VRAM), |
| 124 # attr[Vec3f]("translate", perInstance=true) | 139 attr[Vec3f]("translate", perInstance=true) |
| 125 ] | 140 ] |
| 126 vertexOutput = @[attr[Vec3f]("outcolor")] | 141 vertexOutput = @[attr[Vec3f]("outcolor")] |
| 127 uniforms = @[attr[float32]("time")] | 142 uniforms = @[attr[float32]("time")] |
| 128 fragOutput = @[attr[Vec4f]("color")] | 143 fragOutput = @[attr[Vec4f]("color")] |
| 129 vertexCode = compileGlslShader( | 144 vertexCode = compileGlslShader( |
| 130 stage=VK_SHADER_STAGE_VERTEX_BIT, | 145 stage=VK_SHADER_STAGE_VERTEX_BIT, |
| 131 inputs=vertexInput, | 146 inputs=vertexInput, |
| 132 uniforms=uniforms, | 147 uniforms=uniforms, |
| 133 outputs=vertexOutput, | 148 outputs=vertexOutput, |
| 134 body="""gl_Position = vec4(position, 1.0); outcolor = color * sin(Uniforms.time) * 0.5 + 0.5;""" | 149 body="""gl_Position = vec4(position + translate, 1.0); outcolor = color;""" |
| 135 ) | 150 ) |
| 136 fragmentCode = compileGlslShader( | 151 fragmentCode = compileGlslShader( |
| 137 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | 152 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
| 138 inputs=vertexOutput, | 153 inputs=vertexOutput, |
| 139 uniforms=uniforms, | 154 uniforms=uniforms, |
| 150 raise newException(Exception, "Unable to create swapchain") | 165 raise newException(Exception, "Unable to create swapchain") |
| 151 | 166 |
| 152 # INIT SCENE | 167 # INIT SCENE |
| 153 var time = initShaderGlobal("time", 0.0'f32) | 168 var time = initShaderGlobal("time", 0.0'f32) |
| 154 | 169 |
| 155 var thescene = scene_simple() | 170 # var thescene = scene_simple() |
| 171 # var thescene = scene_different_mesh_types() | |
| 172 var thescene = scene_primitives() | |
| 156 thescene.root.components.add time | 173 thescene.root.components.add time |
| 157 thescene.setupDrawables(renderPass) | 174 thescene.setupDrawables(renderPass) |
| 158 swapchain.setupUniforms(thescene) | 175 swapchain.setupUniforms(thescene) |
| 159 | 176 |
| 160 # MAINLOOP | 177 # MAINLOOP |
