Page 1 of 1

SFML Texture Loading

PostPosted: Thu Feb 16, 2012 4:33 pm
by LordSupremePerson
Documentation: http://www.sfml-dev.org/documentation/2 ... 1Image.php

Sample code pulled from http://www.cplusplus.com/forum/general/25821/
Code: Select all
void LoadTextures()
{
   sf::Image img;
   if(!img.LoadFromFile("texture.bmp"))
      throw 1;

   glGenTextures(1, textures);
   glBindTexture(GL_TEXTURE_2D, textures[0]);
   glTexImage2D(GL_TEXTURE_2D, 0, 3, img.GetWidth(), img.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, img.GetPixelsPtr());
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

}


Obviously you need to tweak that to whatever you want to use, (for example the "textures" variable isnt defined in that function), but what is above should be fine for any thinking individual to get started

Re: SFML Texture Loading

PostPosted: Wed Feb 22, 2012 5:15 am
by LordSupremePerson
This will save you kids some time, SFML automatically reformats the image to RGBA so you need:
Code: Select all
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img.GetPixelsPtr());


I also noticed my texture wasnt lining up but then I flipped it vertically and that fixed it... not sure if that was an issue with my implementation or not
Code: Select all
img.FlipVertically();

Re: SFML Texture Loading

PostPosted: Thu Feb 23, 2012 4:28 pm
by eawerbaneth
I had to flip my image too. I think it just has to do with the way SFML stores image data.