0

I want to understand how to create loads of similar 2-D objects and then animate each one separately, using OpenGL.

I have a feeling that it will be done using this and glfwGetTime().

Can anyone here help point me in the right direction?

Ok, so here is what is the general thing that have tried so far:

We have this vector that handles translations created the following code, which I have modified slightly to make a shift in location based on time.

glm::vec2 translations[100];
int index = 0;
float offset = 0.1f;
float time = glfwGetTime(); // newcode
for (int y = -10; y < 10; y += 2)
{
    for (int x = -10; x < 10; x += 2)
    {

        glm::vec2 translation;
        translation.x = (float)x / 10.0f + offset + time; // new adjustment
        translation.y = (float)y / 10.0f + offset + time*time; // new adjustmet
        translations[index++] = translation;
    }
}

Later, in the render loop,

while (!glfwWindowShouldClose(window))
{

    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    shader.use();
    glBindVertexArray(quadVAO);
    glDrawArraysInstanced(GL_TRIANGLES, 0, 6, 100); // 100 triangles of 6 vertices each
    glBindVertexArray(0);

    time = glfwGetTime(); // new adjustment

    glfwSwapBuffers(window);
    glfwPollEvents();
}

is what I have tried. I suppose I am misunderstanding the way the graphics pipeline works. As I mentioned earlier, my guess is that I need to use some glm matrices to make this work as I imagined it, but am not sure ...

genpfault
  • 51,148
  • 11
  • 85
  • 139
AKRA
  • 300
  • 2
  • 12
  • 1
    I’m not exactly sure what you want to ask. What is you problem with your code. What do you expect that should happen? And what is actually happening? Which line does not work a expected? I could imagine that you think that `time = glfwGetTime(); // new adjustment` should have an effect, but why? – t.niese Aug 27 '21 at 18:43

1 Answers1

1

The general direction would be, during initialization:

  • Allocate a buffer to hold the positions of your instances (glNamedBufferStorage).
  • Set up an instanced vertex attribute for your VAO that sources the data from that buffer (glVertexArrayBindingDivisor and others).
  • Update your vertex shader to apply the position of your instance (coming from the instanced attribute) to the total transformation calculated within the shader.

Then, once per frame (or when the position changes):

  • Calculate the positions of of all your instances (the code you posted).
  • Submit those to the previously allocated buffer with glNamedBufferSubData.

So far you showed the code calculating the position. From here try to implement the rest, and ask a specific question if you have difficulties with any particular part of it.

I posted an example of using instancing with multidraw that you can use for reference. Note that in your case you don't need the multidraw, however, just the instancing part.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220