Skip to content

Instantly share code, notes, and snippets.

@sanofc
Last active October 4, 2015 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanofc/34dc64419a54db913dcd to your computer and use it in GitHub Desktop.
Save sanofc/34dc64419a54db913dcd to your computer and use it in GitHub Desktop.
とりあえずSwiftでOpenGL ESの一枚四角を描く ref: http://qiita.com/sanofc/items/e69ab6dcc6d9266d605b
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))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment