使用鼠标拖动的 OpenGL 绘图矩形?
OpenGL drawing rectangle with mouse drag?
我正在尝试绘制一个形状,就像您使用画图工具所做的那样,将它拖动到哪里,当您释放鼠标时,它就会绘制出该形状。
目前我的程序所做的是,如果您单击它,它会出现橡皮筋来向您展示形状的样子,但是当您释放鼠标单击时,什么也不会绘制。
相关代码如下:
// 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 来显示我当前的结果和期望的结果:
我现在的节目
我想让它做什么
您必须绘制存储在 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();
}
我正在尝试绘制一个形状,就像您使用画图工具所做的那样,将它拖动到哪里,当您释放鼠标时,它就会绘制出该形状。
目前我的程序所做的是,如果您单击它,它会出现橡皮筋来向您展示形状的样子,但是当您释放鼠标单击时,什么也不会绘制。
相关代码如下:
// 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 来显示我当前的结果和期望的结果:
我现在的节目
我想让它做什么
您必须绘制存储在 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();
}