Skip to content

Commit

Permalink
Switch from backface culling to depth stencil
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Brunoli committed Dec 24, 2015
1 parent 71c0941 commit 1fa6566
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Quake 3 BSP Renderer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ViewController: UIViewController {

pipelineDescriptor.vertexDescriptor = MapMesh.vertexDescriptor()
pipelineDescriptor.colorAttachments[0].pixelFormat = .BGRA8Unorm
pipelineDescriptor.depthAttachmentPixelFormat = .Depth32Float

// Try creating the pipeline
pipeline = try! device.newRenderPipelineStateWithDescriptor(pipelineDescriptor)
Expand Down Expand Up @@ -102,16 +103,35 @@ class ViewController: UIViewController {
// Create Command Buffer
let commandBuffer = commandQueue.commandBuffer()

// Depth buffer
let depthTextureDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(
.Depth32Float,
width: Int(self.view.frame.width),
height: Int(self.view.frame.height),
mipmapped: false
)
let depthTexture = device.newTextureWithDescriptor(depthTextureDescriptor)

// Depth stencil
let stencilDescriptor = MTLDepthStencilDescriptor()
stencilDescriptor.depthCompareFunction = .LessEqual
stencilDescriptor.depthWriteEnabled = true
let depthState = device.newDepthStencilStateWithDescriptor(stencilDescriptor)

// Render Pass Descriptor
let renderPassDescriptor = MTLRenderPassDescriptor()
renderPassDescriptor.colorAttachments[0].texture = drawable.texture
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.8, 0.3, 0.2, 1)
renderPassDescriptor.colorAttachments[0].loadAction = .Clear

renderPassDescriptor.depthAttachment.texture = depthTexture
renderPassDescriptor.depthAttachment.loadAction = .Clear
renderPassDescriptor.depthAttachment.clearDepth = 1.0

// Command Encoder
let commandEncoder = commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor)
commandEncoder.setDepthStencilState(depthState)
commandEncoder.setRenderPipelineState(pipeline)
commandEncoder.setCullMode(.Back)
commandEncoder.setVertexBuffer(uniformBuffer, offset: 0, atIndex: 1)

mapMesh.renderWithEncoder(commandEncoder)
Expand Down

0 comments on commit 1fa6566

Please sign in to comment.