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

标签 ios swift opengl-es

我想使用 Swift 在 iOS 上显示 OpenGLES2 的纹理,但不幸的是纹理没有显示:-(。我不知道我做错了什么。显然“测试纹理”已正确加载(并且是 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)
}

最佳答案

最后,我找到了问题的原因,就是上面没有包含的以下代码(在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)

关于ios - OpenGL 纹理在使用 Swift 的 iOS 上不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37286757/

相关文章:

ios - 计算数组中的平均值

ios - 更改 tableView 中所有单元格的所有对象的 bool 状态

swift - 在真实设备上模拟时未声明的存储元数据

ios - 可调整大小的 Collection View 单元格

android - 在 opengl-es 中使用 2D 正交投影时从未得到完整的 480*800

iphone - 如何使用GPUImage框架复制CIRandomGenerator

ios - Swift 3 - 如何验证对象的类类型

android - 我什么时候需要使用 AWS 创建用户池?

ios - 如何在Swift中从JSON对象删除属性(key:values)

android - 了解 Android 中的 android.graphics.Camera 对象