使用Swift在iOS上不显示OpenGL纹理

时间:2016-05-17 21:47:31

标签: ios swift opengl-es

我想在iOS上使用Swift显示OpenGLES2的纹理,但遗憾的是纹理没有出现:-(。我不知道我做错了什么。显然'Test-Texture'正确加载(并且是POT)。也许调用glEnableVertexAttribArray是错误的?

func BUFFER_OFFSET(i: Int) -> UnsafePointer<Void> {
    let p: UnsafePointer<Void> = nil
    return p.advancedBy(i)
}

let vertices:[GLfloat] = [
    // Positions       // Colors        // Texture Coords
    0.5,  0.5, 0.0,   1.0, 0.0, 0.0,   1.0, 1.0, // Top Right
    0.5, -0.5, 0.0,   0.0, 1.0, 0.0,   1.0, 0.0, // Bottom Right
    -0.5, -0.5, 0.0,   0.0, 0.0, 1.0,   0.0, 0.0, // Bottom Left
    -0.5,  0.5, 0.0,   1.0, 1.0, 0.0,   0.0, 1.0  // Top Left
]

let indices:[GLuint] = [  // Note that we start from 0!
    0, 1, 3,  // First Triangle
    1, 2, 3   // Second Triangle
]

func setupGL() {
    EAGLContext.setCurrentContext(self.context)
    self.loadShaders()

    self.effect = GLKBaseEffect()
    self.effect!.light0.enabled = GLboolean(GL_TRUE)
    self.effect!.light0.diffuseColor = GLKVector4Make(1.0, 0.4, 0.4, 1.0)

    do {
        testTexture = try GLKTextureLoader.textureWithContentsOfFile(NSBundle.mainBundle().pathForResource("MyTexture1024x1024", ofType: "png")!, options: [GLKTextureLoaderOriginBottomLeft:true, GLKTextureLoaderApplyPremultiplication:true])
        print("successfully loaded test texture")
    }
    catch let error as NSError {
        print("could not load test texture \(error)")
    }

    glEnable(GLenum(GL_DEPTH_TEST))
    glGenVertexArraysOES(1, &vertexArray)
    glBindVertexArrayOES(vertexArray)

    glGenBuffers(1, &vertexBuffer)
    glBindBuffer(GLenum(GL_ARRAY_BUFFER), vertexBuffer)
    glBufferData(GLenum(GL_ARRAY_BUFFER), GLsizeiptr(sizeof(GLfloat) * vertices.count), vertices, GLenum(GL_STATIC_DRAW))

    glGenBuffers(1, &indexBuffer)
    glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), indexBuffer)
    glBufferData(GLenum(GL_ELEMENT_ARRAY_BUFFER), GLsizeiptr(sizeof(GLfloat) * indices.count), indices, GLenum(GL_STATIC_DRAW))

    glEnableVertexAttribArray(GLuint(GLKVertexAttrib.Position.rawValue))
    glVertexAttribPointer(GLuint(GLKVertexAttrib.Position.rawValue), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(GLfloat) * 8), BUFFER_OFFSET(0))
    glEnableVertexAttribArray(GLuint(GLKVertexAttrib.TexCoord0.rawValue))
    glVertexAttribPointer(GLuint(GLKVertexAttrib.TexCoord0.rawValue), 2, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(GLfloat) * 8), BUFFER_OFFSET(sizeof(GLfloat) * 6))

    glBindVertexArrayOES(0)
}

override func glkView(view: GLKView, drawInRect rect: CGRect) {
    glClearColor(0.65, 0.65, 0.65, 1.0)
    glClear(GLbitfield(GL_COLOR_BUFFER_BIT) | GLbitfield(GL_DEPTH_BUFFER_BIT))

    //use specified vertex buffer
    glBindVertexArrayOES(vertexArray)

    //use specified index buffer
    glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), indexBuffer)

    glEnable(GLenum(GL_BLEND));
    glBlendFunc(GLenum(GL_SRC_ALPHA), GLenum(GL_ONE_MINUS_SRC_ALPHA));

    self.effect!.texture2d0.name = testTexture.name
    self.effect!.texture2d0.enabled = 1

    // Render the object with GLKit
    self.effect!.prepareToDraw()    
    glDrawElements(GLenum(GL_TRIANGLES), 6, GLenum(GL_UNSIGNED_INT), nil)
}

1 个答案:

答案 0 :(得分:0)

最后,我找到了问题的原因,即上面未包含的以下代码(在setupGL()中):

self.effect = GLKBaseEffect()
self.effect!.light0.enabled = GLboolean(GL_TRUE)
self.effect!.light0.diffuseColor = GLKVector4Make(1.0, 0.4, 0.4, 1.0)

所以原因实际上就是光明。禁用时可以正常工作!

self.effect = GLKBaseEffect()
self.effect!.light0.enabled = GLboolean(GL_FALSE)