用鼠标在OpenGL GLUT中绘制多边形

6

我希望能够在使用鼠标交互的情况下,在OpenGL Glut中绘制多边形。每次单击左键时,都将创建一个顶点,并在每个顶点之间绘制一条线段。当单击右键时,多边形将关闭,并从最后一个顶点到第一个顶点绘制一条线段。我已经想出了一种方法,但似乎不起作用。

void draw_polygon(int button, int state, int x, int y) {

    bool right_pushed = 0;
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINTS);

    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        p1.x = x;
        p1.y = 480 - y;

        //if right is clicked draw a line to here
        first.x = x;
        first.y = 480 - y;
    }

    while (right_pushed == false) {

        if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
            p2.x = x;
            p2.y = 480 - y;
        }

        GLfloat dx = p2.x - p1.x;
        GLfloat dy = p2.y - p1.y;

        GLfloat x1 = p1.x;
        GLfloat y1 = p1.y;

        GLfloat step = 0;

        if (abs(dx) > abs(dy)) {
            step = abs(dx);
        }
        else {
            step = abs(dy);
        }

        GLfloat xInc = dx / step;
        GLfloat yInc = dy / step;


        for (float i = 1; i <= step; i++) {
            glVertex2i(x1, y1);
            x1 += xInc;
            y1 += yInc;
        }


        p1.x = p2.x;
        p1.y = 480 - y;

        if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {

            right_pushed = 1;
            p2.x = first.x;
            p2.y = first.y;

            dx = p2.x - p1.x;
            dy = p2.y - p1.y;

            x1 = p1.x;
            y1 = p1.y;

            step = 0;

            if (abs(dx) > abs(dy)) {
                step = abs(dx);
            }
            else {
                step = abs(dy);
            }

            xInc = dx / step;
            yInc = dy / step;


            for (float i = 1; i <= step; i++) {

                glVertex2i(x1, y1);
                x1 += xInc;
                y1 += yInc;

            }

        }
    }

    glEnd();
    glFlush();


}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(200, 200);
    glutInitWindowSize(640, 480);
    glutCreateWindow("windows");
    glutDisplayFunc(display);
    glutMouseFunc(draw_polygon);//
    init();
    glutMainLoop();
    return 0;
}

我也在尝试找出如何添加功能,使得当我从菜单中选择时,我可以从创建此多边形转到以一种方式进行编辑,即我可以选择一个顶点,将其移动,然后形状相应地改变。


你正在鼠标函数中进行绘图。不要这样做。将多边形保存在变量中(例如点列表)。然后,您的鼠标函数可以修改此列表,您的显示函数可以呈现它。类似地,可以进行多边形修改。顺便说一句,如果您刚开始学习OpenGL,请不要学习已弃用的固定功能管道,其中包括 glBegin / glEnd / glVertex ... - Nico Schertler
1个回答

6
你需要将鼠标事件和绘图功能分开。
在鼠标事件中,你只需收集输入。我建议使用一个std::vector来完成这个任务。以下函数会在按下左键时向std::vector添加一个点。如果按下右键,则表示该多边形已封闭。如果再次按下左键,则清空多边形并重新开始该过程。
#include <vector>
#include <array>

int vp_width = 640;
int vp_height = 480;

std::array<int, 2> currentPt;
std::vector<std::array<int, 2>> pts;
bool closed = false;

void draw_polygon(int button, int state, int x, int y)
{
    currentPt = std::array<int, 2>{x, vp_height-y}; 

    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        if ( closed )
            pts.clear(); // restart if last action was close
        closed = false;
        pts.push_back( currentPt );
    }
    if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
        closed = true;
}

在鼠标移动事件函数中,您可以跟踪当前鼠标位置:
void mouse_move(int x, int y)
{
    currentPt = std::array<int, 2>{x, vp_height-y};
    glutPostRedisplay();
}

在主循环中,您可以不断绘制当前点之间的连线。下面的函数会在一组点之间绘制直线。如果设置了“closed”标志,则多边形将闭合。否则,将从列表中的最后一个点到当前鼠标位置绘制一条直线。

void display(void)
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    if ( !pts.empty() )
    {
        glBegin(GL_LINE_STRIP);
        for ( auto &pt : pts )
            glVertex2f( (float)pt[0], (float)pt[1] );
        auto &endPt = closed ? pts.front() : currentPt;
        glVertex2f( (float)endPt[0], (float)endPt[1] );
        glEnd();
    }

    //glFlush();
    glutSwapBuffers();
}

int main()
{
    ..... 
    glutDisplayFunc(display);
    glutPassiveMotionFunc (mouse_move);
    glutMouseFunc(draw_polygon);

    glMatrixMode( GL_PROJECTION );
    glOrtho(0.0f, (float)vp_width, 0.0f, (float)vp_height, -1.0, 1.0);
    .....
}


预览:

预览


(注:本文无需翻译)

这样更清晰了,我找不到关于这些东西的教程和示例。在绘制多边形函数中,我试图让画布中有更多的形状,以便我可以将它们合并成一个联合体,我删除了pts.clear()函数,并递归添加绘图。这是个好主意吗? - leo_bouts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接