일단 Swift로 OpenGLES의 사각형을 그려볼게요.

엑스코드7에서도 스위프트가 OpenGLES를 사용할 수 있기 때문에 일단 사각형을 그려보자.

1. XCode를 통해 iOS->Application->게임에서 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))
    }

}

좋은 웹페이지 즐겨찾기