Get all the pieces together

Still doesn't render though. :\
This commit is contained in:
Eryn Wells 2018-11-03 14:01:34 -04:00
parent 8a664fff70
commit 67134c157b
5 changed files with 158 additions and 22 deletions

View file

@ -9,40 +9,52 @@
import Cocoa
class Terrain: NSObject {
static let vertexesPerCell = 6
var buffer: MTLBuffer?
override init() {
super.init()
}
func vertexCount(forGridSize size: CGSize) -> Int {
let (width, height) = (Int(size.width), Int(size.height))
let count = Terrain.vertexesPerCell * width * height
return count
}
func minimumBufferSize(forGridSize size: CGSize) -> Int {
let float3Stride = MemoryLayout<Float3>.stride
let count = vertexCount(forGridSize: size)
let minimumLength = float3Stride * count
return minimumLength
}
/// 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
let expectedLength = minimumBufferSize(forGridSize: size)
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)
vertexes.reserveCapacity(vertexCount(forGridSize: size))
let (cellWidth, cellHeight) = (Float(2.0) / Float(width), Float(2.0) / Float(height))
let (cellWidth, cellHeight) = (Float(2.0) / Float(size.width), Float(2.0) / Float(size.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)
vertexes.append(Float3(x: Float(x) * cellWidth, y: Float(y) * cellHeight, z: 0.0))
vertexes.append(Float3(x: Float(x) * cellWidth, y: Float(y) * cellHeight + cellHeight, z: 0.0))
vertexes.append(Float3(x: Float(x) * cellWidth + cellWidth, y: Float(y) * cellHeight, z: 0.0))
vertexes.append(Float3(x: Float(x) * cellWidth + cellWidth, y: Float(y) * cellHeight, z: 0.0))
vertexes.append(Float3(x: Float(x) * cellWidth, y: Float(y) * cellHeight + cellHeight, z: 0.0))
vertexes.append(Float3(x: Float(x) * cellWidth + cellWidth, y: Float(y) * cellHeight + cellHeight, z: 0.0))
}
}
self.buffer = buffer
memcpy(buffer.contents(), vertexes, expectedLength)
}
}