とりあえずSwiftでOpenGL ESの一枚四角を描く

  • 17
    Like
  • 0
    Comment
More than 1 year has passed since last update.

XCode7でSwiftでもOpenGLESが使えるようになったのでとりあえず四角形を描いてみます。
Screen Shot 2015-10-05 at 3.06.31 AM.png

1.XCodeでiOS->Application->GameでSwiftとOpenGLESを選んでプロジェクトを作ります。
2.GameViewController.swiftに下のコードを上書きして実行してみます。
ソースの中身は http://enamelsystems.com/0017/ とかにObjective-Cの場合の解説があります。

GameViewController.swift

import GLKit
import OpenGLES

let gVertices: [GLfloat] = [
  -0.5, -0.5, 0.0,
  -0.5, 0.5, 0.0,
  0.5, -0.5, 0.0,
  0.5, 0.5, 0.0,
]

let gIndices: [GLubyte] = [
  0,1,2,3
]

let gColors: [GLfloat] = [
  1.0, 0.0, 0.0, 1.0,
  0.0, 1.0, 0.0, 1.0,
  0.0, 0.0, 1.0, 1.0,
  1.0, 1.0, 1.0, 1.0
]

func BUFFER_OFFSET(i: Int) -> UnsafePointer<Void> {
    let p: UnsafePointer<Void> = nil
    return p.advancedBy(i)
}


class GameViewController: GLKViewController {

    var vertexBuffer: GLuint = 0
    var indexBuffer: GLuint = 0;
    var colorBuffer: GLuint = 0;

    var context: EAGLContext? = nil
    var effect: GLKBaseEffect? = nil

    deinit {
        self.tearDownGL()

        if EAGLContext.currentContext() === self.context {
            EAGLContext.setCurrentContext(nil)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.context = EAGLContext(API: .OpenGLES2)

        if !(self.context != nil) {
            print("Failed to create ES context")
        }

        let view = self.view as! GLKView
        view.context = self.context!
        view.drawableDepthFormat = .Format24

        self.setupGL()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        if self.isViewLoaded() && (self.view.window != nil) {
            self.view = nil

            self.tearDownGL()

            if EAGLContext.currentContext() === self.context {
                EAGLContext.setCurrentContext(nil)
            }
            self.context = nil
        }
    }
    func setupGL() {
        EAGLContext.setCurrentContext(self.context)

        self.effect = GLKBaseEffect()
        self.effect?.colorMaterialEnabled = GLboolean(GL_TRUE)

        glGenBuffers(1, &vertexBuffer)
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vertexBuffer)
        glBufferData(GLenum(GL_ARRAY_BUFFER), GLsizeiptr(sizeof(GLfloat) * gVertices.count), gVertices, GLenum(GL_STATIC_DRAW))
        glEnableVertexAttribArray(GLuint(GLKVertexAttrib.Position.rawValue))
        glVertexAttribPointer(GLuint(GLKVertexAttrib.Position.rawValue), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(GLfloat) * 3), BUFFER_OFFSET(0))

        glGenBuffers(1, &colorBuffer)
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), colorBuffer)
        glBufferData(GLenum(GL_ARRAY_BUFFER), GLsizeiptr(sizeof(GLfloat) * gColors.count), gColors, GLenum(GL_STATIC_DRAW))
        glEnableVertexAttribArray(GLuint(GLKVertexAttrib.Color.rawValue))
        glVertexAttribPointer(GLuint(GLKVertexAttrib.Color.rawValue), 4, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(GLfloat) * 4), BUFFER_OFFSET(0))

        glGenBuffers(1, &indexBuffer)
        glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER),indexBuffer)
        glBufferData(GLenum(GL_ELEMENT_ARRAY_BUFFER),GLsizeiptr(sizeof(GLuint) * gIndices.count),gIndices,GLenum(GL_STATIC_DRAW))

    }

    func tearDownGL() {
        EAGLContext.setCurrentContext(self.context)

        glDeleteBuffers(1, &vertexBuffer)
        glDeleteBuffers(1, &indexBuffer)
        glDeleteBuffers(1, &colorBuffer)

    }

    // MARK: - GLKView and GLKViewController delegate methods

    func update() {
    }

    override func glkView(view: GLKView, drawInRect rect: CGRect) {
        glClearColor(0.65, 0.65, 0.65, 1.0)
        glClear(GLbitfield(GL_COLOR_BUFFER_BIT))

        // Render the object with GLKit
        self.effect?.prepareToDraw()

        glDrawElements(GLenum(GL_TRIANGLE_STRIP),GLsizei(gIndices.count),GLenum(GL_UNSIGNED_BYTE),BUFFER_OFFSET(0))
    }

}