Switching rendering from SDL to OpenGL

Started by
7 comments, last by solewalker 11 years, 3 months ago

Hi!

I took it upon myself to finally start migrating from SDL to OpenGl in my project today. So far I've managed to (in isolated code) translate an SDL_Surface to an OpenGl texture which i then use to apply on polygons.

In my previous rendering method i used a lot of clips. Meaning I've got one image with maybe 3-5 images on it which i then clip before i render to get the desired image to show. I've managed to translate this over to OpenGl as well but it takes a bit of work and i need to know the loaded image dimensions before i do.

Just to clarify how the rendering process works now, so you get a better scope of my problem:

1. Loads image to an SDL_Surface

2. Saves it to an array

3. Something fetches the image memory location from the array

4. Converts the SDL_Surface to a OpenGl texture

5. Clips the texture

6. Renders it to QUADS

7. Destroys the texture

My Convert code is basically the same as this one here: http://content.gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL

Question time:

1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.

2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png

3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?

4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

I think that's all! Thanks for your time, i really appreciate answers!

// Tallkotten

Advertisement

I'm no expert, but surely you aren't converting those images every frame? Even if you're holding onto the surface to draw with it elsewhere, I can't imagine a scenario where converting it every frame would be good for performance.

My suggestions is to do what you yourself suggest in #2, and see how your performance changes. I don't know a whole lot about OpenGL, but I suspect that may be your issue.

On #3, there must be a way to load textures via OGL without SDL, Unfortunately I'm not proficient enough to know it offhand, but I suspect a pretty quick search will turn up the result. If you aren't using the SDL Surface for anything but converting to an OGL Texture, there's no real need to load it that way (save, perhaps ease).

One thing I'm led to understand about SDL is that it makes using OpenGL a bit easier than if you were trying to do it raw (especially as it relates to dealing with platform specific things like window management). There are other helper libraries out there that help to this end, GLFW and glut are ones I recall seeing pretty regularly for example, but since you already seem familiar with SDL, you may as well use it.

My Games -- My Music 

Come join us for some friendly game dev discussions over in XNA Chat!

Hi!

I took it upon myself to finally start migrating from SDL to OpenGl in my project today. So far I've managed to (in isolated code) translate an SDL_Surface to an OpenGl texture which i then use to apply on polygons.

In my previous rendering method i used a lot of clips. Meaning I've got one image with maybe 3-5 images on it which i then clip before i render to get the desired image to show. I've managed to translate this over to OpenGl as well but it takes a bit of work and i need to know the loaded image dimensions before i do.

Just to clarify how the rendering process works now, so you get a better scope of my problem:

1. Loads image to an SDL_Surface

2. Saves it to an array

3. Something fetches the image memory location from the array

4. Converts the SDL_Surface to a OpenGl texture

5. Clips the texture

6. Renders it to QUADS

7. Destroys the texture

My Convert code is basically the same as this one here: http://content.gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL

Question time:

1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.

2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png

3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?

4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

I think that's all! Thanks for your time, i really appreciate answers!

// Tallkotten

the part 7 of your code is I'm worried about. are you deleting the textures on every frame?? or before exiting the program ? if you are deleting the texture on every frame than that explains your low FPS. you should be init and generate textures once and then use the glBindTexture( ) to assign them to the quads.


1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.

Copy the image data from the SDL_Surface into an OpenGL texture once during startup, delete the surface, and reuse the texture. Only when you no longer need the texture should you delete it on the OpenGL side.
2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png

You don't even need the array. When you pass the data to OpenGL, the driver makes a copy. So any resources you have allocated to manipulate the image data on your side, be it an SDL_Surface or an array, can safely be released. The drive will manage the texture data and you can forget about it. Until you no longer need the texture, at which point you call glDeleteTextures.
3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?

OpenGL does not provide any mechanism for loading image files from disk. You can use any one of a number of libraries or implement your own loader, but OpenGL isn't going to offer any help there. The SDL_image library works just as well as any other.
4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

Every image loading library provides a means to query the width and height of an image. And if you decide one day to implement your own loader, all of the commonly used image formats store width/height data in the file header (as should you if you ever create your own image file format).

Thank you both for the answers. I suspected that was the FPS theif but like i said i am only trying out this new way atm.

I'm no expert, but surely you aren't converting those images every frame? Even if you're holding onto the surface to draw with it elsewhere, I can't imagine a scenario where converting it every frame would be good for performance.

My suggestions is to do what you yourself suggest in #2, and see how your performance changes. I don't know a whole lot about OpenGL, but I suspect that may be your issue.

On #3, there must be a way to load textures via OGL without SDL, Unfortunately I'm not proficient enough to know it offhand, but I suspect a pretty quick search will turn up the result. If you aren't using the SDL Surface for anything but converting to an OGL Texture, there's no real need to load it that way (save, perhaps ease).

One thing I'm led to understand about SDL is that it makes using OpenGL a bit easier than if you were trying to do it raw (especially as it relates to dealing with platform specific things like window management). There are other helper libraries out there that help to this end, GLFW and glut are ones I recall seeing pretty regularly for example, but since you already seem familiar with SDL, you may as well use it.

I use SDL for other stuff like keyboard detection as well, so i might stick around with it.

---

Thanks very much you both! I'll try altering my code a bit when i get home and see if i see any performance boost

4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

Every image loading library provides a means to query the width and height of an image. And if you decide one day to implement your own loader, all of the commonly used image formats store width/height data in the file header (as should you if you ever create your own image file format).

I'm thinking of saving the image size from the SDL_Surface when i load an image. Then i link the size to a texture or something,

Thanks for the tip, i'll have to look into that when i get home!

You can follow this site for SDL tutorials http://www.lazyfoo.net/SDL_tutorials/http://www.lazyfoo.net/SDL_tutorials/
And tutorial number 36 is what you need http://www.lazyfoo.net/SDL_tutorials/lesson36/index.php
But in this tutorial no opengl texturing is done, so I have added texture mapping component from your gpwiki link, so that you that you have a basic idea of how to do. I am showing you where to change what,

1. Declare a global textuer object like this


GLuint texture ; // Texture object handle

2. Add the following code in initGL()


    SDL_Surface *surface; // Gives us the information to make the texture
    
    if ( (surface = SDL_LoadBMP("image.bmp")) ) {
    
        // Check that the image's width is a power of 2
        if ( (surface->w & (surface->w - 1)) != 0 ) {
            printf("warning: image.bmp's width is not a power of 2\n");
        }
    
        // Also check if the height is a power of 2
        if ( (surface->h & (surface->h - 1)) != 0 ) {
            printf("warning: image.bmp's height is not a power of 2\n");
        }
    
        // Have OpenGL generate a texture object handle for us
        glGenTextures( 1, &texture );
    
        // Bind the texture object
        glBindTexture( GL_TEXTURE_2D, texture );
        
        // Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        
        // Edit the texture object's image data using the information SDL_Surface gives us
        glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,
                      GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );
    }
    else {
        printf("SDL could not load image.bmp: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }    
    
    // Free the SDL_Surface only if it was successfully created
    if ( surface ) {
        SDL_FreeSurface( surface );
    }

    glViewport( 0, 0, 640, 480 );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    glOrtho( 0, 640, 480, 0, -1, 1 );
    
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glEnable( GL_TEXTURE_2D );
    //Initialize clear color
    glClearColor( 0.f, 0.f, 0.f, 1.f );

3. Draw the quad in render function


    // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, texture );

    glBegin( GL_QUADS );
        // Top-left vertex (corner)
        glTexCoord2i( 0, 0 );
        glVertex3f( 100, 100, 0 );
    
        // Bottom-left vertex (corner)
        glTexCoord2i( 1, 0 );
        glVertex3f( 228, 100, 0 );
    
        // Bottom-right vertex (corner)
        glTexCoord2i( 1, 1 );
        glVertex3f( 228, 228, 0 );
    
        // Top-right vertex (corner)
        glTexCoord2i( 0, 1 );
        glVertex3f( 100, 228, 0 );
    glEnd();

4. Delete your textures in clean_up()


glDeleteTextures( 1, &texture );

Sorry I totally missed your questions tongue.png

1. I've noticed a sudden drop of FPS since the tryout change (only the tiles are being rendered at the

moment since haven't made it global yet). The drop if from 400+ FPS to around 60 FPS after i use glDeleteTextures(); on the texture I've just drawn. Why is this? I suspect it's the conversion that drains the fps.

It's because creating and deleting texture is expensive operation, so you should only create a texture once and delete once you about to close your program or dont want to use that texture again.

2. If it is the conversion that drains the FPS, then it is smarter to convert the image once just as it loads and save the texture in the array instead of the surface? Sounds like it... tongue.png

Yes, there is, the global you declared is the identifier for that image, its GLuint, so to create more textuer you need an array , like


Gluint texture [5] // this will hold 5 texture for you 

Changes in InitGL()


// then you have to change to following where i = 0, 1 , 2 , 3 , 4
glGenTextures( 1, &texture[i] );
glBindTexture( GL_TEXTURE_2D, texture[i] );

3. Is there any reason for me to save the image as a SDL_Surface instead of loading it from OpenGL directly? Can i do that?

Yes, the above way shows you how to convert your SDL_Surface to opengl texture for later use, you need to convert each image exactly once.

4. If there is no reason at all for me to involve SDL in the rendering process then how to i keep track of the loaded image width and height? Right now i use surface->w/surface->h. Is there similar ways in OpenGl?

You have to write a bit of code, for this, you can create a class or struct that will hold the image information for you, like image width, height, image_id and other information.

I am just a learner so tried you help you, hope this helps smile.png

Quote

Hi and thanks for the help man!

I actually have a pretty sophisticated image loader that i've written myself but i only takes SDL_Surface's so i only have to make it work with the GL texture instead.

I plan to:

1.open a image in SDL

2.convert it to a texture and save the proportions

3.maybe make a class that keeps track of if the image is in use (like a smart pointer) and if it isnt dispose of it

4.release the SDL_Surface and save all of the other stuff for later use

If you dont want to use SDL_Surface then you can use any third party image library, I found SOIL very easy and simple to work with opengl, it supports wide range of Images and comes with handy functions. But always do what you are comfortable with :)

This topic is closed to new replies.

Advertisement