在按下并释放鼠标,opengl时绘制一个矩形

时间:2012-10-17 01:44:50

标签: c++ opengl mouseevent glut

我正在尝试从用户输入中绘制一个矩形,从左键单击中收集x1,y1坐标,然后从左键单击的释放中指定x2,y2。我成功地基于此鼠标操作保存坐标(基于cout<<声明确认coords被保存的语句)但在释放左按钮后没有显示任何内容。

以下是我尝试的内容:(请记住,没有任何崩溃,因此存在逻辑错误)

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;
}

1 个答案:

答案 0 :(得分:1)

您没有设置GL_PROJECTION矩阵。我不确定默认值是什么(可能是单位矩阵),但肯定不是您在display()中假设的Y翻转像素比例坐标系。

尝试这样的事情:

#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;
}