Joel Niemelä
Joel Niemelä

Reputation: 308

OpenGL images not displaying properly with some window sizes

I'm new to OpenGL and am trying to display images. I've got the "Load image from file" part done and am now trying to bind it to my shape (GL_TRIANGLES). Sometimes the image get's displayed correctly, but sometimes it does not. I've realized that it depends on the window size, so if I resize the window I can get a clear image. Here are some screenshots of how the screen looks:

Working: enter image description here

Broken: enter image description here

Here's my draw loop:

void display() {
    // tick the frame counter
    frame++;
    start_time = clock();

    // update screen
    int width = glutGet(GLUT_WINDOW_WIDTH);
    int height = glutGet(GLUT_WINDOW_HEIGHT);
    update_screen(width, height);

    // clear screen
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glClear(GL_COLOR_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, png::get_textures()[1]);

    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

    glBegin(GL_TRIANGLES);
        glVertex2f(1000, 1000);
        glVertex2f(0, 1000);
        glVertex2f(0, 0);

        glVertex2f(1000, 1000);
        glVertex2f(1000, 0);
        glVertex2f(0, 0);
    glEnd();

    glDisable(GL_TEXTURE_2D);

    glPopMatrix();
    glFlush();

    glutSwapBuffers();

    elapsed_time = ((clock() - start_time) / (double(CLOCKS_PER_SEC) / 10000));

    // tick all entities
    player.tick(elapsed_time);

    glutPostRedisplay();
}

Also, if I remove glEnable(GL_TEXTURE_GEN_S) or glEnable(GL_TEXTURE_GEN_T); or both, the image gets completely blue or gray.

Edit: Oh, and I'm using PNG files, in case that maters.

Upvotes: 0

Views: 310

Answers (1)

Joel Niemelä
Joel Niemelä

Reputation: 308

Well, I feel sheepish!

I forgot to add glTexCoord2f...

hear's my new file just in case someone else forgot their brain somewhere. :)

void display() {
    // tick the frame counter
    frame++;
    start_time = clock();

    // update screen
    int width = glutGet(GLUT_WINDOW_WIDTH);
    int height = glutGet(GLUT_WINDOW_HEIGHT);
    update_screen(width, height);

    //glMatrixMode(GL_MODELVIEW);
    //glLoadIdentity();

    // clear screen
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

    glClear(GL_COLOR_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, png::get_textures()[3]);

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex2f(0, 0);
        glTexCoord2f(1, 0); glVertex2f(1000, 0);
        glTexCoord2f(1, 1); glVertex2f(1000, 1000);
        glTexCoord2f(0, 1); glVertex2f(0, 1000);
    glEnd();

    glDisable(GL_TEXTURE_2D);

    glPopMatrix();
    glFlush();

    glutSwapBuffers();

    elapsed_time = ((clock() - start_time) / (double(CLOCKS_PER_SEC) / 10000));

    // tick all entities
    player.tick(elapsed_time);

    glutPostRedisplay();
}

Upvotes: 1

Related Questions