[color="#555555"][font="arial, helvetica, clean, sans-serif"] [/font]
[color="#555555"][font="arial, helvetica, clean, sans-serif"] [/font]
[font="arial, helvetica, clean, sans-serif"][color="#555555"]
Ovgl::Texture* Ovgl::MediaLibrary::ImportTexture( const std::string& file )
{
// Create new texture
Ovgl::Texture* texture = new Ovgl::Texture;
// Set the texture's media library handle to this media library
texture->MLibrary = this;
// Image format
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
// Check the file signature and deduce its format
fif = FreeImage_GetFileType( file.c_str(), 0 );
// If still unknown, try to guess the file format from the file extension
if( fif == FIF_UNKNOWN )
{
fif = FreeImage_GetFIFFromFilename( file.c_str() );
}
// If still unkown, return NULL
if( fif == FIF_UNKNOWN )
{
return NULL;
}
// Load texture
FIBITMAP* dib = FreeImage_Load( fif, file.c_str() );
// Convert to RGB format
dib = FreeImage_ConvertTo32Bits( dib );
// Get raw data and dimensions
BYTE* data = FreeImage_GetBits( dib );
DWORD width = FreeImage_GetWidth( dib );
DWORD height = FreeImage_GetHeight( dib );
// Create OpenGL texture
if() // if 2d texture
{
glGenTextures( 1, &texture->Image );
glBindTexture( GL_TEXTURE_2D, texture->Image );
glTexImage2D( GL_TEXTURE_2D, 1, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
}
else if() // if 3d texture
{
glGenTextures( 1, &texture->Image );
glBindTexture( GL_TEXTURE_3D, texture->Image );
glTexImage2D( GL_TEXTURE_3D, 1, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
}
else if() // if cubemap
{
glBindTexture(GL_TEXTURE_CUBE_MAP, texture->Image);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
}
// Release FreeImage's copy of the image
FreeImage_Unload( dib );
// Add texture to media library
Textures.push_back( texture );
// Return texture pointer
return texture;
}
[/font]