title: Parameter type UnsafePointer<Void> in glDrawElements
body: <p>I am trying to get my <code>context</code> (<code>EAGLContext</code>) to render in Swift; but for days I haven't been able to the function <code>glDrawElements</code> to work.</p>
  
  <p>I have read a couple of similar questions here on Stackoverflow but to no avail.</p>
  
  <p>My <code>glDrawElements</code> is as follows:</p>
  
  <pre><code>glDrawElements(GLenum(GL_TRIANGLES), GLsizei(Indices.count), GLenum(GL_UNSIGNED_BYTE), &amp;offset)
  </code></pre>
  
  <p>I am having problem with the last parameter - <strong><em>offset</em></strong>, which expects a <code>UnsafePointer&lt;Void&gt;</code>.</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>let offset: CConstVoidPointer = COpaquePointer(UnsafePointer&lt;Int&gt;(0))
  </code></pre>
  
  <p>The above no longer works, because <code>CConstVoidPointer</code> doesn't seem to be available anymore in Swift 1.2.</p>
  
  <p>And:</p>
  
  <pre><code>var offset = UnsafePointer&lt;Int&gt;(other: 0)
  </code></pre>
  
  <p>The above gives a warning that I should use <code>bitPattern:</code> instead.</p>
  
  <p>Although I don't believe <code>bitPattern:</code> should be used here (because the parameter expects a <code>Word</code>), I decided to give it a try according to the suggestion provided such as the following:</p>
  
  <pre><code>var offset = UnsafePointer&lt;Int&gt;(bitPattern: 0)
  glDrawElements(GLenum(GL_TRIANGLES), GLsizei(Indices.count), GLenum(GL_UNSIGNED_BYTE), &amp;offset)
  </code></pre>
  
  <p>I would get a <code>EXE_BAD_ACCESS (code=EXC_I386_GPFLT)</code> error.</p>
  
  <p>In vain, I also tried something as simple as the following using:</p>
  
  <pre><code>var offsetZero : Int = 0
  </code></pre>
  
  <p>and subsequently feeding it to the last parameter of <code>glDrawElements</code> like so:</p>
  
  <pre><code>glDrawElements(GLenum(GL_TRIANGLES), GLsizei(Indices.count), GLenum(GL_UNSIGNED_BYTE), &amp;offsetZero)
  </code></pre>
  
  <p>I am getting the same <code>EXE_BAD_ACCESS</code> like in the case above.</p>
  
  <p>How can I properly form a type suitable for the last parameter of <code>glDrawElements</code> that expects the type <code>UnsafePointer&lt;Void&gt;</code>?</p>
  
  <hr>
  
  <p>@RobMayoff</p>
  
  <p><strong><em>Update</em></strong> (adding code for VBO set-up and variables declarations and definitions):</p>
  
  <pre><code>struct vertex
  {
      var position: (CFloat, CFloat, CFloat)
      var color: (CFloat, CFloat, CFloat, CFloat)
  }
  
  var vertices =
  [
      vertex(position: (-1, -1, 0) , color: (1, 1, 0, 1)),
      vertex(position: (1, -1, 0)  , color: (1, 1, 1, 1)),
      vertex(position: (-1, 0, 1) , color: (0, 1, 0, 1)),
      vertex(position: (-1, 1, 1), color: (1, 0, 0, 1))
  ]
  
  let indices: [UInt8] = [ 0, 2, 3, 3, 1, 0 ]
  
  class mainGLView : UIView
  {
      var layer : CAEAGLLayer!
      var context : EAGLContext!
      var cBuffer : GLuint = GLuint()
      var pos : GLuint = GLuint()
      var color : GLuint = GLuint()
      var iBuffer : GLuint = GLuint()
      var vBuffer : GLuint = GLuint()
      var vao : GLuint = GLuint()
  
      override class func layerClass() -&gt; AnyClass 
      {  
          return CAEAGLLayer.self
      }
  
      required init(coder aDecoder: NSCoder)
      {
          super.init(coder: aDecoder)
  
         //setting up context, buffers, shaders, etc.
  
          self.configureVBO()
          self.setupRendering()
      }
  
      func configureVBO()
      {
  
          glGenVertexArraysOES(1, &amp;vao);
          glBindVertexArrayOES(vao);
  
          glGenBuffers(GLsizei(1), &amp;vBuffer)
          glBindBuffer(GLenum(GL_ARRAY_BUFFER), vBuffer)
          glBufferData(GLenum(GL_ARRAY_BUFFER), vertices.size(), vertices, GLenum(GL_STATIC_DRAW))
  
          glEnableVertexAttribArray(pos)
  
  
          var ptr = COpaquePointer(UnsafePointer&lt;Int&gt;(bitPattern: 0))
  
          glVertexAttribPointer(GLuint(pos), GLint(3), GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(vertex)), &amp;ptr)
  
          glEnableVertexAttribArray(color)
  
          let fColor = UnsafePointer&lt;Int&gt;(bitPattern: sizeof(Float) * 3)
  
          glVertexAttribPointer(GLuint(color), 4, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(vertex)), fColor)
  
          glGenBuffers(1, &amp;iBuffer)
          glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), iBuffer)
          glBufferData(GLenum(GL_ELEMENT_ARRAY_BUFFER), indices.size(), indices, GLenum(GL_STATIC_DRAW))
  
          glBindBuffer(GLenum(GL_ARRAY_BUFFER), 0)
          glBindVertexArrayOES(0)
      }
  
      func setupRendering()
      {
          glBindVertexArrayOES(vao);
          glViewport(0, 0, GLint(self.frame.size.width), GLint(self.frame.size.height));
  
          indices.withUnsafeBufferPointer
          {
              (pointer : UnsafeBufferPointer&lt;UInt8&gt;) -&gt; Void in
              glDrawElements(GLenum(GL_TRIANGLES), GLsizei(indices.count), GLenum(GL_UNSIGNED_BYTE), UnsafePointer&lt;Void&gt;(pointer.baseAddress))
              Void()
          }
  
          self.context.presentRenderbuffer(Int(GL_RENDERBUFFER))
  
          glBindVertexArrayOES(0)
      }
  }
  </code></pre>