快速的GLKit什么都没有显示?

时间:2015-05-07 01:57:11

标签: xcode swift opengl-es glkbaseeffect

我正在关注this tutorialthis。它很好,但它没有在屏幕上显示任何内容。

渲染:

 override func glkView(view: GLKView!, drawInRect rect: CGRect) {

    glClearColor(0.1, 0.5, 0.0, 1.0)
    glClear(GLbitfield(GL_COLOR_BUFFER_BIT))

    //
    effect.texture2d0.name = textrue.name
    effect.texture2d0.enabled = GLboolean(GL_TRUE)


    //
    self.effect.transform.modelviewMatrix = modelMatrix()
    //
    effect.prepareToDraw()
    //
    glEnableVertexAttribArray(GLenum( GLKVertexAttrib.Position.rawValue))
    glEnableVertexAttribArray(GLenum( GLKVertexAttrib.TexCoord0.rawValue))

    //
    var _quad = TexturedQuad()

    withUnsafePointer(&_quad.bl.geometryVertex) { (pointer) -> Void in
        glVertexAttribPointer(GLuint(GLKVertexAttrib.Position.rawValue), 2, GLenum(GL_FLOAT), GLboolean(GL_FALSE),
            GLsizei(sizeof(TexturedVertex)), pointer)
        //                        println(sizeof(TexturedVertex))
    }

    withUnsafePointer(&_quad.bl.textureVertex) { (pointer) -> Void in
        glVertexAttribPointer(GLuint(GLKVertexAttrib.TexCoord0.rawValue), 2, GLenum(GL_FLOAT), GLboolean(GL_FALSE),
            GLsizei(sizeof(TexturedVertex)), pointer)
    }
    //

    glDrawArrays(GLenum( GL_TRIANGLE_STRIP), 0, 4)

}

TexturedQuad

struct v2Point {
    var x : GLfloat
    var y : GLfloat
    init(_ x: GLfloat ,_ y:GLfloat){
        self.x = x
        self.y = y
    }
}
struct TexturedVertex {
    var geometryVertex : v2Point = v2Point(0,0)
    var textureVertex : v2Point = v2Point(0,0)

}
struct TexturedQuad {
    var bl : TexturedVertex = TexturedVertex()
    var br : TexturedVertex = TexturedVertex()
    var tl : TexturedVertex = TexturedVertex()
    var tr : TexturedVertex = TexturedVertex()
    init(){}
}

初始纹理:

 func initWithSpriteName(str: String){

        var options  = [NSNumber(bool: true) : GLKTextureLoaderOriginBottomLeft]

        var cgiImg = UIImage(named: "background.png")?.CGImage
        assert(cgiImg != nil, "Image not found")
        textrue = GLKTextureLoader.textureWithCGImage(cgiImg, options: options, error: nil)
        //
        quad =  TexturedQuad()
        quad.bl = TexturedVertex(geometryVertex: v2Point(0,0) , textureVertex: v2Point( 0,0))
        quad.br = TexturedVertex(geometryVertex: v2Point(0,1), textureVertex: v2Point(0, textrue.height.toF()))
        quad.tl = TexturedVertex(geometryVertex: v2Point(1,0), textureVertex: v2Point(textrue.width.toF(), 0))
        quad.tr = TexturedVertex(geometryVertex: v2Point(1,1), textureVertex: v2Point(textrue.width.toF() , textrue.height.toF()))
    }

请帮助我如何使用swift在glkit中显示图像。 谢谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

您可能错过了GL_DEPTH_BUFFER_BIT

的标记

即。 glClear(GLbitfield(GL_COLOR_BUFFER_BIT) | GLbitfield(GL_DEPTH_BUFFER_BIT))