我已经设置了一个鼠标函数,在我的main()中调用,如下所示:
struct point
{
int x;
int y;
};
std::vector <point> points;
point OnePoint;
void processMouse(int button, int state, int x, int y)
{
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
int yy;
yy = glutGet(GLUT_WINDOW_HEIGHT);
y = yy - y; /* In Glut, Y coordinate increases from top to bottom */
OnePoint.x = x;
OnePoint.y = y;
points.push_back(OnePoint);
}
glutPostRedisplay();
}
并在顶点打印一条线我在显示功能中写了一些代码,这样我就可以做到:
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < points.size();i++)
{
glVertex2i(points[i].x, points[i].y);
}
glEnd();
但是现在我想要做的是当我点击正确的方向键时向左或向右移动所有行,但我无法弄清楚如何。
我知道它可能是这样的:
glVertex2i(points[i].x + 10, points[i].y);
//沿x轴移动点10
但是,由于i
超出for loop
,我收到错误消息
答案 0 :(得分:0)
您应该引入一个新变量:
std::vector <point> points;
point offset; // <- how much to offset points
确保在初始化期间将其设置为零。
然后在绘图代码中将偏移量添加到每个点:
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < points.size();i++)
glVertex2i(points[i].x + offset.x, points[i].y + offset.y);
glEnd();
或者使用翻译矩阵自动执行此操作:
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(offset.x, offset.y, 0);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i < points.size();i++)
glVertex2i(points[i].x, points[i].y);
glEnd();
glPopMatrix();
在按键处理程序上,您只需更改offset
:
// left-arrow:
offset.x -= 1;
// right-arrow:
offset.x += 1;
...