How do I upload a collection of position vectors and rotation quaternions to the GPU in OpenGL?

77 Views Asked by At

First, I upload the mesh of an object to a VAO (which I know how to do) and reuse that data every frame where a new mesh hasn't been introduced.

I want to then use the Vertex Shader to parallelize rotating and translating the many vertices into their real positions, by handing the GPU a position vector and rotation quaternion for each set of data that represents its own contiguous object.

I don't know how to do that second part without making the position and rotation data a uniform upload to the Shader, necessitating a draw call for each contiguous object, which I'm guessing is really inefficient.

Is there any way to upload a series of positions and rotations and tell OpenGL to transform certain sections of the VAO by them?

I tried looking for an answer all day but the documentation is just a bit to confusing for me at the moment.

Here's what my Shader input looks like:

layout (location=0) in vec3 aPos;
layout (location=1) in vec4 aColor;

uniform mat4 uProjection;
uniform mat4 uView;

I'm thinking it would be great to have some form of 'uniform' upload that's only used by a range of vertices when being processed by the GPU, but I'm not sure.

1

There are 1 best solutions below

1
tkap1 On

It sounds like you want some data for each "object", rather than for each vertex in the object? You can do this with glVertexAttribDivisor and glDrawArraysInstanced

It could look something like:

  1. Create and bind a VAO (glGenVertexArrays, glBindVertexArray)
  2. Create and bind a VBO that contains the vertices (glGenBuffers, glBindBuffer, glVertexAttribPointer, glEnableVertexAttribArray, glBufferData)
  3. Create and bind a VBO that contains the data that you want per object/instance (same as above, plus glVertexAttribDivisor)

You should call glDrawArraysInstanced rather than glDrawArrays.