2

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

这是我尝试过的:(请记住,没有崩溃,所以存在逻辑错误)

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

1 回答 1

1

你没有设置GL_PROJECTION矩阵。我不确定默认值是什么(可能是单位矩阵),但它肯定不是您在display().

尝试这样的事情:

#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;
}
于 2012-10-17T02:57:45.063 回答