Created
June 1, 2020 05:30
-
-
Save ooPo/0ae8ee9698883c56eee02568378c3fc1 to your computer and use it in GitHub Desktop.
OpenGL-like Matrix Functions in Swift
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
// | |
// MatrixMath.swift | |
// by Naomi Peori <naomi@peori.ca> | |
// | |
import Foundation | |
// | |
// Helper Functions | |
// | |
func degreesToRadians( _ degrees: Float ) -> Float | |
{ | |
return ( degrees / 180 ) * .pi | |
} | |
// | |
// Matrix Functions | |
// | |
func matrixFrustum( _ left: Float, _ right: Float, _ bottom: Float, _ top: Float, _ znear: Float, _ zfar: Float ) -> matrix_float4x4 | |
{ | |
let A = (2.0 * znear) / (right - left) | |
let B = (2.0 * znear) / (top - bottom) | |
let C = (right + left) / (right - left) | |
let D = (top + bottom) / (top - bottom) | |
let E = (-zfar - znear) / (zfar - znear) | |
let F = (-2.0 * znear * zfar) / (zfar - znear) | |
return matrix_float4x4.init( columns:( | |
vector_float4( A, 0, 0, 0 ), | |
vector_float4( 0, B, 0, 0 ), | |
vector_float4( C, D, E, -1 ), | |
vector_float4( 0, 0, F, 0 ))) | |
} | |
func matrixIdentity() -> matrix_float4x4 | |
{ | |
return matrix_float4x4.init( columns:( | |
vector_float4( 1, 0, 0, 0 ), | |
vector_float4( 0, 1, 0, 0 ), | |
vector_float4( 0, 0, 1, 0 ), | |
vector_float4( 0, 0, 0, 1 ))) | |
} | |
func matrixRotate( _ angle: Float, _ x: Float, _ y: Float, _ z: Float ) -> matrix_float4x4 | |
{ | |
let radians = degreesToRadians( angle ) | |
let normalized = normalize( vector_float3( x, y, z ) ) | |
return matrixRotateX( radians * normalized[ 0 ] ) * matrixRotateY( radians * normalized[ 1 ] ) * matrixRotateZ( radians * normalized[ 2 ] ); | |
} | |
func matrixRotateX( _ x: Float ) -> matrix_float4x4 | |
{ | |
return matrix_float4x4.init( columns:( | |
vector_float4( 1, 0, 0, 0 ), | |
vector_float4( 0, cos(x), -sin(x), 0 ), | |
vector_float4( 0, sin(x), cos(x), 0 ), | |
vector_float4( 0, 0, 0, 1 ))) | |
} | |
func matrixRotateY( _ y: Float ) -> matrix_float4x4 | |
{ | |
return matrix_float4x4.init( columns:( | |
vector_float4( cos(y), 0, sin(y), 0 ), | |
vector_float4( 0, 1, 0, 0 ), | |
vector_float4( -sin(y), 0, cos(y), 0 ), | |
vector_float4( 0, 0, 0, 1 ))) | |
} | |
func matrixRotateZ( _ z: Float ) -> matrix_float4x4 | |
{ | |
return matrix_float4x4.init( columns:( | |
vector_float4( cos(z), -sin(z), 0, 0 ), | |
vector_float4( sin(z), cos(z), 0, 0 ), | |
vector_float4( 0, 0, 1, 0 ), | |
vector_float4( 0, 0, 0, 1 ))) | |
} | |
func matrixScale( _ x: Float, _ y: Float, _ z: Float ) -> matrix_float4x4 | |
{ | |
return matrix_float4x4.init( columns:( | |
vector_float4( x, 0, 0, 0 ), | |
vector_float4( 0, y, 0, 0 ), | |
vector_float4( 0, 0, z, 0 ), | |
vector_float4( 0, 0, 0, 1 ))) | |
} | |
func matrixTranslate( _ x: Float, _ y: Float, _ z: Float ) -> matrix_float4x4 | |
{ | |
return matrix_float4x4.init( columns:( | |
vector_float4( 1, 0, 0, 0 ), | |
vector_float4( 0, 1, 0, 0 ), | |
vector_float4( 0, 0, 1, 0 ), | |
vector_float4( x, y, z, 1 ))) | |
} | |
func matrixPerspective( _ fov: Float, _ aspect: Float, _ znear: Float, _ zfar: Float ) -> matrix_float4x4 | |
{ | |
let ymax = znear * tan( degreesToRadians( fov ) / 2 ); | |
let xmax = ymax * aspect; | |
return matrixFrustum( -xmax, xmax, -ymax, ymax, znear, zfar ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment