如何使对象在Opengl

时间:2016-10-14 14:50:02

标签: c++ opengl

我的程序允许用户单击窗口上的任何位置,当按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);

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

编辑:

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

        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);

1 个答案:

答案 0 :(得分:0)

您正在迭代整个controlPoints数组,并且只有最后一次调用glUniformMatrix4fv很重要。

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