Skip to content

Instantly share code, notes, and snippets.

@codelynx
Last active November 17, 2021 18:56
Show Gist options
  • Save codelynx/908fd30c93e40ea6408b to your computer and use it in GitHub Desktop.
Save codelynx/908fd30c93e40ea6408b to your computer and use it in GitHub Desktop.
swift: float4x4 extension to scale, rotate, translate 4x4 matrix
//
// 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