PDA

View Full Version : How to load an image in Opengl?



Asvan11
12-12-2013, 01:29 PM
Iam new to opengl. I have researched online but couldn't find any simple way of loading an image. Can someone please help me.

The source code below loads the image but does not show it on the screen.


#include "stdafx.h"
#include <stdlib.h>
#include "glut.h"
#include <math.h>
#include <stdio.h>

GLuint texture; //the array for our texture

GLfloat angle = 0.0;

void line(){

glColor3f(1.0,0.0,0.0);
glLineWidth(2.5);
glBegin(GL_LINES);
glVertex2i(50,50);
glVertex2i(50,100);
glEnd();

}

GLuint LoadTexture (const char * filename, int width, int height ){

// GLuint texture;
unsigned char * data;
FILE * file;

//The following code will read in our RAW file
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );

glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );


// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
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 );

// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );

free(data);

return texture; //return whether it was successful

}

void FreeTexture( GLuint texture ){
glDeleteTextures( 1, &texture );
}

void cube () {
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture

glPushMatrix();
glRotatef( angle, 0.0f, 0.0f, 1.0f );
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
glPopMatrix();
glutSwapBuffers();
//glutSolidCube(2);
}

void display () {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
texture = LoadTexture( "C:/Users/bren/Pictures/New folder/background.jpg", 256, 256 ); //load the texture
glEnable( GL_TEXTURE_2D ); //enable 2D texturing
// glEnable(GL_TEXTURE_GEN_S); //enable texture coordinate generation
// glEnable(GL_TEXTURE_GEN_T);
cube();
FreeTexture( texture );

line();
glFlush();
//glutSwapBuffers();
//angle ++;
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
//glLoadIdentity ();
gluPerspective (50, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}

The Little Body
12-12-2013, 03:54 PM
This is relatively simple with DevIL

Take a look at https://www.opengl.org/discussion_boards/showthread.php/181714-Does-opengl-help-in-the-display-of-an-existing-image

Asvan11
12-12-2013, 05:36 PM
Thanks for the link. I downloaded most of the files, but I get errors.

fatal error C1083: Cannot open include file: 'il.h': No such file or directory. Could you tell me which files I need to download?

rakeshthp
12-12-2013, 06:29 PM
include path of il files into ur project

Asvan11
12-13-2013, 10:12 AM
include path of il files into ur project

How do I include the path? You mean adding this #include <IL/il.h> to the cpp file? I have already done that.

The Little Body
03-26-2014, 03:06 PM
Here a simple example with the use of DevIL :
(this load nearly every graphics files, thanks to DevIL, and you can dynamically resize the displayed size of the picture with it)
[this is a test, it handle too a GLUT menu using but this don't make really something, only the Quit entry is really used]



#include <IL/il.h>
#include <GL/glut.h>


#define DEFAULT_WIDTH 640
#define DEFAULT_HEIGHT 480

int width = DEFAULT_WIDTH;
int height = DEFAULT_HEIGHT;

int nFrames = 0;


static int window;
static int menu_id;
static int submenu_id;
static int value = 2;


void menuFunc0(int num)
{
value = num;

glutPostRedisplay();
}

void menuFunc1(int num)
{
if(num == 0)
{
glutDestroyWindow(window);
exit(0);
}
else
{
value = num;
}

glutPostRedisplay();
}

void createMenu(void)
{
submenu_id = glutCreateMenu(menuFunc0);
glutAddMenuEntry("submenu1", 2);
glutAddMenuEntry("submenu2", 3);
glutAddMenuEntry("submenu3", 4);
glutAddMenuEntry("submenu4", 5);

menu_id = glutCreateMenu(menuFunc1);
glutAddMenuEntry("Clear", 1);
glutAddSubMenu("Draw", submenu_id);
glutAddMenuEntry("Quit", 0);

glutAttachMenu(GLUT_RIGHT_BUTTON);
}



/* Handler for window-repaint event. Called back when the window first appears and
whenever the window needs to be re-painted. */
void displayFunc()
{

printf("Frame %d \n", ++nFrames);

// Clear color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Operate on model-view matrix
glMatrixMode(GL_MODELVIEW);

/* Draw a fullscreen mapped quad */
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(0, 0);
glTexCoord2i(0, 1); glVertex2i(0, height);
glTexCoord2i(1, 1); glVertex2i(width, height);
glTexCoord2i(1, 0); glVertex2i(width, 0);
glEnd();

glutSwapBuffers();
}

/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
void reshapeFunc(GLsizei newwidth, GLsizei newheight)
{

printf("reshape(%d, %d) \n", newwidth, newheight );

// Set the viewport to cover the new window
glViewport(0, 0, width=newwidth, height=newheight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, height, 0.0, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW);

glutPostRedisplay();
}


/* Initialize OpenGL Graphics */
void initGL(int w, int h)
{
glViewport(0, 0, w, h); // use a screen size of WIDTH x HEIGHT
glEnable(GL_TEXTURE_2D); // Enable 2D texturing

glMatrixMode(GL_PROJECTION); // Make a simple 2D projection on the entire window
glLoadIdentity();
glOrtho(0.0, w, h, 0.0, 0.0, 100.0);

glMatrixMode(GL_MODELVIEW); // Set the matrix mode to object modeling

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the window
}

/* Load an image using DevIL and return the devIL handle (-1 if failure) */
int LoadImage(char *filename)
{
ILuint image;
ILboolean success;

ilGenImages(1, &image); /* Generation of one image name */
ilBindImage(image); /* Binding of image name */


/* Loading of the image filename by DevIL */
if ( success = ilLoadImage(filename) )
{
/* Convert every colour component into unsigned byte */
/* You can replace IL_RGB with IL_RGBA if your image contains alpha channel */

success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

if (!success)
{
return -1;
}
}
else
return -1;

return image;
}

int main(int argc, char **argv)
{

GLuint texid;
int image;

if ( argc < 2)
{
printf("%s image1.[jpg,bmp,tga,...] \n", argv[0] );
return 0;
}

/* GLUT init */
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // Set the window's initial width & height

window = glutCreateWindow(argv[0]); // Create window with the name of the executable

createMenu();

glutDisplayFunc(displayFunc); // Register callback handler for window re-paint event
glutReshapeFunc(reshapeFunc); // Register callback handler for window re-size event

/* OpenGL 2D generic init */
initGL(DEFAULT_WIDTH, DEFAULT_HEIGHT);

/* Initialization of DevIL */
if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
{
printf("wrong DevIL version \n");
return -1;
}
ilInit();


/* load the file picture with DevIL */
image = LoadImage(argv[1]);
if ( image == -1 )
{
printf("Can't load picture file %s by DevIL \n", argv[1]);
return -1;
}

/* OpenGL texture binding of the image loaded by DevIL */
glGenTextures(1, &texid); /* Texture name generation */
glBindTexture(GL_TEXTURE_2D, texid); /* Binding of texture name */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear interpolation for magnification filter */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear interpolation for minifying filter */
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),
0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData()); /* Texture specification */

/* Main loop */
glutMainLoop();

/* Delete used resources and quit */
ilDeleteImages(1, &image); /* Because we have already copied image data into texture data we can release memory used by image. */
glDeleteTextures(1, &texid);

return 0;
}



And this can be compiled using gcc with this :


gcc test.c -o test -lglut -lGL -lGLU -lIL


DevIL headers are on the /usr/include/IL repertory on my Ubuntu 13.10