# How does one build "gl::Batch" using "gl::vertBatch"?

**URL:** https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186
**Category:** Uncategorized
**Created:** [June 6, 2018, 12:56pm UTC](https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186 "2018-06-06T12:56:24Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Nitin\_Patel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/nitin_patel/32/415_2.png) [@Nitin\_Patel](https://discourse.libcinder.org/u/Nitin_Patel)
#### Post date: [June 6, 2018, 12:56pm UTC](https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186/1 "2018-06-06T12:56:24Z")

</div>

Hey guys,

In the reference page of “gl:vertBatch” it is recommended to construct gl::Batch using gl::vertBatch rather than using vertBatch in immediate mode . I looked in to both classes but could not find any method to achieve the same. any light on the subject will be appreciated.

thank you  
Nitin

---

<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: [June 6, 2018, 4:11pm UTC](https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186/2 "2018-06-06T16:11:03Z")

</div>

Hi,

`VertBatch` can be used to “record” a bunch of draw commands, then draw them all at once. It uses an interface similar to the old OpenGL’s immediate mode, where you’d draw lines or triangles more or less by hand. But it is potentially faster, because it will not send the commands to the GPU until you call `draw()`.

As long as you don’t call the `clear()` method, the commands remain stored, so you can draw it over and over again without paying the CPU cost of issuing the commands again. Furthermore, the commands are stored on the GPU, which makes it even more efficient.

Technically speaking, it’s not the commands that are stored, but the resulting primitives.

Here’s an example:

```auto
gl::VertBatchRef mBatch;

void MyApp::setup()
{
    mBatch = gl::VertBatch::create( GL_LINES );

    mBatch->color( 1, 0, 0 ); // red
    mBatch->vertex( 50, 50 );
    mBatch->vertex( 100, 50 );

    mBatch->color( 1, 1, 0 ); // yellow
    mBatch->vertex( 100, 50 );
    mBatch->vertex( 100, 100 );

    mBatch->color( 0, 1, 0 ); // green
    mBatch->vertex( 100, 100 );
    mBatch->vertex( 50, 100 );

    mBatch->color( 0, 1, 1 ); // cyan
    mBatch->vertex( 50, 100 );
    mBatch->vertex( 50, 50 );
}

void MyApp::draw()
{
    gl::clear();
    gl::color( 1, 1, 1 ); // white

    mBatch->draw();
}

```

-Paul

---

<div class="post-metadata">

### Author: ![Nitin\_Patel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/nitin_patel/32/415_2.png) [@Nitin\_Patel](https://discourse.libcinder.org/u/Nitin_Patel)
#### Post date: [June 6, 2018, 5:24pm UTC](https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186/3 "2018-06-06T17:24:59Z")

</div>

Hey paul!!

That is a good law level information about vertBatch which I did not know. But my question still is if there is anyway to construct “gl::Batch” using “gl::vertBatch”?!! On the reference page for “vertBatch” it is mentioned that one could construct a gl::Batch" using gl:: vertBatch". I could not find a direct way to do so. Since it’s mentioned in the reference I am wondering if there is a way to do so.

Thanks a ton for the example 😊

---

<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: [June 6, 2018, 5:32pm UTC](https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186/4 "2018-06-06T17:32:01Z")

</div>

Hi!

🙂

The [constructor](https://github.com/cinder/Cinder/blob/35e555f1c631cc58a7f2bf3ea916ddbdf74ba477/include/cinder/gl/Batch.h#L49) of `Batch` accepts a `geom::Source` and `VertBatch` happens to [be one](https://github.com/cinder/Cinder/blob/35e555f1c631cc58a7f2bf3ea916ddbdf74ba477/include/cinder/gl/Batch.h#L103). So you can do this:

```auto
VertBatch vertBatch;
// ...issue your drawing command, similar to the example above...
// e.g. vertBatch.color(1, 0, 0);

// Create a shader, or use a stock shader
gl::GlslProgRef glsl = gl::getStockShader( gl::ShaderDef().color() );

// Now, create the Batch
gl::BatchRef batch = gl::Batch::create( vertBatch, glsl );

```

-Paul

---

<div class="post-metadata">

### Author: ![Nitin\_Patel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/nitin_patel/32/415_2.png) [@Nitin\_Patel](https://discourse.libcinder.org/u/Nitin_Patel)
#### Post date: [June 6, 2018, 7:14pm UTC](https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186/5 "2018-06-06T19:14:57Z")

</div>

Hi,

it worked 😃

After looking at your code I realised I was making a simple mistake.

Instead of creating “vertBatch” I created "vertBatchRef.

so I was passing vertBatchRef(pointer) instead of passing vertBatch as an agrument.

thanks a lot for your support. hey I would love to see your work.

please do share a link to your work if you don’t mind.

gratitude 🙂

---

<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: [June 6, 2018, 7:35pm UTC](https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186/6 "2018-06-06T19:35:26Z")

</div>

Hey,

you could still use a `VertBatchRef` if you want, just dereference the pointer:

```auto
auto vertbatch = gl::VertBatch::create( ...bla... );
auto batch = gl::Batch::create( *vertbatch, glsl );

```

You can find a few Cinder samples I made [here](https://github.com/paulhoux/Cinder-Samples). My YouTube channel is [here](https://www.youtube.com/watch?v=fNa_Gh5gFWY&list=PLqA8L-BIjbrdZDXB-qVF2PBk62yjGFkk5). I won’t bore you with my portfolio, because to be honest I still need to put it online 🙂 but a very recent project was [this one](https://www.youtube.com/watch?v=8Af12Vk8WE4), of which you can see a screen capture below.

-Paul

---

<div class="post-metadata">

### Author: ![Nitin\_Patel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/nitin_patel/32/415_2.png) [@Nitin\_Patel](https://discourse.libcinder.org/u/Nitin_Patel)
#### Post date: [June 6, 2018, 9:24pm UTC](https://discourse.libcinder.org/t/how-does-one-build-gl-batch-using-gl-vertbatch/1186/7 "2018-06-06T21:24:15Z")

</div>

I did exactly that! It was easy after I realized my mistake.

I am getting closer to my first ever project in cinder.

Thanks a lot man😊
