Startfield Shader based back on std::vector

This commit is contained in:
Sylvain PILLOT 2023-01-15 14:38:09 +01:00
parent 3251d16dc0
commit f33cb39837
4 changed files with 164 additions and 19 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 389 B

View File

@ -11,6 +11,7 @@ void azrp_pixellist(std::vector<Pixel*> const &list, int fragnum );
void azrp_starfield( void );
void azrp_starfield_init( void );
void azrp_starfield_init( uint8_t nbstars );
void azrp_starfield_close( void );
#endif //MYAZURSHADERS_H

View File

@ -112,7 +112,11 @@ static void get_inputs( void )
if(keydown(KEY_F3)) {texttodraw=2;}
if(keydown(KEY_F4)) {texttodraw=3;}
if(keydown(KEY_F6)) {azrp_starfield_init();}
if(keydown(KEY_F6))
{
azrp_starfield_close( );
azrp_starfield_init( 200 );
}
}
@ -135,7 +139,7 @@ int main(void)
azrp_shader_image_p4_configure();
azrp_shader_triangle_configure();
azrp_starfield_init();
azrp_starfield_init( 200 );
usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL };
@ -247,6 +251,8 @@ int main(void)
MyParticles.clear();
azrp_starfield_close( );
prof_quit();
usb_close();

View File

@ -7,6 +7,9 @@
#include <gint/usb.h>
#include <gint/usb-ff-bulk.h>
#include <vector>
uint8_t AZRP_SHADER_STARFIELD = -1;
@ -15,10 +18,17 @@ uint8_t AZRP_SHADER_STARFIELD = -1;
struct particle
{
int x,y,s,c,frag,off;
int x, y, s, c, frag, off;
};
particle datastar[NBPARTS];
struct star
{
int x, y, s, n, t;
};
std::vector<particle> pixels;
std::vector<star> starfield;
__attribute__((constructor))
@ -38,23 +48,150 @@ void azrp_shader_starfield_configure(void)
struct command {
uint8_t shader_id;
uint8_t current_frag;
uint8_t nbpixel;
particle *data;
};
#define DARKSKY 0x2106
void azrp_starfield_init( void )
#define BLUE1 0x39EE
#define BLUE2 0x3310
#define BLUE3 0x64DF
#define PINK1 0x4147
#define PINK2 0x7211
#define PINK3 0xD3D7
#define GREEN1 0x31E7
#define GREEN2 0x4245
#define GREEN3 0x34AD
#define ORANGE1 0x61C6
#define ORANGE2 0x8AA7
#define ORANGE3 0xDB83
void azrp_starfield_init( uint8_t nbstars )
{
srand(rtc_ticks());
for(int i=0; i<NBPARTS; i++)
star temp;
for(int i=0; i<nbstars; i++)
{
datastar[i].x = rand( ) % 396;
datastar[i].y = rand( ) % 224;
datastar[i].s = 1 + rand( ) % 4;
datastar[i].c = 0xFFFF;
datastar[i].frag = datastar[i].y / 16;
datastar[i].off = datastar[i].y & 15;
temp.x = rand( ) % 394;
temp.y = rand( ) % 222;
temp.s = 1 + rand( ) % 3; // 1,2,3 : speed of the star
temp.n = 1 + rand( ) % 4; // 1,2,3,4 : size of the star
temp.t = 1 + rand( ) % 12; // 1..12 : colorcode of the star
starfield.push_back( temp );
}
particle ptemp;
for(auto& s : starfield)
{
uint16_t col1, col2, col3;
if (s.t==1 || s.t==2)
{
col1 = BLUE1;
col2 = BLUE2;
col3 = BLUE3;
}
else if (s.t==3 || s.t==4)
{
col1 = PINK1;
col2 = PINK2;
col3 = PINK3;
}
else if (s.t==5 || s.t==6)
{
col1 = GREEN1;
col2 = GREEN2;
col3 = GREEN3;
}
else if (s.t==7 || s.t==8)
{
col1 = ORANGE1;
col2 = ORANGE2;
col3 = ORANGE3;
}
else
{
col1 = 0xFFFF;
col2 = 0XFFFF;
col3 = 0XFFFF;
}
if (s.n==2)
{
ptemp = { s.x, s.y, s.s, col2, s.y/16, s.y&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y, s.s, col2, s.y/16, s.y&15 }; pixels.push_back( ptemp );
ptemp = { s.x, s.y+1, s.s, col2, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y+1, s.s, col2, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
}
else if (s.n==3)
{
ptemp = { s.x, s.y, s.s, DARKSKY, s.y/16, s.y&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y, s.s, col3, s.y/16, s.y&15 }; pixels.push_back( ptemp );
ptemp = { s.x+2, s.y, s.s, DARKSKY, s.y/16, s.y&15 }; pixels.push_back( ptemp );
ptemp = { s.x, s.y+1, s.s, col3, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y+1, s.s, col2, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+2, s.y+1, s.s, col3, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x, s.y+2, s.s, DARKSKY, (s.y+2)/16, (s.y+2)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y+2, s.s, col3, (s.y+2)/16, (s.y+2)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+2, s.y+2, s.s, DARKSKY, (s.y+2)/16, (s.y+2)&15 }; pixels.push_back( ptemp );
}
else if (s.n==4)
{
ptemp = { s.x+1, s.y, s.s, col1, s.y/16, s.y&15 }; pixels.push_back( ptemp );
ptemp = { s.x+2, s.y, s.s, col1, s.y/16, s.y&15 }; pixels.push_back( ptemp );
ptemp = { s.x+3, s.y, s.s, col1, s.y/16, s.y&15 }; pixels.push_back( ptemp );
ptemp = { s.x, s.y+1, s.s, col1, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y+1, s.s, col2, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+2, s.y+1, s.s, col2, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+3, s.y+1, s.s, col2, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+4, s.y+1, s.s, col1, (s.y+1)/16, (s.y+1)&15 }; pixels.push_back( ptemp );
ptemp = { s.x, s.y+2, s.s, col1, (s.y+2)/16, (s.y+2)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y+2, s.s, col2, (s.y+2)/16, (s.y+2)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+2, s.y+2, s.s, col3, (s.y+2)/16, (s.y+2)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+3, s.y+2, s.s, col2, (s.y+2)/16, (s.y+2)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+4, s.y+2, s.s, col1, (s.y+2)/16, (s.y+2)&15 }; pixels.push_back( ptemp );
ptemp = { s.x, s.y+3, s.s, col1, (s.y+3)/16, (s.y+3)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y+3, s.s, col2, (s.y+3)/16, (s.y+3)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+2, s.y+3, s.s, col2, (s.y+3)/16, (s.y+3)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+3, s.y+3, s.s, col2, (s.y+3)/16, (s.y+3)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+4, s.y+3, s.s, col1, (s.y+3)/16, (s.y+3)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+1, s.y+4, s.s, col1, (s.y+4)/16, (s.y+4)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+2, s.y+4, s.s, col1, (s.y+4)/16, (s.y+4)&15 }; pixels.push_back( ptemp );
ptemp = { s.x+3, s.y+4, s.s, col1, (s.y+4)/16, (s.y+4)&15 }; pixels.push_back( ptemp );
}
else
{
ptemp = { s.x, s.y, s.s, col3, s.y/16, s.y&15 }; pixels.push_back( ptemp );
}
}
starfield.clear();
}
void azrp_starfield_close( void )
{
pixels.clear();
starfield.clear();
}
void azrp_starfield( void )
@ -64,13 +201,14 @@ void azrp_starfield( void )
struct command cmd;
cmd.shader_id = AZRP_SHADER_STARFIELD;
cmd.current_frag = 0;
cmd.data = datastar;
cmd.nbpixel = pixels.size();
cmd.data = pixels.data();
for(int i=0; i<NBPARTS; i++)
for(int i=0; i<pixels.size(); i++)
{
datastar[i].y = (datastar[i].y + datastar[i].s) % 224;
datastar[i].frag = datastar[i].y / 16;
datastar[i].off = datastar[i].y & 15;
pixels[i].y = (pixels[i].y + pixels[i].s) % 224;
pixels[i].frag = pixels[i].y / 16;
pixels[i].off = pixels[i].y & 15;
}
@ -88,7 +226,7 @@ void azrp_shader_starfield( void *uniforms, void *comnd, void *fragment )
uint16_t *frag = (uint16_t *) fragment;
particle *data = cmd->data;
for(int i=0; i<NBPARTS; i++)
for(int i=0; i<cmd->nbpixel; i++)
if (cmd->current_frag == data[i].frag)
{
frag[azrp_width * data[i].off + data[i].x] = data[i].c;