Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-11-2010, 10:00 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 7
Cors is on a distinguished road
Post 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.
Cors is offline   Reply With Quote
Old 12-11-2010, 10:02 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 7
Cors is on a distinguished road
Default 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.
Cors is offline   Reply With Quote
Old 12-11-2010, 11:54 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 7
Cors is on a distinguished road
Default

Anybody?

Do you know another method to reach and undo/redo function in openGL drawing app?

Thanks.
Cors is offline   Reply With Quote
Old 12-11-2010, 01:06 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Cors View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 12-11-2010, 01:37 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 7
Cors is on a distinguished road
Default

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?
Cors is offline   Reply With Quote
Old 12-12-2010, 05:26 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 7
Cors is on a distinguished road
Question

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.
Cors is offline   Reply With Quote
Old 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
Kalimba is on a distinguished road
Default

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.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 01-17-2011, 06:37 AM   #8 (permalink)
Registered Member
 
darth_steff's Avatar
 
Join Date: Nov 2009
Location: Germany
Posts: 14
darth_steff is on a distinguished road
Default

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.
darth_steff is offline   Reply With Quote
Reply

Bookmarks

Tags
big texture, drawing, ipad, opengl

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 382
7 members and 375 guests
chemistry, daudrizek, HemiMG, jeroenkeij, whitey99
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:56 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0