我正在尝试使用 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);
}