//
//  MBECubeMesh.m
//  Mipmapping
//
//  Created by Warren Moore on 11/8/14.
//  Copyright (c) 2014 Metal By Example. All rights reserved.
//------------------------------------------------------------------------
//  converted to Swift by Jamnitzer (Jim Wrenholt)
//------------------------------------------------------------------------
import UIKit
import Metal
import simd
import Accelerate

//------------------------------------------------------------------------------
class MBJCubeMesh : MBJMesh
{
    let vertices:[Float] = [
        // -X
        -0.5, -0.5, -0.5, 1.0, -1.0,  0.0,  0.0, 0.0, 0.0, 0.0,
        -0.5, -0.5,  0.5, 1.0, -1.0,  0.0,  0.0, 0.0, 0.0, 1.0,
        -0.5,  0.5,  0.5, 1.0, -1.0,  0.0,  0.0, 0.0, 1.0, 1.0,
        -0.5,  0.5, -0.5, 1.0, -1.0,  0.0,  0.0, 0.0, 1.0, 0.0,
        // +X
        0.5, -0.5,  0.5, 1.0,  1.0,  0.0,  0.0, 0.0, 0.0, 0.0,
        0.5, -0.5, -0.5, 1.0,  1.0,  0.0,  0.0, 0.0, 0.0, 1.0,
        0.5,  0.5, -0.5, 1.0,  1.0,  0.0,  0.0, 0.0, 1.0, 1.0,
        0.5,  0.5,  0.5, 1.0,  1.0,  0.0,  0.0, 0.0, 1.0, 0.0,
        // -Y
        -0.5, -0.5, -0.5, 1.0,  0.0, -1.0,  0.0, 0.0, 0.0, 0.0,
        0.5, -0.5, -0.5, 1.0,  0.0, -1.0,  0.0, 0.0, 0.0, 1.0,
        0.5, -0.5,  0.5, 1.0,  0.0, -1.0,  0.0, 0.0, 1.0, 1.0,
        -0.5, -0.5,  0.5, 1.0,  0.0, -1.0,  0.0, 0.0, 1.0, 0.0,
        // + Y
        -0.5,  0.5,  0.5, 1.0,  0.0,  1.0,  0.0, 0.0, 0.0, 0.0,
        0.5,  0.5,  0.5, 1.0,  0.0,  1.0,  0.0, 0.0, 0.0, 1.0,
        0.5,  0.5, -0.5, 1.0,  0.0,  1.0,  0.0, 0.0, 1.0, 1.0,
        -0.5,  0.5, -0.5, 1.0,  0.0,  1.0,  0.0, 0.0, 1.0, 0.0,
        // -Z
        0.5, -0.5, -0.5, 1.0,  0.0,  0.0, -1.0, 0.0, 0.0, 0.0,
        -0.5, -0.5, -0.5, 1.0,  0.0,  0.0, -1.0, 0.0, 0.0, 1.0,
        -0.5,  0.5, -0.5, 1.0,  0.0,  0.0, -1.0, 0.0, 1.0, 1.0,
        0.5,  0.5, -0.5, 1.0,  0.0,  0.0, -1.0, 0.0, 1.0, 0.0,
        // +Z
        -0.5, -0.5,  0.5, 1.0,  0.0,  0.0,  1.0, 0.0, 0.0, 0.0,
        0.5, -0.5,  0.5, 1.0,  0.0,  0.0,  1.0, 0.0, 0.0, 1.0,
        0.5,  0.5,  0.5, 1.0,  0.0,  0.0,  1.0, 0.0, 1.0, 1.0,
        -0.5,  0.5,  0.5, 1.0,  0.0,  0.0,  1.0, 0.0, 1.0, 0.0,
    ]
    
    let indices:[UInt16] = [
        3,  1,  2,  0,  1,  3,
        7,  5,  6,  4,  5,  7,
        11,  9, 10,  8,  9, 11,
        15, 13, 14, 12, 13, 15,
        19, 17, 18, 16, 17, 19,
        23, 21, 22, 20, 21, 23,
    ]
    
    //------------------------------------------------------------------------------
    init(device:MTLDevice)
    {
        super.init()
        
        self.vertexBuffer = device.newBufferWithBytes(
            vertices,
            length:24 * sizeof(MBJVertex),
            options: .CPUCacheModeDefaultCache)
        self.vertexBuffer.label = "cube vertexBuffer"
        
        self.indexBuffer = device.newBufferWithBytes(
            indices,
            length:36 * sizeof(MBJIndexType),
            options: .CPUCacheModeDefaultCache)
        self.indexBuffer.label = "cube indexBuffer"
    }
    //------------------------------------------------------------------------------
}
//------------------------------------------------------------------------------
