I considered using Apple's GLPaint example (http://developer.apple.com/library/ios/#samplecode/GLPaint) to do some finger painting.
However, I noticed that the playback of the recorded data in the sample is VERY slow. I stepped through the code and开发者_开发问答 couldn't find any artificial delays (there's a 0.01 second delay before drawing each line segment, but this doesn't explain why the whole playback is so slow, and performance doesn't change if this delay is removed).
I need to be able to record the data in my app and show it when the user asks, but the data should be displayed immediately rather than being animated.
I can't just save the final image because I need the actual points.
Is drawing in OpenGL ES really that slow, or am I missing something?
The drawing mechanism is set up to call -presentRenderbuffer:
after connecting every touch point. If you want to draw everything at once, remove the 0.01s delay, loop through all the geometry and draw it at the same time, and move the set up/present of the renderbuffer out of the loop so you only do it once.
I'm using OpenGL to paint. And to do UNDO and REDO, was keeping the points at the "touchesMoved" in an array of points. Then, using renderLineFromPoint, this reproduced the points very very slowly. I solved this by painting all points with the following code. Hope you work it and make you happy as me :)
NSMutableArray *puntos;
// this code in renderLineFromPoint
NSData * pointObject = [NSData dataWithBytes:&start length:sizeof(CGPoint)];
[puntos addObject:pointObject];
pointObject = [NSData dataWithBytes:&end length:sizeof(CGPoint)];
[puntos addObject:pointObject];
// Drawings a line onscreen based on points in nsmutable array
- (void) renderLineFromPoints:(NSMutableArray *) ptos
{
static GLfloat* vertexBuffer = NULL;
static NSUInteger vertexMax = 64;
NSUInteger vertexCount = 0,
count,
i;
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
CGFloat scale = self.contentScaleFactor;
CGPoint start;
CGPoint end;
for (int j=0; j < ptos.count; j++){
// Convert locations from Points to Pixels
start = *(CGPoint*)[[ptos objectAtIndex:j] bytes];
end = *(CGPoint*)[[ptos objectAtIndex:j+1] bytes];
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;
// Allocate vertex array buffer
if(vertexBuffer == NULL)
vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));
// Add points to the buffer so there are drawing points every X pixels
count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);
for(i = 0; i < count; ++i) {
if(vertexCount == vertexMax) {
vertexMax = 2 * vertexMax;
vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
}
vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
vertexCount += 1;
}
j++; // de 2 en 2
}
// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
精彩评论