Mercurial > games > semicongine
diff examples/E03_hello_cube.nim @ 602:0d8858aa0671
did: migrate to new engine version
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 22 Apr 2023 17:34:59 +0700 |
parents | 21c276c0a968 |
children | f41c1b78cf5b |
line wrap: on
line diff
--- a/examples/E03_hello_cube.nim Sat Apr 22 17:34:42 2023 +0700 +++ b/examples/E03_hello_cube.nim Sat Apr 22 17:34:59 2023 +0700 @@ -13,48 +13,15 @@ import semicongine -type - # define type of vertex - VertexDataA = object - position: PositionAttribute[Vec3] - color: ColorAttribute[Vec3] - Uniforms = object - model: Descriptor[Mat44] - view: Descriptor[Mat44] - projection: Descriptor[Mat44] - -var - pipeline: RenderPipeline[VertexDataA, Uniforms] - uniforms: Uniforms - - -proc globalUpdate(engine: var Engine, t, dt: float32) = - let ratio = float32(engine.vulkan.frameSize.y) / float32( - engine.vulkan.frameSize.x) - uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, - Yf32) # * rotate3d(float32(PI), Yf32) - - uniforms.view.value = Unit44f32 - uniforms.projection.value = Mat44(data: [ - ratio, 0'f32, 0'f32, 0'f32, - 0'f32, 1'f32, 0'f32, 0'f32, - 0'f32, 0'f32, 1'f32, 0'f32, - 0'f32, 0'f32, 0'f32, 1'f32, - ]) - uniforms.projection.value = perspective(float32(PI / 4), float32( - engine.vulkan.frameSize.x) / float32( - engine.vulkan.frameSize.y), 0.1'f32, 100'f32) - engine.vulkan.device.updateUniformData(pipeline, uniforms) - const - TopLeftFront = Vec3([-0.5'f32, -0.5'f32, -0.5'f32]) - TopRightFront = Vec3([0.5'f32, -0.5'f32, -0.5'f32]) - BottomRightFront = Vec3([0.5'f32, 0.5'f32, -0.5'f32]) - BottomLeftFront = Vec3([-0.5'f32, 0.5'f32, -0.5'f32]) - TopLeftBack = Vec3([0.5'f32, -0.5'f32, 0.5'f32]) - TopRightBack = Vec3([-0.5'f32, -0.5'f32, 0.5'f32]) - BottomRightBack = Vec3([-0.5'f32, 0.5'f32, 0.5'f32]) - BottomLeftBack = Vec3([0.5'f32, 0.5'f32, 0.5'f32]) + TopLeftFront = newVec3f(-0.5'f32, -0.5'f32, -0.5'f32) + TopRightFront = newVec3f(0.5'f32, -0.5'f32, -0.5'f32) + BottomRightFront = newVec3f(0.5'f32, 0.5'f32, -0.5'f32) + BottomLeftFront = newVec3f(-0.5'f32, 0.5'f32, -0.5'f32) + TopLeftBack = newVec3f(0.5'f32, -0.5'f32, 0.5'f32) + TopRightBack = newVec3f(-0.5'f32, -0.5'f32, 0.5'f32) + BottomRightBack = newVec3f(-0.5'f32, 0.5'f32, 0.5'f32) + BottomLeftBack = newVec3f(0.5'f32, 0.5'f32, 0.5'f32) const cube_pos = @[ TopLeftFront, TopRightFront, BottomRightFront, BottomLeftFront, # front @@ -80,30 +47,60 @@ tris.add [off + 2'u16, off + 3'u16, off + 0'u16] when isMainModule: - var myengine = igniteEngine("Hello cube") - - # build a mesh - var trianglemesh = new Mesh[VertexDataA, uint16] - trianglemesh.indexed = true - trianglemesh.vertexData = VertexDataA( - position: PositionAttribute[Vec3](data: cube_pos), - color: ColorAttribute[Vec3](data: cube_color), - ) - trianglemesh.indices = tris - var cube = newThing("cube", trianglemesh) + var myengine = initEngine("Hello cube") - # upload data, prepare shaders, etc - const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" - out_position = (uniforms.projection * uniforms.view * uniforms.model) * vec4(in_position, 1); - """) - const fragmentShader = generateFragmentShaderCode[VertexDataA]() - pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( - myengine, - cube, - vertexShader, - fragmentShader - ) - # show something - myengine.run(pipeline, globalUpdate) - pipeline.trash() - myengine.trash() + const + vertexInput = @[ + attr[Vec3f]("position", memoryLocation=VRAM), + attr[Vec3f]("color", memoryLocation=VRAMVisible), + ] + vertexOutput = @[attr[Vec3f]("outcolor")] + uniforms = @[ + attr[Mat4]("projection"), + attr[Mat4]("view"), + attr[Mat4]("model"), + ] + fragOutput = @[attr[Vec4f]("color")] + vertexCode = compileGlslShader( + stage=VK_SHADER_STAGE_VERTEX_BIT, + inputs=vertexInput, + uniforms=uniforms, + outputs=vertexOutput, + main="""outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""" + ) + fragmentCode = compileGlslShader( + stage=VK_SHADER_STAGE_FRAGMENT_BIT, + inputs=vertexOutput, + uniforms=uniforms, + outputs=fragOutput, + main="color = vec4(outcolor, 1);" + ) + myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) + var + projection = initShaderGlobal("projection", Unit4f32) + view = initShaderGlobal("view", Unit4f32) + model = initShaderGlobal("model", Unit4f32) + cube = newEntity("cube", newMesh(positions=cube_pos, indices=tris, colors=cube_color)) + cube.components.add projection + cube.components.add view + cube.components.add model + myengine.addScene(cube, vertexInput) + + var t: float32 = cpuTime() + while myengine.running and not myengine.keyWasPressed(Escape): + myengine.updateInputs() + setValue[Mat4](model.value, translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t, Yf32)) + setValue[Mat4]( + projection.value, + perspective( + float32(PI / 4), + float32(myengine.getWindow().size[0]) / float32(myengine.getWindow().size[0]), + 0.1'f32, + 100'f32 + ) + ) + t = cpuTime() + + myengine.renderScene(cube) + + myengine.destroy()