# Beginner question: managing multiple objects

**URL:** <https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416>\
**Category:** Uncategorized\
**Created:** [November 27, 2016, 3:42pm UTC](https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416 "2016-11-27T15:42:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stdout](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@stdout](https://discourse.libcinder.org/u/stdout)\
**Post date:** [November 27, 2016, 3:42pm UTC](https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416/1 "2016-11-27T15:42:59Z")

</div>

Hello,

I want to draw several circles “along a circle”  
(like the example [https://www.libcinder.org/docs/guides/opengl/part1.html](https://www.libcinder.org/docs/guides/opengl/part1.html) (chapter Transformations)) and several lines between them. It is supposed to be a very simple graph layout algorithm.

in the example they generate a fixed amount of circles and Batch-\>draw() them.  
I can not really do that since circles will be added, removed and so on.  
This is what i tried to do (I guess i am used to general c++ logic):

- Ideally i would want a vector of my nodes.
- generate a vector of circle objects (+ properties) and store those in a vector.
- generate a vectorgl::BatchRef Batch of references to the circle objects in the vector.
- just call draw(),update() whatever and iteratively call Batch[i]-\>draw() over the vector.

what works is directly storing refs to circles in a vector.  
vectorgl::BatchRef Batch;  
Batch.push\_back(gl::Batch::create( circle, shader ));

what i can not get to work is referencing the circles in the circle-vector (last line):  
vectorgeom::Circle() Nodes;  
vectorgl::BatchRef Batch;  
Nodes.push\_back(circle);  
Batch.push\_back(gl::Batch::create( Nodes[i], shader ));

The approach is flawed but how do i do this elegantly?  
I guess there is a propper way to solve such a situation.

Any help appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [November 27, 2016, 4:27pm UTC](https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416/2 "2016-11-27T16:27:03Z")

</div>

Hi,

you could use instanced rendering, which is a relatively advanced topic. You can find a similar sample [here](https://github.com/paulhoux/Cinder-Samples/blob/0994f84ed4ad088eb3619a0b6af7eba12e78d846/All/common/Pistons.cpp#L128-L182). It works like this:

- Create a `Batch` from a `geom::Circle`. Define the circle with a position of (0,0) and a radius of 1.
- Create a `std::vector<ci::vec4>`. The `vec4` describes the origin (xyz) and radius (w) for each of the circles in your scene. Your `std::vector` should be large enough to hold the data for the maximum number of circles. We will call this the “instanced data”.
- Add a `Vbo` to your `VboMesh` to hold the instanced data. Assign it to the `geom::CUSTOM_0` attribute.
- On `update()`, call `mapReplace` on the instanced data buffer and write the position and radius of each active circle to it. Skip circles that are off-screen or otherwise invisible. Also count the number of active circles. Don’t forget to `unmap` the buffer when you’re done.
- On `draw()`, render all circles using a single draw call:  
`mBatch->drawInstanced( numCircles )`.
- In your vertex shader, use the position to offset the vertices of the `geom::Circle` and use the radius to scale them.

-Paul

P.S.: I’m happy to create a sample application if you need more info.

---

<div class="post-metadata">

**Author:** ![stdout](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@stdout](https://discourse.libcinder.org/u/stdout)\
**Post date:** [November 27, 2016, 4:49pm UTC](https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416/3 "2016-11-27T16:49:33Z")

</div>

Hello.

Thanks for your answer.  
That points me into the right direction.  
Since i have to pause development until next weekend,  
a sample app would be really nice.

thanks alot

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [November 27, 2016, 5:29pm UTC](https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416/4 "2016-11-27T17:29:45Z")

</div>

Ok, [here you go](https://gist.github.com/paulhoux/3150014ac1107bec17eb744e7004506d) 🙂

---

<div class="post-metadata">

**Author:** ![stdout](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@stdout](https://discourse.libcinder.org/u/stdout)\
**Post date:** [November 27, 2016, 6:06pm UTC](https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416/5 "2016-11-27T18:06:02Z")

</div>

splendid. Thanks a ton.

---

<div class="post-metadata">

**Author:** ![stdout](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@stdout](https://discourse.libcinder.org/u/stdout)\
**Post date:** [November 27, 2016, 6:20pm UTC](https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416/6 "2016-11-27T18:20:01Z")

</div>

Wow, this really helps me get going. No way i would have gotten there alone.

Thanks again

---

<div class="post-metadata">

**Author:** ![dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dave](https://discourse.libcinder.org/u/dave)\
**Post date:** [June 7, 2017, 6:53pm UTC](https://discourse.libcinder.org/t/beginner-question-managing-multiple-objects/416/7 "2017-06-07T18:53:13Z")

</div>

Hi everyone,

I have a quick question around this topic. I’ve been playing around with drawInstanced and a simple question appeared. Can I get pixel values after rendering it with shader?

Here is n example: Using round circles (bashes), the image is then drawn on these bashes using drawInstanced. Is there a clean and quick way I can get to these pixel rgbs?

Example here:

![](https://canada1.discourse-cdn.com/flex030/uploads/libcinder/original/1X/8a3b3e45f63a046b1b814970a1a95a0540e662a9.png)

Thanks!
