GLKit在Swift游乐场工作吗?

时间:2015-08-11 22:07:29

标签: swift opengl-es swift-playground

我在Swift游乐场看过几篇展示GLKView的文章,但我无法让它发挥作用。我只看到一个浅灰色的视图。我希望它是蓝色的。

我正在使用Xcode 6.4

3

2 个答案:

答案 0 :(得分:2)

使用GLKit或其他方式的OpenGL视图目前不适用于游乐场。如果您愿意,可以file a bug

答案 1 :(得分:0)

此代码适用于Xcode 9.2 Playground:

import GLKit
import PlaygroundSupport

// Subclass of GLKViewController.
final class ViewController: GLKViewController {
  private var _glkView: GLKView!
  private var _context: EAGLContext?

  override func viewDidLoad() {
    super.viewDidLoad()

    setupGLContext()
    setupGLView()
  }

  override func glkView(_ view: GLKView, drawIn rect: CGRect) {
    glClearColor(0.0, 0.2, 0.2, 1.0)
    glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
  }

}


extension ViewController {

  func setupGLContext() {
    _context = EAGLContext(api: .openGLES2)
    if _context == nil {
      print("Failed to create ES context.")
    }

    EAGLContext.setCurrent(_context)
  }

  func setupGLView() {
    _glkView = self.view as! GLKView
    _glkView.context = _context!
    _glkView.drawableDepthFormat = .format24
    _glkView.isOpaque = true
    _glkView.delegate = self
    _glkView.frame.size = CGSize(width: 350, height: 500)
  }

}


// Create instance of ViewController and tell playground to show it in live view
let vc = ViewController()
PlaygroundPage.current.liveView = vc.view