# HG changeset patch # User Sam # Date 1685440741 -25200 # Node ID 07c755e65a4a2b966dc07c0f339e09762d615d82 # Parent dcc12ab20a919ea3c5540c2ffabd1d996428c82c add: final font-rendering, API changes fixed diff -r dcc12ab20a91 -r 07c755e65a4a tests/test_font.nim --- a/tests/test_font.nim Tue May 30 16:58:35 2023 +0700 +++ b/tests/test_font.nim Tue May 30 16:59:01 2023 +0700 @@ -1,51 +1,67 @@ +import std/unicode +import std/tables + import semicongine proc main() = - var scene = newScene("main", root=newEntity("rect", rect())) - var font = loadFont("DejaVuSans.ttf", "myfont") - var img = loadImage("flag.png") - - var textbox = newTextbox(10, 10, font, "Joni") - scene.root.add textbox - var sampler = DefaultSampler() sampler.magnification = VK_FILTER_NEAREST sampler.minification = VK_FILTER_NEAREST - scene.addTextures("my_texture", @[ - Texture(image: img, sampler: sampler) - ]) - scene.addShaderGlobalArray("test2", @[1'f32, 2'f32]) + var font = loadFont("DejaVuSans.ttf", color=newVec4f(1, 0.5, 0.5, 1), resolution=20) + + var scene = newScene("main", root=newEntity("rect")) - var engine = initEngine("Test fonts") + var flag = rect() + flag.setInstanceData("material", @[0'u8]) + # scene.root.add flag + scene.addMaterial Material(name: "material", textures: {"textures": Texture(image: loadImage("flag.png"), sampler: sampler)}.toTable) + + var textbox = newTextbox(32, font, "".toRunes) + scene.addMaterial Material(name: "fontMaterial", textures: {"textures": font.fontAtlas}.toTable) + textbox.mesh.setInstanceData("material", @[1'u8]) + textbox.transform = scale3d(0.1, 0.1) + scene.root.add textbox const vertexInput = @[ + attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true), attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), + attr[uint8]("material", memoryPerformanceHint=PreferFastRead, perInstance=true), ] - vertexOutput = @[attr[Vec2f]("uvout")] - uniforms = @[attr[float32]("test2", arrayCount=2)] - samplers = @[attr[Sampler2DType]("my_texture", arrayCount=1)] + intermediate = @[attr[Vec2f]("uvout"), attr[uint8]("materialId", noInterpolation=true)] + samplers = @[attr[Sampler2DType]("textures", arrayCount=2)] + uniforms = @[attr[Mat4]("perspective")] fragOutput = @[attr[Vec4f]("color")] - vertexCode = compileGlslShader( - stage=VK_SHADER_STAGE_VERTEX_BIT, + (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( inputs=vertexInput, - uniforms=uniforms, + intermediate=intermediate, + outputs=fragOutput, samplers=samplers, - outputs=vertexOutput, - main="""gl_Position = vec4(position, 1.0); uvout = uv;""" + uniforms=uniforms, + vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); uvout = uv; materialId = material;""", + fragmentCode="""color = texture(textures[materialId], uvout);""", ) - fragmentCode = compileGlslShader( - stage=VK_SHADER_STAGE_FRAGMENT_BIT, - inputs=vertexOutput, - uniforms=uniforms, - samplers=samplers, - outputs=fragOutput, - main="""color = texture(my_texture[0], uvout);""" - ) + + var engine = initEngine("Test fonts") engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) engine.addScene(scene, vertexInput, samplers) + scene.addShaderGlobal("perspective", Unit4F32) + while engine.updateInputs() == Running and not engine.keyIsDown(Escape): + if engine.windowWasResized(): + var winSize = engine.getWindow().size + scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0])) + for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S, Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]: + if engine.keyWasPressed(c): + if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR): + textbox.text = textbox.text & ($c).toRunes + else: + textbox.text = textbox.text & ($c).toRunes[0].toLower() + if engine.keyWasPressed(Space): + textbox.text = textbox.text & " ".toRunes[0] + if engine.keyWasPressed(Backspace) and textbox.text.len > 0: + textbox.text = textbox.text[0 ..< ^1] engine.renderScene(scene) engine.destroy() diff -r dcc12ab20a91 -r 07c755e65a4a tests/test_materials.nim --- a/tests/test_materials.nim Tue May 30 16:58:35 2023 +0700 +++ b/tests/test_materials.nim Tue May 30 16:59:01 2023 +0700 @@ -1,4 +1,5 @@ import std/times +import std/tables import semicongine @@ -21,7 +22,8 @@ var sampler = DefaultSampler() sampler.magnification = VK_FILTER_NEAREST sampler.minification = VK_FILTER_NEAREST - scene.addTextures("my_texture", @[Texture(image: t1, sampler: sampler), Texture(image: t2, sampler: sampler)]) + scene.addMaterial(Material(name:"my material", textures: {"my_texture": Texture(image: t1, sampler: sampler)}.toTable)) + scene.addMaterial(Material(name:"my material", textures: {"my_texture": Texture(image: t2, sampler: sampler)}.toTable)) scene.addShaderGlobalArray("test2", @[0'f32, 0'f32]) var engine = initEngine("Test materials") @@ -55,7 +57,7 @@ """ ) engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) - engine.addScene(scene, vertexInput, samplers) + engine.addScene(scene, vertexInput, samplers, transformAttribute="") var t = cpuTime() while engine.updateInputs() == Running and not engine.keyIsDown(Escape): var d = float32(cpuTime() - t) diff -r dcc12ab20a91 -r 07c755e65a4a tests/test_vulkan_wrapper.nim --- a/tests/test_vulkan_wrapper.nim Tue May 30 16:58:35 2023 +0700 +++ b/tests/test_vulkan_wrapper.nim Tue May 30 16:59:01 2023 +0700 @@ -1,3 +1,5 @@ +import std/tables + import semicongine proc scene_different_mesh_types(): Entity = @@ -124,14 +126,14 @@ for scene in scenes.mitems: scene.addShaderGlobal("time", 0.0'f32) let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8]) - scene.addTexture("my_little_texture", Texture(image: Image(width: 5, height: 5, imagedata: @[ + scene.addMaterial(Material(name: "my_material", textures: {"my_little_texture": Texture(image: Image(width: 5, height: 5, imagedata: @[ R, R, R, R, R, R, R, W, R, R, R, W, W, W, R, R, R, W, R, R, R, R, R, R, R, - ]), sampler: sampler)) - engine.addScene(scene, vertexInput, samplers) + ]), sampler: sampler)}.toTable)) + engine.addScene(scene, vertexInput, samplers, transformAttribute="") # MAINLOOP echo "Setup successfull, start rendering"