Brandon
Brandon

Reputation: 23498

Drawing Square with Window Coords

I can draw text by specifying the glRasterPos2i(WindowX, WindowY); and it draws the text at the specified position within my window just fine.

For example: glPrint(150, 150, "dgdg"); Where my viewport is 0, 0, 800, 500.

However, doing the same for a square doesn't work. So I tried setting up my view again:

const int W = 800, H = 500;
    glViewport(0, 0, W, H);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, W, H, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3f(1.0f, 1.0f, 0.0f);
    glBegin(GL_QUADS);
        glVertex2f(100, 100);
        glVertex2f(150, 100);
        glVertex2f(150, 150);
        glVertex2f(100, 150);
    glEnd();

But my square never shows up :l
I disabled DepthTesting and GL_RECTANGLE_2D. Still no go. What am I doing wrong? How can I get it to draw at window coords instead of specifying 1.0f, etc?

Upvotes: 2

Views: 162

Answers (2)

Alex Brooks
Alex Brooks

Reputation: 1151

You can add glFlush() after your OpenGL code to require the drawings in the buffer to flush to the window.

For some reference to glFlush(), checkout this link.

Upvotes: 1

genpfault
genpfault

Reputation: 52083

Workin' fine here:

#include <GL/glut.h>

void display()
{
    glClear( GL_COLOR_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );
    glOrtho( 0, w, h, 0, -1, 1);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor3f(1.0f, 1.0f, 0.0f);
    glBegin(GL_QUADS);
        glVertex2f(100, 100);
        glVertex2f(150, 100);
        glVertex2f(150, 150);
        glVertex2f(100, 150);
    glEnd();

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}

Upvotes: 2

Related Questions