2016-05-17 50 views
0

我想用Swift在iOS上顯示一個紋理,但不幸的是紋理不顯示:-(我不知道我在做什麼錯誤,顯然「測試紋理」正確裝入(並且是POT)。也許調用glEnableVertexAttribArray是錯誤的?OpenGL紋理不能在iOS上用Swift顯示

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) 
} 

回答

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) 

所以原因實際上是光。 KS!

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