iPod/iPhone OpenGL ES UIView flashes when updating

Posted by Dave Viner on Stack Overflow See other posts from Stack Overflow or by Dave Viner
Published on 2010-03-10T04:14:02Z Indexed on 2010/03/13 16:55 UTC
Read the original article Hit count: 786

Filed under:
|
|

I have a simple iPhone application which uses OpenGL ES (v1) to draw a line based on the touches of the user. In the XCode Simulator, the code works perfectly. However, when I install the app onto an iPod or iPhone, the OpenGL ES view "flashes" when drawing the line. If I disable the line drawing, the flash disappears. By "flash", I mean that the background image (which is an OpenGL texture) disappears momentarily, and then reappears. It appears as if the entire scene is completely erased and redrawn.

The code which handles the line drawing is the following:

renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
    static GLfloat*       vertexBuffer = NULL;
    static NSUInteger vertexMax = 64;
    NSUInteger            vertexCount = 0,
    count,
    i;

    //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;
    }

    //Render the vertex array
    glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
    glDrawArrays(GL_POINTS, 0, vertexCount);

    //Display the buffer
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

(This function is based on the function of the same name from the GLPaint sample application.) For the life of me, I can not figure out why this causes the screen to flash. The line is drawn properly (both in the Simulator and in the iPod). But, the flash makes it unusable.

Anyone have ideas on how to prevent the "flash"?

© Stack Overflow or respective owner

Related posts about opengl-es

Related posts about cocoa-touch