Skip to content

Commit

Permalink
Merge pull request #64 from treeform/dev
Browse files Browse the repository at this point in the history
Fix for lookAt: #63
  • Loading branch information
treeform committed Mar 1, 2023
2 parents 6f23206 + 5c34751 commit b078254
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/vmath.nim
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ proc `zmod`*(a, b: float32): float32 =
template lowerType(a: typed): string =
($type(a)).toLowerAscii()

template genConstructor(lower, upper, typ: untyped) =
template genVecConstructor*(lower, upper, typ: untyped) =
## Generate vector constructor for your own type.

proc `lower 2`*(): `upper 2` = gvec2[typ](typ(0), typ(0))
proc `lower 3`*(): `upper 3` = gvec3[typ](typ(0), typ(0), typ(0))
Expand Down Expand Up @@ -511,11 +512,11 @@ template genConstructor(lower, upper, typ: untyped) =
proc `$`*(a: `upper 4`): string =
lowerType(a) & "(" & $a.x & ", " & $a.y & ", " & $a.z & ", " & $a.w & ")"

genConstructor(bvec, BVec, bool)
genConstructor(ivec, IVec, int32)
genConstructor(uvec, UVec, uint32)
genConstructor(vec, Vec, float32)
genConstructor(dvec, DVec, float64)
genVecConstructor(bvec, BVec, bool)
genVecConstructor(ivec, IVec, int32)
genVecConstructor(uvec, UVec, uint32)
genVecConstructor(vec, Vec, float32)
genVecConstructor(dvec, DVec, float64)

proc vec2*(ivec2: Ivec2): Vec2 =
vec2(ivec2.x.float32, ivec2.y.float32)
Expand Down Expand Up @@ -872,8 +873,8 @@ proc matToString[T](a: T, n: int): string =
result.setLen(result.len - 2)
result.add "\n)"

template genMatConstructor(lower, upper, T: untyped) =

template genMatConstructor*(lower, upper, T: untyped) =
## Generate matrix constructor for your own type.
proc `lower 2`*(
m00, m01,
m10, m11: T
Expand Down Expand Up @@ -1345,7 +1346,7 @@ proc lookAt*[T](eye, center, up: GVec3[T]): GMat4[T] =
centerz = center[2]

if eyex == centerx and eyey == centery and eyez == centerz:
return mat4[T]()
return

var
# vec3.direction(eye, center, z)
Expand Down Expand Up @@ -1434,7 +1435,8 @@ type
Quat* = GVec4[float32]
DQuat* = GVec4[float64]

template genQuatConstructor(lower, upper, typ: untyped) =
template genQuatConstructor*(lower, upper, typ: untyped) =
## Generate quaternion constructor for your own type.
proc `lower`*(): `upper` = gvec4[typ](0, 0, 0, 1)
proc `lower`*(x, y, z, w: typ): `upper` = gvec4[typ](x, y, z, w)
proc `lower`*(x: typ): `upper` = gvec4[typ](x, x, x, x)
Expand Down
4 changes: 4 additions & 0 deletions tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,10 @@ block:
)
doAssert lookAt(vec3(0, 0, 1), vec3(0, 0, 0)).quat ~= quat(0.0, 0.0, 0.0, 1.0)

let
a = lookAt(vec3(1, 2, 3), vec3(0, 0, 0))
b = lookAt(dvec3(1, 2, 3), dvec3(0, 0, 0))

doAssert ortho[float32](-1, 1, 1, -1, -1000, 1000) ~= mat4(
1.0, 0.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0,
Expand Down

0 comments on commit b078254

Please sign in to comment.