Load PNG File with Open GL, ILUT, and DevIL Library

DevIL library

include

#define ILUT_USE_OPENGL    // This MUST be defined before calling the DevIL headers or we don't get OpenGL functionality
#include <IL/il.h>
#include <IL/ilu.h>
#include <IL/ilut.h>

GLuint MenuFruitTexture; //global variable

initiation, put it in GL inititaion

// Initialise all DevIL functionality
ilutRenderer(ILUT_OPENGL);
ilInit();
iluInit();
ilutInit();
ilutRenderer(ILUT_OPENGL);    // Tell DevIL that we're using OpenGL for our rendering

MenuFruitTexture=loadImage("yourImage.png"); //load texture at gl Initiation


function to call, return gluInt

// Function load a image, turn it into a texture, and return the texture ID as a GLuint for use
GLuint loadImage(const char* theFileName)
{
ILuint imageID;                // Create an image ID as a ULuint

GLuint textureID=0;            // Create a texture ID as a GLuint

ILboolean success;            // Create a flag to keep track of success/failure

ILenum error;                // Create a flag to keep track of the IL error state

ilGenImages(1, &imageID);         // Generate the image ID

ilBindImage(imageID);             // Bind the image

success = ilLoadImage(theFileName);     // Load the image file

// If we managed to load the image, then we can start to do things with it...
if (success)
{
std::cout << "Texture load successful." << std::endl;
// The Image is flipped (i.e. upside-down and mirrored, flip it the right way up!)
iluFlipImage();

// Convert the image into a suitable format to work with
// NOTE: If your image contains alpha channel you can replace IL_RGB with IL_RGBA
success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

// Quit out if we failed the conversion
if (!success)
{
error = ilGetError();
std::cout << "Image conversion failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
exit(-1);
}

// Generate a new texture
glGenTextures(1, &textureID);

// Bind the texture to a name
glBindTexture(GL_TEXTURE_2D, textureID);

// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

// Set texture interpolation method to use linear interpolation (no MIPMAPS)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// Specify the texture specification
glTexImage2D(GL_TEXTURE_2D,                 // Type of texture
0,                // Pyramid level (for mip-mapping) - 0 is the top level
ilGetInteger(IL_IMAGE_BPP),    // Image colour depth
ilGetInteger(IL_IMAGE_WIDTH),    // Image width
ilGetInteger(IL_IMAGE_HEIGHT),    // Image height
0,                // Border width in pixels (can either be 1 or 0)
ilGetInteger(IL_IMAGE_FORMAT),    // Image format (i.e. RGB, RGBA, BGR etc.)
GL_UNSIGNED_BYTE,        // Image data type
ilGetData());            // The actual image data itself
}
else // If we failed to open the image file in the first place...
{
error = ilGetError();
std::cout << "Image load failed - IL reports error: " << error << " - " << iluErrorString(error) << std::endl;
exit(-1);
}

ilDeleteImages(1, &imageID); // Because we have already copied image data into texture data we can release memory used by image.

std::cout << "Texture creation successful." << std::endl;

return textureID; // Return the GLuint to the texture so you can use it!
}

example how to use, at display function


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
/* create a square on the XY
note that OpenGL origin is at the lower left
but texture origin is at upper left
=> it has to be mirrored
(gasman knows why i have to mirror X as well) */
glBindTexture(GL_TEXTURE_2D,MenuFruitTexture);
glBegin(GL_QUADS);

glTexCoord2i(0, 0); glVertex2i(0,   0);
glTexCoord2i(0, 1); glVertex2i(0,   500);
glTexCoord2i(1, 1); glVertex2i(500, 500);
glTexCoord2i(1, 0); glVertex2i(500, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();

courtesy to this page

Advertisements

One thought on “Load PNG File with Open GL, ILUT, and DevIL Library

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s