일단 Swift로 OpenGLES의 사각형을 그려볼게요.
19125 단어 다짜고짜로 시작하다XCode7SwiftOpenGLESSwift2
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))
}
}
Reference
이 문제에 관하여(일단 Swift로 OpenGLES의 사각형을 그려볼게요.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/sanofc/items/e69ab6dcc6d9266d605b텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)