为什么 glCreateShader 在 iOS 上返回 0?

Why is glCreateShader returning 0 on iOS?

目前正试图在 iOS 上将我的 C++ 源代码上传到 运行。它在 OpenGLES 2.0 Android 上 运行s 并且我已经得到了所有要编译的东西,但是我在编译着色器时遇到了问题。出于某种原因 glCreateShader(GL_VERTEX_SHADER) returns 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))
    }
}