Added configuration constants to the gpu kernel
This commit is contained in:
parent
5595821b4f
commit
aa3906bf0b
|
@ -10,6 +10,15 @@ static float *ground;
|
||||||
static float *working_ground;
|
static float *working_ground;
|
||||||
static int2 ground_dimensions;
|
static int2 ground_dimensions;
|
||||||
|
|
||||||
|
constant float sense_distance = 3.0f;
|
||||||
|
constant float move_distance = 3.0f;
|
||||||
|
constant float diffusion_rate = 0.05f;
|
||||||
|
constant float cone_of_vision = 2.0f*M_PI_F/4.0f;
|
||||||
|
constant float vision_sensing_steps = 8.0f;
|
||||||
|
constant float color_r = 0.0f;
|
||||||
|
constant float color_g = 255.0f;
|
||||||
|
constant float color_b = 255.0f;
|
||||||
|
|
||||||
uint hash(uint input)
|
uint hash(uint input)
|
||||||
{
|
{
|
||||||
input ^= 2747636419u;
|
input ^= 2747636419u;
|
||||||
|
@ -56,7 +65,7 @@ void kernel update_ground()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
working_ground[current_index] = sum/9.0f-0.005f;
|
working_ground[current_index] = sum/9.0f-diffusion_rate;
|
||||||
|
|
||||||
if (working_ground[current_index] < 0)
|
if (working_ground[current_index] < 0)
|
||||||
{
|
{
|
||||||
|
@ -78,27 +87,32 @@ void kernel move_agents()
|
||||||
if (psuedorandom_int % 100 < 12)
|
if (psuedorandom_int % 100 < 12)
|
||||||
{
|
{
|
||||||
psuedorandom_int = hash(psuedorandom_int);
|
psuedorandom_int = hash(psuedorandom_int);
|
||||||
agents[current_index].direction += (float)(psuedorandom_int%1000)*M_PI_F/3.0f/1000.0f-M_PI_F/6.0f;
|
agents[current_index].direction += (float)(psuedorandom_int%1000)*cone_of_vision/1000.0f-cone_of_vision/2.0f;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
float lsense = ground[(int)(agents[current_index].x+2.0f*cos(agents[current_index].direction+M_PI_F/6.0f)) + (int)(agents[current_index].y+2.0f*sin(agents[current_index].direction+M_PI_F/6.0f)) * ground_dimensions[0]];
|
float directional_change = cone_of_vision/2.0f;
|
||||||
float ssense = ground[(int)(agents[current_index].x+2.0f*cos(agents[current_index].direction)) + (int)(agents[current_index].y+2.0f*sin(agents[current_index].direction)) * ground_dimensions[0]];
|
float mx = agents[current_index].x + sense_distance*cos(agents[current_index].direction+directional_change);
|
||||||
float rsense = ground[(int)(agents[current_index].x+2.0f*cos(agents[current_index].direction-M_PI_F/6.0f)) + (int)(agents[current_index].y+2.0f*sin(agents[current_index].direction-M_PI_F/6.0f)) * ground_dimensions[0]];
|
float my = agents[current_index].y + sense_distance*sin(agents[current_index].direction+directional_change);
|
||||||
|
float max_sense = ground[(int)mx+(int)my*ground_dimensions[0]];
|
||||||
|
|
||||||
if (lsense > ssense && lsense > rsense)
|
for(int i = 1; i <= 8; i++)
|
||||||
{
|
{
|
||||||
agents[current_index].direction += M_PI_F/6.0f;
|
mx = agents[current_index].x + sense_distance*cos(agents[current_index].direction+cone_of_vision/2.0f-(float)i*cone_of_vision/vision_sensing_steps);
|
||||||
}
|
my = agents[current_index].y + sense_distance*sin(agents[current_index].direction+cone_of_vision/2.0f-(float)i*cone_of_vision/vision_sensing_steps);
|
||||||
|
|
||||||
if (rsense > ssense && rsense > lsense)
|
if (ground[(int)mx+(int)my*ground_dimensions[0]] > max_sense)
|
||||||
{
|
{
|
||||||
agents[current_index].direction += -M_PI_F/6.0f;
|
directional_change = cone_of_vision/2.0f-(float)i*cone_of_vision/vision_sensing_steps;
|
||||||
|
max_sense = ground[(int)mx+(int)my*ground_dimensions[0]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
float nx = agents[current_index].x + cos(agents[current_index].direction);
|
agents[current_index].direction += directional_change;
|
||||||
float ny = agents[current_index].y + sin(agents[current_index].direction);
|
}
|
||||||
|
|
||||||
|
float nx = agents[current_index].x + move_distance*cos(agents[current_index].direction);
|
||||||
|
float ny = agents[current_index].y + move_distance*sin(agents[current_index].direction);
|
||||||
|
|
||||||
if(nx < 0.0f || nx >= ground_dimensions[0] || ny < 0.0f || ny >= ground_dimensions[1])
|
if(nx < 0.0f || nx >= ground_dimensions[0] || ny < 0.0f || ny >= ground_dimensions[1])
|
||||||
{
|
{
|
||||||
|
@ -126,10 +140,8 @@ void kernel fill_draw_data(global uchar* draw_data)
|
||||||
{
|
{
|
||||||
int index = get_global_id(0);
|
int index = get_global_id(0);
|
||||||
|
|
||||||
uchar val = (uchar)(255.0f*ground[index]);
|
draw_data[4*index] = (uchar)(color_b*ground[index]);
|
||||||
|
draw_data[4*index+1] = (uchar)(color_g*ground[index]);
|
||||||
draw_data[4*index] = val;
|
draw_data[4*index+2] = (uchar)(color_r*ground[index]);
|
||||||
draw_data[4*index+1] = val;
|
|
||||||
draw_data[4*index+2] = val;
|
|
||||||
draw_data[4*index+3] = 255;
|
draw_data[4*index+3] = 255;
|
||||||
}
|
}
|
|
@ -73,7 +73,7 @@ int main(int argc, char* argv[])
|
||||||
SDL_LockTexture(start_texture, NULL, (void**)&u8_pixels,&i_pitch);
|
SDL_LockTexture(start_texture, NULL, (void**)&u8_pixels,&i_pitch);
|
||||||
|
|
||||||
int i_created = 0;
|
int i_created = 0;
|
||||||
uint32_t u32_radius = 4*((u32_width<u32_height)?u32_width:u32_height)/10;
|
uint32_t u32_radius = 1*((u32_width<u32_height)?u32_width:u32_height)/10;
|
||||||
while(i_created < i_agent_count)
|
while(i_created < i_agent_count)
|
||||||
{
|
{
|
||||||
float r = u32_radius*sqrt((float)rand()/(float)RAND_MAX);
|
float r = u32_radius*sqrt((float)rand()/(float)RAND_MAX);
|
||||||
|
@ -215,7 +215,7 @@ int main(int argc, char* argv[])
|
||||||
unsigned long long ull_time_old, ull_time_now;
|
unsigned long long ull_time_old, ull_time_now;
|
||||||
ull_time_now = ull_time_old = sys_time_us();
|
ull_time_now = ull_time_old = sys_time_us();
|
||||||
|
|
||||||
int iteration_duration = 16666;
|
int iteration_duration = 16666/10;
|
||||||
bool pause = true;
|
bool pause = true;
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
|
|
Loading…
Reference in New Issue