Jump to content
  • Advertisement
Sign in to follow this  
Yamulley

OpenGL SDL_image to OpenGL texture

This topic is 3775 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

Advertisement

bool SYM_TEXTURE::Load(string Filename)
{
//Prefix filename with texture path from engine config
Filename = Engine->TexturesPath + Filename;

//New SDL surface
SDL_Surface *Surface;

//Open the file
FILE *File = fopen(Filename.c_str(), "r");

//Check if file exists
if(!File)
{
Engine->Logger.AddEntry("Error: Could not load texture " + Filename);
Engine->Console.PrintLine("> Error: Could not load texture " + Filename);
return false;
}

//Close the file
fclose(File);

//Load the image
Surface = IMG_Load(Filename.c_str());

//Check if image data loaded ok
if(Surface == 0)
{
return false;
}

//Get dimentions
Width = Surface->w;
Height = Surface->h;

// Check that the image's width is a power of 2
if ( (Surface->w & (Surface->w - 1)) != 0 )
{
Engine->Logger.AddEntry("Warning: Non power-of-two texture loaded: " + Filename);
}
// Also check if the height is a power of 2
else if ( (Surface->h & (Surface->h - 1)) != 0 )
{
Engine->Logger.AddEntry("Warning: Non power-of-two texture loaded: " + Filename);
}

//Check colour format
if (Surface->format->BytesPerPixel == 4)
{
if (Surface->format->Rmask == 0x000000ff)
{
Mode = GL_RGBA;
}
else
{
Mode = GL_BGRA;
}
}
else if (Surface->format->BytesPerPixel == 3)
{
if (Surface->format->Rmask == 0x000000ff)
{
Mode = GL_RGB;
}
else
{
Mode = GL_BGR;
}
}
else
{
//Unsupported type
Engine->Logger.AddEntry("Error: Unsupported texture colour format: " + Filename);
SDL_FreeSurface(Surface);
return false;
}

//Generate texure
glGenTextures(1, &Texture);

//Bind the texture
glBindTexture(GL_TEXTURE_2D, Texture);

//Copy all pixels in SDL surface to GL texture
glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);

//Texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);

//Generate mipmaps
gluBuild2DMipmaps(GL_TEXTURE_2D, 0, Surface->w, Surface->h, Mode, GL_UNSIGNED_BYTE, Surface->pixels);

//Free SDL surface
SDL_FreeSurface(Surface);

return true;
}



Alter it to take endianness into account though.

&Texture is the GLuint.

Enjoy.

EDIT: Pasted the wrong code, fixed :P

Share this post


Link to post
Share on other sites

//Generate texure
glGenTextures(1, &Texture);

//Bind the texture
glBindTexture(GL_TEXTURE_2D, Texture);

//Copy all pixels in SDL surface to GL texture
glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);

//Texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);

//Generate mipmaps
gluBuild2DMipmaps(GL_TEXTURE_2D, 0, Surface->w, Surface->h, Mode, GL_UNSIGNED_BYTE, Surface->pixels);

//Free SDL surface
SDL_FreeSurface(Surface);

return true;



Did you notice that this code calls glTexImage2D and also gluBuild2DMipmaps?
Also, if you want mipmaps, don't use gluBuild2DMipmaps.
http://www.opengl.org/wiki/index.php/Common_Mistakes#Creating_a_Texture

Share this post


Link to post
Share on other sites
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!