Created
February 24, 2017 02:57
-
-
Save jay18001/cc217a294e61c4345d60c9445a2981a1 to your computer and use it in GitHub Desktop.
Swift version of helper class from Ray Wenderlich: Metal Tutorial with Swift 3 Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import GLKit | |
extension Float { | |
var radians: Float { | |
return GLKMathDegreesToRadians(self) | |
} | |
} | |
class Matrix4 { | |
var glkMatrix: GLKMatrix4 | |
static func makePerspectiveView(angle: Float, aspectRatio: Float, nearZ: Float, farZ: Float) -> Matrix4 { | |
let matrix = Matrix4() | |
matrix.glkMatrix = GLKMatrix4MakePerspective(angle, aspectRatio, nearZ, farZ) | |
return matrix | |
} | |
init() { | |
glkMatrix = GLKMatrix4Identity | |
} | |
func copy() -> Matrix4 { | |
let newMatrix = Matrix4() | |
newMatrix.glkMatrix = self.glkMatrix | |
return newMatrix | |
} | |
func scale(x: Float, y: Float, z: Float) { | |
glkMatrix = GLKMatrix4Scale(glkMatrix, x, y, z) | |
} | |
func rotateAround(x: Float, y: Float, z: Float) { | |
glkMatrix = GLKMatrix4Rotate(glkMatrix, x, 1, 0, 0) | |
glkMatrix = GLKMatrix4Rotate(glkMatrix, y, 0, 1, 0) | |
glkMatrix = GLKMatrix4Rotate(glkMatrix, z, 0, 0, 1) | |
} | |
func translate(x: Float, y: Float, z: Float) { | |
glkMatrix = GLKMatrix4Translate(glkMatrix, x, y, z) | |
} | |
func multiply(left: Matrix4) { | |
glkMatrix = GLKMatrix4Multiply(left.glkMatrix, glkMatrix) | |
} | |
var raw: [Float] { | |
let value = glkMatrix.m | |
//I cannot think of a better way of doing this | |
return [value.0, value.1, value.2, value.3, value.4, value.5, value.6, value.7, value.8, value.9, value.10, value.11, value.12, value.13, value.14, value.15] | |
} | |
func transpose() { | |
glkMatrix = GLKMatrix4Transpose(glkMatrix) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was a drop-in replacement for the Objective-C class. Nicely done 👍