【问题标题】:OpenGL drawing rectangle with mouse drag?OpenGL用鼠标拖动绘制矩形?
【发布时间】:2017-10-01 19:15:45
【问题描述】:

我正在尝试绘制一个形状,就像你在绘图中使用工具所做的那样,你将它拖动到哪里,当你释放鼠标时,它就会绘制形状。

目前我的程序所做的是,如果您单击它,它会弹出橡皮筋来向您显示形状的样子,但是当您松开鼠标单击时,什么都不会绘制。

相关代码如下:

// this function draws the list of objects
void drawList() {
    glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
    if (list[numObj].t==1){
        glBegin(GL_LINE_LOOP);
        glVertex2i(list[numObj].x1, list[numObj].y1);
        glVertex2i(list[numObj].x1, list[numObj].y2);
        glVertex2i(list[numObj].x2, list[numObj].y2);
        glVertex2i(list[numObj].x2, list[numObj].y1);
        glEnd();
    }
    glFlush();
}

void mouseDraw(GLint button, GLint action, GLint xMouse, GLint yMouse) {
    if (type == 1){
        if (button == GLUT_LEFT_BUTTON) {
            if (action == GLUT_DOWN && button != GLUT_RIGHT_BUTTON && numObj < MaxNumObj -1) {
                list[numObj].x1 = xMouse;
                list[numObj].y1 = yMouse;
                list[numObj].x2 = xMouse;
                list[numObj].y2 = yMouse;
                list[numObj].t = type;
                list[numObj].s = 2;
                list[numObj].r = red;
                list[numObj].g = green;
                list[numObj].b = blue;
                glutPostRedisplay();

            } else if (action == GLUT_UP && button != GLUT_RIGHT_BUTTON) {
                list[numObj].x2 = xMouse;
                list[numObj].y2 = yMouse;
                list[numObj].s = style;
                numObj++;
                glutPostRedisplay();
            }
        }
    }
}

// this function takes the mouse position while moving mouse, use this for intermediate drawing
void Motion(GLint x, GLint y) {
    // add the (x, y) coordinates of the second point to the intermediate rectangle
    list[numObj].x2 = x;
    list[numObj].y2 = y;
    // redisplay the object list after the above insertion. It will give a rubber band effect

    glutPostRedisplay ( );
}

相关主要代码:

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("SimpleDraw Example");

init();

// register call back funtions
glutDisplayFunc(drawList);
glutReshapeFunc(winReshapeFcn);
glutMouseFunc(mouseDraw);
glutMotionFunc(Motion);

//add right click menu
glutAttachMenu(GLUT_RIGHT_BUTTON);

glutMainLoop();

请忽略 .s .t 之类的东西,唯一相关的应该是 x 和 ys。

这里有一些 gif 来展示我目前的结果和期望的结果:

我目前的计划

我想让它做什么

【问题讨论】:

  • 它看起来应该可以工作......什么编译器?我敢打赌,(action == GLUT_UP &amp;&amp; button != GLUT_RIGHT_BUTTON) 没有像您认为的那样被评估,尝试((action == GLUT_UP) &amp;&amp; (button != GLUT_RIGHT_BUTTON)) 并且在另一个情况下……list[] 分配的(静态或动态)大小大于 1?您如何决定渲染已编辑的对象?要么添加一些布尔值,要么在鼠标按下时增加 numobj 并稍后使用 numobj-1 作为索引。你会循环渲染对象吗?
  • 现在可以使用了!正如 Rabbid76 建议的那样,我只需要绘制我的对象列表,所以我只是使用一个 for 循环从 0 转到 numObj 并在绘图函数的开头绘制现有对象

标签: c++ opengl graphics glut


【解决方案1】:

您必须从第一个到最后一个绘制所有存储在list 中的矩形:

void drawList() {

    glClear(GL_COLOR_BUFFER_BIT); // Clear display window.

    for( size_t i = 0; i <= numObj; ++ i ) {

        if (list[numObj].t==1){

            glBegin(GL_LINE_LOOP);
            glVertex2i(list[i].x1, list[i].y1);
            glVertex2i(list[i].x1, list[i].y2);
            glVertex2i(list[i].x2, list[i].y2);
            glVertex2i(list[i].x2, list[i].y1);
            glEnd();
        }
    }
    glFlush();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多