I’m experiencing a really strange glitch in my ipad app. It’s super simple: I just use the “touchesMoved” handler to draw a line between two points. Since I would like lines to stay on screen, I’m not calling “glClear” in my draw function, but for some reason some of the lines just drop out, and it appears completely random. Stranger yet, it works perfectly in the simulator. Does anybody have any insight into why this might be? I’ve included my touch and draw routines.
Many thanks!
Pete
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
switch ([allTouches count])
{
case 1:
{ //Single touch
} break;
case 2:
{ //Double Touch
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
CGPoint location1 = [touch1 locationInView: [touch1 view]];
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
CGPoint location2 = [touch2 locationInView: [touch2 view]];
[self drawLineWithStart:location1 end:location2];
} break;
default:
break;
}
}
- (void)drawLineWithStart:(CGPoint)start end:(CGPoint) end
{
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
GLfloat lineVertices[] =
{
(start.x/(768.0/2.0)) - 1.0, -1.5 * ((start.y/(1024.0/2.0)) - 1.0),
(end.x/(768.0/2.0)) - 1.0, -1.5 * ((end.y/(1024.0/2.0)) - 1.0)
};
glDisable(GL_TEXTURE_2D);
// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, lineVertices);
glDrawArrays(GL_LINE_STRIP, 0, 2);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
Depending on the situation, you can also retain the backing. This will solve your issue here, but will cost you performance.
In your OpenGLES2DView.m change kEAGLDrawablePropertyRetainedBacking to YES. It will look like this:
But to reiterate, it will cost performance, however I would say if you are trying to write a drawing application (from the looks of it) it might be what you are looking for. Since in drawing apps, it is not a good idea to rerun the logic to redraw every frame, this is a good solution.