Nibirue
Nibirue

Reputation: 411

Drawing a rectangle on press and release of mouse, opengl

I'm trying to draw a rectangle from user input that collects x1,y1 coordinates from left click, and then specifies x2,y2 from release of the left click. I save the coordinates based on this mouse action successfully (based on cout<< statements confirming the coords are saved) but nothing is displayed after the left button is released.

Here is what I've tried: (keep in mind, nothing crashes, so there is a logic error)

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glColor3f(1,1,0);
    glBegin(GL_POLYGON);
        glVertex2f(clip_start.x_coord,clip_start.y_coord);
        glVertex2f(clip_finish.x_coord,clip_start.y_coord);
        glVertex2f(clip_finish.x_coord,clip_finish.y_coord);
        glVertex2f(clip_start.x_coord,clip_finish.y_coord);
    glEnd();
    glPopMatrix();
        glutSwapBuffers();
        glutPostRedisplay();
    drawit();
}

void mouse(int button, int state, int x, int y){
    if(c_pressed){
        switch(button){
        case GLUT_LEFT_BUTTON:

            if(state==GLUT_DOWN){
                clip_start.x_coord=x; //x1
                clip_start.y_coord=y; //y1          
                cout<<"Initial pos set"<<endl;                  
            }
            if(state==GLUT_UP){
                clip_finish.x_coord=x; //x2
                clip_finish.y_coord=y; //y2
                cout<<"Final pos set"<<endl;  

            }
            break;  
        }
        clip_ready==true;
    }
}
void keyboard(unsigned char key, int x, int y){
    if(key == 'c'){
        c_pressed = true;
        cout<<"c pressed"<<endl;
    }
}
int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(ImageW,ImageH);
    glutInitWindowPosition(100,100);
    glutCreateWindow("");
    init(); 
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

Upvotes: 2

Views: 8613

Answers (1)

genpfault
genpfault

Reputation: 52084

You didn't set a GL_PROJECTION matrix. I'm not sure what the default is (probably the identity matrix) but it certainly isn't the Y-flipped, pixel-scale coordinate system you are assuming in display().

Try something like this:

#include <GL/glut.h>

struct Position
{
    Position() : x(0), y(0) {}
    float x, y;
};

Position start, finish;
void mouse( int button, int state, int x, int y )
{
    if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    {
        start.x = finish.x = x;
        start.y = finish.y = y;
    }
    if( button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    {
        finish.x=x;
        finish.y=y;
    }
    glutPostRedisplay();
}

void motion( int x, int y )
{
    finish.x = x;
    finish.y = y;
    glutPostRedisplay();
}

void display()
{
    glClearColor( 0, 0, 0, 1 );
    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();

    glPushMatrix();
    glColor3f(1,1,0);
    glBegin(GL_QUADS);
        glVertex2f(start.x,start.y);
        glVertex2f(finish.x,start.y);
        glVertex2f(finish.x,finish.y);
        glVertex2f(start.x,finish.y);
    glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100,100);
    glutCreateWindow("GLUT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

Upvotes: 1

Related Questions