# HG changeset patch # User sam # Date 1722242977 -25200 # Node ID a89a70ea3da2e80a6598dee458983b92537f0324 # Parent 01138e6257dd9ac78ce6ad06064c611b15e88b1f did: undo part of stupid API renaming a few weeks back ;( diff -r 01138e6257dd -r a89a70ea3da2 semicongine/contrib/algorithms/collision.nim --- a/semicongine/contrib/algorithms/collision.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/semicongine/contrib/algorithms/collision.nim Mon Jul 29 15:49:37 2024 +0700 @@ -19,25 +19,25 @@ case collider.theType: of Box: let - P1 = collider.transform * NewVec3f(0, 0, 0) # origin + P1 = collider.transform * vec3(0, 0, 0) # origin P2 = collider.transform * Z P4 = collider.transform * X P5 = collider.transform * Y - u = (P1 - P4).Cross(P1 - P5) - v = (P1 - P2).Cross(P1 - P5) - w = (P1 - P2).Cross(P1 - P4) - uP1 = u.Dot(P1) - uP2 = u.Dot(P2) - vP1 = v.Dot(P1) - vP4 = v.Dot(P4) - wP1 = w.Dot(P1) - wP5 = w.Dot(P5) - ux = u.Dot(x) - vx = v.Dot(x) - wx = w.Dot(x) + u = (P1 - P4).cross(P1 - P5) + v = (P1 - P2).cross(P1 - P5) + w = (P1 - P2).cross(P1 - P4) + uP1 = u.dot(P1) + uP2 = u.dot(P2) + vP1 = v.dot(P1) + vP4 = v.dot(P4) + wP1 = w.dot(P1) + wP5 = w.dot(P5) + ux = u.dot(x) + vx = v.dot(x) + wx = w.dot(x) ux.between(uP1, uP2) and vx.between(vP1, vP4) and wx.between(wP1, wP5) of Sphere: - (collider.transform * x).Length < (collider.transform * NewVec3f()).Length + (collider.transform * x).length < (collider.transform * vec3(0, 0, 0)).length of Points: raise newException(Exception, "Points are not supported yet for 'contains'") @@ -49,7 +49,7 @@ func findFurthestPoint(points: openArray[Vec3f], direction: Vec3f): Vec3f = var maxDist = low(float32) for p in points: - let dist = direction.Dot(p) + let dist = direction.dot(p) if dist > maxDist: maxDist = dist result = p @@ -57,7 +57,7 @@ func findFurthestPoint(transform: Mat4, direction: Vec3f): Vec3f = return findFurthestPoint( [ - transform * NewVec3f(0, 0, 0), + transform * vec3(0, 0, 0), transform * X, transform * Y, transform * Z, @@ -71,7 +71,7 @@ func findFurthestPoint(collider: Collider, direction: Vec3f): Vec3f = case collider.theType of Sphere: - let directionNormalizedToSphere = ((direction / direction.Length) * collider.radius) + let directionNormalizedToSphere = ((direction / direction.length) * collider.radius) collider.transform * directionNormalizedToSphere of Box: findFurthestPoint(collider.transform, direction) @@ -82,7 +82,7 @@ a.findFurthestPoint(direction) - b.findFurthestPoint(-direction) func sameDirection(direction: Vec3f, ao: Vec3f): bool = - direction.Dot(ao) > 0 + direction.dot(ao) > 0 func line(simplex: var seq[Vec3f], direction: var Vec3f): bool = let @@ -92,7 +92,7 @@ ao = - a if sameDirection(ab, ao): - direction = Cross(Cross(ab, ao), ab) + direction = cross(cross(ab, ao), ab) else: simplex = @[a] direction = ao @@ -107,17 +107,17 @@ ab = b - a ac = c - a ao = - a - abc = ab.Cross(ac) + abc = ab.cross(ac) - if sameDirection(abc.Cross(ac), ao): + if sameDirection(abc.cross(ac), ao): if sameDirection(ac, ao): simplex = @[a, c] - direction = ac.Cross(ao).Cross(ac) + direction = ac.cross(ao).cross(ac) else: simplex = @[a, b] return line(simplex, direction) else: - if (sameDirection(ab.Cross(abc), ao)): + if (sameDirection(ab.cross(abc), ao)): simplex = @[a, b] return line(simplex, direction) else: @@ -141,9 +141,9 @@ ac = c - a ad = d - a ao = - a - abc = ab.Cross(ac) - acd = ac.Cross(ad) - adb = ad.Cross(ab) + abc = ab.cross(ac) + acd = ac.cross(ad) + adb = ad.cross(ab) if sameDirection(abc, ao): simplex = @[a, b, c] @@ -169,8 +169,8 @@ b = polytope[faces[i + 1]] c = polytope[faces[i + 2]] - var normal = (b - a).Cross(c - a).Normalized() - var distance = normal.Dot(a) + var normal = (b - a).cross(c - a).normalized() + var distance = normal.dot(a) if distance < 0: normal = normal * -1'f32 @@ -217,7 +217,7 @@ minDistance = normals[minFace].w var support = supportPoint(a, b, minNormal) - sDistance = minNormal.Dot(support) + sDistance = minNormal.dot(support) if abs(sDistance - minDistance) > 0.001'f32: minDistance = high(float32) @@ -286,8 +286,8 @@ vertexJ = polytope[j] ij = vertexJ - vertexI var - normal = NewVec2f(ij.y, -ij.x).Normalized() - distance = normal.Dot(vertexI) + normal = vec2(ij.y, -ij.x).normalized() + distance = normal.dot(vertexI) if (distance < 0): distance *= -1'f32 @@ -300,51 +300,51 @@ let support = supportPoint(a, b, minNormal.ToVec3) - sDistance = minNormal.Dot(support) + sDistance = minNormal.dot(support) if(abs(sDistance - minDistance) > 0.001): minDistance = high(float32) polytope.insert(support, minIndex) inc iterCount - result = (normal: NewVec3f(minNormal.x, minNormal.y), penetrationDepth: minDistance + 0.001'f32) + result = (normal: vec3(minNormal.x, minNormal.y, 0), penetrationDepth: minDistance + 0.001'f32) func Intersects*(a, b: Collider, as2D = false): bool = var - support = supportPoint(a, b, NewVec3f(0.8153, -0.4239, if as2D: 0.0 else: 0.5786)) # just random initial vector + support = supportPoint(a, b, vec3(0.8153, -0.4239, if as2D: 0.0 else: 0.5786)) # just random initial vector simplex = newSeq[Vec3f]() direction = -support n = 0 simplex.insert(support, 0) while n < MAX_COLLISON_DETECTION_ITERATIONS: support = supportPoint(a, b, direction) - if support.Dot(direction) <= 0: + if support.dot(direction) <= 0: return false simplex.insert(support, 0) if nextSimplex(simplex, direction, twoDimensional = as2D): return true # prevent numeric instability - if direction == NewVec3f(0, 0, 0): + if direction == vec3(0, 0, 0): direction[0] = 0.0001 inc n func Collision*(a, b: Collider, as2D = false): tuple[hasCollision: bool, normal: Vec3f, penetrationDepth: float32] = var - support = supportPoint(a, b, NewVec3f(0.8153, -0.4239, if as2D: 0.0 else: 0.5786)) # just random initial vector + support = supportPoint(a, b, vec3(0.8153, -0.4239, if as2D: 0.0 else: 0.5786)) # just random initial vector simplex = newSeq[Vec3f]() direction = -support n = 0 simplex.insert(support, 0) while n < MAX_COLLISON_DETECTION_ITERATIONS: support = supportPoint(a, b, direction) - if support.Dot(direction) <= 0: + if support.dot(direction) <= 0: return result simplex.insert(support, 0) if nextSimplex(simplex, direction, twoDimensional = as2D): let (normal, depth) = if as2D: collisionPoint2D(simplex, a, b) else: collisionPoint3D(simplex, a, b) return (true, normal, depth) # prevent numeric instability - if direction == NewVec3f(0, 0, 0): + if direction == vec3(0, 0, 0): direction[0] = 0.0001 inc n @@ -376,9 +376,9 @@ if theType == Points: result = Collider(theType: Points, points: @points) else: - result = Collider(theType: theType, transform: Translate(minX, minY, minZ) * Scale(scaleX, scaleY, scaleZ)) + result = Collider(theType: theType, transform: translate(minX, minY, minZ) * scale(scaleX, scaleY, scaleZ)) if theType == Sphere: - result.transform = Translate(center) + result.transform = translate(center) for p in points: - result.radius = max(result.radius, (p - center).Length) + result.radius = max(result.radius, (p - center).length) diff -r 01138e6257dd -r a89a70ea3da2 semicongine/contrib/algorithms/noise.nim --- a/semicongine/contrib/algorithms/noise.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/semicongine/contrib/algorithms/noise.nim Mon Jul 29 15:49:37 2024 +0700 @@ -1,6 +1,6 @@ proc randomGradient(pos: Vec2f, seed: int32 = 0): Vec2f = let randomAngle: float32 = TAU * (float32(int(hash((pos.x, pos.y, seed)))) / float32(high(int))) - return NewVec2f(cos(randomAngle), sin(randomAngle)) + return vec2(cos(randomAngle), sin(randomAngle)) proc interpolate(a: float32, b: float32, weight: float32): float32 = # with Smootherstep @@ -9,15 +9,15 @@ proc Perlin*(pos: Vec2f, seed: int32 = 0): float32 = let # grid coordinates around target point - topleft = NewVec2f(trunc(pos.x), trunc(pos.y)) - topright = topleft + NewVec2f(1, 0) - bottomleft = topleft + NewVec2f(0, 1) - bottomright = topleft + NewVec2f(1, 1) + topleft = vec2(trunc(pos.x), trunc(pos.y)) + topright = topleft + vec2(1, 0) + bottomleft = topleft + vec2(0, 1) + bottomright = topleft + vec2(1, 1) # products for weights - topleft_dot = topleft.randomGradient(seed).Dot((pos - topleft)) - topright_dot = topright.randomGradient(seed).Dot((pos - topright)) - bottomleft_dot = bottomleft.randomGradient(seed).Dot((pos - bottomleft)) - bottomright_dot = bottomright.randomGradient(seed).Dot((pos - bottomright)) + topleft_dot = topleft.randomGradient(seed).dot((pos - topleft)) + topright_dot = topright.randomGradient(seed).dot((pos - topright)) + bottomleft_dot = bottomleft.randomGradient(seed).dot((pos - bottomleft)) + bottomright_dot = bottomright.randomGradient(seed).dot((pos - bottomright)) xinterpol = pos.x - topleft.x yinterpol = pos.y - topleft.y diff -r 01138e6257dd -r a89a70ea3da2 semicongine/core/matrix.nim --- a/semicongine/core/matrix.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/semicongine/core/matrix.nim Mon Jul 29 15:49:37 2024 +0700 @@ -32,16 +32,16 @@ Mat43* = TMat43[float32] Mat4* = TMat4[float32] -func MakeUnit2*[T: SomeNumber](): auto {.compiletime.} = TMat2[T](data: [ +func makeUnit2[T: SomeNumber](): auto {.compiletime.} = TMat2[T](data: [ T(1), T(0), T(0), T(1), ]) -func MakeUnit3*[T: SomeNumber](): auto {.compiletime.} = TMat3[T](data: [ +func makeUnit3[T: SomeNumber](): auto {.compiletime.} = TMat3[T](data: [ T(1), T(0), T(0), T(0), T(1), T(0), T(0), T(0), T(1), ]) -func MakeUnit4*[T: SomeNumber](): auto {.compiletime.} = TMat4[T](data: [ +func makeUnit4[T: SomeNumber](): auto {.compiletime.} = TMat4[T](data: [ T(1), T(0), T(0), T(0), T(0), T(1), T(0), T(0), T(0), T(0), T(1), T(0), @@ -61,22 +61,22 @@ typename = typename & theType[^1] result.add(newConstStmt( postfix(ident("Unit2" & typename), "*"), - newCall(nnkBracketExpr.newTree(ident("MakeUnit2"), ident(theType))) + newCall(nnkBracketExpr.newTree(ident("makeUnit2"), ident(theType))) )) result.add(newConstStmt( postfix(ident("Unit3" & typename), "*"), - newCall(nnkBracketExpr.newTree(ident("MakeUnit3"), ident(theType))) + newCall(nnkBracketExpr.newTree(ident("makeUnit3"), ident(theType))) )) result.add(newConstStmt( postfix(ident("Unit4" & typename), "*"), - newCall(nnkBracketExpr.newTree(ident("MakeUnit4"), ident(theType))) + newCall(nnkBracketExpr.newTree(ident("makeUnit4"), ident(theType))) )) generateAllMatrixConsts() -const Unit2* = MakeUnit2[float32]() -const Unit3* = MakeUnit3[float32]() -const Unit4* = MakeUnit4[float32]() +const Unit2* = makeUnit2[float32]() +const Unit3* = makeUnit3[float32]() +const Unit4* = makeUnit4[float32]() template RowCount*(m: typedesc): int = when m is TMat2: 2 @@ -137,21 +137,21 @@ func `[]`*[T: TMat](m: T, i: int): auto = m.data[i] func `[]=`*[T: TMat, U](m: var T, i: int, value: U) = m.data[i] = value -func Row*[T: TMat2](m: T, i: 0..1): auto = TVec2([m[i, 0], m[i, 1]]) -func Row*[T: TMat32](m: T, i: 0..2): auto = TVec2([m[i, 0], m[i, 1]]) -func Row*[T: TMat23](m: T, i: 0..1): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) -func Row*[T: TMat3](m: T, i: 0..2): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) -func Row*[T: TMat43](m: T, i: 0..3): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) -func Row*[T: TMat34](m: T, i: 0..2): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]]) -func Row*[T: TMat4](m: T, i: 0..3): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]]) +func row*[T: TMat2](m: T, i: 0..1): auto = TVec2([m[i, 0], m[i, 1]]) +func row*[T: TMat32](m: T, i: 0..2): auto = TVec2([m[i, 0], m[i, 1]]) +func row*[T: TMat23](m: T, i: 0..1): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) +func row*[T: TMat3](m: T, i: 0..2): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) +func row*[T: TMat43](m: T, i: 0..3): auto = TVec3([m[i, 0], m[i, 1], m[i, 2]]) +func row*[T: TMat34](m: T, i: 0..2): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]]) +func row*[T: TMat4](m: T, i: 0..3): auto = TVec4([m[i, 0], m[i, 1], m[i, 2], m[i, 3]]) -func Col*[T: TMat2](m: T, i: 0..1): auto = TVec2([m[0, i], m[1, i]]) -func Col*[T: TMat23](m: T, i: 0..2): auto = TVec2([m[0, i], m[1, i]]) -func Col*[T: TMat32](m: T, i: 0..1): auto = TVec3([m[0, i], m[1, i], m[2, i]]) -func Col*[T: TMat3](m: T, i: 0..2): auto = TVec3([m[0, i], m[1, i], m[2, i]]) -func Col*[T: TMat34](m: T, i: 0..3): auto = TVec3([m[0, i], m[1, i], m[2, i]]) -func Col*[T: TMat43](m: T, i: 0..2): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]]) -func Col*[T: TMat4](m: T, i: 0..3): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]]) +func col*[T: TMat2](m: T, i: 0..1): auto = TVec2([m[0, i], m[1, i]]) +func col*[T: TMat23](m: T, i: 0..2): auto = TVec2([m[0, i], m[1, i]]) +func col*[T: TMat32](m: T, i: 0..1): auto = TVec3([m[0, i], m[1, i], m[2, i]]) +func col*[T: TMat3](m: T, i: 0..2): auto = TVec3([m[0, i], m[1, i], m[2, i]]) +func col*[T: TMat34](m: T, i: 0..3): auto = TVec3([m[0, i], m[1, i], m[2, i]]) +func col*[T: TMat43](m: T, i: 0..2): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]]) +func col*[T: TMat4](m: T, i: 0..3): auto = TVec4([m[0, i], m[1, i], m[2, i], m[3, i]]) proc createMatMatMultiplicationOperator(leftType: typedesc, rightType: typedesc, outType: typedesc): NimNode = var data = nnkBracket.newTree() @@ -160,9 +160,9 @@ data.add(newCall( ident("sum"), infix( - newCall(newDotExpr(ident("a"), ident("Row")), newLit(i)), + newCall(newDotExpr(ident("a"), ident("row")), newLit(i)), "*", - newCall(newDotExpr(ident("b"), ident("Col")), newLit(j)) + newCall(newDotExpr(ident("b"), ident("col")), newLit(j)) ) )) @@ -206,7 +206,7 @@ infix( ident("v"), "*", - newCall(newDotExpr(ident("m"), ident("Row")), newLit(i)) + newCall(newDotExpr(ident("m"), ident("row")), newLit(i)) ) )) @@ -325,72 +325,72 @@ func `*`*(mat: Mat4, vec: Vec3f): Vec3f = (mat * vec.ToVec4(1)).ToVec3 -func Transposed*[T](m: TMat2[T]): TMat2[T] = TMat2[T](data: [ +func transposed*[T](m: TMat2[T]): TMat2[T] = TMat2[T](data: [ m[0, 0], m[1, 0], m[0, 1], m[1, 1], ]) -func Transposed*[T](m: TMat23[T]): TMat32[T] = TMat32[T](data: [ +func transposed*[T](m: TMat23[T]): TMat32[T] = TMat32[T](data: [ m[0, 0], m[1, 0], m[0, 1], m[1, 1], m[0, 2], m[1, 2], ]) -func Transposed*[T](m: TMat32[T]): TMat23[T] = TMat23[T](data: [ +func transposed*[T](m: TMat32[T]): TMat23[T] = TMat23[T](data: [ m[0, 0], m[1, 0], m[2, 0], m[0, 1], m[1, 1], m[2, 1], ]) -func Transposed*[T](m: TMat3[T]): TMat3[T] = TMat3[T](data: [ +func transposed*[T](m: TMat3[T]): TMat3[T] = TMat3[T](data: [ m[0, 0], m[1, 0], m[2, 0], m[0, 1], m[1, 1], m[2, 1], m[0, 2], m[1, 2], m[2, 2], ]) -func Transposed*[T](m: TMat43[T]): TMat34[T] = TMat34[T](data: [ +func transposed*[T](m: TMat43[T]): TMat34[T] = TMat34[T](data: [ m[0, 0], m[1, 0], m[2, 0], m[3, 0], m[0, 1], m[1, 1], m[2, 1], m[3, 1], m[0, 2], m[1, 2], m[2, 2], m[3, 2], ]) -func Transposed*[T](m: TMat34[T]): TMat43[T] = TMat43[T](data: [ +func transposed*[T](m: TMat34[T]): TMat43[T] = TMat43[T](data: [ m[0, 0], m[1, 0], m[2, 0], m[0, 1], m[1, 1], m[2, 1], m[0, 2], m[1, 2], m[2, 2], m[0, 3], m[1, 3], m[2, 3], ]) -func Transposed*[T](m: TMat4[T]): TMat4[T] = TMat4[T](data: [ +func transposed*[T](m: TMat4[T]): TMat4[T] = TMat4[T](data: [ m[0, 0], m[1, 0], m[2, 0], m[3, 0], m[0, 1], m[1, 1], m[2, 1], m[3, 1], m[0, 2], m[1, 2], m[2, 2], m[3, 2], m[0, 3], m[1, 3], m[2, 3], m[3, 3], ]) -func Translate2d*[T](x, y: T): TMat3[T] = TMat3[T](data: [ +func translate2d*[T](x, y: T): TMat3[T] = TMat3[T](data: [ T(1), T(0), x, T(0), T(1), y, T(0), T(0), T(1), ]) -func Scale2d*[T](sx, sy: T): TMat3[T] = TMat3[T](data: [ +func scale2d*[T](sx, sy: T): TMat3[T] = TMat3[T](data: [ sx, T(0), T(0), T(0), sy, T(0), T(0), T(0), T(1), ]) -func Rotate2d*[T](angle: T): TMat3[T] = TMat3[T](data: [ +func rotate2d*[T](angle: T): TMat3[T] = TMat3[T](data: [ cos(angle), -sin(angle), T(0), sin(angle), cos(angle), T(0), T(0), T(0), T(1), ]) -func Translate*(x = 0'f32, y = 0'f32, z = 0'f32): TMat4[float32] = Mat4(data: [ +func translate*(x = 0'f32, y = 0'f32, z = 0'f32): TMat4[float32] = Mat4(data: [ 1'f32, 0'f32, 0'f32, x, 0'f32, 1'f32, 0'f32, y, 0'f32, 0'f32, 1'f32, z, 0'f32, 0'f32, 0'f32, 1'f32, ]) -func Translate*[T: TVec3](v: T): TMat4[float32] = Translate(v[0], v[1], v[2]) -func Scale*(x = 1'f32, y = 1'f32, z = 1'f32): Mat4 = Mat4(data: [ +func translate*[T: TVec3](v: T): TMat4[float32] = translate(v[0], v[1], v[2]) +func scale*(x = 1'f32, y = 1'f32, z = 1'f32): Mat4 = Mat4(data: [ x, 0'f32, 0'f32, 0'f32, 0'f32, y, 0'f32, 0'f32, 0'f32, 0'f32, z, 0'f32, 0'f32, 0'f32, 0'f32, 1'f32, ]) -func Scale*[T: TVec3](v: T): TMat4[float32] = Scale(v[0], v[1], v[2]) -func Rotate*(angle: float32, a: Vec3f): Mat4 = +func scale*[T: TVec3](v: T): TMat4[float32] = scale(v[0], v[1], v[2]) +func rotate*(angle: float32, a: Vec3f): Mat4 = let cosa = cos(angle) sina = sin(angle) @@ -412,12 +412,13 @@ ]) -func Inversed*(m: Mat4): Mat4 = - var m3 = m.asMat3.Transposed +func inversed*(m: Mat4): Mat4 = + # TODO: is this correct? + var m3 = m.asMat3.transposed m3[0, 0] = 1'f32 / m3[0, 0] m3[1, 1] = 1'f32 / m3[1, 1] m3[2, 2] = 1'f32 / m3[2, 2] - let col3 = -(m3 * m.Col(3).xyz) + let col3 = -(m3 * m.col(3).xyz) return Mat4(data: [ m3[0, 0], m3[0, 1], m3[0, 2], col3.x, m3[1, 0], m3[1, 1], m3[1, 2], col3.y, @@ -443,7 +444,7 @@ makeRandomMatrixInit(TMat43) makeRandomMatrixInit(TMat4) -func Projection*(fovy, aspect, zNear, zFar: float32): Mat4 = +func projection*(fovy, aspect, zNear, zFar: float32): Mat4 = let tanHalfFovy = 1 / tan(fovy / 2) return Mat4(data: [ tanHalfFovy / aspect, 0, 0, 0, @@ -452,26 +453,10 @@ 0, 0, 1, 0, ]) -func Ortho*(left, right, top, bottom, zNear, zFar: float32): Mat4 = +func ortho*(left, right, top, bottom, zNear, zFar: float32): Mat4 = Mat4(data: [ 2 / (right - left), 0, 0, -(right + left) / (right - left), 0, 2 / (bottom - top), 0, -(bottom + top) / (bottom - top), 0, 0, 1 / (zFar - zNear), zNear / (zFar - zNear), 0, 0, 0, 1, ]) - -# create an orthographic perspective that will map from -1 .. 1 on all axis and keep a 1:1 aspect ratio -# the smaller dimension (width or height) will always be 1 and the larger dimension will be larger, to keep the ratio -func OrthoWindowAspect*(windowAspect: float32): Mat4 = - if windowAspect < 1: - let space = 2 * (1 / windowAspect - 1) / 2 - Ortho(-1, 1, -1 - space, 1 + space, 0, 1) - else: - let space = 2 * (windowAspect - 1) / 2 - Ortho(-1 - space, 1 + space, -1, 1, 0, 1) - -func Position*(mat: Mat4): Vec3f {.deprecated.} = - mat.Col(3).ToVec3 - -func Scaling*(mat: Mat4): Vec3f {.deprecated.} = - NewVec4f(mat[0, 0], mat[1, 1], mat[2, 2]) diff -r 01138e6257dd -r a89a70ea3da2 semicongine/core/vector.nim --- a/semicongine/core/vector.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/semicongine/core/vector.nim Mon Jul 29 15:49:37 2024 +0700 @@ -32,86 +32,37 @@ func ToVec2*[T: SomeNumber](orig: TVec1[T], value: T = default(T)): TVec2[T] = TVec2[T]([orig[0], value]) -# define some often used constants -func ConstOne1[T: SomeNumber](): auto {.compiletime.} = TVec1[T]([T(1)]) -func ConstOne2[T: SomeNumber](): auto {.compiletime.} = TVec2[T]([T(1), T(1)]) -func ConstOne3[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(1), T(1), T(1)]) -func ConstOne4[T: SomeNumber](): auto {.compiletime.} = TVec4[T]([T(1), T(1), T(1), T(1)]) -func ConstX[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(1), T(0), T(0)]) -func ConstY[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(0), T(1), T(0)]) -func ConstZ[T: SomeNumber](): auto {.compiletime.} = TVec3[T]([T(0), T(0), T(1)]) -func ConstR[T: SomeNumber](): auto {.compiletime.} = TVec4[T]([T(1), T(0), T(0), T(1)]) -func ConstG[T: SomeNumber](): auto {.compiletime.} = TVec4[T]([T(0), T(1), T(0), T(1)]) -func ConstB[T: SomeNumber](): auto {.compiletime.} = TVec4[T]([T(0), T(0), T(1), T(1)]) - -func NewVec2f*(x = 0'f32, y = 0'f32): auto = - Vec2f([x, y]) -func NewVec3f*(x = 0'f32, y = 0'f32, z = 0'f32): auto = - Vec3f([x, y, z]) -func NewVec4f*(x = 0'f32, y = 0'f32, z = 0'f32, a = 0'f32): auto = - Vec4f([x, y, z, a]) -func NewVec2i*(x = 0'i32, y = 0'i32): auto = - Vec2i([x, y]) -func NewVec3i*(x = 0'i32, y = 0'i32, z = 0'i32): auto = - Vec3i([x, y, z]) -func NewVec4i*(x = 0'i32, y = 0'i32, z = 0'i32, a = 0'i32): auto = - Vec4i([x, y, z, a]) -func NewVec2u*(x = 0'u32, y = 0'u32): auto = - Vec2u([x, y]) -func NewVec3u*(x = 0'u32, y = 0'u32, z = 0'u32): auto = - Vec3u([x, y, z]) -func NewVec4u*(x = 0'u32, y = 0'u32, z = 0'u32, a = 0'u32): auto = - Vec4u([x, y, z, a]) - # shortcuts -func vec2*[T: SomeNumber](x, y: T): Vec2f = +func vec1*[T: SomeNumber](x: T): Vec1f = + Vec1f([float32(x)]) +func vec2*[T, S: SomeNumber](x: T, y: S): Vec2f = Vec2f([float32(x), float32(y)]) -func vec3*[T: SomeNumber](x, y, z: T): Vec3f = +func vec2*[T: SomeNumber](x: T): Vec2f = vec2(x, 0) +func vec2*(): Vec2f = vec2(0, 0) +func vec3*[T, S, U: SomeNumber](x: T, y: S, z: U): Vec3f = Vec3f([float32(x), float32(y), float32(z)]) -func vec4*[T: SomeNumber](x, y , z, w: T): Vec4f = +func vec3*[T, S: SomeNumber](x: T, y: S): Vec3f = vec3(x, y, 0) +func vec3*[T: SomeNumber](x: T): Vec3f = vec3(x, 0, 0) +func vec3*(): Vec3f = vec3(0, 0, 0) +func vec4*[T, S, U, V: SomeNumber](x: T, y: S, z: U, w: V): Vec4f = Vec4f([float32(x), float32(y), float32(z), float32(w)]) +func vec4*[T, S, U: SomeNumber](x: T, y: S, z: U): Vec4f = vec4(x, y, z, 0) +func vec4*[T, S: SomeNumber](x: T, y: S): Vec4f = vec4(x, y, 0, 0) +func vec4*[T: SomeNumber](x: T): Vec4f = vec4(x, 0, 0, 0) +func vec4*(): Vec4f = vec4(0, 0, 0, 0) -# generates constants: Xf, Xf32, Xf64, Xi, Xi8, Xi16, Xi32, Xi64 -# Also for Y, Z, R, G, B and One -# not sure if this is necessary or even a good idea... -macro generateAllVectorConsts() = - result = newStmtList() - for component in ["X", "Y", "Z", "R", "G", "B", "One2", "One3", "One4"]: - for theType in ["int", "int8", "int16", "int32", "int64", "float", "float32", "float64"]: - var typename = theType[0 .. 0] - if theType[^2].isDigit: - typename = typename & theType[^2] - if theType[^1].isDigit: - typename = typename & theType[^1] - result.add( - newConstStmt( - postfix(ident(component & typename), "*"), - newCall(nnkBracketExpr.newTree(ident("Const" & component), ident(theType))) - ) - ) - -generateAllVectorConsts() +const + X* = vec3(1, 0, 0) + Y* = vec3(0, 1, 0) + Z* = vec3(0, 0, 1) + R* = vec4(1, 0, 0, 1) + G* = vec4(1, 0, 0, 1) + B* = vec4(1, 0, 0, 1) -const X* = ConstX[float32]() -const Y* = ConstY[float32]() -const Z* = ConstZ[float32]() -const R* = ConstR[float32]() -const G* = ConstG[float32]() -const B* = ConstB[float32]() -const One1* = ConstOne1[float32]() -const One2* = ConstOne2[float32]() -const One3* = ConstOne3[float32]() -const One4* = ConstOne4[float32]() - -func NewVec1*[T](x: T): auto = TVec1([x]) -func NewVec2*[T](x, y: T): auto = TVec2([x, y]) -func NewVec3*[T](x, y, z: T): auto = TVec3([x, y, z]) -func NewVec4*[T](x, y, z, w: T): auto = TVec4([x, y, z, w]) - -func To*[T](v: TVec1): auto = TVec1([T(v[0])]) -func To*[T](v: TVec2): auto = TVec2([T(v[0]), T(v[1])]) -func To*[T](v: TVec3): auto = TVec3([T(v[0]), T(v[1]), T(v[2])]) -func To*[T](v: TVec4): auto = TVec4([T(v[0]), T(v[1]), T(v[2]), T(v[3])]) +func to*[T](v: TVec1): auto = TVec1([T(v[0])]) +func to*[T](v: TVec2): auto = TVec2([T(v[0]), T(v[1])]) +func to*[T](v: TVec3): auto = TVec3([T(v[0]), T(v[1]), T(v[2])]) +func to*[T](v: TVec4): auto = TVec4([T(v[0]), T(v[1]), T(v[2]), T(v[3])]) func toVecString[T: TVec](value: T): string = var items: seq[string] @@ -124,29 +75,29 @@ func `$`*(v: TVec3[SomeNumber]): string = toVecString[TVec3[SomeNumber]](v) func `$`*(v: TVec4[SomeNumber]): string = toVecString[TVec4[SomeNumber]](v) -func Length*(vec: TVec1): auto = vec[0] -func Length*(vec: TVec2[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1]) -func Length*(vec: TVec2[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1])) -func Length*(vec: TVec3[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]) -func Length*(vec: TVec3[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2])) -func Length*(vec: TVec4[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]) -func Length*(vec: TVec4[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3])) +func length*(vec: TVec1): auto = vec[0] +func length*(vec: TVec2[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1]) +func length*(vec: TVec2[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1])) +func length*(vec: TVec3[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]) +func length*(vec: TVec3[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2])) +func length*(vec: TVec4[SomeFloat]): auto = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3]) +func length*(vec: TVec4[SomeInteger]): auto = sqrt(float(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2] + vec[3] * vec[3])) -func Normal*[T: SomeFloat](vec: TVec2[T]): auto = +func normal*[T: SomeFloat](vec: TVec2[T]): auto = TVec2[T]([vec[1], -vec[0]]) -func Normalized*[T: SomeFloat](vec: TVec1[T]): auto = +func normalized*[T: SomeFloat](vec: TVec1[T]): auto = return T(1) -func Normalized*[T: SomeFloat](vec: TVec2[T]): auto = - let l = vec.Length +func normalized*[T: SomeFloat](vec: TVec2[T]): auto = + let l = vec.length if l == 0: vec else: TVec2[T]([vec[0] / l, vec[1] / l]) -func Normalized*[T: SomeFloat](vec: TVec3[T]): auto = - let l = vec.Length +func normalized*[T: SomeFloat](vec: TVec3[T]): auto = + let l = vec.length if l == 0: return vec else: TVec3[T]([vec[0] / l, vec[1] / l, vec[2] / l]) -func Normalized*[T: SomeFloat](vec: TVec4[T]): auto = - let l = vec.Length +func normalized*[T: SomeFloat](vec: TVec4[T]): auto = + let l = vec.length if l == 0: return vec else: TVec4[T]([vec[0] / l, vec[1] / l, vec[2] / l, vec[3] / l]) @@ -260,19 +211,19 @@ # special operations -func Pow*(a: TVec1, b: SomeNumber): auto = +func pow*(a: TVec1, b: SomeNumber): auto = TVec1([pow(a[0], b)]) -func Pow*(a: TVec2, b: SomeNumber): auto = +func pow*(a: TVec2, b: SomeNumber): auto = TVec2([pow(a[0], b), pow(a[1], b)]) -func Pow*(a: TVec3, b: SomeNumber): auto = +func pow*(a: TVec3, b: SomeNumber): auto = TVec3([pow(a[0], b), pow(a[1], b), pow(a[2], b)]) -func Pow*(a: TVec4, b: SomeNumber): auto = +func pow*(a: TVec4, b: SomeNumber): auto = TVec4([pow(a[0], b), pow(a[1], b), pow(a[2], b), pow(a[3], b)]) -func Dot*(a, b: TVec1): auto = a[0] * b[0] -func Dot*(a, b: TVec2): auto = a[0] * b[0] + a[1] * b[1] -func Dot*(a, b: TVec3): auto = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] -func Dot*(a, b: TVec4): auto = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3] -func Cross*(a, b: TVec3): auto = TVec3([ +func dot*(a, b: TVec1): auto = a[0] * b[0] +func dot*(a, b: TVec2): auto = a[0] * b[0] + a[1] * b[1] +func dot*(a, b: TVec3): auto = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] +func dot*(a, b: TVec4): auto = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3] +func cross*(a, b: TVec3): auto = TVec3([ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0], @@ -360,10 +311,10 @@ # call e.g. Vec2[int]().randomized() to get a random matrix template makeRandomVectorInit(mattype: typedesc) = - proc Randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = + proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = for i in 0 ..< result.len: result[i] = rand(low(typeof(m[0])) .. high(typeof(m[0]))) - proc Randomized*[T: SomeFloat](m: mattype[T]): mattype[T] = + proc randomized*[T: SomeFloat](m: mattype[T]): mattype[T] = for i in 0 ..< result.len: result[i] = rand(1.0) @@ -375,5 +326,5 @@ converter Vec2VkExtent*(vec: TVec2[uint32]): VkExtent2D = VkExtent2D(width: vec[0], height: vec[1]) converter Vec3VkExtent*(vec: TVec2[uint32]): VkExtent3D = VkExtent3D(width: vec[0], height: vec[1], depth: vec[2]) -func AngleBetween*(a, b: Vec3f): float32 = - arccos(a.Dot(b) / (a.Length * b.Length)) +func angleBetween*(a, b: Vec3f): float32 = + arccos(a.dot(b) / (a.length * b.length)) diff -r 01138e6257dd -r a89a70ea3da2 semicongine/gltf.nim --- a/semicongine/gltf.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/semicongine/gltf.nim Mon Jul 29 15:49:37 2024 +0700 @@ -170,8 +170,7 @@ result.wrapV = SAMPLER_WRAP_MODE_MAP[sampler["wrapT"].getInt()] proc getVec4f(node: JsonNode): Vec4f = - NewVec4f(node[0].getFloat(), node[1].getFloat(), node[2].getFloat(), node[ - 3].getFloat()) + vec4(node[0].getFloat(), node[1].getFloat(), node[2].getFloat(), node[3].getFloat()) proc loadMaterial[TMaterial]( root: JsonNode, @@ -249,26 +248,26 @@ if "matrix" in node: for i in 0 ..< node["matrix"].len: result.transform[i] = node["matrix"][i].getFloat() - result.transform = result.transform.Transposed() + result.transform = result.transform.transposed() else: var (t, r, s) = (Unit4, Unit4, Unit4) if "translation" in node: - t = Translate( + t = translate( float32(node["translation"][0].getFloat()), float32(node["translation"][1].getFloat()), float32(node["translation"][2].getFloat()) ) if "rotation" in node: - t = Rotate( + t = rotate( float32(node["rotation"][3].getFloat()), - NewVec3f( + vec3( float32(node["rotation"][0].getFloat()), float32(node["rotation"][1].getFloat()), float32(node["rotation"][2].getFloat()) ) ) if "scale" in node: - t = Scale( + t = scale( float32(node["scale"][0].getFloat()), float32(node["scale"][1].getFloat()), float32(node["scale"][2].getFloat()) diff -r 01138e6257dd -r a89a70ea3da2 semicongine/input.nim --- a/semicongine/input.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/semicongine/input.nim Mon Jul 29 15:49:37 2024 +0700 @@ -24,7 +24,7 @@ input.mouseWasPressed = {} input.mouseWasReleased = {} input.mouseWheel = 0 - input.mouseMove = NewVec2f() + input.mouseMove = vec2(0, 0) input.windowWasResized = false if input.lockMouse and input.hasFocus: @@ -50,7 +50,7 @@ input.mouseWasReleased.incl event.button input.mouseIsDown.excl event.button of MouseMoved: - let newPos = NewVec2(float32(event.x), float32(event.y)) + let newPos = vec2(event.x, event.y) input.mouseMove = newPos - input.mousePosition input.mousePosition = newPos of MouseWheel: diff -r 01138e6257dd -r a89a70ea3da2 semicongine/text/font.nim --- a/semicongine/text/font.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/semicongine/text/font.nim Mon Jul 29 15:49:37 2024 +0700 @@ -92,12 +92,12 @@ stbtt_GetCodepointHMetrics(addr fontinfo, cint(codePoint), addr advance, addr leftBearing) result.glyphs[codePoint] = GlyphInfo( - dimension: NewVec2f(float32(image.width), float32(image.height)), + dimension: vec2(float32(image.width), float32(image.height)), uvs: [ - NewVec2f((coord.x + 0.5) / w, (coord.y + ih - 0.5) / h), - NewVec2f((coord.x + 0.5) / w, (coord.y + 0.5) / h), - NewVec2f((coord.x + iw - 0.5) / w, (coord.y + 0.5) / h), - NewVec2f((coord.x + iw - 0.5) / w, (coord.y + ih - 0.5) / h), + vec2((coord.x + 0.5) / w, (coord.y + ih - 0.5) / h), + vec2((coord.x + 0.5) / w, (coord.y + 0.5) / h), + vec2((coord.x + iw - 0.5) / w, (coord.y + 0.5) / h), + vec2((coord.x + iw - 0.5) / w, (coord.y + ih - 0.5) / h), ], topOffset: float32(topOffsets[codePoint]), leftOffset: float32(leftBearing) * result.fontscale, diff -r 01138e6257dd -r a89a70ea3da2 semicongine/text/textbox.nim --- a/semicongine/text/textbox.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/semicongine/text/textbox.nim Mon Jul 29 15:49:37 2024 +0700 @@ -63,10 +63,10 @@ if textbox.processedText[i] == Rune('\n'): offsetX = 0 offsetY -= textbox.font.lineAdvance - textbox.position.data[vertexOffset + 0] = NewVec3f() - textbox.position.data[vertexOffset + 1] = NewVec3f() - textbox.position.data[vertexOffset + 2] = NewVec3f() - textbox.position.data[vertexOffset + 3] = NewVec3f() + textbox.position.data[vertexOffset + 0] = vec3(0, 0, 0) + textbox.position.data[vertexOffset + 1] = vec3(0, 0, 0) + textbox.position.data[vertexOffset + 2] = vec3(0, 0, 0) + textbox.position.data[vertexOffset + 3] = vec3(0, 0, 0) inc lineIndex anchorX = case textbox.horizontalAlignment of Left: 0'f32 @@ -80,10 +80,10 @@ top = offsetY - glyph.topOffset bottom = offsetY - glyph.topOffset - glyph.dimension.y - textbox.position.data[vertexOffset + 0] = NewVec3f(left - anchorX, bottom - anchorY) - textbox.position.data[vertexOffset + 1] = NewVec3f(left - anchorX, top - anchorY) - textbox.position.data[vertexOffset + 2] = NewVec3f(right - anchorX, top - anchorY) - textbox.position.data[vertexOffset + 3] = NewVec3f(right - anchorX, bottom - anchorY) + textbox.position.data[vertexOffset + 0] = vec3(left - anchorX, bottom - anchorY, 0) + textbox.position.data[vertexOffset + 1] = vec3(left - anchorX, top - anchorY, 0) + textbox.position.data[vertexOffset + 2] = vec3(right - anchorX, top - anchorY, 0) + textbox.position.data[vertexOffset + 3] = vec3(right - anchorX, bottom - anchorY, 0) textbox.uv.data[vertexOffset + 0] = glyph.uvs[0] textbox.uv.data[vertexOffset + 1] = glyph.uvs[1] @@ -94,10 +94,10 @@ if i < textbox.processedText.len - 1: offsetX += textbox.font.kerning[(textbox.processedText[i], textbox.processedText[i + 1])] else: - textbox.position.data[vertexOffset + 0] = NewVec3f() - textbox.position.data[vertexOffset + 1] = NewVec3f() - textbox.position.data[vertexOffset + 2] = NewVec3f() - textbox.position.data[vertexOffset + 3] = NewVec3f() + textbox.position.data[vertexOffset + 0] = vec3(0, 0, 0) + textbox.position.data[vertexOffset + 1] = vec3(0, 0, 0) + textbox.position.data[vertexOffset + 2] = vec3(0, 0, 0) + textbox.position.data[vertexOffset + 3] = vec3(0, 0, 0) UpdateGPUBuffer(textbox.position) UpdateGPUBuffer(textbox.uv) textbox.lastRenderedText = textbox.processedText @@ -183,8 +183,8 @@ font: Font, text: T = default(T), scale: float32 = 1, - position: Vec3f = NewVec3f(), - color: Vec4f = NewVec4f(0, 0, 0, 1), + position: Vec3f = vec3(0, 0, 0), + color: Vec4f = vec4(0, 0, 0, 1), maxLen: int = text.len, verticalAlignment: VerticalAlignment = Center, horizontalAlignment: HorizontalAlignment = Center, diff -r 01138e6257dd -r a89a70ea3da2 tests/test_gltf.nim --- a/tests/test_gltf.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/tests/test_gltf.nim Mon Jul 29 15:49:37 2024 +0700 @@ -19,14 +19,14 @@ normal: Mat4 projection: Mat4 Material = object - color: Vec4f = NewVec4f(1, 1, 1, 1) + color: Vec4f = vec4(1, 1, 1, 1) # colorTexture: int32 = -1 metallic: float32 = 0 roughness: float32 = 0 # metallicRoughnessTexture: int32 = -1 # normalTexture: int32 = -1 # occlusionTexture: int32 = -1 - emissive: Vec4f = NewVec4f(0, 0, 0, 0) + emissive: Vec4f = vec4(0, 0, 0, 0) # emissiveTexture: int32 = -1 MainDescriptors = object materials: array[32, GPUValue[Material, UniformBuffer]] @@ -125,7 +125,7 @@ descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer) for mesh in mitems(gltfData.meshes): for primitive in mitems(mesh): - primitive[0].color = asGPUArray(newSeqWith(primitive[0].position.data.len, NewVec4f(1, 1, 1, 1)), VertexBuffer) + primitive[0].color = asGPUArray(newSeqWith(primitive[0].position.data.len, vec4(1, 1, 1, 1)), VertexBuffer) renderdata.AssignBuffers(primitive[0]) renderdata.AssignBuffers(descriptors) @@ -169,21 +169,21 @@ if KeyIsDown(A): sideward -= 2 if KeyIsDown(D): sideward += 2 - let camDir = (Rotate(camYaw, Y) * Rotate(camPitch, X)) * Z - let camDirSide = camDir.Cross(-Y).Normalized + let camDir = (rotate(camYaw, Y) * rotate(camPitch, X)) * Z + let camDirSide = camDir.cross(-Y).normalized camPos += camDir * forward * dt camPos += camDirSide * sideward * dt - let view = Rotate(-camPitch, X) * Rotate(-camYaw, Y) * Translate(-camPos) + let view = rotate(-camPitch, X) * rotate(-camYaw, Y) * translate(-camPos) descriptors.data.camera.data.view = view descriptors.data.camera.data.normal = view - descriptors.data.camera.data.projection = Projection(PI / 2, aspect = GetAspectRatio(), zNear = 0.01, zFar = 20) + descriptors.data.camera.data.projection = projection(PI / 2, aspect = GetAspectRatio(), zNear = 0.01, zFar = 20) UpdateGPUBuffer(descriptors.data.camera) WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, pipeline): WithBind(commandbuffer, (descriptors, ), pipeline): @@ -192,7 +192,7 @@ commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId, - transform = Rotate(PI / 2, Z) + transform = rotate(PI / 2, Z) ) # cleanup diff -r 01138e6257dd -r a89a70ea3da2 tests/test_rendering.nim --- a/tests/test_rendering.nim Mon Jul 29 15:04:52 2024 +0700 +++ b/tests/test_rendering.nim Mon Jul 29 15:49:37 2024 +0700 @@ -29,8 +29,8 @@ position: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec3f, VertexBuffer] var mesh = TriangleMesh( - position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), - color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), + position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), + color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), ) AssignBuffers(renderdata, mesh) renderdata.FlushAllMemory() @@ -42,7 +42,7 @@ WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, pipeline): @@ -82,22 +82,22 @@ pos: GPUArray[Vec3f, VertexBuffer] scale: GPUArray[float32, VertexBuffer] var tri = TriangleMesh( - position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), - color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), + position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), + color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), ) var quad = QuadMesh( - position: asGPUArray([NewVec3f(-0.3, -0.3), NewVec3f(-0.3, 0.3), NewVec3f(0.3, 0.3), NewVec3f(0.3, -0.3)], VertexBuffer), + position: asGPUArray([vec3(-0.3, -0.3, 0), vec3(-0.3, 0.3, 0), vec3(0.3, 0.3, 0), vec3(0.3, -0.3, 0)], VertexBuffer), indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), - color: asGPUArray([NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1), NewVec3f(1, 1, 1)], VertexBuffer), + color: asGPUArray([vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1)], VertexBuffer), ) var instancesA: Instances for n in 1..100: - instancesA.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32)) + instancesA.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32), 0) instancesA.scale.data.add rand(0.3'f32 .. 0.4'f32) var instancesB: Instances for n in 1..100: - instancesB.pos.data.add NewVec3f(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32)) + instancesB.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32), 0) instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) AssignBuffers(renderdata, tri) @@ -113,7 +113,7 @@ WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, pipeline): @@ -163,18 +163,18 @@ let W = BGRA([255'u8, 255'u8, 255'u8, 255'u8]) var quad = QuadMesh( - position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), + position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(-0.5, 0.5, 0), vec3(0.5, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), ) uniforms1 = asDescriptorSet( Uniforms( - material: asGPUValue(Material(baseColor: NewVec3f(1, 1, 1)), UniformBuffer), + material: asGPUValue(Material(baseColor: vec3(1, 1, 1)), UniformBuffer), texture1: Image[BGRA](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST), ) ) uniforms2 = asDescriptorSet( Uniforms( - material: asGPUValue(Material(baseColor: NewVec3f(0.5, 0.5, 0.5)), UniformBuffer), + material: asGPUValue(Material(baseColor: vec3(0.5, 0.5, 0.5)), UniformBuffer), texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]), ) ) @@ -196,7 +196,7 @@ WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, pipeline): @@ -256,12 +256,12 @@ indices: GPUArray[uint16, IndexBuffer] var quad = QuadMesh( - position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), + position: asGPUArray([vec3(-0.5, -0.5), vec3(-0.5, 0.5), vec3(0.5, 0.5), vec3(0.5, -0.5)], VertexBuffer), indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), ) var constset = asDescriptorSet( ConstSet( - constants: asGPUValue(Constants(offset: NewVec2f(-0.3, 0.2)), UniformBuffer), + constants: asGPUValue(Constants(offset: vec2(-0.3, 0.2)), UniformBuffer), ) ) let G = Gray([50'u8]) @@ -270,8 +270,8 @@ MainSet( renderSettings: asGPUValue(RenderSettings(brigthness: 0), UniformBufferMapped), material: [ - asGPUValue(Material(baseColor: NewVec3f(1, 1, 0)), UniformBuffer), - asGPUValue(Material(baseColor: NewVec3f(1, 0, 1)), UniformBuffer), + asGPUValue(Material(baseColor: vec3(1, 1, 0)), UniformBuffer), + asGPUValue(Material(baseColor: vec3(1, 0, 1)), UniformBuffer), ], texture1: [ Image[Gray](width: 2, height: 2, data: @[W, G, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST), @@ -310,7 +310,7 @@ WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, pipeline): @@ -358,8 +358,8 @@ color: GPUArray[Vec4f, VertexBuffer] let quad = @[ - NewVec3f(-0.5, -0.5), NewVec3f(-0.5, +0.5), NewVec3f(+0.5, +0.5), - NewVec3f(+0.5, +0.5), NewVec3f(+0.5, -0.5), NewVec3f(-0.5, -0.5), + vec3(-0.5, -0.5), vec3(-0.5, +0.5), vec3(+0.5, +0.5), + vec3(+0.5, +0.5), vec3(+0.5, -0.5), vec3(-0.5, -0.5), ] proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] = for v in data: @@ -371,34 +371,34 @@ normals: seq[Vec3f] # front, red - vertices.add quad.transf(Translate(0, 0, -0.5)) - colors.add newSeqWith(6, NewVec4f(1, 0, 0, 1)) - normals.add newSeqWith(6, NewVec3f(0, 0, -1)) + vertices.add quad.transf(translate(0, 0, -0.5)) + colors.add newSeqWith(6, vec4(1, 0, 0, 1)) + normals.add newSeqWith(6, vec3(0, 0, -1)) # back, cyan - vertices.add quad.transf(Rotate(PI, Y) * Translate(0, 0, -0.5)) - colors.add newSeqWith(6, NewVec4f(0, 1, 1, 1)) - normals.add newSeqWith(6, NewVec3f(0, 0, 1)) + vertices.add quad.transf(rotate(PI, Y) * translate(0, 0, -0.5)) + colors.add newSeqWith(6, vec4(0, 1, 1, 1)) + normals.add newSeqWith(6, vec3(0, 0, 1)) # right, green - vertices.add quad.transf(Rotate(PI / 2, Y) * Translate(0, 0, -0.5)) - colors.add newSeqWith(6, NewVec4f(0, 1, 0, 1)) - normals.add newSeqWith(6, NewVec3f(-1, 0, 0)) + vertices.add quad.transf(rotate(PI / 2, Y) * translate(0, 0, -0.5)) + colors.add newSeqWith(6, vec4(0, 1, 0, 1)) + normals.add newSeqWith(6, vec3(-1, 0, 0)) # left, magenta - vertices.add quad.transf(Rotate(-PI / 2, Y) * Translate(0, 0, -0.5)) - colors.add newSeqWith(6, NewVec4f(1, 0, 1, 1)) - normals.add newSeqWith(6, NewVec3f(1, 0, 0)) + vertices.add quad.transf(rotate(-PI / 2, Y) * translate(0, 0, -0.5)) + colors.add newSeqWith(6, vec4(1, 0, 1, 1)) + normals.add newSeqWith(6, vec3(1, 0, 0)) # bottom, blue - vertices.add quad.transf(Rotate(PI / 2, X) * Translate(0, 0, -0.5)) - colors.add newSeqWith(6, NewVec4f(0, 0, 1, 1)) - normals.add newSeqWith(6, NewVec3f(0, -1, 0)) + vertices.add quad.transf(rotate(PI / 2, X) * translate(0, 0, -0.5)) + colors.add newSeqWith(6, vec4(0, 0, 1, 1)) + normals.add newSeqWith(6, vec3(0, -1, 0)) # top, yellow - vertices.add quad.transf(Rotate(-PI / 2, X) * Translate(0, 0, -0.5)) - colors.add newSeqWith(6, NewVec4f(1, 1, 0, 1)) - normals.add newSeqWith(6, NewVec3f(0, 1, 0)) + vertices.add quad.transf(rotate(-PI / 2, X) * translate(0, 0, -0.5)) + colors.add newSeqWith(6, vec4(1, 1, 0, 1)) + normals.add newSeqWith(6, vec3(0, 1, 0)) var renderdata = InitRenderData() @@ -410,8 +410,8 @@ AssignBuffers(renderdata, mesh) var floor = Mesh( - position: asGPUArray(quad.transf(Scale(10, 10, 10) * Rotate(-PI / 2, X) * Translate(0, 0, 0.05)), VertexBuffer), - color: asGPUArray(newSeqWith(6, NewVec4f(0.1, 0.1, 0.1, 1)), VertexBuffer), + position: asGPUArray(quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)), VertexBuffer), + color: asGPUArray(newSeqWith(6, vec4(0.1, 0.1, 0.1, 1)), VertexBuffer), normals: asGPUArray(newSeqWith(6, Y), VertexBuffer), ) AssignBuffers(renderdata, floor) @@ -436,16 +436,16 @@ let tStartLoop = getMonoTime() - tStart uniforms1.data.data.data.mvp = ( - Projection(-PI / 2, GetAspectRatio(), 0.01, 100) * - Translate(0, 0, 2) * - Rotate(PI / 4, X) * - Rotate(PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y) + projection(-PI / 2, GetAspectRatio(), 0.01, 100) * + translate(0, 0, 2) * + rotate(PI / 4, X) * + rotate(PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y) ) UpdateGPUBuffer(uniforms1.data.data, flush = true) WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, pipeline): WithBind(commandbuffer, (uniforms1, ), pipeline): @@ -483,12 +483,12 @@ position: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec3f, VertexBuffer] var triangle = TriangleMesh( - position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), - color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), + position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), + color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), ) var lines = TriangleMesh( - position: asGPUArray([NewVec3f(-0.9, 0), NewVec3f(-0.05, -0.9), NewVec3f(0.05, -0.9), NewVec3f(0.9, 0)], VertexBuffer), - color: asGPUArray([NewVec3f(1, 1, 0), NewVec3f(1, 1, 0), NewVec3f(0, 1, 0), NewVec3f(0, 1, 0)], VertexBuffer), + position: asGPUArray([vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)], VertexBuffer), + color: asGPUArray([vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer), ) AssignBuffers(renderdata, triangle) AssignBuffers(renderdata, lines) @@ -502,7 +502,7 @@ var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, pipeline1): Render(commandbuffer = commandbuffer, pipeline = pipeline1, mesh = triangle) WithPipeline(commandbuffer, pipeline2): @@ -547,12 +547,12 @@ uv: GPUArray[Vec2f, VertexBuffer] var mesh = Quad( position: asGPUArray([ - NewVec3f(-0.8, -0.5), NewVec3f(-0.8, 0.5), NewVec3f(0.8, 0.5), - NewVec3f(0.8, 0.5), NewVec3f(0.8, -0.5), NewVec3f(-0.8, -0.5), + vec3(-0.8, -0.5), vec3(-0.8, 0.5), vec3(0.8, 0.5), + vec3(0.8, 0.5), vec3(0.8, -0.5), vec3(-0.8, -0.5), ], VertexBuffer), uv: asGPUArray([ - NewVec2f(0, 1), NewVec2f(0, 0), NewVec2f(1, 0), - NewVec2f(1, 0), NewVec2f(1, 1), NewVec2f(0, 1), + vec2(0, 1), vec2(0, 0), vec2(1, 0), + vec2(1, 0), vec2(1, 1), vec2(0, 1), ], VertexBuffer), ) AssignBuffers(renderdata, mesh) @@ -572,7 +572,7 @@ WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, pipeline): @@ -633,11 +633,11 @@ position: GPUArray[Vec2f, VertexBuffer] indices: GPUArray[uint16, IndexBuffer] var mesh = TriangleMesh( - position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(0, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), - color: asGPUArray([NewVec3f(0, 0, 1), NewVec3f(0, 1, 0), NewVec3f(1, 0, 0)], VertexBuffer), + position: asGPUArray([vec3(-0.5, -0.5), vec3(0, 0.5), vec3(0.5, -0.5)], VertexBuffer), + color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), ) var quad = QuadMesh( - position: asGPUArray([NewVec2f(-1, -1), NewVec2f(-1, 1), NewVec2f(1, 1), NewVec2f(1, -1)], VertexBuffer), + position: asGPUArray([vec2(-1, -1), vec2(-1, 1), vec2(1, 1), vec2(1, -1)], VertexBuffer), indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), ) var uniforms1 = asDescriptorSet( @@ -735,11 +735,11 @@ WithNextFrame(framebuffer, commandbuffer): - WithRenderPass(offscreenRP, offscreenFB, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(offscreenRP, offscreenFB, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, drawPipeline): Render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh) - WithRenderPass(presentRP, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): + WithRenderPass(presentRP, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): WithPipeline(commandbuffer, presentPipeline):