Beginning Metal - Part 7: Matrices 2D to 3D | Ray Wenderlich

In this beginning Metal video tutorial, you'll learn how to use matrices to translate, scale and rotate models.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3537-beginning-metal/lessons/7

hi,
In directx we have ā€œsimd friendly DirectXMathā€ for OpenGL es we have GLKIT maths.
is there any library for use with metal at this stage or do we have to wrap our own matrix functions for now.

thanks

@hsg - there is the SIMD library. matrix_float4x4 for example is a SIMD data type.

You can say for example:

let m = matrix_identity_float4x4
let n = m.inverse
let t = m.transpose
let d = m.determinant
let q = m * n

Etc.

MatrixMath.swift has an extension to matrix_float4x4 which has methods to set up the necessary matrices.

What exactly are you looking for?

hi, thanks for getting back to me so quickly, enjoying. course so far.

yes if have seen MatrixMath.swift (have written some algebra libs in the far distant past). However I really enjoyed relying on bundled algebra libraries such as dxMaths (directx) and glKit(OpenGL es). These had many tried and tested helper functions such as creating projection matrices and rotations around a arbitrary axis etc. I just remember in the past when I had to create my own I spent a huge amount of time debugging themā€¦

so my question

  1. simd - I have always treated this as a black box and never had to time to research, I have just relied on helper libs like dxMath to handle. - thanks I get this now ( you are using types taken from the simd lib)

  2. is metalkit going, to get/does it have, a helper library for projection and all the various transform matrices - just like GLKit has. - MatrixMath.swift is one you have created? not bundled with metal Api.

thanks h
Where are you c++, all is forgiven! :slight_smile:

@hsg - this answer took a bit longer :slight_smile: - Australian zā€‹:zzz:.

You might want to investigate Accelerate - it goes beyond linear algebra. Iā€™m afraid I do anything math-y the hard way so that I know whatā€™s happening, but itā€™s very inefficient I know. As yet my math is lacking, so I canā€™t be of great help; but Iā€™m working on it :smile: .

In 2014, Apple updated Accelerate, and thereā€™s a video: WWDC14 - Videos - Apple Developer

2017: WWDC17 - Videos - Apple Developer

Jeff Biggus made some playgrounds, but you might need to update the Swift code to be able to run them: GitHub - hyperjeff/Accelerate-in-Swift: Swift example codes for the Accelerate.framework

I find it frustrating that thereā€™s not more documentation and explanations, but I suppose if you know math already, then itā€™s not necessary.

You could use GLKit, I guess - I did when I started, but didnā€™t like the opacity. But be careful to realise that the Z axis in OpenGL is -1 to 1, whereas in Metal itā€™s 0 to 1. I work with perspective projection, which is why I havenā€™t noticed a great deal of clipping, but according to http://blog.athenstean.com/post/135771439196/from-opengl-to-metal-the-projection-matrix if you work with orthogonal projection it makes a difference.

#awesome

many thanks, didnā€™t know about accelerate.
fingers crossed for additional support funcs in MetalKit in future.

Thanks for pointing out the problem with using glKit, after further investigation looks like ndc in metal is the same as directx (0 to 1), the projection matrices in apple sample code match the projection matrices defined in DirectX documentation. (looks like Vulcan has gone the same way).

Thanks again for the saveā€¦had forgotten about this !!

hello Caroline,

thanks for the awesome course!

I need to inverse a matrix_float4x4 but I get this compile error: value of type ā€˜matrix_float4x4ā€™ has no member ā€˜inverseā€™
am I missing something?

thanks!!

@luizelias - Iā€™m glad youā€™re enjoying the course :smiley:

Try something like:

let matrix = matrix_identity_float4x4
let inverse = matrix.inverse

Btw, you can now call it just float4x4 and not matrix_float4x4. They are both type aliases of simd_float4x4

This video course is more than six months old, so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]