user6328
user6328

Reputation:

Drawing Textures in OpenGL using gluOrtho2D

I'm using the following piece of code to display a raw image file, but it displays nothing:

GLuint texture;
GLuint LoadTexture( const char * filename, int width, int height);
void FreeTexture(GLuint texture);

void display()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glBindTexture( GL_TEXTURE_2D, texture ); 
    glBegin (GL_QUADS); //begin drawing our quads
    glTexCoord2d(0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0); //with our vertices we have to assign a texcoord

    glTexCoord2d(1.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0); //so that our texture has some points to draw to

    glTexCoord2d(1.0, 1.0);
    glVertex3f(1.0, 1.0, 0.0);

    glTexCoord2d(0.0, 1.0);
    glVertex3f(0.0, 1.0, 0.0);
    glEnd();
    glPopMatrix();
    glFlush();
}

void init()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 640, 0, 480);
    glEnable( GL_TEXTURE_2D );
}

int main(int argc, char **argv)
{
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE);
    glutInitWindowSize (640, 480);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    init();
    texture = LoadTexture("texture.raw", 256, 256);
    glutMainLoop ();
    FreeTexture(texture);

    return 0;
}

All I get as an output is an empty screen. What could be wrong?

Upvotes: 1

Views: 1694

Answers (1)

Tim
Tim

Reputation: 35943

After your gluOrtho2d call your view will map from 0,0 to 640,480. You're drawing a quad with values from 0,0 to 1,1, which will be the size of a single pixel on the screen in the bottom left.

Either use the full screen (glVertex(640,480);) , or use gluOrtho2d to specify a 1x1 viewport (gluOrtho2d(0,1,0,1);)

Upvotes: 2

Related Questions