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: Image loaded from texture is dirty

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Location
    Italy
    Posts
    12

    Image loaded from texture is dirty

    I have a TGA file with contains this image:

    Click image for larger version. 

Name:	Screen Shot 2012-12-11 at 7.15.16 PM.jpg 
Views:	70 
Size:	13.1 KB 
ID:	917

    The texture is 256x256.
    So I'm trying to load it and map it to a cube:

    Code :
    #import <OpenGL/OpenGL.h>
    #import <GLUT/GLUT.h>
    #import <stdlib.h>
    #import <stdio.h>
    #import <assert.h>
     
     
    GLuint width=640, height=480;
    GLuint texture;
    const char* const filename= "/Users/ramy/Documents/C/OpenGL/Test/Test/texture.tga";
     
     
     
     
    void init()
    {
     
        // Initialization
     
        glEnable(GL_DEPTH_TEST);
        glViewport(-500, -500, 1000, 1000);
     
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45, width/(float)height, 1, 1000);
     
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);
     
        // Texture
     
        char bitmap[256][256][3];
        FILE* fp=fopen(filename, "r");
        assert(fp);
        assert(fread(bitmap, 3*sizeof(char), 256*256, fp) == 256*256);
        fclose(fp);
     
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmap);
     
    }
     
     
    void display()
    {
        glClearColor(0, 0, 0, 0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture);
        glColor3ub(255, 255, 255);
     
        glBegin(GL_QUADS);
        glVertex3f(0, 0, 0);
        glTexCoord2f(0.0, 0.0);
     
        glVertex3f(40, 0, 0);
        glTexCoord2f(0.0, 1.0);
     
        glVertex3f(40, 40, 0);
        glTexCoord2f(1.0, 1.0);
     
        glVertex3f(0, 40, 0);
        glTexCoord2f(1.0, 0.0);
     
        glEnd();
     
        glDisable(GL_TEXTURE_2D);
        glutSwapBuffers();
    }
     
     
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(width, height);
        glutCreateWindow(argv[0]);
        glutDisplayFunc(display);
        init();
        glutMainLoop();
        return 0;
    }

    But this is what I get when the window loads:

    Click image for larger version. 

Name:	Screen Shot 2012-12-11 at 7.16.45 PM.jpg 
Views:	72 
Size:	6.9 KB 
ID:	918

    So just half of the image is correctly displayed, and also with different colors.Then if I resize the window I get this:

    Click image for larger version. 

Name:	Screen Shot 2012-12-11 at 7.17.44 PM.jpg 
Views:	77 
Size:	6.7 KB 
ID:	919

    Magically the image seems to fix itself, even if the colors are wrong.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,097
    I am not sure what is causing it but your second image is almost certainly caused by an incorrect texture coordinate.
    You could try changing to rendering 2 triangles instead of a quad.

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Location
    Italy
    Posts
    12
    Nothing to do, I tried this:

    Code :
    glBegin(GL_TRIANGLES);
     
        glVertex2f(0, 0);
        glTexCoord2d(0, 0);
     
        glVertex2f(40, 0);
        glTexCoord2d(1, 0);
     
        glVertex2f(40, 40);
        glTexCoord2d(1, 1);
     
        glVertex2f(0, 0);
        glTexCoord2d(0, 0);
     
        glVertex2f(40, 40);
        glTexCoord2d(1, 1);
     
        glVertex2f(0, 40);
        glTexCoord2d(0, 1);
     
        glEnd();

    But I get this:

    Click image for larger version. 

Name:	Screen Shot 2012-12-12 at 1.53.47 PM.jpg 
Views:	62 
Size:	5.8 KB 
ID:	922

    I also changed the look at this way:

    Code :
    gluLookAt(0, 0, 100, 0, 0, 0, 0, 1, 0);

    But nothing to do.Even if I try not changing the gluLookAt I get a wrong image.Even with GL_QUADS.

  4. #4
    Intern Newbie
    Join Date
    Nov 2002
    Posts
    37
    First thing,I would check:
    TGA file has a header (18 bytes)
    http://paulbourke.net/dataformats/tga/
    Hope it helps.

    Claude

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,097
    ooh - I missed an error - all the values that effect a vertex must come before the glVertex3f call - that means
    Code :
     
    glTexCoord2d(0, 0);
    glVertex2f(0, 0);

Posting Permissions

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