Mercurial > games > semicongine
comparison src/zamikongine/math/matrix.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 | b55d6ecde79d |
children | 71bbe11d8de8 |
comparison
equal
deleted
inserted
replaced
21:316923e9247c | 22:b45a5d338cd0 |
---|---|
337 m[0, 1], m[1, 1], m[2, 1], m[3, 1], | 337 m[0, 1], m[1, 1], m[2, 1], m[3, 1], |
338 m[0, 2], m[1, 2], m[2, 2], m[3, 2], | 338 m[0, 2], m[1, 2], m[2, 2], m[3, 2], |
339 m[0, 3], m[1, 3], m[2, 3], m[3, 3], | 339 m[0, 3], m[1, 3], m[2, 3], m[3, 3], |
340 ]) | 340 ]) |
341 | 341 |
342 func translate2d*[T](x, y: T): Mat33[T] = Mat33[T](data: [ | |
343 T(1), T(0), x, | |
344 T(0), T(1), y, | |
345 T(0), T(0), T(1), | |
346 ]) | |
347 func scale2d*[T](sx, sy: T): Mat33[T] = Mat33[T](data: [ | |
348 sx, T(0), T(0), | |
349 T(0), sy, T(0), | |
350 T(0), T(0), T(1), | |
351 ]) | |
352 func rotate2d*[T](angle: T): Mat33[T] = Mat33[T](data: [ | |
353 cos(angle), -sin(angle), T(0), | |
354 sin(angle), cos(angle), T(0), | |
355 T(0), T(0), T(1), | |
356 ]) | |
357 func translate3d*[T](x, y, z: T): Mat44[T] = Mat44[T](data: [ | |
358 T(1), T(0), T(0), x, | |
359 T(0), T(1), T(0), y, | |
360 T(0), T(0), T(1), z, | |
361 T(0), T(0), T(0), T(1), | |
362 ]) | |
363 func scale3d*[T](sx, sy, sz: T): Mat44[T] = Mat44[T](data: [ | |
364 sx, T(0), T(0), T(0), | |
365 T(0), sy, T(0), T(0), | |
366 T(0), T(0), sz, T(0), | |
367 T(0), T(0), T(0), T(1), | |
368 ]) | |
369 func rotate3d*[T](angle: T, a: Vec3[T]): Mat44[T] = | |
370 let | |
371 cosa = cos(angle) | |
372 sina = sin(angle) | |
373 x = a.x | |
374 y = a.y | |
375 z = a.z | |
376 Mat44[T](data: [ | |
377 x * x * (1 - cosa) + cosa, y * x * (1 - cosa) - z * sina, z * x * (1 - cosa) + y * sina, T(0), | |
378 x * y * (1 - cosa) + z * sina, y * y * (1 - cosa) + cosa, z * y * (1 - cosa) - x * sina, T(0), | |
379 x * z * (1 - cosa) - y * sina, y * z * (1 - cosa) + x * sina, z * z * (1 - cosa) + cosa, T(0), | |
380 T(0), T(0), T(0), 1, | |
381 ]) | |
382 | |
383 | |
342 # call e.g. Mat32[int]().randomized() to get a random matrix | 384 # call e.g. Mat32[int]().randomized() to get a random matrix |
343 template makeRandomInit(mattype: typedesc) = | 385 template makeRandomInit(mattype: typedesc) = |
344 proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = | 386 proc randomized*[T: SomeInteger](m: mattype[T]): mattype[T] = |
345 for i in 0 ..< result.data.len: | 387 for i in 0 ..< result.data.len: |
346 result.data[i] = rand(low(typeof(m.data[0])) .. high(typeof(m.data[0]))) | 388 result.data[i] = rand(low(typeof(m.data[0])) .. high(typeof(m.data[0]))) |