Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

wyświetlanie fragmentu koła allegro c++

Ostatnio zmodyfikowano 2022-11-01 02:56
Autor Wiadomość
AP1994
Temat założony przez niniejszego użytkownika
wyświetlanie fragmentu koła allegro c++
» 2022-10-30 17:21:13
napisałem funkcję:
C/C++
void draw_fragment_ellipse( float cx, float cy, float rx, float ry, float first_proc, float last_proc, ALLEGRO_COLOR color ) //procęntowo określona wartość fragmętu koła
{
   
LOCAL_VERTEX_CACHE;
   
int num_segments, ii;
   
float scale = get_scale(), first, last;
   
ALLEGRO_ASSERT( rx >= 0 );
   
ALLEGRO_ASSERT( ry >= 0 );
   
ALLEGRO_ASSERT( first_proc >= 0 );
   
ALLEGRO_ASSERT( last_proc < 100 || last_proc > 0 );
   
ALLEGRO_ASSERT( last_proc >= first_proc );
   
first = first_proc * 0.01;
   
last = last_proc * 0.01;
   
num_segments = ALLEGRO_PRIM_QUALITY * sqrtf( scale *( rx + ry ) / 2.0f );
   
if( num_segments < 2 )
       
 return;
   
   
if( num_segments >= ALLEGRO_VERTEX_CACHE_SIZE )
   
{
       
num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1;
   
}
   
al_calculate_arc( &( vertex_cache[ 1 ].x ), sizeof( ALLEGRO_VERTEX ), cx, cy, rx, ry, 0, ALLEGRO_PI * 2, 0, num_segments );
   
vertex_cache[ 0 ].x = cx;
   
vertex_cache[ 0 ].y = cy;
   
for( ii = 0; ii < num_segments + 1; ii++ )
   
{
       
vertex_cache[ ii ].color = al_map_rgba( 0, 0, 0, 0 );
       
vertex_cache[ ii ].z = 0;
   
}
   
for( ii = floor( num_segments * first ); ii < floor( num_segments * last ) + 1; ii++ )
   
{
       
vertex_cache[ ii ].color = color;
   
}
   
al_draw_prim( vertex_cache, 0, 0, 0, num_segments + 1, ALLEGRO_PRIM_TRIANGLE_FAN );
}
void draw_fragment_circle( float cx, float cy, float rx, float ry, float first_proc, float last_proc, ALLEGRO_COLOR c1, float thickness )
{
   
LOCAL_VERTEX_CACHE;
   
int num_segments, ii;
   
float scale = get_scale(), first, last;
   
ALLEGRO_ASSERT( rx >= 0 );
   
ALLEGRO_ASSERT( ry >= 0 );
   
ALLEGRO_ASSERT( first_proc >= 0 );
   
ALLEGRO_ASSERT( last_proc < 100 || last_proc > 0 );
   
ALLEGRO_ASSERT( last_proc >= first_proc );
   
ALLEGRO_ASSERT( thickness >= 0 );
   
first = first_proc * 0.01;
   
last = last_proc * 0.01;
   
num_segments = ALLEGRO_PRIM_QUALITY * sqrtf( scale *( rx + ry ) / 2.0f );
   
/* In case rx and ry are both 0. */
   
if( num_segments < 2 )
       
 return;
   
   
if( 2 * num_segments >= ALLEGRO_VERTEX_CACHE_SIZE )
   
{
       
num_segments =( ALLEGRO_VERTEX_CACHE_SIZE - 1 ) / 2;
   
}
   
al_calculate_arc( &( vertex_cache[ 0 ].x ), sizeof( ALLEGRO_VERTEX ), cx, cy, rx, ry, 0, ALLEGRO_PI * 2, thickness, num_segments );
   
for( ii = 0; ii < num_segments + 1; ii++ )
   
{
       
vertex_cache[ ii ].color = al_map_rgba( 0, 0, 0, 0 );
       
vertex_cache[ ii ].z = 0;
   
}
   
for( ii = floor( num_segments * first ); ii < 2 * floor( num_segments * last ); ii++ )
   
{
       
vertex_cache[ ii ].color = c1;
   
}
   
al_draw_prim( vertex_cache, 0, 0, 0, num_segments, ALLEGRO_PRIM_TRIANGLE_STRIP );
}
Próbuje poprawić te dwie funkcje, a zwłaszcza draw_fragment_ellipse.
P-179743
DejaVu
» 2022-10-30 18:18:33
https://www.allegro.cc/manual/5/al_draw_arc

Tu jest chyba jakiś działający kod:
C/C++
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>

int main()
{
   
al_init();
   
al_init_primitives_addon();
   
al_set_new_display_flags( ALLEGRO_RESIZABLE );
   
al_create_display( 1280, 720 );
   
al_clear_to_color( al_map_rgb( 0, 0, 0 ) );
   
   
{
       
const int num_points = 16;
       
float points[ num_points * 2 ][ 2 ];
       
al_calculate_arc( & points[ 0 ][ 0 ], 2 * sizeof( float ), 100, 100, 100, 100, 0, ALLEGRO_PI * 2, 2, num_points );
       
       
for( int i = 0; i + 4 <= num_points * 2; i += 2 )
       
{
           
al_draw_filled_triangle(
           
points[ i ][ 0 ], points[ i ][ 1 ],
           
points[ i + 1 ][ 0 ], points[ i + 1 ][ 1 ],
           
points[ i + 2 ][ 0 ], points[ i + 2 ][ 1 ],
           
al_map_rgb( 255, 255, 255 ) );
           
al_draw_filled_triangle(
           
points[ i + 2 ][ 0 ], points[ i + 2 ][ 1 ],
           
points[ i + 3 ][ 0 ], points[ i + 3 ][ 1 ],
           
points[ i + 1 ][ 0 ], points[ i + 1 ][ 1 ],
           
al_map_rgb( 255, 255, 255 ) );
       
}
    }
   
   
al_flip_display();
   
   
getchar();
   
   
return 0;
}
Źródło: https://www.allegro.cc/forums/thread/618276
P-179746
AP1994
Temat założony przez niniejszego użytkownika
» 2022-10-30 21:55:58
Po kilku próbach użycia al_draw_arc w funkcji draw_circle stwierdziłem, że nie ma sensu kontynuować, ponieważ z al_draw_arc działało gorzej niż z al_draw_prim.
Naprawiłem też draw_fragment_circle.
C/C++
void draw_fragment_circle( float cx, float cy, float rx, float ry, float first_proc, float last_proc, ALLEGRO_COLOR c1, float thickness )
{
   
LOCAL_VERTEX_CACHE;
   
int num_segments, ii;
   
float scale = get_scale(), first, last;
   
ALLEGRO_ASSERT( rx >= 0 );
   
ALLEGRO_ASSERT( ry >= 0 );
   
ALLEGRO_ASSERT( first_proc >= 0 );
   
ALLEGRO_ASSERT( last_proc < 100 || last_proc > 0 );
   
ALLEGRO_ASSERT( last_proc >= first_proc );
   
ALLEGRO_ASSERT( thickness >= 0 );
   
first = first_proc * 0.01;
   
last = last_proc * 0.01;
   
num_segments = ALLEGRO_PRIM_QUALITY * sqrtf( scale *( rx + ry ) / 2.0f );
   
if( num_segments < 2 )
       
 return;
   
   
if( 2 * num_segments >= ALLEGRO_VERTEX_CACHE_SIZE )
   
{
       
num_segments =( ALLEGRO_VERTEX_CACHE_SIZE - 1 ) / 2;
   
}
   
al_calculate_arc( &( vertex_cache[ 0 ].x ), sizeof( ALLEGRO_VERTEX ), cx, cy, rx, ry, 0, ALLEGRO_PI * 2, thickness, num_segments );
   
for( ii = 0; ii < num_segments * 2; ii++ )
   
{
       
vertex_cache[ ii ].color = al_map_rgba( 0, 0, 0, 0 );
       
vertex_cache[ ii ].z = 0;
   
}
   
if( last >= 0.98 )
   
{
       
for( ii = floor( num_segments * first ); ii < 2 * floor( num_segments * last ); ii++ )
       
{
           
vertex_cache[ ii ].color = c1;
       
}
    }
   
else
   
{
       
for( ii = ceil( num_segments * first ); ii < 2 * ceil( num_segments * last ); ii++ )
       
{
           
vertex_cache[ ii ].color = c1;
       
}
    }
   
al_draw_prim( vertex_cache, 0, 0, 0, num_segments * 2, ALLEGRO_PRIM_TRIANGLE_STRIP );
}
draw_fragment_ellipse będę musiał napisać przy pomocy stałej do rysowania lini tak, aby linie szły od środka na zewnątrz okręgu.
P-179747
AP1994
Temat założony przez niniejszego użytkownika
» 2022-11-01 02:56:49
udało mi się napisać wszystko tak, że działa.
C/C++
void draw_fragment_filled_ellipse( float cx, float cy, float rx, float ry, float first_proc, float last_proc, ALLEGRO_COLOR c1 )
{
   
LOCAL_VERTEX_CACHE;
   
int num_segments, ii;
   
float scale = get_scale(), first, last;
   
ALLEGRO_ASSERT( rx >= 0 );
   
ALLEGRO_ASSERT( ry >= 0 );
   
ALLEGRO_ASSERT( first_proc >= 0 );
   
ALLEGRO_ASSERT( last_proc < 100 || last_proc > 0 );
   
ALLEGRO_ASSERT( last_proc >= first_proc );
   
first = first_proc * 0.01;
   
last = last_proc * 0.01;
   
num_segments = ALLEGRO_PRIM_QUALITY * sqrtf( scale *( rx + ry ) / 2.0f );
   
/* In case rx and ry are both 0. */
   
if( num_segments < 2 )
       
 return;
   
   
if( 2 * num_segments >= ALLEGRO_VERTEX_CACHE_SIZE )
   
{
       
num_segments =( ALLEGRO_VERTEX_CACHE_SIZE - 1 ) / 2;
   
}
   
al_calculate_arc( &( vertex_cache[ 0 ].x ), sizeof( ALLEGRO_VERTEX ), cx, cy, rx / 2, ry / 2, 0, ALLEGRO_PI * 2, scale *( rx + ry ) / 2.0f, num_segments );
   
for( ii = 0; ii < num_segments * 2; ii++ )
   
{
       
vertex_cache[ ii ].color = al_map_rgba( 0, 0, 0, 0 );
       
vertex_cache[ ii ].z = 0;
   
}
   
if( last_proc < 99 )
   
{
       
for( ii = ceil( num_segments * first ); ii < 2 * ceil( num_segments * last ); ii++ )
       
{
           
vertex_cache[ ii ].color = c1;
       
}
    }
   
else
   
{
       
for( ii = floor( num_segments * first ); ii < 2 * floor( num_segments * last ); ii++ )
       
{
           
vertex_cache[ ii ].color = c1;
       
}
    }
   
al_draw_prim( vertex_cache, 0, 0, 0, num_segments * 2, ALLEGRO_PRIM_TRIANGLE_STRIP );
}
nie jest to rozwiązanie idealne, ale działa.
P-179748
« 1 »
  Strona 1 z 1