Results 1 to 4 of 4

Thread: Texture Mapping/Loading an image using SOIL

  1. #1
    Member Newbie
    Join Date
    Oct 2011
    Posts
    34

    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?


    Code :
    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'\n", 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;
    }



    Code :
    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(); 
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    458

    Re: Texture Mapping/Loading an image using SOIL

    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.

  3. #3
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896

    Re: Texture Mapping/Loading an image using SOIL

    Code :
    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.

    Code :
    // 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?

  4. #4
    Member Newbie
    Join Date
    Oct 2011
    Posts
    34

    Re: Texture Mapping/Loading an image using SOIL

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

Similar Threads

  1. Segmentation fault when loading texture using SOIL
    By Donnie in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-01-2015, 06:52 AM
  2. Loading embedded textures with SOIL
    By Quaver in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-18-2013, 09:41 AM
  3. SOIL texture atlas rendering still the same part of image
    By imported_butterfly in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-17-2013, 01:12 PM
  4. texture mapping, loading up an image, DevIL
    By annette in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 03-03-2012, 03:12 PM
  5. Image loading and processing (for texture mapping)?
    By daviem01 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-22-2001, 12:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean