changeset 661:9995702c34b2

add: few improvments for working with textures
author Sam <sam@basx.dev>
date Sun, 07 May 2023 18:13:39 +0700
parents c6414ade79f0
children 9848403320f0
files src/semicongine/color.nim src/semicongine/entity.nim src/semicongine/mesh.nim src/semicongine/renderer.nim src/semicongine/vulkan/image.nim tests/test_materials.nim
diffstat 6 files changed, 23 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/color.nim	Sun May 07 17:42:53 2023 +0700
+++ b/src/semicongine/color.nim	Sun May 07 18:13:39 2023 +0700
@@ -24,6 +24,11 @@
 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 asPixel*(color: Vec3f): array[4, uint8] =
+  [uint8(color.r * 255), uint8(color.g * 255), uint8(color.b * 255), 255'u8]
+func asPixel*(color: Vec4f): array[4, uint8] =
+  [uint8(color.r * 255), uint8(color.g * 255), uint8(color.b * 255), uint8(color.a * 255)]
+
 func hexToColorAlpha*(value: string): Vec4f =
   assert value != ""
   var hex = value
--- a/src/semicongine/entity.nim	Sun May 07 17:42:53 2023 +0700
+++ b/src/semicongine/entity.nim	Sun May 07 18:13:39 2023 +0700
@@ -5,6 +5,7 @@
 
 import ./math/matrix
 import ./gpu_data
+import ./vulkan/api
 
 type
   Component* = ref object of RootObj
@@ -28,6 +29,7 @@
     width*: uint32
     height*: uint32
     imagedata*: seq[Pixel]
+    interpolation*: VkFilter
 
 func addShaderGlobal*[T](scene: var Scene, name: string, data: T) =
   var value = DataValue(thetype: getDataType[T]())
--- a/src/semicongine/mesh.nim	Sun May 07 17:42:53 2023 +0700
+++ b/src/semicongine/mesh.nim	Sun May 07 18:13:39 2023 +0700
@@ -124,7 +124,7 @@
 
 proc getMeshData*[T: GPUType|int|uint|float](mesh: Mesh, attribute: string): seq[T] =
   assert attribute in mesh.data
-  get[T](mesh.data[attribute])
+  getValues[T](mesh.data[attribute])
 
 proc initData*(mesh: var Mesh, attribute: ShaderAttribute) =
   assert not (attribute.name in mesh.data)
--- a/src/semicongine/renderer.nim	Sun May 07 17:42:53 2023 +0700
+++ b/src/semicongine/renderer.nim	Sun May 07 18:13:39 2023 +0700
@@ -173,7 +173,7 @@
           )
 
       for name, image in scene.textures.pairs:
-        data.textures[name] = renderer.device.createTexture(image.width, image.height, 4, addr image.imagedata[0][0])
+        data.textures[name] = renderer.device.createTexture(image.width, image.height, 4, addr image.imagedata[0][0], image.interpolation)
       pipeline.setupDescriptors(data.uniformBuffers, data.textures, inFlightFrames=renderer.swapchain.inFlightFrames)
       for frame_i in 0 ..< renderer.swapchain.inFlightFrames:
         pipeline.descriptorSets[frame_i].writeDescriptorSet()
--- a/src/semicongine/vulkan/image.nim	Sun May 07 17:42:53 2023 +0700
+++ b/src/semicongine/vulkan/image.nim	Sun May 07 18:13:39 2023 +0700
@@ -187,12 +187,12 @@
     image.memoryAllocated = false
   image.vk.reset
 
-proc createSampler*(device: Device): Sampler =
+proc createSampler*(device: Device, interpolation: VkFilter): Sampler =
   assert device.vk.valid
   var samplerInfo = VkSamplerCreateInfo(
     sType: VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
-    magFilter: VK_FILTER_LINEAR,
-    minFilter: VK_FILTER_LINEAR,
+    magFilter: interpolation,
+    minFilter: interpolation,
     addressModeU: VK_SAMPLER_ADDRESS_MODE_REPEAT,
     addressModeV: VK_SAMPLER_ADDRESS_MODE_REPEAT,
     addressModeW: VK_SAMPLER_ADDRESS_MODE_REPEAT,
@@ -255,12 +255,12 @@
   imageview.image.device.vk.vkDestroyImageView(imageview.vk, nil)
   imageview.vk.reset()
 
-proc createTexture*(device: Device, width, height: uint32, depth: PixelDepth, data: pointer): Texture =
+proc createTexture*(device: Device, width, height: uint32, depth: PixelDepth, data: pointer, interpolation: VkFilter): Texture =
   assert device.vk.valid
   
-  result.image = createImage(device=device, width=width, height=width, depth=depth, data=data)
+  result.image = createImage(device=device, width=width, height=height, depth=depth, data=data)
   result.imageView = result.image.createImageView()
-  result.sampler = result.image.device.createSampler()
+  result.sampler = result.image.device.createSampler(interpolation)
 
 proc destroy*(texture: var Texture) =
   texture.image.destroy()
--- a/tests/test_materials.nim	Sun May 07 17:42:53 2023 +0700
+++ b/tests/test_materials.nim	Sun May 07 18:13:39 2023 +0700
@@ -3,13 +3,15 @@
 proc main() =
   var scene = newScene("main", root=newEntity("rect", rect()))
   let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8])
-  scene.addTexture("my_texture", TextureImage(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,
+  let (RT, WT, PT) = (hexToColorAlpha("A51931").asPixel, hexToColorAlpha("F4F5F8").asPixel, hexToColorAlpha("2D2A4A").asPixel)
+  scene.addTexture("my_texture", TextureImage(width: 13, height: 5, imagedata: @[
+    R, R, R, R, R, W, RT, RT, RT, RT, RT, RT, RT,
+    R, R, W, R, R, W, WT, WT, WT, WT, WT, WT, WT,
+    R, W, W, W, R, W, PT, PT, PT, PT, PT, PT, PT,
+    R, R, W, R, R, W, WT, WT, WT, WT, WT, WT, WT,
+    R, R, R, R, R, W, RT, RT, RT, RT, RT, RT, RT,
   ]))
+  var m: Mesh = Mesh(scene.root.components[0])
 
   var engine = initEngine("Test materials")
   const
@@ -38,7 +40,6 @@
   engine.addScene(scene, vertexInput)
   while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
     engine.renderScene(scene)
-    break
   engine.destroy()