Drawing a rectangle on press and release of mouse, opengl

一笑奈何 提交于 2019-12-06 05:52:17

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!