Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

    glViewport(-500, -500, 1000, 1000);

    gluPerspective(45, width/(float)height, 1, 1000);

    gluLookAt(0, 0, -100, 0, 0, 0, 0, 1, 0);

    // Texture

    char bitmap[256][256][3];
    FILE* fp=fopen(filename, "r");
    assert(fread(bitmap, 3*sizeof(char), 256*256, fp) == 256*256);

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmap);


void display()
    glClearColor(0, 0, 0, 0);

    glBindTexture(GL_TEXTURE_2D, texture);
    glColor3ub(255, 255, 255);

    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);



int main(int argc, char** argv)
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    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?

share|improve this question
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. – Byte56 Dec 12 '12 at 19:18
Maybe theres more to the code were not seeing – GameDev-er Dec 12 '12 at 20:46
That's all the code. – Ramy Al Zuhouri Dec 12 '12 at 20:51
up vote 8 down vote accepted

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).

share|improve this answer
Great answer, it solved the problem. – Ramy Al Zuhouri Dec 13 '12 at 9:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.