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
|
Texture::Texture(char* location)
{
// We use SDL_image's function to load the image
SDL_Surface* img = IMG_Load(location);
if (img == NULL) { std::cout << "Failed to load " << location << " - " << IMG_GetError() << "\n"; std::cin.get(); exit(4); }
w = img->w; // We take the width and height from the SDL_Surface*
h = img->h;
typedef uint8_t byte_type;
const unsigned bytesPerRow = img->pitch;
const byte_type* originalRaw = static_cast<byte_type*>(img->pixels);
byte_type* flippedRaw = new byte_type[bytesPerRow * img->h];
for (unsigned i = 0; i < img->h; ++i)
{
const byte_type* srcBeg = originalRaw + (bytesPerRow *(img->h - i - 1));
const byte_type* srcEnd = srcBeg + bytesPerRow;
std::copy(srcBeg, srcEnd, flippedRaw + bytesPerRow * i);
}
glGenTextures(1, &texture); // Generate a texture
glBindTexture(GL_TEXTURE_2D, texture); // Bind that texture temporarily
int Mode = GL_RGB; // Set the mode
if (img->format->BytesPerPixel == 4) // If the surface contains an alpha value, tell openGL
{
Mode = GL_RGBA;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Set how the texture looks
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // (Both should be linear)
// Create the texture. We get the offsets from teh image, then we use it with the image's
// pixel data to create it.
glTexImage2D(GL_TEXTURE_2D, 0, Mode, img->w, img->h, 0, Mode, GL_UNSIGNED_BYTE, flippedRaw); // *
// Unbind the texture
glBindTexture(GL_TEXTURE_2D, NULL);
delete [] flippedRaw; // *
SDL_FreeSurface(img);
}
|