1
\$\begingroup\$

I have a TGA texture containing this image:

enter image description here

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

#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:

enter image description here

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

enter image description here

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

\$\endgroup\$
3
  • 3
    \$\begingroup\$ The first one looks like an error with your texture coordinates (glTexCoord2f). Though, why that would be fixed by re-sizing, I have no idea. Especially since you're doing immediate mode rendering. \$\endgroup\$
    – House
    Dec 12, 2012 at 19:18
  • \$\begingroup\$ Maybe theres more to the code were not seeing \$\endgroup\$
    – GameDev-er
    Dec 12, 2012 at 20:46
  • \$\begingroup\$ That's all the code. \$\endgroup\$ Dec 12, 2012 at 20:51

1 Answer 1

8
\$\begingroup\$

Your problem is the order in which you specify your vertices and texture coordinates:

glVertex3f(0, 0, 0);
glTexCoord2f(0.0, 0.0);

should be:

glTexCoord2f(0.0, 0.0);
glVertex3f(0, 0, 0);

OpenGL is a state machine, and in immediate mode, whenever you call glVertex3f the vertex will take on the different attributes (color, texture coordinates, etc.) currently set. The very first vertex that you specify, you have not set texture coordinates, so it will get (0,0). Then you actually call glTexCoord2f(0.0f, 0.0f), and so your second vertex also has texture coordinates of (0,0). This leads to the distortion.

Now, since you are not calling glutPostRedisplay anywhere, the display function will not be called until it actually needs to be, which happens to be when you resize the window. This time around, your first vertex will have the last texture coordinates that you sent through glTexCoord2f, namely:

glTexCoord2f(1.0, 0.0);

which you set in the previous call to update. On this frame though, each vertex gets different texture coordinates, so the texture looks normal (but it will actually be rotated).

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .