在OpenGL中绘制对象的轨迹

时间:2017-04-02 04:08:53

标签: c++ c opengl

当我点击屏幕上的任何位置时,我想制作绘制轨迹的代码作为对象的移动。

我将初始中心点设置为起点。我想点击鼠标按钮(目标点)作为变量设置另一个点,但我无法弄清楚如何做到这一点。

float v1[3] = { -35.0f,  22.5f, 0.0f };
float v2[3] = { -35.0f, -22.5f, 0.0f };
float v3[3] = { 0.0f,  42.5f, 0.0f };
float v4[3] = { 0.0f, -42.5f, 0.0f };
float v5[3] = { 35.0f,  22.5f, 0.0f };
float v6[3] = { 35.0f, -22.5f, 0.0f };

这是对象的初始位置。 (带有2个三角形的6点星)

float px, py;
float center_s[3] = { 0.0f, 0.0f, 0.0f };
float center_d[3] = { px, py, 0.0f };

center_s是起点,center_d是变量为pxpy的目标点。

void lines(void) {
    glColor3f(1.0, 0.0, 0.0);

    glPointSize(5);
    glBegin(GL_POINTS);
    glVertex3fv(center_s);

    glLineWidth(1);
    glBegin(GL_LINES);
    glVertex3fv(center_s);
    glVertex3fv(center_d);
}

此功能使用从center_scenter_d的红线绘制轨迹。它也绘制了一个中心点。

case GLUT_LEFT_BUTTON:
    if (state == GLUT_DOWN) {
        x = mx;
        y = glutGet(GLUT_WINDOW_HEIGHT) - my;
        px = x + 35.0;
        py = y + 42.5;
        glutIdleFunc(lines);
    }
    glutPostRedisplay();
    break;

这是问题所在。按下鼠标左键时,星星必须移动到单击的位置并计算位置的中心点并绘制线条。但是如果我运行这些代码,则不会绘制移动轨迹。

请让我知道问题所在。此外,恒星必须以恒定速度移动到点击的位置。 (不是传送)

以下是完整代码:

#include <stdlib.h>
#include <GL/glut.h>

float v1[3] = { -35.0f,  22.5f, 0.0f };
float v2[3] = { -35.0f, -22.5f, 0.0f };
float v3[3] = { 0.0f,  42.5f, 0.0f };
float v4[3] = { 0.0f, -42.5f, 0.0f };
float v5[3] = { 35.0f,  22.5f, 0.0f };
float v6[3] = { 35.0f, -22.5f, 0.0f };

float px, py;
float center_s[3] = { 0.0f, 0.0f, 0.0f };
float center_d[3] = { px, py, 0.0f };

static GLfloat spin = 0.0;

float x = 400.0f, y = 442.5f;

float color1[3] = { 1.0f, 1.0f, 1.0f };
float color2[3] = { 1.0f, 1.0f, 1.0f };

int mode = 1;
int rotate = 1;

void init(void);
void triangle_1(void);
void triangle_2(void);
void lines(void);
void display(void);
void spinDisplay_1(void);
void spinDisplay_2(void);
void reshape(int, int);
void changeColor(int);
void mouse(int, int, int, int);

////////////////////////////////////////////////////////////////////

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(300, 300);
    glutCreateWindow("6-Point Star");

    init();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMainLoop();

    return 0;
}

////////////////////////////////////////////////////////////////////

void init(void) {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
}

void triangle_1(void) {
    glColor3fv(color1);

    glBegin(GL_TRIANGLE_FAN);
    glVertex3fv(v1);
    glVertex3fv(v4);
    glVertex3fv(v5);

    glEnd();
}

void triangle_2(void) {
    glColor3fv(color2);

    glBegin(GL_TRIANGLE_FAN);
    glVertex3fv(v2);
    glVertex3fv(v3);
    glVertex3fv(v6);

    glEnd();
}

void lines(void) {
    glColor3f(1.0, 0.0, 0.0);

    glPointSize(5);
    glBegin(GL_POINTS);
    glVertex3fv(center_s);

    glLineWidth(1);
    glBegin(GL_LINES);
    glVertex3fv(center_s);
    glVertex3fv(center_d);
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();

    glTranslatef(x, y, 0.0f);
    glRotatef(spin, 0.0, 0.0, 1.0);

    triangle_1();
    triangle_2();

    glPopMatrix();

    glutSwapBuffers();
}

void spinDisplay_1(void) {
    spin = spin + 2.0;

    if (spin > 360.0) {
        spin = spin - 360.0;
    }

    glutPostRedisplay();
}

void spinDisplay_2(void) {
    spin = spin - 2.0;

    if (spin < 360.0) {
        spin = spin + 360.0;
    }

    glutPostRedisplay();
}

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 500.0, 0.0, 500.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void changeColor(int n) {
    if (n == 1) {
        color1[0] = 0.0f, color1[1] = 0.0f, color1[2] = 1.0f;
        color2[0] = 0.0f, color2[1] = 1.0f, color2[2] = 0.0f;
    }
    else if (n == 2) {
        color1[0] = 1.0f, color1[1] = 1.0f, color1[2] = 1.0f;
        color2[0] = 1.0f, color2[1] = 1.0f, color2[2] = 1.0f;
    }
}

void mouse(int button, int state, int mx, int my) {
    switch (button) {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN) {
            x = mx;
            y = glutGet(GLUT_WINDOW_HEIGHT) - my;
            px = x + 35.0;
            py = y + 42.5;
            glutIdleFunc(lines);
        }
        glutPostRedisplay();
        break;
    case GLUT_MIDDLE_BUTTON:
        if (state == GLUT_DOWN) {
            if (mode == 1) {
                changeColor(mode);
                mode = 2;
            }
            else if (mode == 2) {
                changeColor(mode);
                mode = 1;
            }
        }
        glutPostRedisplay();
        break;
    case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
            if (rotate == 1) {
                glutIdleFunc(spinDisplay_1);
                rotate = 2;
            }
            else if (rotate == 2) {
                glutIdleFunc(spinDisplay_2);
                rotate = 1;
            }
        break;
    default:
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

您错过了一些glEnd(),除非您有center_scenter_d的其他计划,否则您不需要它们。由于xycenter_s,而pxpycenter_d

首先,mouse()只需将鼠标位置指定为pxpy,而不是xy

case GLUT_LEFT_BUTTON:
    if (state == GLUT_DOWN) {
        px = mx;
        py = glutGet(GLUT_WINDOW_HEIGHT) - my;
    }

现在接下来你需要一种方法来获得增量时间。增量时间是自最后一帧以来经过的时间。 为方便起见,我将以下代码添加到display()的顶部。

int timeNow = glutGet(GLUT_ELAPSED_TIME);
float delta = (float)(timeNow - timeLastFrame) / 1000.0f;
timeLastFrame = timeNow;

请记住在全局变量中声明int timeLastFrame = 0;

现在(仍然在display())我们可以计算出行进路径的方向。我们通过计算两点之间的差异来做到这一点。然后计算长度并将差异归一化。

float dx = px - x;
float dy = py - y;

float length = sqrt(dx * dx + dy * dy);

dx /= length;
dy /= length;

现在你把它们放在一起。

if (length > 1.0f) {
    x += dx * speed * delta;
    y += dy * speed * delta;
}

因此,在length > 1.0时,我们以px的速度向pyfloat speed = 100.0f;移动(在您的全局变量中也声明这一点)。

重新强调一下。是的,请从glutIdleFunc(lines)删除mouse()。然后我们将其添加到display()中。 display()的完整范围现在应该如下所示:

void display(void) {
    int timeNow = glutGet(GLUT_ELAPSED_TIME);
    float delta = (float)(timeNow - timeLastFrame) / 1000.0f;
    timeLastFrame = timeNow;

    float dx = px - x;
    float dy = py - y;

    float length = sqrt(dx * dx + dy * dy);

    dx /= length;
    dy /= length;

    if (length > 1.0f) {
        x += dx * speed * delta;
        y += dy * speed * delta;
    }

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
    glTranslatef(x, y, 0.0f);
    glRotatef(spin, 0.0, 0.0, 1.0);
    triangle_1();
    triangle_2();
    glPopMatrix();

    lines();

    glutSwapBuffers();

    glutPostRedisplay();
}

记住glutPostRedisplay()。如果没有,那么它不会重绘,因此没有动画。另请记住#include <math.h>的{​​{1}}。

sqrt()之前的所有内容都可以移至glClear()回调。

由于不需要glutIdleFunc()center_s,因此center_d可归结为:

lines()

结果应如下所示:

供将来参考。如果你不想手工做线性代数。然后你可以使用像GLM这样的东西。

相关问题