-
-
Save shsteven/e62d09dc989e212b2883f9227ff3071c to your computer and use it in GitHub Desktop.
swift: float4x4 extension to scale, rotate, translate 4x4 matrix
This file contains 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
// | |
// simd+ext.swift | |
// | |
// Created by Kaz Yoshikawa on 11/6/15. | |
// | |
// | |
import Foundation | |
import simd | |
import GLKit | |
extension float4x4 { | |
static func makeScale(x: Float, _ y: Float, _ z: Float) -> float4x4 { | |
return unsafeBitCast(GLKMatrix4MakeScale(x, y, z), float4x4.self) | |
} | |
static func makeRotate(radians: Float, _ x: Float, _ y: Float, _ z: Float) -> float4x4 { | |
return unsafeBitCast(GLKMatrix4MakeRotation(radians, x, y, z), float4x4.self) | |
} | |
static func makeTranslation(x: Float, _ y: Float, _ z: Float) -> float4x4 { | |
return unsafeBitCast(GLKMatrix4MakeTranslation(x, y, z), float4x4.self) | |
} | |
static func makePerspective(fovyRadians: Float, _ aspect: Float, _ nearZ: Float, _ farZ: Float) -> float4x4 { | |
return unsafeBitCast(GLKMatrix4MakePerspective(fovyRadians, aspect, nearZ, farZ), float4x4.self) | |
} | |
static func makeFrustum(left: Float, _ right: Float, _ bottom: Float, _ top: Float, _ nearZ: Float, _ farZ: Float) -> float4x4 { | |
return unsafeBitCast(GLKMatrix4MakeFrustum(left, right, bottom, top, nearZ, farZ), float4x4.self) | |
} | |
static func makeOrtho(left: Float, _ right: Float, _ bottom: Float, _ top: Float, _ nearZ: Float, _ farZ: Float) -> float4x4 { | |
return unsafeBitCast(GLKMatrix4MakeOrtho(left, right, bottom, top, nearZ, farZ), float4x4.self) | |
} | |
static func makeLookAt(eyeX: Float, _ eyeY: Float, _ eyeZ: Float, _ centerX: Float, _ centerY: Float, _ centerZ: Float, _ upX: Float, _ upY: Float, _ upZ: Float) -> float4x4 { | |
return unsafeBitCast(GLKMatrix4MakeLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ), float4x4.self) | |
} | |
func scale(x: Float, y: Float, z: Float) -> float4x4 { | |
return self * float4x4.makeScale(x, y, z) | |
} | |
func rotate(radians: Float, _ x: Float, _ y: Float, _ z: Float) -> float4x4 { | |
return self * float4x4.makeRotate(radians, x, y, z) | |
} | |
func translate(x: Float, _ y: Float, _ z: Float) -> float4x4 { | |
return self * float4x4.makeTranslation(x, y, z) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment