开发者

problem while displayin the texture image on view that works fine on iphone simulator but not on device

i am trying to display an image on iphone by converting it into texture and then displaying it on the UIView.

here is the code to load an image from an UIImage object

- (void)loadImage:(UIImage *)image mipmap:(BOOL)mipmap texture:(uint32_t)texture
{

 int width, height;
 CGImageRef cgImage;
 GLubyte *data;
 CGContextRef cgContext;
 CGColorSpaceRef colorSpace;
 GLenum err;

 if (image == nil)
 {
  NSLog(@"Failed to load");
  return;
 }

 cgImage = [image CGImage];
 width = CGImageGetWidth(cgImage);
 height = CGImageGetHeight(cgImage);
 colorSpace = CGColorSpaceCreateDeviceRGB();

 // Malloc may be used instead of calloc if your cg image has dimensions equal to the dimensions of the cg bitmap context
 data = (GLubyte *)calloc(width * height * 4, sizeof(GLubyte));
 cgContext = CGBitmapContextCreate(data, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedLast);
 if (cgContext != NULL)
 {
  // Set the blend mode to copy. We don't care about the previous contents.
  CGContextSetBlendMode(cgContext, kCGBlendModeCopy);
  CGContextDrawImage(cgContext, CGRectMake(0.0f, 0.0f, width, height), cgImage);

  glGenTextures(1, &(_textures[texture]));
  glBindTexture(GL_TEXTURE_2D, _textures[texture]);

  if (mipmap)
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  else
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

  if (mipmap)
   glGenerateMipmapOES(GL_TEXTURE_2D);

  err = glGetError();
  if (err != GL_NO_ERROR)
   NSLog(@"Error uploading texture. glError: 0x%04X", err);

  CGContextRelease(cgContext);
 }

 free(data);
 CGColorSpaceRelease(colorSpace);
}

The problem that i currently am facing is this code workd perfectly fine and displays the image on simulator where as on the device as seen on debugger an error i开发者_运维知识库s displayed i.e. Error uploading texture. glError: 0x0501

any idea how to tackle this bug.... thnx in advance 4 ur soluitons


If the texture has non-power of two dimensions you need to use EXT_texture_rectangle. (GL_TEXTURE_RECTANGLE_EXT instead of GL_TEXTURE_2D and different texture coordinates).

If that doesn't help it would be good to know which line(s) causes the GL_INVALID_VALUE (0x0501) error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜