我正在尝试使用绘画中的工具绘制一个形状,在此处拖动它,当您释放鼠标时,它将绘制形状。
目前我的程序所做的是,如果你点击它会产生橡皮筋来显示形状的样子,但是当你释放鼠标点击时什么也没有画出来。
相关代码如下:
// 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:
答案 0 :(得分:0)
您必须从第一个到最后一个绘制存储在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();
}