mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 10:59:39 +00:00 
			
		
		
		
	cube now spinn in 3d
This commit is contained in:
		@ -6,6 +6,9 @@ add_executable(Voxelotl MACOSX_BUNDLE
 | 
			
		||||
  shadertypes.h
 | 
			
		||||
  shader.metal
 | 
			
		||||
 | 
			
		||||
  FloatExtensions.swift
 | 
			
		||||
  Matrix4x4.swift
 | 
			
		||||
 | 
			
		||||
  NSImageLoader.swift
 | 
			
		||||
  Renderer.swift
 | 
			
		||||
  FPSCalculator.swift
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										4
									
								
								Sources/Voxelotl/FloatExtensions.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								Sources/Voxelotl/FloatExtensions.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
public extension FloatingPoint {
 | 
			
		||||
  @inline(__always) var degrees: Self { self * (180 / Self.pi) }
 | 
			
		||||
  @inline(__always) var radians: Self { self * (Self.pi / 180) }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										60
									
								
								Sources/Voxelotl/Matrix4x4.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								Sources/Voxelotl/Matrix4x4.swift
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,60 @@
 | 
			
		||||
import simd
 | 
			
		||||
 | 
			
		||||
public extension simd_float4x4 {
 | 
			
		||||
  typealias T = Float
 | 
			
		||||
 | 
			
		||||
  @inline(__always) static var identity: Self { matrix_identity_float4x4 }
 | 
			
		||||
 | 
			
		||||
  @inline(__always) static func translate(_ v: SIMD3<T>) -> Self {
 | 
			
		||||
    Self(
 | 
			
		||||
      .init(  1,   0,   0, 0),
 | 
			
		||||
      .init(  0,   1,   0, 0),
 | 
			
		||||
      .init(  0,   0,   1, 0),
 | 
			
		||||
      .init(v.x, v.y, v.z, 1))
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @inline(__always) static func scale(_ v: SIMD3<T>) -> Self { Self(diagonal: .init(v.x, v.y, v.z, 1)) }
 | 
			
		||||
  @inline(__always) static func scale(_ s: T) -> Self { Self(diagonal: .init(s, s, s, 1)) }
 | 
			
		||||
 | 
			
		||||
  static func rotate(x theta: T) -> Self {
 | 
			
		||||
    let c = cos(theta), s = sin(theta)
 | 
			
		||||
    return Self(
 | 
			
		||||
      .init(1,  0, 0, 0),
 | 
			
		||||
      .init(0,  c, s, 0),
 | 
			
		||||
      .init(0, -s, c, 0),
 | 
			
		||||
      .init(0,  0, 0, 1))
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static func rotate(y theta: T) -> Self {
 | 
			
		||||
    let c = cos(theta), s = sin(theta)
 | 
			
		||||
    return Self(
 | 
			
		||||
      .init(c, 0, -s, 0),
 | 
			
		||||
      .init(0, 1,  0, 0),
 | 
			
		||||
      .init(s, 0,  c, 0),
 | 
			
		||||
      .init(0, 0,  0, 1))
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static func rotate(z theta: T) -> Self {
 | 
			
		||||
    let c = cos(theta), s = sin(theta)
 | 
			
		||||
    return Self(
 | 
			
		||||
      .init(c, -s, 0, 0),
 | 
			
		||||
      .init(s,  c, 0, 0),
 | 
			
		||||
      .init(0,  0, 1, 0),
 | 
			
		||||
      .init(0,  0, 0, 1))
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static func perspective(verticalFov: T, aspect: T, near: T, far: T) -> Self {
 | 
			
		||||
    let h = 1 / tan(verticalFov * T(0.5))
 | 
			
		||||
    let w = h / aspect
 | 
			
		||||
 | 
			
		||||
    let invClipRange = 1 / (far - near)
 | 
			
		||||
    let z = -(far + near) * invClipRange
 | 
			
		||||
    let z2 = -(2 * far * near) * invClipRange
 | 
			
		||||
 | 
			
		||||
    return .init(
 | 
			
		||||
      .init(w, 0,  0,  0),
 | 
			
		||||
      .init(0, h,  0,  0),
 | 
			
		||||
      .init(0, 0,  z, -1),
 | 
			
		||||
      .init(0, 0, z2,  0))
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -217,10 +217,23 @@ class Renderer {
 | 
			
		||||
      zfar: -1.0)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  var time: Float = 0  //FIXME: temp
 | 
			
		||||
 | 
			
		||||
  func paint() throws {
 | 
			
		||||
    var uniforms = ShaderUniforms(
 | 
			
		||||
      model: .init(diagonal: .init(0.5, 0.5, 0.5, 1.0)),
 | 
			
		||||
      projView: matrix_identity_float4x4)
 | 
			
		||||
    let projection = matrix_float4x4.perspective(
 | 
			
		||||
      verticalFov: Float(90.0).radians,
 | 
			
		||||
      aspect: Float(self.viewport.width / self.viewport.height),
 | 
			
		||||
      near: 0.1,
 | 
			
		||||
      far: 10)
 | 
			
		||||
    let view = matrix_float4x4.identity
 | 
			
		||||
    let model: matrix_float4x4 =
 | 
			
		||||
      .translate(.init(0, sin(time * 0.5) * 0.5, -2)) *
 | 
			
		||||
      .scale(0.5) *
 | 
			
		||||
      .rotate(y: time)
 | 
			
		||||
 | 
			
		||||
    time += 0.025
 | 
			
		||||
 | 
			
		||||
    var uniforms = ShaderUniforms(model: model, projView: projection * view)
 | 
			
		||||
 | 
			
		||||
    guard let rt = layer.nextDrawable() else {
 | 
			
		||||
      throw RendererError.drawFailure("Failed to get next drawable render target")
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user