GLuint texture;
SDL_Surface* surface;
GLenum texture_format;
GLuint nOfColors;
if (surface = SDL_LoadBMP("C:/dwighttest32.bmp"))
{
if ((surface->w & (surface->w-1)) != 0)
MessageBox(NULL, _T("Width not a power of 2!"), _T("Error"), NULL);
if ((surface->h & (surface->h-1)) != 0)
MessageBox(NULL, _T("Height not a power of 2!"), _T("Error"), NULL);
nOfColors = surface->format->BitsPerPixel;
if (nOfColors == 32)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
else if (nOfColors == 24)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
else
{
MessageBox(NULL, _T("This is not a truecolor texture!"), _T("Error"), NULL);
SDL_Quit();
return 1;
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
}
else
{
MessageBox(NULL, _T("Image could not be loaded!"), _T("Error"), NULL);
SDL_Quit();
return 1;
}
if (surface)
SDL_FreeSurface(surface);