2012-07-25 25 views
1

我正在嘗試使用GLKit做一些非常簡單的2D繪圖,包括使用紋理。但是,如果我加載紋理,但甚至不在繪圖中使用它,不知何故它會阻止繪圖發生!iOS5上的GLKit:設置紋理可以防止其他繪圖

這裏是我的設置代碼:

-(void) setup { 

    effect = [[GLKBaseEffect alloc] init]; 

    // comment this out - drawing takes place 
    texture = [GLKTextureLoader textureWithCGImage:[UIImage imageNamed: @"arrow.png"].CGImage options:nil error: nil]; 
    if (texture) { 
    effect.texture2d0.envMode = GLKTextureEnvModeReplace; 
    effect.texture2d0.target = GLKTextureTarget2D; 
    effect.texture2d0.name = texture.name; 
    }; 
    // end of comment this out... 

    effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(0.0, self.bounds.size.width, self.bounds.size.height, 0.0, 1, -1); 

} 

這裏是我的繪製代碼:

- (void)drawRect:(CGRect)rect { 

    GLKVector2 vertices[4]; 
    GLKVector4 colors[4]; 

    vertices[0] = GLKVector2Make(20.0, 30.0); 
    vertices[1] = GLKVector2Make(120.0, 45.0); 
    vertices[2] = GLKVector2Make(70.0, 88.0); 
    vertices[3] = GLKVector2Make(20.0, 80.0); 

    for(int i = 0; i < 4; i++) { 

    colors[i] = GLKVector4Make(0.3, 0.8, 0.5, 1.0); 
    }; 

    glClearColor(0.85, 0.05, 0.1, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    [effect prepareToDraw]; 

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

    glEnableVertexAttribArray(GLKVertexAttribPosition); 
    glEnableVertexAttribArray(GLKVertexAttribColor); 

    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, vertices); 
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, colors); 

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 3); 


    glDisableVertexAttribArray(GLKVertexAttribPosition); 
    glDisableVertexAttribArray(GLKVertexAttribColor); 

    glDisable(GL_BLEND); 
} 
+0

做一些基本的事情,比如用黑色以外的東西清除顏色緩衝區,是否會給你預期的結果? GLKTextureLoader是否可以通過上傳紋理來啓用紋理化?如果您上傳的是純色的紋理並將其設置爲邊緣,您會看到什麼? – user1118321 2012-07-25 17:56:48

回答

0

出於調試OpenGL的,運行在調試模式下的代碼,然後暫停它,然後選擇「捕捉的OpenGL ES框架「從菜單欄。這允許您逐步完成繪圖代碼,並在每次調用後查看OpenGL狀態。因此,您可以比較執行設置代碼和不執行時的狀態,並可以確定錯誤。

對於問題本身:設置效果的texture2d屬性已經足以使用紋理。如果你把它放在那裏,它會自動用在圖形中。在[effect prepareToDraw]中,應用效果的整個狀態 - 包括指定的紋理。

+0

嗨Etan,我想我可能誤解了GLKBaseEffect的本質 - 如果你添加一個紋理效果,這是否停止任何其他類型的繪圖(即填充區域和線)?如果是這樣的話,怎麼可能將紋理和繪圖結合起來? – 2012-07-25 21:23:51

+0

我正在調查類似的問題。見http://stackoverflow.com/questions/14404779/using-glkit-and-opengl-es-how-to-draw-two-triangles-one-textured-another-soli你有沒有運氣?到目前爲止,我可以找出如何做到這一點的唯一方法是創建另一個基礎效果實例並在其上調用prepareToDraw – 2013-01-21 16:59:30