Jump to content
  • Advertisement
Sign in to follow this  
MadsGustaf

OpenGL unsure. unnecesary copy/leak when loading textures ?

This topic is 3895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using the following function to load all my textures, but tonight i got a little unsure, as this is my first project using opengl. I am returning a copy of the final texture after doing some changes to it(i am loading the images first with SDL, but those i am freeing, so i will spare you for that)
[source langg = "cpp"]
GLuint load_image( std::string filename ) 
{

//load the image with SDL and put the final image into SDL_Surface*
//optimizedImage

GLuint texture; // Texture object handle
GLenum texture_format;
GLint  nOfColors;

if ( optimizedImage ) { 
 
 
	// 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 );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
 gluBuild2DMipmaps(GL_TEXTURE_2D,nOfColors,optimizedImage->w,optimizedImage->h,texture_format,GL_UNSIGNED_BYTE,optimizedImage->pixels);
	// Edit the texture object's image data using the information SDL_Surface gives us
	
    //Return the optimized image
    } 
 
 
// Free the SDL_Surface only if it was successfully created
 if ( optimizedImage ) 
 { 
  SDL_FreeSurface( optimizedImage);
 }

 return texture;
}
..........................
 GLuint background = load_image("bg.bmp");


I was wondering if i could avoid this (unnecessary?) copying by return a pointer to the texture stored on the heap or something like that, or maybe load image another way, or is this the way i am supposed to create the opengl texture?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!