Combing Batches with pre-computed vertices

Hi,

In the sample project ParticleSphereCS we see an example of using a compute shader for physical simulation.
The particles are later on drawn to the scene as vertices, in a way that avoids copying stuff between the GPU’s memory an the CPU’s one.

Now let’s say I’d like to draw each particle as a sphere (centered at particle’s location) instead of just a point.
Is there a way to use batches (as explained here https://libcinder.org/docs/guides/opengl/part3.html) , without copying the particles location back to RAM?

Hi,

In this case, I think the best approach is to create a batch containing your sphere model (or any other models in your mind), and then draw them in an Instancing manner. The sample you probably would look into is the InstancedTeapots one.

here is a snippet of code that might help:

//load an obj model
ObjLoader loader(loadAsset("models/sphere.obj"));
auto tri = TriMesh::create(loader);
auto mesh = gl::VboMesh::create(*tri);

//make an index vbo    
std::vector<size_t> positions(NumOfParticle);
for (size_t i = 0; i < NumOfParticle; i++)
    positions[i] = i;
mInstanceDataVbo = gl::Vbo::create(GL_ARRAY_BUFFER, positions.size() * sizeof(size_t), positions.data(), GL_STATIC_DRAW);
//create the batch
geom::BufferLayout instanceDataLayout;
instanceDataLayout.append(geom::Attrib::CUSTOM_0, geom::DataType::INTEGER, 1, 0, 0, 1 /* per instance */);
mesh->appendVbo(instanceDataLayout, mInstanceDataVbo);
mBatch = gl::Batch::create(mesh, shader, { { geom::Attrib::CUSTOM_0, "vInstanceIdx" } });

then in the vertex shader, you simply need to
texelFetch(uPosition, vInstanceIdx);
to get your particles’ positions.

cheers,
seph