Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Fastest way to draw many (many!) primitives

This thread is locked; no one can reply to it. rss feed Print
Fastest way to draw many (many!) primitives
GololCohan
Member #17,042
February 2019

I want to draw a very large amount of triangles (up to 600000). What I did first was to:

al_init();
display = al_create_display();

and then loop through my triangles and

al_draw_triangle(); each one.

Finally I did

This was not very fast though. I read that it helps to draw to a bitmap on hold first and then draw that bitmap to the display. I tried to do this in the following fashion (sketched):

This is just as fast as the previous method though. How can I correctly buffer my triangles to reduce the amount of draws? What is the most efficient way to draw many primitives in Allegro 5?

Thanks for all answers

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

You'll get better performance from al_draw_vertex_buffer and al_draw_indexed_buffer.

Also, al_hold_bitmap_drawing does not do what you think it does. It pauses BITMAP drawing, not drawing in general. There is currently no way to batch primitive drawing, except through the API.

If you need something more advanced, there is DX and OpenGL.

GololCohan
Member #17,042
February 2019

Thanks for the suggestion.
I'm not sure how to use al_draw_vertex_buffer. It seems like I pick a vertex for each triangle and then create a small bitmap which has the triangle and a transparent background. Then I set that triangle as the texture for that vertex.
Is that how it works?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

The texture is optional. Set a coordinate and a color for every vertex and then pass it to the function. I'll give a better example in a little bit.

GololCohan
Member #17,042
February 2019

Ah yes I see now that vertex buffers are exactly what I need, I read that transferring the data to the GPU is the problem and the vertex buffer description says it solves exactly that.
I just need to figure out how exactly to use it, maybe Ill do it tomorrow or see it in your example if you post one.
Thx a lot

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Simplest way to do it is to use an array of ALLEGRO_VERTEX to initialize it with.

#SelectExpand
1srand(time(NULL)); 2 3ALLEGRO_VERTEX varray[100000*3]; 4for(int i = 0 ; i < 100000 ; ++i) { 5 char r = rand()%256; 6 char g = rand()%256; 7 char b = rand()%256; 8 ALLEGRO_VERTEX* v = &varray[i*3]; 9 ALLEGRO_COLOR c = al_map_rgb(r,g,b); 10 for (int j = 0 ; j < 3 ; ++j) { 11 int x1 = rand()%SCREENW; 12 int y1 = rand()%SCREENH; 13 v[j].x = x1; 14 v[j].y = y1; 15 v[j].z = 0; 16 v[j].color = c; 17 } 18} 19 20ALLEGRO_VERTEX_BUFFER* vbuf = al_create_vertex_buffer(NULL , varray , 300000 , ALLEGRO_PRIM_BUFFER_DYNAMIC); 21 22// later 23al_draw_vertex_buffer(vbuf , NULL , 0 , 300000 , ALLEGRO_PRIM_TRIANGLE_LIST); 24 25// and 26al_destroy_vertex_buffer(vbuf);

Go to: