12-11-2010, 10:00 AM
#1 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 7
OpenGL texture memory problem
Hi everybody,
i'm developing a drawing app for iPad, and it's all working fine, except this. I can change texture for brush style etc, but now i wanna implement an undo functionality, every time the user finish to draw a path, i take a photo of the screen, then, if he touches the "Undo" button, i redraw the last image i've taken in the context. Before this i divide the image in 12 pieces, because i can't get to work texture bigger than 256x256, and here the iPad screen is 1024*768.
Last edited by Cors; 12-11-2010 at 10:29 AM .
12-11-2010, 10:02 AM
#2 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 7
Here the code i've used (the post was too long)
I use this code to redraw the suddivided image on the screen:
Code:
CGContextRef brushContext;
GLubyte *brushData;
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
}
brushImage = textureImage1.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256);
glColor4f(1.0, 1.0, 1.0, 1.0);
[self renderLineFromPoint:CGPointMake(128, 640) toPoint:CGPointMake(128, 640)];
brushImage = textureImage2.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(384, 640) toPoint:CGPointMake(384, 640)];
brushImage = textureImage3.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(640, 640) toPoint:CGPointMake(640, 640)];
brushImage = textureImage4.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(896, 640) toPoint:CGPointMake(896, 640)];
brushImage = textureImage5.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(128, 384) toPoint:CGPointMake(128, 384)];
brushImage = textureImage6.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(384, 384) toPoint:CGPointMake(384, 384)];
brushImage = textureImage7.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(640, 384) toPoint:CGPointMake(640, 384)];
brushImage = textureImage8.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(896, 384) toPoint:CGPointMake(896, 384)];
brushImage = textureImage9.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(128, 128) toPoint:CGPointMake(128, 128)];
brushImage = textureImage10.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(384, 128) toPoint:CGPointMake(384, 128)];
brushImage = textureImage11.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(640, 128) toPoint:CGPointMake(640, 128)];
brushImage = textureImage12.CGImage;
brushData = (GLubyte *) calloc(256 * 256 * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, 256, 256, 8, 256 * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)256, (CGFloat)256), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
glPointSize(256); [self renderLineFromPoint:CGPointMake(896, 128) toPoint:CGPointMake(896, 128)];
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(oldRed * kBrushOpacity, oldGreen * kBrushOpacity, oldBlue * kBrushOpacity, kBrushOpacity);
Where textureImage1, textureImage2 ecc are UIImage object passed by function.
I just create a texture from every piece of image, then display them at different points on the screen to recreate the image. Anyway, after 9 or 10 calls to this function, the app crashes.. What am i doing so wrong?
Thanks to all.
12-11-2010, 11:54 AM
#3 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 7
Anybody?
Do you know another method to reach and undo/redo function in openGL drawing app?
Thanks.
12-11-2010, 01:06 PM
#4 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
Cors
Hi everybody,
i'm developing a drawing app for iPad, and it's all working fine, except this. I can change texture for brush style etc, but now i wanna implement an undo functionality, every time the user finish to draw a path, i take a photo of the screen, then, if he touches the "Undo" button, i redraw the last image i've taken in the context. Before this i divide the image in 12 pieces, because i can't get to work texture bigger than 256x256, and here the iPad screen is 1024*768.
I don't know what's wrong with your code, and haven't done any OpenGL programming in long enough that I'd have to crack the books to figure out what's wrong.
Textures must be square, and a power of 2 in height/width. You should be able to create a texture that's 1024x1024 in size and save the entire screen to one texture. It would be big, and use a lot of memory, but it should work.
12-11-2010, 01:37 PM
#5 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 7
Thanks for your reply but i've just tried it some days ago, and it doesn't work. The texture that openGL allow is at least 256x256 px. I know that a texture must be square, infact i divide my 1024x768 in 12 blocks 256x256 each, if you link them together, you'll get the initial image, and this is what i'm doing whit this code, but it takes a lot of memory every time i call the function, i'm not releasing something somewhere?
12-12-2010, 05:26 PM
#6 (permalink )
Registered Member
Join Date: Dec 2010
Posts: 7
Please, anybody can say to me if there is any error?
I really need this piece of function in many parts of my app, an this memory issue is driving me crazy.
12-12-2010, 05:50 PM
#7 (permalink )
Pro. Game Developer
iPhone Dev SDK Supporter
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
If you know you're running out of memory, you're going to have to learn how to profile your app and see where the allocations are taking place, and what isn't being released/freed properly. FWIW, iPad should be able to do 2048x2048 textures, so this nonsense about having a 256x256 limit is ... well ... nonsense. Also, while the dimension of each side of a texture must be a power of two, I'm relatively certain that it does not have to be square. IOW, you can have a textures that are 1024x512, 256x2048, 32x64, etc.
01-17-2011, 06:37 AM
#8 (permalink )
Registered Member
Join Date: Nov 2009
Location: Germany
Posts: 14
I would do what Duncan suggested: grab a 1024x1024 texture. Here's how I do it:
-create a 1k by 1k texture ONCE and reserve enough texture memory:
glGenTextures(1, &screenTexture);
glBindTexture(GL_TEXTURE_2D, screenTexture);
CGSize sz = CGSizeMake(1024.0, 1024.0);
void *data = malloc(sz.width * sz.height * 4);
bzero(data, sz.width * sz.height * 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sz.width, sz.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
free(data);
-grab what's currently in the render buffer into that texture (any time you need to):
glBindTexture(GL_TEXTURE_2D, screenTexture);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, screenBounds.size.width, screenBounds.size.height, 0);
That way you allocate memory once and overwrite it anytime you grab the renderbuffer's content (if I'm not mistaken). It's working flawless and fast in my case.
Steffen
p.s.: and yes, textures need not to be square but *should* have power-of-two edge sizes.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek