[英]GLKit on iOS5: setting texture prevents other drawing
我正在嘗試使用GLKit做一些非常簡單的2D繪圖,包括使用紋理。 但是,如果我加載紋理,但是甚至在圖形中甚至不使用紋理,則某種程度上它會阻止圖形的發生!
這是我的設置代碼:
-(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);
}
要調試OpenGL,請在“調試”模式下運行代碼,然后將其暫停,然后從菜單欄中選擇“捕獲OpenGL ES框架”。 這使您可以逐步查看繪圖代碼,並在每次調用后查看OpenGL狀態。 因此,您可以在執行設置代碼和不執行設置代碼之間進行狀態比較,並可以確定錯誤。
對於問題本身:設置效果的texture2d屬性已經足以使用該紋理。 如果將其放在圖紙中,它將自動在圖紙中使用。 在[effect prepareToDraw]
,將應用效果的整個狀態-包括指定的紋理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.