0

次のコードを使用して、openGL を使用して UIImage をレンダリングします。Texture2D クラスは Apple のものなので、正しいと思います。画像が表示されません。glClearColor によって生成された背景色を取得するだけです。私のアプリは Apple の GLpaint サンプル コードに基づいているため、セットアップは正しく、openGL を使用して線をうまく描画できます。

以下のレンダリング コードに何か不足がありますか?

- (void) render:(UIImage *)image
{
[EAGLContext setCurrentContext:context];
glViewport(0, 0, backingWidth, backingHeight);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// texturing will need these
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_2D);

glOrthof(0, backingWidth, 0, backingHeight, -1, 1);

glMatrixMode(GL_MODELVIEW);

glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

Texture2D *texture = [[Texture2D alloc] initWithImage:image];

[texture drawInRect: self.frame];


// This application only creates a single color renderbuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple renderbuffers.
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}
4

1 に答える 1

0

update :元の回答の壊れたリンクを実際のコードに置き換えました。

これが私のために働いたコードです:

-(void) mergeWithImage:(UIImage*) image
{
if(image==nil)
{
    return;
}
glPushMatrix();
glColor4f(256,
          256,
          256,
          1.0);
GLuint stampTexture; // = texture.id;

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glGenTextures(1, &stampTexture);
glBindTexture(GL_TEXTURE_2D, stampTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

GLuint imgwidth = CGImageGetWidth(image.CGImage);
GLuint imgheight = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( imgheight * imgwidth * 4 );
CGContextRef context2 = CGBitmapContextCreate( imageData, imgwidth, imgheight, 8, 4 * imgwidth, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGContextTranslateCTM (context2, 0, imgheight);
CGContextScaleCTM (context2, 1.0, -1.0);
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context2, CGRectMake( 0, 0, imgwidth, imgheight ) );
CGContextTranslateCTM( context2, 0, imgheight - imgheight );
CGContextDrawImage( context2, CGRectMake( 0, 0, imgwidth, imgheight ), image.CGImage );

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgwidth, imgheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

CGContextRelease(context2);

free(imageData);

static const GLfloat texCoords[] = {
    0.0, 1.0,
    1.0, 1.0,
    0.0, 0.0,
    1.0, 0.0
};

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

/*
 These arrays would need to be changed if the size of the paintview changes. You must make sure that all image imput is 64x64, 256x256, 512x512 or 1024x1024.  Here we are using 512, but you can use 1024 as follows:

 use the numbers:
 {
 0.0, height, 0.0,
 1024, height, 0.0,
 0.0, height-1024, 0.0,
 1024, height-1024, 0.0
 }
 */

NSLog(@"height of me: %f", self.bounds.size.height);

static const GLfloat vertices[] = {
    0.0,  643, 0.0,
    1024,  643, 0.0,
    0.0, -381, 0.0,
    1024, -381, 0.0
};

static const GLfloat normals[] = {
    0.0, 0.0, 1024,
    0.0, 0.0, 1024,
    0.0, 0.0, 1024,
    0.0, 0.0, 1024
};


glBindTexture(GL_TEXTURE_2D, stampTexture);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glPopMatrix();

glDeleteTextures( 1, &stampTexture );

// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

}



-(void) addImageToView{
UIImage* imageToAdd= [UIImage imageNamed:@"IMAGE_TO_ADD_TO_GL_VIEW.png"];


//     all images added to the paining view MUST be 512x512.
//     You can also add smaller images (even transformed ones) using this method - just add it to a UIView and then get it's graphics context

UIView* imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 512, 512)];



UIImageView* subView   = [[UIImageView alloc] initWithImage:imageToAdd];


[imageView addSubview:subView];
UIImage* blendedImage =nil;

UIGraphicsBeginImageContext(imageView.frame.size);

[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];

blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self mergeWithImage: blendedImage ];
于 2013-09-08T01:42:12.357 に答える