I've seen a couple of articles showing GLKView's in Swift playgrounds, but I can't get it to work. All I see is a light gray view. I would expect it to be blue.
我看过几篇文章,说GLKView在Swift的操场上,但我无法让它发挥作用。我所看到的只是一种淡淡的灰色。我希望它是蓝色的。
I'm using Xcode 6.4
我使用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)
2 个解决方案
#1
2
OpenGL views, using GLKit or otherwise, do not currently work in playgrounds. If you’d like them to, you should file a bug.
OpenGL视图,使用GLKit或其他方法,目前在操场上不工作。如果你想让他们这么做,你应该提交一个bug。
#2
0
This code works in an Xcode 9.2 Playground:
本代码适用于Xcode 9.2游乐场:
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
#1
2
OpenGL views, using GLKit or otherwise, do not currently work in playgrounds. If you’d like them to, you should file a bug.
OpenGL视图,使用GLKit或其他方法,目前在操场上不工作。如果你想让他们这么做,你应该提交一个bug。
#2
0
This code works in an Xcode 9.2 Playground:
本代码适用于Xcode 9.2游乐场:
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