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

更新:用实际代码替换原始答案中的断开链接。

这是对我有用的代码:

-(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 回答