Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Textures not working

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2009
    Location
    Copenhagen, Denmark
    Posts
    14

    Textures not working

    Hi,

    As I wrote in another thread, I am porting my OpenGL application from Mac to Windows. I am experiencing the following problem:

    I have a PPM image, which I am loading into the following variable:

    Code :
    GLubyte texels[512][512][3];

    In my init function, I am setting up a texture, using automatic generation of texture coordinates:

    Code :
    // texture image
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_RGB,GL_UNSIGNED_BYTE, texels);
     
    // set drawing mode to GL_DECAL so that the textured polygons are
    //drawn using the colors from the texture map, and not polygon colors
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
     
    // enable automatic texture coords generation
    // (in two dimensions as the texture map is 2D)
    glEnable( GL_TEXTURE_GEN_S);
    glEnable( GL_TEXTURE_GEN_T);
     
    // generate texture coords automatically, s is in the x-direction...
    GLfloat x_dir[] = { 1, 0, 0 };
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_S, GL_OBJECT_PLANE, x_dir);
     
    // .. t is in the z-direction
    GLfloat z_dir[] = { 0, 0, 1 };
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGenfv(GL_T, GL_OBJECT_PLANE, z_dir);
     
    // repeat the texture in both directions
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     
    // filtering (not totally sure what this is for)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    Then, when I draw the polygons that I want to texture on, I specify

    Code :
    glEnable( GL_TEXTURE_2D);

    before drawing the polygon and

    Code :
    glDisable( GL_TEXTURE_2D);

    afterwards. On Mac this worked perfectly but now (on Windows) the textures are not used - instead, my polygons are just green. If I print out the values of the texels variable, they seem alright (they are values from 0 to 255) so I don't think it's the loading of the file that's gone wrong. What can I do?

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Textures not working

    GL_RGB is 3 bytes, and may be problematic with the 4 byte alignment which is the default with GL.

    It is easier to set it to "tightly packed" like this :
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glPixelStorei(GL_PACK_ALIGNMENT, 1);

    More details on the following page, search for "pixelstore" : http://www.opengl.org/resources/feat...es/oglpitfall/
    Almost obsolete but still useful.



    Can you describe exactly how "texels" array is build and filled in ?

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2009
    Location
    Copenhagen, Denmark
    Posts
    14

    Re: Textures not working

    Sure, first I open the PPM file (it is plain text) as an ifstream and then a use the values from the file to build the texels array. This is the for-loop that populates the array:

    Code :
    for (int i = 0; i < width; i++)
    {
      for (int j = 0; j < height; j++)
      {
        for (int c = 0; c < 3; c++)
        {
          getline(file, line);
          texels[i][j][c] = atoi(line.c_str());
        }
      }
    }

    If I print them as integers, I can see that the texels variable now holds RGB-triples in the inner array, such as: (78,192,63)

  4. #4
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Textures not working

    texels[i][j][c] <- this may be a problem, depending on alignement.

    Try this :
    for (int t = 0; t<height*width*3 ;t++) {
    getline(file, line);
    texels[t] = atoi(line.c_str());
    }

  5. #5
    Junior Member Newbie
    Join Date
    Sep 2009
    Location
    Copenhagen, Denmark
    Posts
    14

    Re: Textures not working

    What do you mean by "allignment"? If you mean the order in which the RGB-values are specified, they should be alright. However, I tried you suggestion, and it gives the same result. So now I have a single-dimensional texels array of length height x width x 3... Should I still create the texture image like this?

    Code :
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, height, width,
                 0, GL_RGB, GL_UNSIGNED_BYTE, texels);

    Anything else I can try?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •