#include #include #include // Include OpenGL headers #include // Include OpenGL Framework headers #define ILUT_USE_OPENGL // This MUST be defined before calling the DevIL headers or we don't get OpenGL functionality #include #include #include using namespace std; GLuint texture[1]; // Create a GLuint to hold our texture ID (don't forget the first texture is texture[0]!!) ILuint imageID[1]; // Create a ILuint to hold our image ID const char* imageName = "SpringLandscape.jpg"; // Specify the filename of the image to load GLfloat frameCount = 0; // 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 a image ID as a ULuint GLuint textureID; // 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) { // If the image is flipped (i.e. upside-down and mirrored, flip it the right way up!) ILinfo ImageInfo; iluGetImageInfo(&ImageInfo); if (ImageInfo.Origin == IL_ORIGIN_UPPER_LEFT) { iluFlipImage(); } // ... then attempt to conver it. // 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(); cout << "Image conversion failed - IL reports error: " << error << 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(); cout << "Image load failed - IL reports error: " << error << endl; exit(-1); } ilDeleteImages(1, &imageID); // Because we have already copied image data into texture data we can release memory used by image. cout << "Texture creation successful." << endl; return textureID; // Return the GLuint to the texture so you can use it! } void initGL(int width, int height) { // ----- Initialise GLEW ----- GLenum err = glewInit(); if (GLEW_OK != err) { cout << "GLEW initialisation error: " << glewGetErrorString(err) << endl; exit(-1); } cout << "GLEW intialised successfully. Using GLEW version: " << glewGetString(GLEW_VERSION) << endl; // ----- Window and Projection Settings ----- // Set the window title glfwSetWindowTitle("OpenGL DevIL Image Loader | r3dux.org"); // Setup our viewport to be the entire size of the window glViewport(0, 0, (GLsizei)width, (GLsizei)height); GLfloat ratio = (float)width / (float)height; // Calculate the window ratio // Change to the projection matrix, reset the matrix and set up our projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, ratio, 0.1f, 1000.0f); // Params: Field-of-Vision, window-ration, near-clip-plane, far-clip-plane // ----- OpenGL settings ----- glfwSwapInterval(1); // Lock to vertical sync of monitor (normally 60Hz, so 60fps) glEnable(GL_SMOOTH); // Enable (gouraud) shading glEnable(GL_DEPTH_TEST); // Enable the depth testing glDepthFunc(GL_LEQUAL); // Set our depth function to overwrite if new value less than or equal to current value glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Ask for nicest perspective correction glEnable(GL_TEXTURE_2D); // Enable 2D textures // ----- Initialize DevIL libraries ----- // DevIL sanity check if ( (iluGetInteger(IL_VERSION_NUM) < IL_VERSION) || (iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION) || (ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION) ) { cout << "DevIL versions are different... Exiting." << endl; exit(-1); } // Initialise all DevIL functionality ilutRenderer(ILUT_OPENGL); ilInit(); iluInit(); ilutInit(); ilutRenderer(ILUT_OPENGL); // Tell DevIL that we're using OpenGL for our rendering } void drawScene() { // Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Reset the matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Move things back into the screen glTranslatef(0.0f, 0.0f, -40.0f); // Rotate around the y-axis glRotatef(frameCount, 0.0f, 1.0f, 0.0f); // Select the texture to use glBindTexture(GL_TEXTURE_2D, texture[0]); float hsize = 15.0f; // Vertical size of the quad float vsize = 10.0f; // Vertical size of the quad // Draw our texture glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); // Top left glTexCoord2f(0.0, 0.0); glVertex3f(-hsize, -vsize, 0.0f); // Bottom left glTexCoord2f(0.0, 1.0); glVertex3f(-hsize, vsize, 0.0f); // Bottom right glTexCoord2f(1.0, 1.0); glVertex3f(hsize, vsize, 0.0f); // Top right glTexCoord2f(1.0, 0.0); glVertex3f(hsize, -vsize, 0.0f); glEnd(); glDisable(GL_TEXTURE_2D); // ----- Stop Drawing Stuff! ------ glfwSwapBuffers(); // Swap the buffers to display the scene (so we don't have to watch it being drawn!) frameCount++; } int main() { // Frame counter and window settings variables int frame = 0, width = 800, height = 600; int redBits = 8, greenBits = 8, blueBits = 8; int alphaBits = 8, depthBits = 0, stencilBits = 0; // Flag to keep our main loop running bool running = true; // Initialise glfw glfwInit(); // Create a window if(!glfwOpenWindow(width, height, redBits, greenBits, blueBits, alphaBits, depthBits, stencilBits, GLFW_WINDOW)) { cout << "Failed to open window!" << endl; glfwTerminate(); return 0; } // Call our initGL function to set up our OpenGL options initGL(width, height); // Load an image and bind it to a texture texture[0] = loadImage(imageName); while (running == true) { // Draw our scene drawScene(); // exit if ESC was pressed or window was closed running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED); } glfwTerminate(); return 0; }