1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
m_pImage = IMG_Load (C_STRING (fname));
if (m_pImage == NULL)
ore::Diagnostics::Warning (true)
.Append ("texture.cpp | CTexture::LoadFromFile - Unable to load file ")
.Append (fname)
.Display ();
// ... Eh
// TODO: Make format detection less "... Eh"
int imgFormat;
if (m_pImage->format->BitsPerPixel == 32)
imgFormat = GL_RGBA;
else
imgFormat = GL_RGB;
m_Width = m_pImage->w;
m_Height = m_pImage->h;
m_TexWidth = powf (2, ceilf (log2 (m_pImage->w)));
m_TexHeight = powf (2, ceilf (log2 (m_pImage->h)));
// Resize, if necessary
if (m_Width != m_TexWidth || m_Height != m_TexHeight)
{
SDL_Surface * newSurface =
SDL_CreateRGBSurface (SDL_SWSURFACE, m_TexWidth, m_TexHeight,
m_pImage->format->BitsPerPixel,
m_pImage->format->Rmask, m_pImage->format->Gmask, m_pImage->format->Bmask, m_pImage->format->Amask);
SDL_BlitSurface (m_pImage, NULL, newSurface, NULL);
SDL_FreeSurface (m_pImage);
m_pImage = newSurface;
}
glGenTextures (1, &m_Texture);
glBindTexture (GL_TEXTURE_2D, m_Texture);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// Interpolation method for scaling
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// the texture wraps over at the edges (repeat)
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// For good measure
SDL_LockSurface (m_pImage);
Uint8 * data = (Uint8 *)m_pImage->pixels;
glTexImage2D (GL_TEXTURE_2D, 0, imgFormat, m_TexWidth, m_TexHeight, 0, imgFormat, GL_UNSIGNED_BYTE, data);
SDL_UnlockSurface (m_pImage);
|