Results 1 to 3 of 3

Thread: Texture looks strange?

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    2

    Texture looks strange?

    Hi Guys,

    I have a function for loading PPM images, the image loaded into GIMP looks like this:



    But when it's running in my opengl app, it looks like this:



    What have I done wrong..?

    I got the code for loading PPM images from another web site, it looks like this:

    Code :
    int  environ_load_texture_ppm(const char *filename, GLuint *texid) {
        FILE* fp;
        int i, w, h, d;
        unsigned char* image;
        char head[70];          /* max line <= 70 in PPM (per spec). */
     
        fp = fopen(filename, "rb");
        if (!fp) {
            perror(filename);
            return -1;
        }
     
        /* grab first two chars of the file and make sure that it has the
        correct magic cookie for a raw PPM file. */
        fgets(head, 70, fp);
     
        if (strncmp(head, "P6", 2)) {
            fprintf(stderr, "%s is not a raw PPM file\n", filename);
            return -1;
        }
     
        /* grab the three elements in the header (width, height, maxval). */
        i = 0;
        while (i < 3) {
            fgets(head, 70, fp);
            if (head[0] == '#') {
                /* skip comments. */
                continue;
            }
            switch (i) {
                case 0:
                    i += sscanf(head, "%d %d %d", &amp;w, &amp;h, &amp;d);
                    break;
                case 1:
                    i += sscanf(head, "%d %d", &amp;h, &amp;d);
                    break;
                case 2:
                    i += sscanf(head, "%d", &amp;d);
                    break;
            }
        }
     
        /* grab all the image data in one fell swoop. */
        image = (unsigned char*)malloc(sizeof(unsigned char)*w*h*3);
        fread(image, sizeof(unsigned char), w*h*3, fp);
        fclose(fp);
     
        /* Bind */
        glGenTextures(1, texid);
        glBindTexture(GL_TEXTURE_2D, *texid);
     
        // select modulate to mix texture with color for shading
        glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
     
        /* Set parameters */
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
        /* Build mipmaps */
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 
                            w, h, 
                            GL_RGB, GL_UNSIGNED_BYTE, 
                            image);
        free(image);
        printf("loaded %s as a texture\n", filename);
        return 0;
    }

    Then, when I want to draw the texture to a quad surface (whilst in ortho projection), I do this:

    Code :
    glPolygonMode(GL_FRONT, GL_FILL);
        glColor3f(1, 1, 1);
        glTranslatef(width-294-20, height-64-20, 0);
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, textureBanner);
        glBegin(GL_QUADS);
        {
            glTexCoord2f(0, 1); glVertex2f(0, 0);
            glTexCoord2f(1, 1); glVertex2f(294, 0);
            glTexCoord2f(1, 0); glVertex2f(294, 64);
            glTexCoord2f(0, 0); glVertex2f(0, 64);
        }
        glEnd();
        glDisable(GL_TEXTURE_2D);
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    What's wrong with this? Many thanks!!

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Texture looks strange?

    Hi,
    Call
    Code :
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);

    before you call glTexParameteri funcs.
    Reason: Opengl's default alignment is 4 bytes. if say your image has a width of 397, then in each line three bytes will be added to make it divisible by 4. Calling the above func. tells opengl that your data is tightly packed.
    Source: Opengl super bible 5th ed. (page 182).
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    2

    Re: Texture looks strange?

    Brilliant, works a charm! Thank you very much!

Similar Threads

  1. Strange error when copying texture to fbo
    By imported_pettersson in forum OpenGL: Advanced Coding
    Replies: 7
    Last Post: 11-30-2018, 05:51 PM
  2. Really strange gl texture write bug
    By cpugh in forum OpenCL
    Replies: 1
    Last Post: 02-16-2011, 04:41 PM
  3. Lookup-Texture instead of pow causes strange gfx errors
    By namespace in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 12-13-2004, 11:17 AM
  4. strange lighting ...not in original texture
    By in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 07-02-2003, 03:53 AM
  5. strange texture size
    By JanHH in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 05-08-2003, 05:22 AM

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