目前正在尝试让我的C ++源代码在iOS上运行。它可以在OpenGLES 2.0的Android上运行,我已经可以编译所有内容,但是在编译着色器时遇到了问题。由于某些原因,glCreateShader(GL_VERTEX_SHADER)返回0。这是我的视图控制器代码:

import Foundation
import GLKit
import OpenGLES

class SampleViewController: GLKViewController {
    @IBOutlet var glview: GLKView!

    override func viewDidLoad() {
        super.viewDidLoad()
        glview.context = EAGLContext(API: .OpenGLES2)

        glview.drawableColorFormat = .RGBA8888
        glview.drawableDepthFormat = .Format16
        glview.drawableMultisample = .MultisampleNone
        glview.drawableStencilFormat = .FormatNone

        preferredFramesPerSecond = 60
        let s = NSBundle.mainBundle().URLForResource("Arial", withExtension: ".ttf")?.relativePath
        let cs = (s! as NSString).UTF8String
        let buffer = UnsafeMutablePointer<Int8>(cs)
        appInit(buffer)
    }

    override func glkView(view: GLKView, drawInRect rect: CGRect) {
        super.glkView(view, drawInRect: rect)
        glview.bindDrawable()
        appRender(Int(timeSinceLastDraw * 1000), Int32(rect.width), Int32(rect.height))
    }
}


它连接到情节提要上的GLKViewController

注意:appInit(buffer)是对通过桥接头导入的C ++代码的调用。

最佳答案

显然,我必须在appInit覆盖内运行我的drawInRect函数,因为这是glContext存在的第一个地方。

固定代码:

import Foundation
import GLKit
import OpenGLES

class GanttViewController: GLKViewController {
    @IBOutlet var glview: GLKView!
    private var program = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        glview.context = EAGLContext(API: .OpenGLES2)

        glview.drawableColorFormat = .RGBA8888
        glview.drawableDepthFormat = .Format16
        glview.drawableMultisample = .MultisampleNone
        glview.drawableStencilFormat = .FormatNone

        preferredFramesPerSecond = 60
        program = 0
    }

    override func glkView(view: GLKView, drawInRect rect: CGRect) {
        super.glkView(view, drawInRect: rect)
        if(program == 0){
            let s = NSBundle.mainBundle().URLForResource("Arial", withExtension: ".ttf")?.relativePath
            let cs = (s! as NSString).UTF8String
            let buffer = UnsafeMutablePointer<Int8>(cs)
            appInit(buffer)
            program = 1
        }

        glview.bindDrawable()
        appRender(Int(timeSinceLastDraw * 1000), Int32(rect.width), Int32(rect.height))
    }
}

10-08 06:29