在Swift 3中设置OpenGL
我有一个受人尊敬的应用程序,我已经将obj-c中的苹果示例代码GLEssentials强制转换为Swift 2,现在我正试图转换为Swift 3。它的核心是一个NSOpenGLView,主渲染器仍然没有在Swift中(我知道:我很快就会咬紧牙关,迁移到Metal或类似的东西),但其他一切都是 目前在OSX 10.12中,对于Xcode 8,窗口只是空白。我不得不对主(Swift)OpenGLView类进行一些编译器建议的更改,以使其在Swift 3中编译并成功运行,我觉得这就是问题所在,因为renderer类似乎运行得很好;我可以让它报告FPS为60,并且能够放置断点,使代码每帧停止一次。我的OpenGLView类设置如下,这就是大多数新的Swift 3更改发生的地方:在Swift 3中设置OpenGL,opengl,swift3,xcode8,macos-sierra,Opengl,Swift3,Xcode8,Macos Sierra,我有一个受人尊敬的应用程序,我已经将obj-c中的苹果示例代码GLEssentials强制转换为Swift 2,现在我正试图转换为Swift 3。它的核心是一个NSOpenGLView,主渲染器仍然没有在Swift中(我知道:我很快就会咬紧牙关,迁移到Metal或类似的东西),但其他一切都是 目前在OSX 10.12中,对于Xcode 8,窗口只是空白。我不得不对主(Swift)OpenGLView类进行一些编译器建议的更改,以使其在Swift 3中编译并成功运行,我觉得这就是问题所在,因为re
class BQSOpenGLView: NSOpenGLView {
var m_renderer: OpenGLRenderer = OpenGLRenderer()
var have_setup_renderer: Bool = false
var displayLink: CVDisplayLink?
var has_setup = false
var is_fullscreen: Bool = false
var grid_size: QSGridSize = QSMakeGridSize(1, 1)
var backing_scalar_changed: Bool = false
var backing_scalar: GLfloat = 0.0
override func awakeFromNib() {
backing_scalar = GLfloat((self.window?.backingScaleFactor)!)
NotificationCenter.default.addObserver(self, selector: #selector(BQSOpenGLView.willEnterFullscreen(_:)), name: NSNotification.Name.NSWindowWillEnterFullScreen, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(BQSOpenGLView.willExitFullscreen(_:)), name: NSNotification.Name.NSWindowWillExitFullScreen, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(BQSOpenGLView.backingScaleChange(_:)), name: NSNotification.Name.NSWindowDidChangeBackingProperties, object: nil)
let attributes : [NSOpenGLPixelFormatAttribute] = [
NSOpenGLPixelFormatAttribute(NSOpenGLPFADoubleBuffer),
NSOpenGLPixelFormatAttribute(NSOpenGLPFADepthSize),
NSOpenGLPixelFormatAttribute(24),
NSOpenGLPixelFormatAttribute(NSOpenGLPFASampleBuffers),
NSOpenGLPixelFormatAttribute(1),
NSOpenGLPixelFormatAttribute(NSOpenGLPFASamples),
NSOpenGLPixelFormatAttribute(2),
NSOpenGLPixelFormatAttribute(NSOpenGLPFAOpenGLProfile),
NSOpenGLPixelFormatAttribute(NSOpenGLProfileVersion3_2Core),
NSOpenGLPixelFormatAttribute(0)
]
self.pixelFormat = NSOpenGLPixelFormat(attributes: attributes)
self.wantsBestResolutionOpenGLSurface = true
let context:NSOpenGLContext = NSOpenGLContext.init(format: self.pixelFormat!, share: nil)!
CGLEnable(context.cglContextObj!, kCGLCECrashOnRemovedFunctions)
self.openGLContext = context
}
override func prepareOpenGL() {
super.prepareOpenGL()
// The callback function is called everytime CVDisplayLink says its time to get a new frame.
func displayLinkOutputCallback(_ displayLink: CVDisplayLink, _ inNow: UnsafePointer<CVTimeStamp>, _ inOutputTime: UnsafePointer<CVTimeStamp>, _ flagsIn: CVOptionFlags, _ flagsOut: UnsafeMutablePointer<CVOptionFlags>, _ displayLinkContext: UnsafeMutableRawPointer?) -> CVReturn {
unsafeBitCast(displayLinkContext, to: BQSOpenGLView.self).renderFrame()
return kCVReturnSuccess
}
self.wantsLayer = true
self.wantsBestResolutionOpenGLSurface = true
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink)
let success = CVDisplayLinkSetOutputCallback(displayLink!, displayLinkOutputCallback, Unmanaged.passUnretained(self).toOpaque())
if success != kCVReturnSuccess {
Swift.print ("No opengl")
}
CVDisplayLinkStart(displayLink!)
has_setup = true
NotificationCenter.default.post(name: Notification.Name(rawValue: kOPENGL_SETUP_NOTE), object: nil)
self.backingScaleChange(nil)
}
//..
}
是否有一种新的Swift 3设置OpenGL的方法,或者上述代码是否正确,以及其他地方的问题?证明是一个微妙的损坏的Xcode项目。唯一不起作用的是OpenGL视图。也许只是笔尖坏了 我启动了一个新项目,将代码剪切并粘贴到新文件中,然后费力地重新构建UI,一切都很好。这将教会我不要以编程方式设置windows
func renderFrame() {
self.openGLContext?.makeCurrentContext()
CGLLockContext((self.openGLContext?.cglContextObj)!)
if !have_setup_renderer { //init only when locked all the context
self.backingScaleChange(nil)
m_renderer = OpenGLRenderer.init(defaultFBO: 0, screenSize: (self.window?.screen?.frame.size)!, backingScalar: self.backing_scalar)
have_setup_renderer = true
}
m_renderer.render()
CGLFlushDrawable((self.openGLContext?.cglContextObj)!)
CGLUnlockContext((self.openGLContext?.cglContextObj)!)
}