|
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): al_init(); display = al_create_display(); bitmap = al_create_bitmap(); al_set_target_bitmap(bitmap); al_hold_bitmap_drawing(1); for every triangle: al_draw_triangle(); al_hold_bitmap_drawing(0); al_set_target_bitmap(al_get_backbuffer(display)); al_draw_bitmap(bitmap, 0, 0, 0); al_flip_display(); 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
|
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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
GololCohan
Member #17,042
February 2019
|
Thanks for the suggestion. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
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. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Simplest way to do it is to use an array of ALLEGRO_VERTEX to initialize it with. 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);
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|