Mercurial > games > semicongine
diff examples/alotof_triangles.nim @ 22:b45a5d338cd0
did: improve alotof_triangles example, remove glslang lib, use only binary, easier cross-compilation
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 11 Jan 2023 00:43:17 +0700 |
parents | 316923e9247c |
children | b1b05d4efb52 |
line wrap: on
line diff
--- a/examples/alotof_triangles.nim Tue Jan 10 00:24:37 2023 +0700 +++ b/examples/alotof_triangles.nim Wed Jan 11 00:43:17 2023 +0700 @@ -1,3 +1,4 @@ +import std/math import std/random import zamikongine/engine @@ -13,13 +14,19 @@ position11: VertexAttribute[Vec2[float32]] color22: VertexAttribute[Vec3[float32]] +proc randomtransform(): Mat33[float32] = + let randomscale = scale2d(float32(rand(1.0) + 0.5), float32(rand(1.0) + 0.5)) + let randomrotate = rotate2d(float32(rand(2 * PI))) + let randomtranslate = translate2d(float32(rand(1.6) - 0.8), float32(rand(1.6) - 0.8)) + result = randomtranslate * randomrotate * randomscale + when isMainModule: randomize() var myengine = igniteEngine() const baseTriangle = [ - Vec3([-0.3'f32, -0.3'f32, 1'f32]), - Vec3([ 0.3'f32, 0.3'f32, 1'f32]), - Vec3([-0.3'f32, 0.3'f32, 1'f32]), + Vec3([-0.1'f32, -0.1'f32, 1'f32]), + Vec3([ 0.1'f32, 0.1'f32, 1'f32]), + Vec3([-0.1'f32, 0.1'f32, 1'f32]), ] var scene = new Thing @@ -27,13 +34,13 @@ for i in 1 .. 300: var randommesh = new Mesh[VertexDataA] # TODO: create randomized position11 from baseTriangle with random transformation matrix - var transform = (Mat33[float32]().randomized() * 2'f32) - 1'f32 + let transform1 = randomtransform() randommesh.vertexData = VertexDataA( position11: VertexAttribute[Vec2[float32]]( data: @[ - Vec2[float32](transform * baseTriangle[0]), - Vec2[float32](transform * baseTriangle[1]), - Vec2[float32](transform * baseTriangle[2]), + Vec2[float32](transform1 * baseTriangle[0]), + Vec2[float32](transform1 * baseTriangle[1]), + Vec2[float32](transform1 * baseTriangle[2]), ] ), color22: VertexAttribute[Vec3[float32]]( @@ -45,13 +52,14 @@ ) ) + let transform2 = randomtransform() var randomindexedmesh = new IndexedMesh[VertexDataA, uint16] randomindexedmesh.vertexData = VertexDataA( position11: VertexAttribute[Vec2[float32]]( data: @[ - Vec2[float32](transform * baseTriangle[0]), - Vec2[float32](transform * baseTriangle[1]), - Vec2[float32](transform * baseTriangle[2]), + Vec2[float32](transform2 * baseTriangle[0]), + Vec2[float32](transform2 * baseTriangle[1]), + Vec2[float32](transform2 * baseTriangle[2]), ] ), color22: VertexAttribute[Vec3[float32]]( @@ -62,7 +70,7 @@ ] ) ) - randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16], [0'u16, 2'u16, 1'u16]] + randomindexedmesh.indices = @[[0'u16, 1'u16, 2'u16]] var childthing = new Thing childthing.parts.add randommesh childthing.parts.add randomindexedmesh