Pregunta

I am currently working on a project which needs to load image. I am using SOIL library to load JPEG image. I have to move the ball in the projection area. The movement of the ball works fine without the image loaded but it becomes discretely slow with the image loaded. What should I do to make the graphics update smoothly with the image still loaded.

GLuint tex_2d = SOIL_load_OGL_texture
(
    "<image_path>ImageName.jpg",
    SOIL_LOAD_AUTO,
    SOIL_CREATE_NEW_ID,
    SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);

glBindTexture(GL_TEXTURE_2D, tex_2d);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
    glTexCoord3f(0.0f,0.0f,0.0f);        glVertex3f(factor*BOARD_BREADTH/2,-factor*BOARD_LENGTH/2,0);
    glTexCoord3f(0.0f,1.0f,0.0f);        glVertex3f(factor*BOARD_BREADTH/2,factor*BOARD_LENGTH/2,0);
    glTexCoord3f(1.0f,1.0f,0.0f);        glVertex3f(-factor*BOARD_BREADTH/2,factor*BOARD_LENGTH/2,0);
    glTexCoord3f(1.0f,0.0f,0.0f);        glVertex3f(-factor*BOARD_BREADTH/2,-factor*BOARD_LENGTH/2,0);
glEnd();
¿Fue útil?

Solución

What should I do to make the graphics update smoothly with the image still loaded.

Not reloading the image with every redraw. Also doing what you do right now causes a lot of memory to be leaked, as you probably not delete the textures you create each frame. Move the whole tex_2d = SOIL_load_OGL_texture into one-time initialization code.

Otros consejos

simple procedural style:

init() {
    // load texture here
    // load shaders and other resources
}

render() {
    // use resources here to draw something       
}

main code:

main() {
    init();
    while(still_running())
    {
        // update all
        render();
    }
    // clean up here
}

Note that init is called only once, but render is called, let us say, 60 times per second. In your code you create 60 textures per second! (although they have the same pixels!)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top