从 UIView 子类中使用 OpenGL 绘制 UIImage
我只是想使用 OpenGL 将 backgroung_pic.png
绘制到整个视图。请帮忙!我是 OpenGL 的菜鸟,我正在尝试制作一个类似 Photoshop 的应用程序,并且正在学习 OpenGL 来完成在图像上绘图。
到目前为止我的代码。
[EAGLContext setCurrentContext:context];
CGContextRef imageContext;
GLubyte *imageData;
size_t width, height;
CGImageRef backgroundImage;
backgroundImage = [UIImage imageNamed:@"background_pic.png"].CGImage;
// Get the width and height of the image
width = CGImageGetWidth(backgroundImage);
height = CGImageGetHeight(backgroundImage);
// Make sure the image exists
if(backgroundImage) {
// Allocate memory needed for the bitmap context
imageData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
// Use the bitmatp creation function provided by the Core Graphics framework.
imageContext = CGBitmapContextCreate(imageData, width, height, 8, width * 4, CGImageGetColorSpace(backgroundImage), kCGImageAlphaPremultipliedLast);
// After you create the context, you can draw the image to the context.
CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), backgroundImage);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
CGContextRelease(imageContext);
//I'm thinking the drawing goes in here
free(imageData);
}
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
I am just trying to draw backgroung_pic.png
to the entire view using OpenGL. Please help! I am a noob to OpenGL and I'm trying to make a photoshop-ish app and am just learning OpenGL to accomplish drawing on images.
My code so far.
[EAGLContext setCurrentContext:context];
CGContextRef imageContext;
GLubyte *imageData;
size_t width, height;
CGImageRef backgroundImage;
backgroundImage = [UIImage imageNamed:@"background_pic.png"].CGImage;
// Get the width and height of the image
width = CGImageGetWidth(backgroundImage);
height = CGImageGetHeight(backgroundImage);
// Make sure the image exists
if(backgroundImage) {
// Allocate memory needed for the bitmap context
imageData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
// Use the bitmatp creation function provided by the Core Graphics framework.
imageContext = CGBitmapContextCreate(imageData, width, height, 8, width * 4, CGImageGetColorSpace(backgroundImage), kCGImageAlphaPremultipliedLast);
// After you create the context, you can draw the image to the context.
CGContextDrawImage(imageContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), backgroundImage);
// You don't need the context at this point, so you need to release it to avoid memory leaks.
CGContextRelease(imageContext);
//I'm thinking the drawing goes in here
free(imageData);
}
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论