简体   繁体   English

iOS5上的GLKit:设置纹理可防止其他绘图

[英]GLKit on iOS5: setting texture prevents other drawing

I am attempting to do some very simple 2D drawing with GLKit, including using a texture. 我正在尝试使用GLKit做一些非常简单的2D绘图,包括使用纹理。 However, if I load the texture, but don't even use it in the drawing, somehow it prevents the drawing from taking place! 但是,如果我加载纹理,但是甚至在图形中甚至不使用纹理,则某种程度上它会阻止图形的发生!

Here is my setup code: 这是我的设置代码:

-(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);

}

Here is my drawing code: 这是我的绘图代码:

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

For debugging OpenGL, run the code in Debug mode, then pause it and choose "Capture OpenGL ES frame" from the menu bar. 要调试OpenGL,请在“调试”模式下运行代码,然后将其暂停,然后从菜单栏中选择“捕获OpenGL ES框架”。 This allows you to step through your drawing code and see the OpenGL state after each call. 这使您可以逐步查看绘图代码,并在每次调用后查看OpenGL状态。 Therefore, you can compare the states between when you do the setup code and when you don't do it, and can pin down the bug. 因此,您可以在执行设置代码和不执行设置代码之间进行状态比较,并可以确定错误。

For the problem itself: Setting the texture2d property of the effect is already enough to use the texture. 对于问题本身:设置效果的texture2d属性已经足以使用该纹理。 It is automatically used in the drawing if you put it there. 如果将其放在图纸中,它将自动在图纸中使用。 In [effect prepareToDraw] , the whole state of the effect is applied - including the specified texture. [effect prepareToDraw] ,将应用效果的整个状态-包括指定的纹理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM