gpt4 book ai didi

objective-c - 将 UIImage(或 JPEG)绘制到 EAGLView 上

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:14:19 24 4
gpt4 key购买 nike

我正在制作一个 PDF 注释器,当您切换页面时,它必须重新绘制所有以前绘制的 OpenGL 内容(以 JSON 格式保存到文件中)。问题是绘制的内容越多,花费的时间越长。我为每个页面都将一个 UIImage 保存到磁盘,因此我希望通过一次大笔画将该 UIImage 绘制到 EAGLContext 上来加快这个过程。

我想知道如何获取 UIImage(或 JPEG/PNG 文件)并将其直接绘制到屏幕上。它必须在 EAGLView 上的原因是因为它需要支持橡皮擦,而使用常规的 UIKit 方式将无法使用。

我假设有一些方法可以将画笔设置为整个图像,然后只用它在屏幕上标记一次。有什么建议吗?

最佳答案

作为迂腐的注释,there is no standard class named EAGLView ,但我假设您指的是 Apple 的示例 UIView 子类之一,该子类托管 OpenGL ES 内容。

这样做的第一步是将 UIImage 加载到纹理中。以下是我在我的图像处理框架中为此使用的一些代码(newImageSource 是输入 UIImage):

CGSize pointSizeOfImage = [newImageSource size];
CGFloat scaleOfImage = [newImageSource scale];
pixelSizeOfImage = CGSizeMake(scaleOfImage * pointSizeOfImage.width, scaleOfImage * pointSizeOfImage.height);
CGSize pixelSizeToUseForTexture = pixelSizeOfImage;

BOOL shouldRedrawUsingCoreGraphics = YES;

// For now, deal with images larger than the maximum texture size by resizing to be within that limit
CGSize scaledImageSizeToFitOnGPU = [GPUImageOpenGLESContext sizeThatFitsWithinATextureForSize:pixelSizeOfImage];
if (!CGSizeEqualToSize(scaledImageSizeToFitOnGPU, pixelSizeOfImage))
{
pixelSizeOfImage = scaledImageSizeToFitOnGPU;
pixelSizeToUseForTexture = pixelSizeOfImage;
shouldRedrawUsingCoreGraphics = YES;
}

if (self.shouldSmoothlyScaleOutput)
{
// In order to use mipmaps, you need to provide power-of-two textures, so convert to the next largest power of two and stretch to fill
CGFloat powerClosestToWidth = ceil(log2(pixelSizeOfImage.width));
CGFloat powerClosestToHeight = ceil(log2(pixelSizeOfImage.height));

pixelSizeToUseForTexture = CGSizeMake(pow(2.0, powerClosestToWidth), pow(2.0, powerClosestToHeight));

shouldRedrawUsingCoreGraphics = YES;
}

GLubyte *imageData = NULL;
CFDataRef dataFromImageDataProvider;

if (shouldRedrawUsingCoreGraphics)
{
// For resized image, redraw
imageData = (GLubyte *) calloc(1, (int)pixelSizeToUseForTexture.width * (int)pixelSizeToUseForTexture.height * 4);

CGColorSpaceRef genericRGBColorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(imageData, (int)pixelSizeToUseForTexture.width, (int)pixelSizeToUseForTexture.height, 8, (int)pixelSizeToUseForTexture.width * 4, genericRGBColorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, pixelSizeToUseForTexture.width, pixelSizeToUseForTexture.height), [newImageSource CGImage]);
CGContextRelease(imageContext);
CGColorSpaceRelease(genericRGBColorspace);
}
else
{
// Access the raw image bytes directly
dataFromImageDataProvider = CGDataProviderCopyData(CGImageGetDataProvider([newImageSource CGImage]));
imageData = (GLubyte *)CFDataGetBytePtr(dataFromImageDataProvider);
}

glBindTexture(GL_TEXTURE_2D, outputTexture);
if (self.shouldSmoothlyScaleOutput)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)pixelSizeToUseForTexture.width, (int)pixelSizeToUseForTexture.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, imageData);

if (self.shouldSmoothlyScaleOutput)
{
glGenerateMipmap(GL_TEXTURE_2D);
}

if (shouldRedrawUsingCoreGraphics)
{
free(imageData);
}
else
{
CFRelease(dataFromImageDataProvider);
}

如你所见,这有一些函数用于调整超过设备最大纹理大小的图像(上面代码中的类方法仅查询最大纹理大小),以及一个 bool 标志是否为纹理生成 mipmap 以实现更平滑的下采样。如果您不关心这些情况,可以删除这些。这也是 OpenGL ES 2.0 代码,因此您可能需要将一两个 OES 后缀添加到上面的某些函数中,以便它们与 1.1 一起使用。

在纹理中拥有 UIImage 后,您可以使用带纹理的四边形(两个三角形组成一个矩形,角具有适当的纹理坐标)将其绘制到屏幕上。如何做到这一点在 OpenGL ES 1.1 和 2.0 之间会有所不同。对于 2.0,您使用直通着色器程序,该程序仅从纹理中的该位置读取颜色并将其绘制到屏幕上;对于 1.1,您只需为几何体设置纹理坐标并绘制两个三角形。

我在 this answer 中有一些用于此的 OpenGL ES 2.0 代码.

关于objective-c - 将 UIImage(或 JPEG)绘制到 EAGLView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11958037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com