Search code examples
c++animationopenglglfwglm-math

OpenGL - animating the data of the vector as separate frames


I have the output of calculation result (basically the certain amount of cuboids in the certain rotations) stored in the std::vector Box, based on which I am creating the model matrices for OpenGl visualization:

    std::vector<glm::mat4> modelMatrices;
    for (int32_t i = 0; i < Box.number_of_cuboids(); i++)
    {
            float rx, ry, rz, teta;
            Box.cuboid(i).get_rotation(rx, ry, rz, teta, j);
            float x, y, z;
            Box.cuboid(i).position(x, y, z, j);
            glm::mat4  model = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z))
                                 * glm::rotate(glm::mat4(1.0f), teta, glm::vec3(rx, ry, rz))
                               * glm::scale(glm::mat4(1.0f), glm::vec3(
            Box.cuboid(i).width(), Box.cuboid(i).length(j), Box.cuboid.height()));
            modelMatrices.push_back(model);
        }
    }

and I can successfully visualise them like that:

 while (!glfwWindowShouldClose(window))
    {
        processInput(window, Box.size_x(), Box.size_y(), Box.size_z());
        glClearColor(0.95f, 0.95f, 0.95f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        /* shaders part <...>*/

        for (int32_t i = 0; i < modelMatrices.size(); i++)
        {
            ourShader.setMat4("model", modelMatrices[i]);
            glDrawArrays(GL_TRIANGLES, 0, 36);
        }
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

My problem is that Box is already the final output of the calculations. I would like to see the all the iterations steps, so basically what I would like to do:

function ManyCalculations (std::vector<Box>)
{
   at every iteration I save the current status Box in the vector, i.e. 
}

so basically after lets say 10000 iterations I end up with the same amount of Box elements, and now I would like to run such vector as frames/animation(video?) in my OpenGl function, and so I could see the evolving calculation of the contents.


Solution

  • To animate your objects with each frame you would need to keep updating you Vertex Buffer Objects(VBOs).

    Either you can keep adding each new box with a frame or change the Translation Matrices.

    Than set the new data in your VBO before drawing.

    If you know the maximum size of your data in that case you can build a large VBO and keep updating the data with glBufferSubData.

    glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
    glBufferData(GL_ARRAY_BUFFER, Box.number_of_cuboids() * sizeof(cuboids), &Box[0], GL_DYNAMIC_DRAW);