title: Draw a line with SceneKit using Swift on iOS: line drawn but not visible?
body: <p>Follow-up to my previous question in Objective-C here (specifically, I'm trying to translate the answer I provided with Objective-C into Swift):</p>
  
  <p><a href="https://stackoverflow.com/questions/21886224/drawing-a-line-between-two-points-using-scenekit">Drawing a line between two points using SceneKit</a></p>
  
  <p>Problem: SceneKit statistics show that line is being drawn, but it doesn't appear on screen.</p>
  
  <p>Minimal example:</p>
  
  <ul>
  <li>Start a new Game project in Xcode using Swift and SceneKit.</li>
  <li>Remove boilerplate:
  
  <ul>
  <li><code>let scene = SCNScene()</code> instead of <code>SCNScene(named: "art.scnassets/ship.dae")!</code></li>
  <li>comment out <code>let ship = ...</code> and <code>ship.runAction(...)</code></li>
  </ul></li>
  <li>Change <code>scnView.backgroundColor = UIColor.blackColor()</code> to <code>scnView.backgroundColor = UIColor.grayColor()</code> (I originally thought maybe the lines were just black on a black background, so changed the background colour accordingly)</li>
  <li><p>Add the following code:</p>
  
  <pre><code>var positions: [SCNVector3] = [SCNVector3Make(0.0,0.0,0.0),SCNVector3Make(10.0,10.0,10.0)]
  
  // using Swift's Int gives an error with unsupported "SceneKit: error, C3DRendererContextBindMeshElement unsupported byte per index (8)"
  // when constructing element: SCNGeometryElement below, so switched to CInt
  var indicies: [CInt] = [0,1]
  
  // for whatever reason, the following doesn't work on iOS:
  // let vertexSource = SCNGeometrySource(vertices: &amp;positions, count: positions.count)
  // see https://stackoverflow.com/questions/26890739/how-to-solve-scenekit-double-not-supported-error
  // so using more complex SCNGeometrySource() initializer instead
  
  let vertexData = NSData(bytes: &amp;positions, length: positions.count)
  let vertexSource = SCNGeometrySource(data: vertexData, semantic: SCNGeometrySourceSemanticVertex,
  vectorCount: positions.count, floatComponents: true, componentsPerVector: 3,
  bytesPerComponent: sizeof(Float), dataOffset: 0, dataStride: sizeof(SCNVector3))
  
  let indexData = NSData(bytes: &amp;indicies, length: indicies.count)
  let element = SCNGeometryElement(data: indexData, primitiveType: SCNGeometryPrimitiveType.Line,
  primitiveCount: indicies.count, bytesPerIndex: sizeof(CInt))
  
  let lines = SCNGeometry(sources: [vertexSource], elements: [element])
  
  let cellNode = SCNNode(geometry: lines)
  scene.rootNode.addChildNode(cellNode)
  </code></pre></li>
  </ul>
  
  <p>As mentioned, the SceneKit statistics suggest the line <em>is</em> being drawn, but it doesn't seem to appear, and there are no compilation or runtime errors, which makes it a little tricky to track down.</p>
  
  <p>Edit: Using the GPU 'Analyse' tool in the debug navigator gave the following errors:</p>
  
  <p><code>Draw call exceeded element array buffer bounds</code></p>
  
  <p><code>Draw call exceeded array buffer bounds</code></p>
  
  <p>in <code>glDrawElements(GL_LINES, 4, GL_UNSIGNED_INT, NULL)</code> which suggests there's something wrong with my SCNGeometryElement construction?</p>