SFML Texture Loading

SFML Texture Loading

Postby LordSupremePerson » Thu Feb 16, 2012 4:33 pm

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
LordSupremePerson
 
Posts: 7
Joined: Thu Feb 09, 2012 4:42 pm

Re: SFML Texture Loading

Postby LordSupremePerson » Wed Feb 22, 2012 5:15 am

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();
LordSupremePerson
 
Posts: 7
Joined: Thu Feb 09, 2012 4:42 pm

Re: SFML Texture Loading

Postby eawerbaneth » Thu Feb 23, 2012 4:28 pm

I had to flip my image too. I think it just has to do with the way SFML stores image data.
eawerbaneth
 
Posts: 5
Joined: Thu Feb 09, 2012 5:38 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron