PDA

View Full Version : Use glGenTextures every time I load a Texture?



shotoreaper
01-26-2013, 07:25 AM
Hi!
If I have a texture class:


Texture::Texture(){};

Texture::~Texture()
{
glDeleteTextures(1, &ID);
ExitOnGLError("ERROR: Could not destroy the textures");
};

void Texture::Initialize(void)
{
glActiveTexture(GL_TEXTURE0+n_texture);
n_texture++;
glBindTexture(GL_TEXTURE_2D, ID);
ExitOnGLError("Error: Could not bind the texture");
}

void Texture::Load(std::string filename)
{

name = filename;

FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(filename.c_str(),0);

// Si no soporta el formato, lanzamos error
if(!FreeImage_FIFSupportsReading(formato))
{
ExitOnGLError("ERROR: Formato de textura no soportado");
exit(EXIT_FAILURE);
}

FIBITMAP* imagen = FreeImage_Load(formato, filename.c_str());

imagen = FreeImage_ConvertTo32Bits(imagen);

width = FreeImage_GetWidth(imagen);
height = FreeImage_GetHeight(imagen);
pixels = (char*)FreeImage_GetBits(imagen);

std::cout<<"PIXELES"<<pixels[0 ]<<std::endl;
// Gneramos la textura para OpenGL
glGenTextures(1,&ID);
glBindTexture(GL_TEXTURE_2D, ID);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, width, height, 0, GL_BGRA,GL_UNSIGNED_BYTE,pixels );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glGenerateMipmap(GL_TEXTURE_2D);

FreeImage_Unload(imagen); // Descartamos la imagen



Initialize();
}

If every time that I load a texture, i call this method, Does it load fine the textures? or Should I load the textures at the same time?

tonyo_au
01-26-2013, 10:49 PM
This class doesn't make sense. Typcally a texture class looks after only 1 texture. You load the texture once and bind it a texture binding point anytime you wish to render with it. So "load" should not call

"Initialize" and "Initialize" should have a parameter saying what texture bind point to activate not just "+n_texture".

V-man
01-28-2013, 05:36 AM
Instead of Initialize, call it BindTexture.

Why have you hard coded values in your Load function? You should pick up that information from the file itself (Is it 24 bpp or 32 bpp, is it RGBA or BGRA?).