2016-08-22 16 views
1

基本的には、レンダリングにOPenGLを使用するImageViewを作成したいと考えています。私の最終的な計画は、これをCIFilterを備えたビデオプレーヤーのベースとして使用することです。SwiftでNSOpenGLViewに画像を描画する方法は?

私はtutorialに続いて、OpenGLテクノロジを使ってGPUを利用することを強調しました。このチュートリアルはiOS向けです。私はそれをCocoaにマッピングしました。

私はどこで失敗しているのかわかりませんが、空白の画面が表示されます。

ここはビューです。

import Cocoa 
import OpenGL.GL3 

class CoreImageView: NSOpenGLView { 
    var coreImageContext: CIContext? 
    var image: CIImage? { 
     didSet { 
      display() 
     } 
    } 

    override init?(frame frameRect: NSRect, pixelFormat format: NSOpenGLPixelFormat?) { 
     //Bad programming - Code duplication 
     let attrs: [NSOpenGLPixelFormatAttribute] = [ 
      UInt32(NSOpenGLPFAAccelerated), 
      UInt32(NSOpenGLPFAColorSize), UInt32(32), 
      UInt32(NSOpenGLPFAOpenGLProfile), 
      UInt32(NSOpenGLProfileVersion3_2Core), 
      UInt32(0) 
     ] 
     let pf = NSOpenGLPixelFormat(attributes: attrs) 
     super.init(frame: frameRect, pixelFormat: pf) 
    } 

    required init?(coder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     initialize() 
    } 

    //Bad programming - Code duplication 
    func defaultPixelFormat()->NSOpenGLPixelFormat?{ 
     let attrs: [NSOpenGLPixelFormatAttribute] = [ 
      UInt32(NSOpenGLPFAAccelerated), 
      UInt32(NSOpenGLPFAColorSize), UInt32(32), 
      UInt32(NSOpenGLPFAOpenGLProfile), 
      UInt32(NSOpenGLProfileVersion3_2Core), 
      UInt32(0) 
     ] 
     return NSOpenGLPixelFormat(attributes: attrs) 
    } 

    func initialize(){ 

     guard let pf = defaultPixelFormat() else { 
      Swift.print("pixelFormat could not be constructed") 
      return 
     } 
     self.pixelFormat = pf 

     guard let context = NSOpenGLContext(format: pf, share: nil) else { 
      Swift.print("context could not be constructed") 
      return 
     } 
     self.openGLContext = context 

     if let cglContext = context.cglContextObj { 
      coreImageContext = CIContext(cglContext: cglContext, pixelFormat: pixelFormat?.cglPixelFormatObj, colorSpace: nil, options: nil) 
     }else{ 
      Swift.print("cglContext could not be constructed") 
      coreImageContext = CIContext(options: nil) 
     } 
    } 

    //-------------------------- 

    override func draw(_ dirtyRect: NSRect) { 
     if let img = image { 
      let scale = self.window?.screen?.backingScaleFactor ?? 1.0 
      let destRect = bounds.applying(CGAffineTransform(scaleX: scale, y: scale)) 
      coreImageContext?.draw(img, in: destRect, from: img.extent) 
     } 
    } 

} 

助けていただければ幸いです。完全なプロジェクトがhere (XCode 8)here(Xcode 7)

+0

あなたはピクセル形式のためNSOpenGLView.defaultPixelFormat()を使用してみましたか? –

答えて

-1

である私は、この上でサイモンのコアイメージヘルパーをチェックアウトしてお勧めかもしれない - 彼は基本的にOpenGLES 2.0コンテキストを使用してGPUを経由してレンダリングするためにコアイメージを伝え、彼githubの上でこの事を持っています。私がGPU経由でレンダリングする方法を理解しようとしていたとき、本当に役に立ちました。転送に時間がかかります(比較的)ので、レンダリングするCPUに転送しないというのは本当に良い考えです。

https://github.com/FlexMonkey/CoreImageHelpers

関連する問題