Mercurial > games > semicongine
changeset 146:253dd797e719
add: improvments and E10 (pong)
author | Sam <sam@basx.dev> |
---|---|
date | Thu, 27 Apr 2023 00:23:23 +0700 |
parents | a4e6e76128e6 |
children | 3dffa1f38a33 |
files | examples/E04_input.nim examples/E10_pong.nim src/semicongine/color.nim src/semicongine/gpu_data.nim src/semicongine/mesh.nim src/semicongine/renderer.nim tests/test_vulkan_wrapper.nim |
diffstat | 7 files changed, 151 insertions(+), 74 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/E04_input.nim Wed Apr 26 02:15:43 2023 +0700 +++ b/examples/E04_input.nim Thu Apr 27 00:23:23 2023 +0700 @@ -135,10 +135,6 @@ indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], ) - backgroundmesh.setInstanceData("transform", @[Unit4f32]) - keyboardmesh.setInstanceData("transform", @[Unit4f32]) - cursormesh.setInstanceData("transform", @[Unit4f32]) - # define mesh objects scene = newEntity("scene") scene.add newEntity("background", backgroundmesh)
--- a/examples/E10_pong.nim Wed Apr 26 02:15:43 2023 +0700 +++ b/examples/E10_pong.nim Thu Apr 27 00:23:23 2023 +0700 @@ -1,86 +1,104 @@ +import std/times + import semicongine -const - barcolor = RGBfromHex("5A3F00").gamma(2.2) +let + barcolor = hexToColor("5A3F00").gamma(2.2).colorToHex() barSize = 0.1'f barWidth = 0.01'f - ballcolor = RGBfromHex("B17F08").gamma(2.2) + ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex() levelRatio = 1 ballSize = 0.01'f - backgroundColor = RGBAfromHex("FAC034").gamma(2.2) + backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2) ballSpeed = 60'f var level: Entity ballVelocity = newVec2f(1, 1).normalized * ballSpeed -#[ -proc globalUpdate(engine: var Engine; t, dt: float32) = - var height = float32(engine.vulkan.frameSize.y) / float32( - engine.vulkan.frameSize.x) - var width = 1'f - uniforms.view.value = ortho[float32]( - 0'f, width, - 0'f, height, - 0'f, 1'f, - ) - engine.vulkan.device.updateUniformData(pipeline, uniforms) - var player = level.firstWithName("player") - if Down in engine.input.keysDown and (player.transform.col(3).y + barSize/2) < height: - player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f) - if Up in engine.input.keysDown and (player.transform.col(3).y - barSize/2) > 0: - player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f) - - var ball = level.firstWithName("ball") - ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, - ballVelocity[1] * dt, 0'f) - - # loose - if ball.transform.col(3).x - ballSize/2 <= 0: - ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed - ball.transform[0, 3] = width / 2 - ball.transform[1, 3] = height / 2 - - # bounce level - if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[ - 0] = -ballVelocity[0] - if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] - if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[ - 1] = -ballVelocity[1] - - # bar - if ball.transform.col(3).x - ballSize/2 <= barWidth: - let - barTop = player.transform.col(3).y - barSize/2 - barBottom = player.transform.col(3).y + barSize/2 - ballTop = ball.transform.col(3).y - ballSize/2 - ballBottom = ball.transform.col(3).y + ballSize/2 - if ballTop >= barTop and ballBottom <= barBottom: - ballVelocity[0] = abs(ballVelocity[0]) -]# - when isMainModule: var myengine = initEngine("Pong") level = newEntity("Level") - var playerbarmesh = rect() - playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor] + var playerbarmesh = rect(color=barcolor) var playerbar = newEntity("playerbar", playerbarmesh) playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f) var player = newEntity("player", playerbar) player.transform = translate3d(0'f, 0.3'f, 0'f) level.add player - var ballmesh = circle() - ballmesh.vertexData.color.data = newSeq[Vec3](ballmesh.vertexData.position.data.len) - for i in 0 ..< ballmesh.vertexData.color.data.len: - ballmesh.vertexData.color.data[i] = ballcolor - ballmesh.vertexData.transform.data = @[Unit44] + var ballmesh = circle(color=ballcolor) var ball = newEntity("ball", ballmesh) ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) level.add ball - pipeline.clearColor = backgroundColor - # show something - myengine.run(pipeline, globalUpdate) + const + vertexInput = @[ + attr[Vec3f]("position", memoryLocation=VRAM), + attr[Vec3f]("color", memoryLocation=VRAMVisible), + attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true), + ] + vertexOutput = @[attr[Vec3f]("outcolor")] + uniforms = @[attr[Mat4]("projection")] + fragOutput = @[attr[Vec4f]("color")] + vertexCode = compileGlslShader( + stage=VK_SHADER_STAGE_VERTEX_BIT, + inputs=vertexInput, + uniforms=uniforms, + outputs=vertexOutput, + main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""" + ) + fragmentCode = compileGlslShader( + stage=VK_SHADER_STAGE_FRAGMENT_BIT, + inputs=vertexOutput, + uniforms=uniforms, + outputs=fragOutput, + main="color = vec4(outcolor, 1);" + ) + + # set up rendering + myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor)) + myengine.addScene(level, vertexInput, transformAttribute="transform") + var projection = initShaderGlobal("projection", Unit4f32) + level.add projection - myengine.destroy() + var + winsize = myengine.getWindow().size + height = float32(winsize[1]) / float32(winsize[0]) + width = 1'f + currentTime = cpuTime() + while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape): + let dt: float32 = cpuTime() - currentTime + currentTime = cpuTime() + if myengine.windowWasResized(): + winsize = myengine.getWindow().size + height = float32(winsize[1]) / float32(winsize[0]) + width = 1'f + setValue[Mat4](projection.value, ortho[float32](0'f, width, 0'f, height, 0'f, 1'f)) + var player = level.firstWithName("player") + if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: + player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f) + if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: + player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f) + + # bounce level + if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] + if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] + if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] + + ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) + + # loose + if ball.transform.col(3).x - ballSize/2 <= 0: + break + + # bar + if ball.transform.col(3).x - ballSize/2 <= barWidth: + let + barTop = player.transform.col(3).y - barSize/2 + barBottom = player.transform.col(3).y + barSize/2 + ballTop = ball.transform.col(3).y - ballSize/2 + ballBottom = ball.transform.col(3).y + ballSize/2 + if ballTop >= barTop and ballBottom <= barBottom: + ballVelocity[0] = abs(ballVelocity[0]) + + myengine.renderScene(level)
--- a/src/semicongine/color.nim Wed Apr 26 02:15:43 2023 +0700 +++ b/src/semicongine/color.nim Thu Apr 27 00:23:23 2023 +0700 @@ -1,8 +1,9 @@ import std/parseutils +import std/strformat import ./math/vector -func RGBfromHex*(value: string): Vec3f = +func hexToColor*(value: string): Vec3f = assert value != "" var hex = value if hex[0] == '#': @@ -17,7 +18,13 @@ discard parseHex(hex[4 .. 5], b) == 2 return Vec3f([float32(r), float32(g), float32(b)]) / 255'f -func RGBAfromHex*(value: string): Vec4f = +func colorToHex*(color: Vec3f): string = + &"{int(color.r * 255):02X}{int(color.g * 255):02X}{int(color.b * 255):02X}" + +func colorToHex*(color: Vec4f): string = + &"{int(color.r * 255):02X}{int(color.g * 255):02X}{int(color.b * 255):02X}{int(color.a * 255):02X}" + +func hexToColorAlpha*(value: string): Vec4f = assert value != "" var hex = value if hex[0] == '#':
--- a/src/semicongine/gpu_data.nim Wed Apr 26 02:15:43 2023 +0700 +++ b/src/semicongine/gpu_data.nim Thu Apr 27 00:23:23 2023 +0700 @@ -137,7 +137,7 @@ of Mat34F64: mat34f64: seq[TMat34[float64]] of Mat43F32: mat43f32: seq[TMat43[float32]] of Mat43F64: mat43f64: seq[TMat43[float64]] - of Mat4F32: mat4f32: seq[TMat4[float32]] + of Mat4F32: mat4f32*: seq[TMat4[float32]] of Mat4F64: mat4f64: seq[TMat4[float64]] MemoryLocation* = enum VRAM, VRAMVisible, RAM # VRAM is fastest, VRAMVisible allows updating memory directly, may be slower @@ -481,6 +481,52 @@ of Mat4F32: result[0] = addr value.mat4f32[0] of Mat4F64: result[0] = addr value.mat4f64[0] +func initData*(value: var DataList, len: uint32) = + value.len = len + case value.thetype + of Float32: value.float32.setLen(len) + of Float64: value.float64.setLen(len) + of Int8: value.int8.setLen(len) + of Int16: value.int16.setLen(len) + of Int32: value.int32.setLen(len) + of Int64: value.int64.setLen(len) + of UInt8: value.uint8.setLen(len) + of UInt16: value.uint16.setLen(len) + of UInt32: value.uint32.setLen(len) + of UInt64: value.uint64.setLen(len) + of Vec2I32: value.vec2i32.setLen(len) + of Vec2I64: value.vec2i64.setLen(len) + of Vec3I32: value.vec3i32.setLen(len) + of Vec3I64: value.vec3i64.setLen(len) + of Vec4I32: value.vec4i32.setLen(len) + of Vec4I64: value.vec4i64.setLen(len) + of Vec2U32: value.vec2u32.setLen(len) + of Vec2U64: value.vec2u64.setLen(len) + of Vec3U32: value.vec3u32.setLen(len) + of Vec3U64: value.vec3u64.setLen(len) + of Vec4U32: value.vec4u32.setLen(len) + of Vec4U64: value.vec4u64.setLen(len) + of Vec2F32: value.vec2f32.setLen(len) + of Vec2F64: value.vec2f64.setLen(len) + of Vec3F32: value.vec3f32.setLen(len) + of Vec3F64: value.vec3f64.setLen(len) + of Vec4F32: value.vec4f32.setLen(len) + of Vec4F64: value.vec4f64.setLen(len) + of Mat2F32: value.mat2f32.setLen(len) + of Mat2F64: value.mat2f64.setLen(len) + of Mat23F32: value.mat23f32.setLen(len) + of Mat23F64: value.mat23f64.setLen(len) + of Mat32F32: value.mat32f32.setLen(len) + of Mat32F64: value.mat32f64.setLen(len) + of Mat3F32: value.mat3f32.setLen(len) + of Mat3F64: value.mat3f64.setLen(len) + of Mat34F32: value.mat34f32.setLen(len) + of Mat34F64: value.mat34f64.setLen(len) + of Mat43F32: value.mat43f32.setLen(len) + of Mat43F64: value.mat43f64.setLen(len) + of Mat4F32: value.mat4f32.setLen(len) + of Mat4F64: value.mat4f64.setLen(len) + func setValue*[T: GPUType|int|uint|float](value: var DataValue, data: T) = when T is float32: value.float32 = data elif T is float64: value.float64 = data @@ -581,6 +627,7 @@ elif T is TMat43[float64]: value.mat43f64 = data elif T is TMat4[float32]: value.mat4f32 = data elif T is TMat4[float64]: value.mat4f64 = data + func setValue*[T: GPUType|int|uint|float](value: var DataList, i: uint32, data: T) = assert i < value.len when T is float32: value.float32[i] = data
--- a/src/semicongine/mesh.nim Wed Apr 26 02:15:43 2023 +0700 +++ b/src/semicongine/mesh.nim Thu Apr 27 00:23:23 2023 +0700 @@ -120,6 +120,15 @@ assert attribute in mesh.data get[T](mesh.data[attribute]) +proc initData*(mesh: var Mesh, attribute: ShaderAttribute) = + assert not (attribute.name in mesh.data) + mesh.data[attribute.name] = DataList(thetype: attribute.thetype) + echo "Init ", attribute, " of ", mesh + if attribute.perInstance: + mesh.data[attribute.name].initData(mesh.instanceCount) + else: + mesh.data[attribute.name].initData(mesh.vertexCount) + proc setMeshData*[T: GPUType|int|uint|float](mesh: var Mesh, attribute: string, data: seq[T]) = assert not (attribute in mesh.data) mesh.data[attribute] = DataList(thetype: getDataType[T]()) @@ -161,7 +170,7 @@ let half_w = width / 2 half_h = height / 2 - c = RGBfromHex(color) + c = hexToColor(color) v = [newVec3f(-half_w, -half_h), newVec3f( half_w, -half_h), newVec3f( half_w, half_h), newVec3f(-half_w, half_h)] setValues(result.data["position"], v.toSeq) @@ -176,7 +185,7 @@ let half_w = width / 2 half_h = height / 2 - colorVec = RGBfromHex(color) + colorVec = hexToColor(color) setValues(result.data["position"], @[ newVec3f(0, -half_h), newVec3f( half_w, half_h), newVec3f(-half_w, half_h), ]) @@ -193,7 +202,7 @@ let half_w = width / 2 half_h = height / 2 - c = RGBfromHex(color) + c = hexToColor(color) step = (2'f32 * PI) / float32(nSegments) var pos = @[newVec3f(0, 0), newVec3f(0, half_h)]
--- a/src/semicongine/renderer.nim Wed Apr 26 02:15:43 2023 +0700 +++ b/src/semicongine/renderer.nim Thu Apr 27 00:23:23 2023 +0700 @@ -66,7 +66,8 @@ for mesh in allComponentsOfType[Mesh](scene): allMeshes.add mesh for inputAttr in inputs: - assert mesh.hasDataFor(inputAttr.name), &"{mesh} missing data for {inputAttr}" + if not mesh.hasDataFor(inputAttr.name): + mesh.initData(inputAttr) var indicesBufferSize = 0'u64 for mesh in allMeshes:
--- a/tests/test_vulkan_wrapper.nim Wed Apr 26 02:15:43 2023 +0700 +++ b/tests/test_vulkan_wrapper.nim Thu Apr 27 00:23:23 2023 +0700 @@ -147,11 +147,10 @@ for i in 0 ..< 3: for scene in scenes: for i in 0 ..< 1000: - engine.updateInputs() - setValue[float32](time.value, get[float32](time.value) + 0.0005) - if not engine.running or engine.keyIsDown(Escape): + if engine.updateInputs() != Running or engine.keyIsDown(Escape): engine.destroy() return + setValue[float32](time.value, get[float32](time.value) + 0.0005) engine.renderScene(scene) echo "Rendered ", engine.framesRendered, " frames" echo "Processed ", engine.eventsProcessed, " events"