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 03-20-2011, 08:56 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
-Jeka- is on a distinguished road
Default OpenGL 1.1 strange slow drawing by zooming

Hello everyone,
I have created a view (derived from UIScrollView) where the cells are drawn and zoom occurs. Everything is drawn with OpenGL ES 1.1.
Zoom is specified by adding of a calculated new distance which is passed by fingers (sliding apart or moving together), which is multiplied by a constant coefficient:

Code:
fNewZoom += fDistance * fCoef;
fDistance may have negative value at minimization

There are 4 arrays of cells, each array contains cells with definite size. At zooming the most optimal cells by size are chosen for drawing and are drawn with the OpenGL ES 1.1. The initialization of all four textures takes place at the start while initialization of the drawing view.

Initialization of the view:

Code:
+ (Class) layerClass
{
	return [CAEAGLLayer class];
}

- (id)initWithCoder:(NSCoder*)coder
{
	if((self = [super initWithCoder:coder])) 
	{
		CAEAGLLayer *eaglLayer = (CAEAGLLayer*) self.layer;
		
		eaglLayer.opaque = NO;
		eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
										[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
				
		m_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
		if(!m_context || ![EAGLContext setCurrentContext:m_context] || ![self createFramebuffer]) 
		{
			[self release];
			return nil;
		}
		
		[self InitGLTextures];
	}
	
	return self;
}
Drawing specifically in function "- (void)drawView":

Code:
- (void)layoutSubviews
{
	[self InvalidateView];
}

- (void)InvalidateView
{	
	[EAGLContext setCurrentContext:m_context];
	[self destroyFramebuffer];
	[self createFramebuffer];
	[self drawView];             //drawing view
}

- (BOOL)createFramebuffer
{
	glGenFramebuffersOES(1, &m_viewFramebuffer);
	glGenRenderbuffersOES(1, &m_viewRenderbuffer);
	
	glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_viewFramebuffer);
	glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_viewRenderbuffer);
	[m_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
	glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, m_viewRenderbuffer);
	
	glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &m_nBackingWidth);
	glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &m_nBackingHeight);
	
	if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) 
	{
		NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
		return NO;
	}
	
	return YES;
}

- (void)destroyFramebuffer
{
	glDeleteFramebuffersOES(1, &m_viewFramebuffer);
	m_viewFramebuffer = nil;
	glDeleteRenderbuffersOES(1, &m_viewRenderbuffer);
	m_viewRenderbuffer = nil;
}

//TEST DRAWING VIEW !!!!
- (void)drawView
{
	const int		nClues = 3; // = 900
	const ImgElDimensionsF	cellDimensions = _skinMgr.GetCellInfo();
	const CGPoint		offset = [self contentOffset];
	
	float	fDrawX = 0;
	float	fDrawY = 0;
	int	nCount1 = 1;	
	
	[self BeginDrawing];
	//
	for (int nRow = 0; nRow < nClues; nRow++)
	{
		for (int nColl = 0; nColl < nClues; nColl++)
		{
			[self AddNewCellType:imgCellTypeSelectedEmpty PosX:fDrawX PosY:fDrawY];
			fDrawX += cellDimensions.fWidth;
		}
		
		fDrawX = 0;
		fDrawY += cellDimensions.fHeight;
	}
	//
	[self EndDrawing];
}
functions used for drawing:

Code:
- (void)BeginDrawing
{
	m_pCellTextureArr = _skinMgr.GL_GetProperlyCellTextInfo();
	
	glViewport(0, 0, m_nBackingWidth, m_nBackingHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrthof(0.0f, (GLfloat)m_nBackingWidth, 0.0f, (GLfloat)m_nBackingHeight, 0.0f, 1.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	// Clears the view with black
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	
	// Make sure that you are drawing to the current context
	[EAGLContext setCurrentContext:m_context];
	glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_viewFramebuffer);
	glClear(GL_COLOR_BUFFER_BIT);
	//correct matrix pos
	glTranslatef(0, (CGFloat)m_nBackingHeight, 0);
	
}

- (void)EndDrawing
{
	ImgSize	curImgSize = _skinMgr.GetCurrentImgSize();
	
	glBindTexture(GL_TEXTURE_2D, m_cellsTextureArr[curImgSize]);
	glEnable(GL_TEXTURE_2D);
	glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);	
	
	//if back cells was added
	if (m_nCurAddBackCell > 0)
	{
		glVertexPointer(2, GL_FLOAT, 0, m_spriteVerticesBack);
		glEnableClientState(GL_VERTEX_ARRAY);
		glTexCoordPointer(2, GL_FLOAT, 0, m_spriteTexcoordsBack);
		glEnable(GL_TEXTURE_2D);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glDrawArrays(GL_TRIANGLES, 0, POINTS_PER_CELL * m_nCurAddBackCell);
		glDisableClientState(GL_VERTEX_ARRAY);
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		//
		m_nCurAddBackCell = 0;
	}
}
With the help of this function the coordinate array of texture vertices for drawing is formed where m_spriteVerticesBack and m_spriteTexcoordsBack are dynamic arrays, seleсted by means of "new" function (m_spriteVerticesBack = new float[MAX_COUNT_CELLS * 12] )

Code:
- (BOOL)AddNewCellType:(ImgCellType)cellType PosX:(float)posX PosY:(float)posY
{
	ImgElDimensionsF	cellDimensions = _skinMgr.GL_GetCurrerntCellDimensions(cellType);
	ImgTextureInfoF		imgCellInfo = m_pCellTextureArr[cellType];
	
	if (m_nCurAddBackCell >= m_nMaxCountBackCell)
		return FALSE;
		
	const int idx = m_nCurAddBackCell * ELEMENTS_PER_CELL;
	//////////
	//*-------
	//|  3--4
	//|  |  |
	//|  1--2
	//*
	// 1X
	m_spriteVerticesBack[idx + 0] = posX;
	// 1Y
	m_spriteVerticesBack[idx + 1] =  -posY - cellDimensions.fHeight;
	// 2X
	m_spriteVerticesBack[idx + 2] = posX + cellDimensions.fWidth;
	// 3Y
	m_spriteVerticesBack[idx + 3] = -posY - cellDimensions.fHeight;
	// 3X
	m_spriteVerticesBack[idx + 4] = posX;
	// 3Y
	m_spriteVerticesBack[idx + 5] = -posY;
	// 2X 
	m_spriteVerticesBack[idx + 6] = posX + cellDimensions.fWidth;
	// 3Y
	m_spriteVerticesBack[idx + 7] = -posY - cellDimensions.fHeight;
	// 3X
	m_spriteVerticesBack[idx + 8] = posX;
	// 3Y
	m_spriteVerticesBack[idx + 9] = -posY;	
	// 4X
	m_spriteVerticesBack[idx + 10] = posX + cellDimensions.fWidth;
	// 4Y
	m_spriteVerticesBack[idx + 11] = -posY;
		
	//////////
	//1--2
	//|  |
	//3--4
	//
	// 1X
	m_spriteTexcoordsBack[idx + 0] = imgCellInfo.fX;
	// 1Y
	m_spriteTexcoordsBack[idx + 1] = imgCellInfo.fY + imgCellInfo.fHeight;
	// 2X
	m_spriteTexcoordsBack[idx + 2] = imgCellInfo.fX + imgCellInfo.fWidth;
	// 2Y
	m_spriteTexcoordsBack[idx + 3] = imgCellInfo.fY + imgCellInfo.fHeight;
	// 3X
	m_spriteTexcoordsBack[idx + 4] = imgCellInfo.fX;
	// 3Y
	m_spriteTexcoordsBack[idx + 5] = imgCellInfo.fY;
	// 2X
	m_spriteTexcoordsBack[idx + 6] = imgCellInfo.fX + imgCellInfo.fWidth;
	// 2Y
	m_spriteTexcoordsBack[idx + 7] = imgCellInfo.fY + imgCellInfo.fHeight;
	// 3X
	m_spriteTexcoordsBack[idx + 8] = imgCellInfo.fX;
	// 3Y
	m_spriteTexcoordsBack[idx + 9] = imgCellInfo.fY;
	// 4X
	m_spriteTexcoordsBack[idx + 10] = imgCellInfo.fX + imgCellInfo.fWidth;
	// 4Y
	m_spriteTexcoordsBack[idx + 11] = imgCellInfo.fY;
	//
	m_nCurAddBackCell++;
	return TRUE;
}
The problem is that indifferently how many cells I draw - 9 (field size 3 х 3) or 900 (field size 30 х 30), the speed of drawing at zoom on (iPhone 2G) is the same!!!
What decelerates drawing? Drawing is performed like jerking. What was my mistake? What have I omited? I want the increment or minimization to become gradually, how can I achieve that?
I draw in the window which is derived from UIScrollView.
-Jeka- is offline   Reply With Quote
Old 03-20-2011, 12:00 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Do I understand that you're drawing views with OpenGL and then including them in a scrollview? Apple doesn't recommend mixing UIview with OpenGL like that and warns of performance problems.

I'd ditch the scrollview and draw everything directly in on EAGLView. That's the path to performance.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-20-2011, 02:46 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
-Jeka- is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Do I understand that you're drawing views with OpenGL and then including them in a scrollview? Apple doesn't recommend mixing UIview with OpenGL like that and warns of performance problems.

I'd ditch the scrollview and draw everything directly in on EAGLView. That's the path to performance.
This is the my view that draws cell:
Code:
@interface OpenGLBasedView : UIScrollView 
{
	EAGLContext*	m_context;
	bool			m_bAllowRedrawing;
	bool			m_bLandskapeOrientation;
	
	/* OpenGL names for the renderbuffer and framebuffers used to render to this view */
	GLuint			m_viewRenderbuffer;
	GLuint			m_viewFramebuffer;
.......................................
I'm used example GLSprite, where drawing view derived from UIView:

Code:
@interface EAGLView : UIView
{
@private
	
	/* The pixel dimensions of the backbuffer */
	GLint backingWidth;
	GLint backingHeight;
	
	EAGLContext *context;
.......................................

Please tell me how I can implement EAGLView and add it to my scrollview?
-Jeka- is offline   Reply With Quote
Old 03-30-2011, 09:08 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 6
-Jeka- is on a distinguished road
Default

Isn't there anybody who knows the solution to my problem? I cannot make a smooth and quick content zoom within the view with the help of OpenGL. And it does not matter how many squares are drawn. By the way, the same situation arises when my drawing class is derived from UIView, as it is shown in the examples of the documentation for developers.
-Jeka- is offline   Reply With Quote
Reply

Bookmarks

Tags
eaglview, opengl es, uiscrollview

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: 356
11 members and 345 guests
7twenty7, condor304, Creativ, Domele, dreamdash3, laureix68, LEARN2MAKE, michelle, mistergreen2011, Sami Gh, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,661
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, tinamm64
Powered by vBadvanced CMPS v3.1.0

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