Texture Mapping/Loading an image using SOIL

Hello,

I am trying to load a .png image using SOIL onto a face I drew using geometric primitives. The code looks right but the image is not uploading. I would like the image to appear behind the face I drew. I made sure the image was in the folder containing the project file. I also made sure that the width and height of the image was the same as the one I put in the code. I can’t think of what else to do. Below are the LoadGLTexture and display functions. Notice that I call LoadGLTexture from the display function. What could be the problem?

GLint LoadGLTexture(const char *filename, int width, int height)
{
     GLuint texture;
     unsigned char *data;
     FILE *file;
 
     // open texture data
     file = fopen(filename, "r");
     if (file == NULL) return 0;
 
     // allocate buffer
     data = (unsigned char*) malloc(width * height * 4);
 
     //read texture data
     fread(data, width * height * 4, 1, file);
     fclose(file);
     

    texture = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture 
	(
		"face.png",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);
	
	
    // check for an error during the load process 
    if(texture == 0)
    {
	    //printf( "SOIL loading error: '%s'
", SOIL_last_result() );
    }

      
    glGenTextures(1, &texture); // allocate a texture name
    glBindTexture(GL_TEXTURE_2D, texture); // select our current texture
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);  // when texture area is small, bilinear filter the closest mipmap
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // when texture area is large, bilinear filter the first mipmap
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);  // texture should tile
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); // build our texture mipmaps

    free(data);  // free buffer
 
    return texture;
}

void display(void)
{
    
    
   glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(Angle, 0.0, 0.0, 0.0);

    GLuint texture = LoadGLTexture("face.png", 594, 738);
    glEnable(GL_TEXTURE_2D);
    glBindTexture( GL_TEXTURE_2D, texture );
    
    
	//Nose
   drawTriangle();

   //Face
   circle (250, 250, 400);
   
   
   //Inner Eyes
   glLoadIdentity();
   circle(200, 280, 10);
   drawCircle1(-30, 280, 10);
   
   //Eye lids
   glLoadIdentity();
   glScalef(0.6,0.2,0);
   circle(530, 2400, 60);
   circle(-150, 2150, 60);
   
   //Arc for eyebrows
   drawArc(500, 500, -70, -140, 100);
   drawArc(270, 500, -70, -140, 100);
   
   //Mouth
   drawArc(380, 250, 110, -180, 180);
  // drawArc(380, 100, -100, -180, 180);
   drawLine();
   
   glPopMatrix();
   
  glFlush(); 
}

Texture mapping is more complicated than what you think. So I advise you to have a look at a tutorial, for example this one: http://nehe.gamedev.net/tutorial/texture_mapping/12038/

It will give you good undergrounds to continue.


texture = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture 
	(
		"face.png",
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
	);
	     
    glGenTextures(1, &texture); // allocate a texture name


You ask the library to create a texture for you and it returns the texture id. The first thing (after the error check) you do with that texture id is overwriting it with one that you allocate yourself by calling glGenTextures().

Also, you should not load the texture each frame. Load it once at the beginning of your program and then just use it for rendering.


// open texture data
     file = fopen(filename, "r");
     if (file == NULL) return 0;
 
     // allocate buffer
     data = (unsigned char*) malloc(width * height * 4);
 
     //read texture data
     fread(data, width * height * 4, 1, file);
     fclose(file);

That looks unnecessary, if the library does the image loading for you, you don’t need to read the file yourself, no?

I looked at that tutorial before writing my code. I’ll take a look again. Maybe I missed something.