繁体   English   中英

OpenGL-如何移动打印线?

[英]OpenGL - How can I move my printed lines?

我已经设置了一个在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 ,因此我收到错误消息

您应该引入一个新变量:

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;

...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM