iPhone OpenGL scrolling background jumps when texture is drawn for first time

Posted by Magnum39 on Stack Overflow See other posts from Stack Overflow or by Magnum39
Published on 2010-04-26T18:38:20Z Indexed on 2010/04/26 18:43 UTC
Read the original article Hit count: 122

Filed under:
|

I have been fighting a problem for a while now and would appreciate any help anybody could give.

I have a sprite that moves within a landscape. The sprite remains in the center of the screen and the background moves to simulate that the sprite is moving within the landscape. I have split the landscape into sections so that I only draw the sections of the landscape that I need (are on screen).

The Problem: As a new texture section of the screen appears on the screen (is drawn for the first time) the movement jumps. Almost like a frame is missed. I have done some timing experiments and I do not thinks a frame is missed. My processing is well below the 30fps that I have the animation set to. It only happens the first time the texture section is drawn.

Is there something extra that is done the first time a texture is drawn?

Here is the code:

- (void) render{    

// Sets up an array of values to use as the sprite vertices.
const GLfloat sVerts[] = {
    -1.6f, -1.6f,
    1.6f, -1.6f,
    -1.6f,  1.6f,
    1.6f,  1.6f,
};

static const GLfloat sTexCoords[] = {
    0.0, 1.0,
    1.0, 1.0,
    0.0, 0.0,
    1.0, 0.0
};
glDisableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Setup opengl to draw the object in correct orientation, size, position, etc  
glLoadIdentity();
// Enable use of the texture
glEnable(GL_TEXTURE_2D);
glVertexPointer(2, GL_FLOAT, 0, sVerts);
glTexCoordPointer(2, GL_FLOAT, 0, sTexCoords);
// draw the texture

// set the position of the first tile
float xOffset = -4.8;
float yOffset = 4.8;

int i;
int y;
int currentTexture = textureA;

for(i=0; i<2; i++)
{
    for(y=0; y<2; y++)
    {
        // test for the texture tile on the screen if not on screen then do not draw
        float localX = xOffset+(3.21*y);
        float localY = yOffset-(3.21*i);
        float xDiff = monkeyX - localX;
        float yDiff = monkeyY - localY;

        if(((xDiff < 3.2) && (xDiff > -3.2)) && ((yDiff <2.7) && (yDiff > -2.7)))
        {
            // bind the texture and set the vertex data pointers
            glBindTexture(GL_TEXTURE_2D, spriteTexture[currentTexture]);

            // move to draw position for the texture
            glLoadIdentity();
            glTranslatef((localX+self.positionX), (localY+self.positionY), 0.0);
            //draw the texture  
            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        }
        currentTexture++;
    }

}   

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about opengl