如何在Opengl中使对象沿着用户定义的线移动

How to make an object move along a user-defined line in Opengl

本文关键字:用户 定义 移动 对象 Opengl      更新时间:2023-10-16

我的程序允许用户单击窗口上的任何位置,当按Enter键时,它沿着这些点跟踪线。另外,当按空格键时,它会在第一个点处画一个三角形,我希望它沿着整条线移动到终点,然后回到起点,然后重复。

我得到了线条跟踪和在起点上绘制三角形的工作,我甚至得到了平移运动的工作。我唯一的问题是,不管线是怎样的,它总是从右下角移动。一定是我找不到方向的问题,但我不知道哪里出了问题。

首先,我是这样画三角形的。用户定义的点定义在一个名为controlPoints的glm::vec3变量中。所以我在第一个点上创建三角形(然后它被传递到VBO,绑定到VAO,等等[triangleVertices也是一个glm::vec3变量]):

        // Lower-left vertex
        triangleVertices.push_back(glm::vec3(controlPoints[0].x - 30.0f, controlPoints[0].y + 30.0f, controlPoints[0].z));
        // Lower-right vertex
        triangleVertices.push_back(glm::vec3(controlPoints[0].x + 30.0f, controlPoints[0].y + 30.0f, controlPoints[0].z));
        // Upper-center vertex
        triangleVertices.push_back(glm::vec3(controlPoints[0].x, controlPoints[0].y, controlPoints[0].z));
        // Bind VAO for the triangle
        glBindVertexArray(VAOTriangle);
        // Bind and implement VBO for the triangle
        glBindBuffer(GL_ARRAY_BUFFER, VBOTriangle);
        glBufferData(GL_ARRAY_BUFFER, triangleVertices.size() * sizeof(glm::vec3), triangleVertices.data(), GL_STATIC_DRAW);
        // Connecting coordinates to shader
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
        glEnableVertexAttribArray(0);
        // Unbind VAO
        glBindVertexArray(0);

然后在我的游戏循环中,我是这样做转换的(模型矩阵已经被乘以到着色器中的位置,对象已经被绑定):

        glBindVertexArray(VAOTriangle);
        float inputX = 0.0;
        float inputY = 0.0;
        GLfloat deltaTime = 0.0f;
        GLfloat lastFrame = 0.0f;
        GLfloat currentFrame = glfwGetTime();
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;
        GLfloat speed = 5.0f * deltaTime;
        glm::vec2 direction;
        for (int i = 0; i < controlPoints.size(); i++)
        {
            inputX = controlPoints[i].x;
            inputY = controlPoints[i].y;
            direction.x = inputX * float((speed * deltaTime) / 1000.0f);
            direction.y = inputY * float((speed * deltaTime) / 1000.0f);
            model_matrix = glm::translate(model_matrix, glm::vec3(direction, 0.0f));    
            glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(model_matrix));    
        }
        glDrawArrays(GL_TRIANGLES, 0, triangleVertices.size());

        glBindVertexArray(0);

如果我在for循环中放入glDrawArrays,它会创建与点相同数量的三角形,这不是我想要的(只有一个三角形)。但是像这样,它看起来只取最后一点的方向,因为它在整个循环结束后才画。但它也不会这样做。就像我说的,不管行结构如何,它都在右下角(直到它从窗口消失)。

编辑:

这是我关于如何沿着线转换对象的新设置。无论我做什么,它仍然会偏离直线,向右下角移动。

        glBindVertexArray(VAOTriangle);
        // Getting time to define translation speed
        GLfloat deltaTime = 0.0f;
        GLfloat lastFrame = 0.0f;
        GLfloat currentFrame = glfwGetTime();
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;
        GLfloat speed = 3.0f * deltaTime;
        // Going through all points of the line
        translationX = controlPoints[translationIndex].x;
        translationY = controlPoints[translationIndex].y;
        if (translationIndex < controlPoints.size() - 1)
        {
            translationIndex++;
            translationIndex %= controlPoints.size();
        }
        // Update direction vector
        translationDirection.x = translationX * float((speed * deltaTime) / 1000.0f);
        translationDirection.y = translationY * float((speed * deltaTime) / 1000.0f);
        model_matrix = glm::translate(model_matrix, glm::vec3(translationDirection, 0.0f));
        glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(model_matrix));    
        glDrawArrays(GL_TRIANGLES, 0, triangleVertices.size());     
        glBindVertexArray(0);

你一次遍历整个控制点数组,只有最后一次调用glUniformMatrix4fv才重要。

你应该尝试做的是删除这个for循环并增加一些值(每帧或特定的步骤),这将取代你的"i"变量