# Combing Batches with pre-computed vertices

**URL:** <https://discourse.libcinder.org/t/combing-batches-with-pre-computed-vertices/978>\
**Category:** Uncategorized\
**Created:** [December 3, 2017, 1:07pm UTC](https://discourse.libcinder.org/t/combing-batches-with-pre-computed-vertices/978 "2017-12-03T13:07:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugula](https://avatars.discourse-cdn.com/v4/letter/h/e480ec/32.png) [@hugula](https://discourse.libcinder.org/u/hugula)\
**Post date:** [December 3, 2017, 1:07pm UTC](https://discourse.libcinder.org/t/combing-batches-with-pre-computed-vertices/978/1 "2017-12-03T13:07:10Z")

</div>

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](https://libcinder.org/docs/guides/opengl/part3.html)) , without copying the particles location back to RAM?

---

<div class="post-metadata">

**Author:** ![seph](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@seph](https://discourse.libcinder.org/u/seph)\
**Post date:** [December 3, 2017, 7:27pm UTC](https://discourse.libcinder.org/t/combing-batches-with-pre-computed-vertices/978/2 "2017-12-03T19:27:05Z")

</div>

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
