【问题标题】:Drawing Multiple Lines with Mouse in OpenGL在 OpenGL 中用鼠标绘制多条线
【发布时间】:2015-11-06 17:08:27
【问题描述】:

我试图在我的 OpenGL 和 C++ 程序中用鼠标绘制多个线段。现在我可以画一个,一旦我开始画另一个,前一个就消失了。

以下是我与鼠标绘图相关的代码。关于如何绘制多条线的任何建议?

LineSegment seg;

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

    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {                 // if left button is clicked, that is the start point
        seg.x1 = seg.x2 = x;
        seg.y1 = seg.y2 = y;
    }

    if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {                   // once they lift the mouse, thats the finish point
        seg.x2 = x;
        seg.y2 = y;
    }

}

void motion(int x, int y) {
    seg.x2 = x;
    seg.y2 = y;

    glutPostRedisplay();                                                    // refresh the screen showing the motion of the line
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);                                           // clear the screen

    glBegin(GL_LINES);                                                          // draw lines
    glVertex2f(seg.x1, seg.y1);
    glVertex2f(seg.x2, seg.y2);

    glEnd();

    glutSwapBuffers();
}

【问题讨论】:

    标签: c++ opengl


    【解决方案1】:
    void display(void) {
        glClear(GL_COLOR_BUFFER_BIT);                                           // clear the screen
    
        glBegin(GL_LINES);                                                          // draw lines
        glVertex2f(seg.x1, seg.y1);
        glVertex2f(seg.x2, seg.y2);
    
        glEnd();
    
        glutSwapBuffers();
    }
    

    您需要将先前的线段保存在数据结构中,并在您单击鼠标时将其添加到其中。然后drawloop需要遍历该数据结构并绘制每个保存的线段。

    std::vector<LineSegment> segmentvector;
    //Each time you release the mouse button, add the current line segment to this vector
    /*...*/
    
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_LINES);
    for(const LineSegment & seg : segmentvector) {
        glVertex2f(seg.x1, seg.y1);
        glVertex2f(seg.x2, seg.y2);
    }
    
    glVertex2f(currseg.x1, currseg.y1);
    glVertex2f(currseg.x2, currseg.y2);
    glEnd();
    

    我还强烈建议在学习 OpenGL 时不要使用 Fixed-Function-Pipeline 函数。 There are lots of tutorials online for learning modern OpenGL.

    【讨论】:

    • 我同意强烈建议; OpenGL 石器时代的大量 tuts 显然已被弃用。
    • 是的。我已经习惯了 Java 中的 for-each 循环,所以这样的怪癖对我来说还不是很明显。
    • 当您开始学习现代版本时,学习旧的 openGL 会派上用场。
    • @Xirema 我已经合并了向量,这是有道理的。现在问题是我的运动功能。当我绘制它们时,这些线条不再显示。我需要为此创建一个单独的向量吗?我试过这样做,但它似乎不起作用。
    【解决方案2】:

    你需要设置一些逻辑来保存你已经绘制的线条的状态。目前,您永远不会开始绘制 另一条 线,您只需重置 当前 线的开始位置。

    这是您正在寻找的可能的解决方案:

    std::vector<LineSegment> segs;
    LineSegment currentSeg;
    
    void mouse(int button, int state, int x, int y) {
    
        if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {                 
            LineSegment newSeg; // Make a new segment
            newSeg.x1 = newSeg.x2 = x;
            newSeg.y1 = newSeg.y2 = y;
            segs.insert(newSeg); // Insert segment into segment vector
            currentSeg = newSeg;
        }
    
        if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {  
            currentSeg.x2 = x;
            currentSeg.y2 = y;
        }
    }
    
    void motion(int x, int y) {
        currentSeg.x2 = x;
        currentSeg.y2 = y;
    
        glutPostRedisplay();                          
    }
    
    void display(void) {
        glClear(GL_COLOR_BUFFER_BIT);  
    
        glBegin(GL_LINES);                                                          
    
        // Iterate through segments in your vector and draw each
        for(const auto& seg : segs) {
          glVertex2f(seg.x1, seg.y1);
          glVertex2f(seg.x2, seg.y2);
        }
    
        glEnd();
    
        glutSwapBuffers();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多