I learned a thing
Forgive the lack of clever double entendres in this straight forward post about iOS development with GLKit / Swift. I know next to nothing about Xcode and even less about Swift. My background is in Javascript, C, and AfterEffects, so given that, some things are very intuitive, others, not so much. As I embarked on my google learning journey, I discovered what Ken observed: there is a lot of GLKit documentation out there... but its all in Objective C. Shout Outs for this post also go to Ray and Swiftly as this “tutorial” is basically just a mash up of those resources.
Without further ado, here is a hello world, if your world is iOS / GLKit / Swift.
Assumptions: - You know enough to be dangerous with an object oriented programming language. - You have Xcode and all that command line tools crud installed. - You have some vague knowledge of {Open,Web}GL.
1. open Xcode 2. choose Single View Application 3. make sure language is set to `Swift` 4. in “ViewController.swift” change "UI"s to "GLK"s:
import GLKit class ViewController: GLKViewController {
5. inside the "viewDidLoad()" function add:
var view: GLKView? = (self.view as? GLKView) view?.context = EAGLContext(api: .openGLES2)
6. after the "viewDidLoad()" function, add this new function:
override func glkView(_ view: GLKView, drawIn rect: CGRect) { glClearColor(1.0, 1.0, 0.0. 1.0) glClear(GLbitfield(GL_COLOR_BUFFER_BIT)) }
7. click on “Main.storyboard” and delete the default view
8. drag a "GLKit View Controller" from bottom right Object Library to the storyboard. Should be looking like this about now:
9. click on buttons `1` and `2` in that order to make this new view the entry point. the arrow circled by `3` should appear
10. click on the top left yellow icon (GLKit View Controller) and change the custom class to "ViewController"
11. hit Command R (or Product > Run if you’re not into the whole brevity thing) to build and test run your code.
12. if the screen on your iPhone / simulator is orange, celebrate! if not, go through your normal troubleshooting shame spiral.
Here is some copy pasta code that adds in a button to toggle background colors. (Watch this 3 min youtube video to see how to link the button to the GUI.)
import GLKit
class ViewController: GLKViewController { var cc = [Float](arrayLiteral: 0.0, 1.0, 1.0, 1.0)
override func viewDidLoad() {
super.viewDidLoad()
// open a GL context var view: GLKView? = (self.view as? GLKView) view?.context = EAGLContext(api: .openGLES2) }
@IBAction func buttonClick(_ sender: AnyObject) { toggleClearColor() }
func toggleClearColor() { if 0.0 == cc[0] { cc[0] = 1.0 cc[2] = 0.0 } else { cc[0] = 0.0 cc[2] = 1.0 } }
override func glkView(_ view: GLKView, drawIn rect: CGRect) { glClearColor(cc[0], cc[1], cc[2], cc[3]) glClear(GLbitfield(GL_COLOR_BUFFER_BIT)) } }













