From aa3906bf0bc9603511f1819e851748a93f80251e Mon Sep 17 00:00:00 2001 From: Marcus Penate Date: Wed, 20 Oct 2021 22:46:08 -0400 Subject: [PATCH] Added configuration constants to the gpu kernel --- gpu_kernel.clcpp | 48 ++++++++++++++++++++++++++++++------------------ src/main.cpp | 4 ++-- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/gpu_kernel.clcpp b/gpu_kernel.clcpp index 75b9883..2900caa 100644 --- a/gpu_kernel.clcpp +++ b/gpu_kernel.clcpp @@ -10,6 +10,15 @@ static float *ground; static float *working_ground; 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) { 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) { @@ -78,27 +87,32 @@ void kernel move_agents() if (psuedorandom_int % 100 < 12) { 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 { - 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 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 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 directional_change = cone_of_vision/2.0f; + float mx = agents[current_index].x + sense_distance*cos(agents[current_index].direction+directional_change); + 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 (ground[(int)mx+(int)my*ground_dimensions[0]] > max_sense) + { + 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]]; + } } - if (rsense > ssense && rsense > lsense) - { - agents[current_index].direction += -M_PI_F/6.0f; - } + agents[current_index].direction += directional_change; } - float nx = agents[current_index].x + cos(agents[current_index].direction); - 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]) { @@ -126,10 +140,8 @@ void kernel fill_draw_data(global uchar* draw_data) { int index = get_global_id(0); - uchar val = (uchar)(255.0f*ground[index]); - - draw_data[4*index] = val; - draw_data[4*index+1] = val; - draw_data[4*index+2] = val; + draw_data[4*index] = (uchar)(color_b*ground[index]); + draw_data[4*index+1] = (uchar)(color_g*ground[index]); + draw_data[4*index+2] = (uchar)(color_r*ground[index]); draw_data[4*index+3] = 255; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 81aad11..6907aaa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,7 +73,7 @@ int main(int argc, char* argv[]) SDL_LockTexture(start_texture, NULL, (void**)&u8_pixels,&i_pitch); int i_created = 0; - uint32_t u32_radius = 4*((u32_width