Add Terrain class that generates terrain
This commit is contained in:
parent
31f2258ec3
commit
2361199267
2 changed files with 52 additions and 0 deletions
|
@ -13,6 +13,7 @@
|
|||
C0C15A9D218DDDC3007494E2 /* TerrainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C15A9B218DDDC3007494E2 /* TerrainViewController.swift */; };
|
||||
C0C15AA0218DE0B2007494E2 /* Renderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C15A9F218DE0B2007494E2 /* Renderer.swift */; };
|
||||
C0C15AA2218DE21E007494E2 /* Math.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C15AA1218DE21E007494E2 /* Math.swift */; };
|
||||
C0C15AA4218DE615007494E2 /* Terrain.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C15AA3218DE615007494E2 /* Terrain.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
|
@ -25,6 +26,7 @@
|
|||
C0C15A9B218DDDC3007494E2 /* TerrainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerrainViewController.swift; sourceTree = "<group>"; };
|
||||
C0C15A9F218DE0B2007494E2 /* Renderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Renderer.swift; sourceTree = "<group>"; };
|
||||
C0C15AA1218DE21E007494E2 /* Math.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Math.swift; sourceTree = "<group>"; };
|
||||
C0C15AA3218DE615007494E2 /* Terrain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Terrain.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
@ -60,6 +62,7 @@
|
|||
C0C15A8D218DDD85007494E2 /* AppDelegate.swift */,
|
||||
C0C15A9B218DDDC3007494E2 /* TerrainViewController.swift */,
|
||||
C0C15A9F218DE0B2007494E2 /* Renderer.swift */,
|
||||
C0C15AA3218DE615007494E2 /* Terrain.swift */,
|
||||
C0C15AA1218DE21E007494E2 /* Math.swift */,
|
||||
C0C15A8F218DDD87007494E2 /* Assets.xcassets */,
|
||||
C0C15A91218DDD87007494E2 /* MainMenu.xib */,
|
||||
|
@ -141,6 +144,7 @@
|
|||
files = (
|
||||
C0C15A9D218DDDC3007494E2 /* TerrainViewController.swift in Sources */,
|
||||
C0C15AA0218DE0B2007494E2 /* Renderer.swift in Sources */,
|
||||
C0C15AA4218DE615007494E2 /* Terrain.swift in Sources */,
|
||||
C0C15A8E218DDD85007494E2 /* AppDelegate.swift in Sources */,
|
||||
C0C15AA2218DE21E007494E2 /* Math.swift in Sources */,
|
||||
);
|
||||
|
|
48
Terrain/Terrain.swift
Normal file
48
Terrain/Terrain.swift
Normal file
|
@ -0,0 +1,48 @@
|
|||
//
|
||||
// Terrain.swift
|
||||
// Terrain
|
||||
//
|
||||
// Created by Eryn Wells on 11/3/18.
|
||||
// Copyright © 2018 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class Terrain: NSObject {
|
||||
override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
/// Generate a grid of triangles and place the result in the given buffer.
|
||||
/// @param buffer The buffer to copy the results into
|
||||
/// @param size The size of the grid. Each square is a pair of triangles.
|
||||
func generateVertexes(intoBuffer buffer: MTLBuffer, size: CGSize) {
|
||||
let VertexesPerCell = 6
|
||||
|
||||
let float3Stride = MemoryLayout<Float3>.stride
|
||||
let (width, height) = (Int(size.width), Int(size.height))
|
||||
let expectedCount = VertexesPerCell * width * height
|
||||
let expectedLength = float3Stride * expectedCount
|
||||
guard buffer.length >= expectedLength else {
|
||||
fatalError("Terrain.generateVertexes: buffer must be at least \(expectedLength) bytes to fix grid of size \(size)")
|
||||
}
|
||||
|
||||
var vertexes = [Float3]()
|
||||
vertexes.reserveCapacity(expectedCount)
|
||||
|
||||
let (cellWidth, cellHeight) = (Float(2.0) / Float(width), Float(2.0) / Float(height))
|
||||
for y in 0..<Int(size.height) {
|
||||
for x in 0..<Int(size.width) {
|
||||
let base = VertexesPerCell * (y * width + x)
|
||||
vertexes[base+0] = Float3(x: Float(x) * cellWidth, y: Float(y) * cellHeight, z: 0.0)
|
||||
vertexes[base+1] = Float3(x: Float(x) * cellWidth, y: Float(y) * cellHeight + cellHeight, z: 0.0)
|
||||
vertexes[base+2] = Float3(x: Float(x) * cellWidth + cellWidth, y: Float(y) * cellHeight, z: 0.0)
|
||||
vertexes[base+3] = Float3(x: Float(x) * cellWidth + cellWidth, y: Float(y) * cellHeight, z: 0.0)
|
||||
vertexes[base+4] = Float3(x: Float(x) * cellWidth, y: Float(y) * cellHeight + cellHeight, z: 0.0)
|
||||
vertexes[base+5] = Float3(x: Float(x) * cellWidth + cellWidth, y: Float(y) * cellHeight + cellHeight, z: 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(buffer.contents(), vertexes, expectedLength)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue