gpt4 book ai didi

c++ - 在 OpenGL 中用鼠标绘制多条线

转载 作者:行者123 更新时间:2023-11-30 02:35:28 26 4
gpt4 key购买 nike

我试图在我的 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();
}

最佳答案

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.

关于c++ - 在 OpenGL 中用鼠标绘制多条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33572001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com