Opengl slow on blit texture

I called this function once per frame, and I needed FPS from 400 to 33. Why?

sw blt(const PtRect *dstRect, Texture *src, const PtRect *srcRect, RenderDevice::bltFlags flags=RenderDevice::bltDefault)
{
    assert(src);
    GL_Texture *glsrc = dynamic_cast<GL_Texture*>(src);
    if (glsrc == 0)
        return -1;

    PtRect srcRect2(0, 0, src->width, src->height);
    if (srcRect == 0)
        srcRect = &srcRect2;

    PtRect dstRect2(0, 0, srcRect->makeWidth(), srcRect->makeHeight());
    if (dstRect == 0)
        dstRect = &dstRect2;

    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, *glsrc->getTex());

    glBegin( GL_QUADS );
    glNormal3f( 0.0f, 0.0f, 1.0f );
    for (size_t i=0; i<350; i++)
    {
        glTexCoord2f( srcRect->left /src->width, srcRect->top/src->height);    glVertex2f(dstRect->left,  dstRect->top);
        glTexCoord2f( srcRect->right/src->width, srcRect->top/src->height);    glVertex2f(dstRect->right, dstRect->top);
        glTexCoord2f( srcRect->right/src->width, srcRect->bottom/src->height); glVertex2f(dstRect->right, dstRect->bottom);
        glTexCoord2f( srcRect->left /src->width, srcRect->bottom/src->height); glVertex2f(dstRect->left,  dstRect->bottom);
    }
    glEnd();
    return 0;
}

      

0


source to share


4 answers


For performance, you should load your textures in the init function and use a list to display, then in the main render function, like so:

// Generate texture object ID
glGenTextures(1, img);
glBindTexture(GL_TEXTURE_2D, img);

// Set Texture mapping parameters
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);

LoadBMP("texture.bmp");

      

Then, in the main render function, you end up with something like:



// Front face of Cube
glBindTexture(GL_TEXTURE_2D, img);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-fSize, fSize, fSize);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-fSize, -fSize, fSize);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(fSize,-fSize, fSize);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(fSize,fSize, fSize);
glEnd();

      

As you can see, you can wrap the texture in parameters rather than repeating it over and over in a loop.

+1


source


It is difficult to tell without any details, potential problems could be using too large a texture, resulting in software failure in your driver. Plus, you see the cycle 350 times for no apparent reason.



You might be able to improve performance by creating an array of data and then calling glDrawArrays instead of every pair of each glTexCoord / glVertex file

+2


source


Perhaps you are not using Textures with a power of 2 (e.g. 1024x768 instead of 1024x512)?

+1


source


It looks like you are drawing a square 350 times on the screen for some reason. If it is a full screen, then texturing each pixel on the screen is 350 times the frame, which can hurt your performance.

0


source







All Articles