-1

OpenGL ES1 3D モデルの作成と保存に取り組んでおり、3D モデル データと同じファイル内に、テクスチャとして使用する画像ファイルを含めたいと考えています。使用可能な形式で画像データを読み込めません。UIImageJPEGRepresentation を使用して画像データを変換し、NSData に保存しています。次に、それをすべての 3D データと共に NSMutableData オブジェクトに追加し、ファイルに書き出します。データはエラーなく読み書きできるようですが、画像データを使用して 3D モデルのテクスチャ データを生成するために使用する「CGImageRef」を作成しようとすると、問題が発生します。「CGContextDrawImage: 無効なコンテキスト 0x0. 「CGImageRef」を作成しようとすると。「CGImageRef」を作成しようとすると、画像データが何らかの形でずれているため、拒否された可能性があります。助けていただければ幸いです。私はこの時点で困惑しています。すべてのデータ サイズとオフセットが加算され、問題なく表示されます。セーブとロードはエラーなしで行われます。画像データが少しずれているように見えますが、その理由はわかりません。

これが私のコードです:

//======================================================
- (BOOL)save3DFile: (NSString *)filePath {


  // load TEST IMAGE into UIIMAGE
  UIImage *image = [UIImage imageNamed:@“testImage.jpg"];

  // convert image to JPEG encoded NSDATA
  NSData *imageData = UIImageJPEGRepresentation(image,1.0);

  // Save length of imageData to global "imDataLen" to use later in “load3DFile”
  imDataLen = [imageData length];

  // TEST: this works fine for CGImageRef creation in “loadTexture” 
  // traceView.image=[UIImage imageWithData:[imageData subdataWithRange:NSMakeRange(0, imageDataLen)]];  
  // [self loadTexture];

  // TEST: this also works fine for CGImageRef creation in “loadTexture” 
  // traceView.image=[UIImage imageWithData:txImData];
  // [self loadTexture];

  fvoh.fileVersion  = FVO_VERSION;
  fvoh.obVertDatLen = obVertDatLen;
  fvoh.obFaceDatLen = obFaceDatLen;
  fvoh.obNormDatLen = obNormDatLen;
  fvoh.obTextDatLen = obTextDatLen;
  fvoh.obCompCount  = obCompCount;
  fvoh.obVertCount  = obVertCount;
  fvoh.obElemCount  = obElemCount;
  fvoh.obElemSize   = obElemSize;
  fvoh.obElemType   = obElemType;

  NSMutableData *obSvData;
  obSvData=[NSMutableData dataWithBytes:&fvoh length:(sizeof(fvoh))];
  [obSvData appendBytes:obElem   length:obFaceDatLen];
  [obSvData appendBytes:mvElem   length:obVertDatLen];
  [obSvData appendBytes:mvNorm   length:obNormDatLen];
  [obSvData appendBytes:obText   length:obTextDatLen];
  [obSvData appendBytes:&ds      length:(sizeof(ds))];

  // next, we append image data, and write all data to a file
  // seems to work fine, no errors, at this point
  [obSvData appendBytes: imageData length:[imageData length]];  

  BOOL success=[obSvData writeToFile: filePath atomically:YES];
  return success; 
}
//======================================================
- (void) load3DFile:(NSString *)filePath {

  NSData *fvoData;
  NSUInteger offSet,fiLen,fhLen,dsLen;
  [[FileList sharedFileList] setCurrFile:(NSString *)filePath];

  fvoData=[NSData dataWithContentsOfFile:filePath];
  fiLen=[fvoData length];
  fhLen=sizeof(fvoh);
  dsLen=sizeof(ds);

  memcpy(&fvoh,[fvoData bytes],fhLen);offSet=fhLen;

  //+++++++++++++++++++++++++++++++
  obVertDatLen = fvoh.obVertDatLen;
  obFaceDatLen = fvoh.obFaceDatLen;
  obNormDatLen = fvoh.obNormDatLen;
  obTextDatLen = fvoh.obTextDatLen;
  obCompCount  = fvoh.obCompCount;
  obVertCount  = fvoh.obVertCount;
  obElemCount  = fvoh.obElemCount;
  obElemSize   = fvoh.obElemSize;
  obElemType   = fvoh.obElemType;
  //+++++++++++++++++++++++++++++++

  memcpy(obElem, [fvoData bytes]+offSet,obFaceDatLen);offSet+=obFaceDatLen;
  memcpy(mvElem, [fvoData bytes]+offSet,obVertDatLen);offSet+=obVertDatLen;
  memcpy(mvNorm, [fvoData bytes]+offSet,obNormDatLen);offSet+=obNormDatLen;
  memcpy(obText, [fvoData bytes]+offSet,obTextDatLen);offSet+=obTextDatLen;
  memcpy(&ds,    [fvoData bytes]+offSet,dsLen);offSet+=dsLen;

  // the following seem to read the data into “imageData” just fine, no errors
  // NSData *imageData = [fvoData subdataWithRange:NSMakeRange(offSet, imDataLen)];
  // NSData *imageData = [fvoData subdataWithRange:NSMakeRange((fiLen-imDataLen), imDataLen)];
  // NSData *imageData = [NSData dataWithBytes:[fvoData bytes]+offSet length: imDataLen];
  NSData *imageData = [NSData dataWithBytes:[fvoData bytes]+(fiLen-imDataLen) length: imDataLen];

  // but the contents of imageData seem to end up in an unexpected format, causing error: 
  // “CGContextDrawImage: invalid context 0x0.” during CGImageRef creation in “loadTexture”

  traceView.image=[UIImage imageWithData:imageData];
  [self loadTexture];
}
//======================================================
- (void)loadTexture {

  CGImageRef image=[traceView.image].CGImage;
  CGContextRef texContext;GLubyte* bytes=nil;GLsizei width,height;

  if(image){
      width=(GLsizei)CGImageGetWidth(image);
      height=(GLsizei)CGImageGetHeight(image);
      bytes=(GLubyte*) calloc(width*height*4,sizeof(GLubyte));
      texContext=CGBitmapContextCreate(bytes,width,height,8,width*4,CGImageGetColorSpace(image),
      kCGImageAlphaPremultipliedLast);
      CGContextDrawImage(texContext,CGRectMake(0.0,0.0,(CGFloat)width,(CGFloat)height),image);
      CGContextRelease(texContext);
  }

  if(bytes){
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
      glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,bytes);
      free(bytes);
  }
}
//======================================================
4

1 に答える 1