我目前正在创建可以使用 OpenGL 选择和拖动的框。现在我遇到的问题是,当我单击一个框时,它会将鼠标捕捉到中心并从中心点拖动框。我想让框从鼠标单击的点而不是中心拖动。
这是我移动鼠标时调用的 appMotionFunc。第 10 行和第 11 行使我的鼠标居中。不过,我不知道如何通过修改这些行来使其正确成比例。坐标 (0,0) 位于框的左上角,它向右绘制宽度,向下绘制高度。
void appMotionFunc(int x, int y) {
// Convert from Window to Scene coordinates
float mx = (float)x;
float my = (float)y;
windowToScene(mx, my);
// Your code here...
if(rects[0]->selected){
rects[0]->x = mx - rects[0]->w/2;
rects[0]->y = my + rects[0]->h/2;
}
// Again, we redraw the scene
glutPostRedisplay();
}
这就是绘制矩形的函数的外观。
void Rect::draw(){
if (selected){
glColor3f(1,1,1);
glBegin(GL_LINES);
glVertex3f(x, y, 0.1);
glVertex3f(x+w, y, 0.1);
glVertex3f(x+w, y, 0.1);
glVertex3f(x+w, y-h, 0.1);
glVertex3f(x+w, y-h, 0.1);
glVertex3f(x, y-h, 0.1);
glVertex3f(x, y-h, 0.1);
glVertex3f(x, y, 0.1);
glEnd();
glColor3f(red, green, blue);
glBegin(GL_POLYGON);
glVertex3f(x, y, 0.1);
glVertex3f(x+w, y, 0.1);
glVertex3f(x+w, y-h, 0.1);
glVertex3f(x, y-h, 0.1);
glEnd();
}
else{
glColor3f(red, green, blue);
glBegin(GL_POLYGON);
glVertex2f(x, y);
glVertex2f(x+w, y);
glVertex2f(x+w, y-h);
glVertex2f(x, y-h);
glEnd();
}
}
如果这不能提供足够完整的上下文来解决问题,我将提供程序的更多部分,但我认为这是所有相关代码。