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

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

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:

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

1 Like

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 :blush:

Hi!

:slight_smile:

The constructor of Batch accepts a geom::Source and VertBatch happens to be one. So you can do this:

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

1 Like

Hi,

it worked :smiley:

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 :slight_smile:

Hey,

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

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

You can find a few Cinder samples I made here. My YouTube channel is here. I won’t bore you with my portfolio, because to be honest I still need to put it online :slight_smile: but a very recent project was this one, of which you can see a screen capture below.

-Paul

2 Likes

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😊