GLKit 在 Swift 操场上工作吗?
Does GLKit work in Swift playgrounds?
我在Swift playgrounds 中看过几篇文章,展示了 GLKView,但我无法让它工作。我所看到的只是一个浅灰色的视图。我希望它是蓝色的。
我正在使用 Xcode 6.4
import UIKit
import GLKit
import XCPlayground
class RGView: GLKView {
override func drawRect(rect: CGRect) {
glClearColor(0.0, 0.0, 1.0, 1.0)
glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
}
}
let view = RGView(frame: CGRectMake(0, 0, 200, 200), context: EAGLContext(API: .OpenGLES2))
view.setNeedsDisplay()
// tried with and without this
XCPShowView("RGView", view)
使用 GLKit 或其他方式的 OpenGL 视图目前无法在 playgrounds 中使用。如果您愿意,您应该 file a bug.
此代码适用于 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
我在Swift playgrounds 中看过几篇文章,展示了 GLKView,但我无法让它工作。我所看到的只是一个浅灰色的视图。我希望它是蓝色的。
我正在使用 Xcode 6.4
import UIKit
import GLKit
import XCPlayground
class RGView: GLKView {
override func drawRect(rect: CGRect) {
glClearColor(0.0, 0.0, 1.0, 1.0)
glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
}
}
let view = RGView(frame: CGRectMake(0, 0, 200, 200), context: EAGLContext(API: .OpenGLES2))
view.setNeedsDisplay()
// tried with and without this
XCPShowView("RGView", view)
使用 GLKit 或其他方式的 OpenGL 视图目前无法在 playgrounds 中使用。如果您愿意,您应该 file a bug.
此代码适用于 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