From 7538a57ed06c0bb544b5efe7b351ca955f859146 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sat, 2 Nov 2024 23:25:21 -0600 Subject: [PATCH] Sane hex region values --- client/Makefile | 6 +-- client/include/hex.h | 6 ++- client/shader/hex.vert | 40 ++++++++--------- client/src/hex.c | 3 +- client/src/main.c | 99 +++++++++++------------------------------- 5 files changed, 52 insertions(+), 102 deletions(-) diff --git a/client/Makefile b/client/Makefile index eb95d75..980979b 100644 --- a/client/Makefile +++ b/client/Makefile @@ -60,10 +60,10 @@ debug: roleplay $(EXTRA_DEBUG_REQUIREMENTS) $(SPV_FILES) $(GDB) roleplay %.vert.spv: %.vert - glslc -o $@ $< + glslc -O -o $@ $< %.frag.spv: %.frag - glslc -o $@ $< + glslc -O -o $@ $< %.comp.spv: %.comp - glslc -o $@ $< + glslc -O -o $@ $< diff --git a/client/include/hex.h b/client/include/hex.h index 058dac6..a2f98bd 100644 --- a/client/include/hex.h +++ b/client/include/hex.h @@ -3,9 +3,9 @@ #include "gpu.h" -#define REGION_SIZE 7 +#define REGION_SIZE (10) #define REGION_HEX_COUNT (3*REGION_SIZE*(REGION_SIZE-1)+1) -#define MAX_LOADED_REGIONS 11*11 +#define MAX_LOADED_REGIONS (25*25) typedef struct GPUHexStruct { float height[6]; @@ -26,6 +26,8 @@ typedef struct HexRegionStruct { VkDeviceAddress address; VkBuffer region; VmaAllocation region_memory; + + GPUHex data[REGION_HEX_COUNT]; } HexRegion; typedef struct GPUHexContextStruct { diff --git a/client/shader/hex.vert b/client/shader/hex.vert index 548f4f8..f487d12 100644 --- a/client/shader/hex.vert +++ b/client/shader/hex.vert @@ -73,14 +73,15 @@ vec4 int2color(uint color_int) { return vec4(r, g, b, a)/255.0; } -const uint region_size = 7; +const int region_size = 10; const float region_width = x*(2*region_size-1); const float region_height = z*(2*region_size-1); -const uint region_hex_count = 3*region_size*(region_size-1)+1; +const int region_hex_count = 3*region_size*(region_size-1)+1; void main() { - uint hex_index = gl_InstanceIndex - ((gl_InstanceIndex/region_hex_count)*region_hex_count); - Region region = pc.context.regions[gl_InstanceIndex/region_hex_count]; + int region_index = gl_InstanceIndex/region_hex_count; + int hex_index = gl_InstanceIndex - (region_index*region_hex_count); + Region region = pc.context.regions[region_index]; vec2 region_qr = vec2(region.q, region.r); @@ -88,18 +89,16 @@ void main() { region_pos.x = (region_qr.x+region_qr.y/2)*region_width - region_qr.y*x/2; region_pos.z = 0.75*region_qr.y*region_height + 0.25*region_qr.y*z + 0.5*region_qr.x*z; - uint radius = 0; - uint ring = 0; - uint side = 0; - uint hex = 0; + float radius = 0; + float ring = 0; + int side = 0; if(hex_index != 0) { - radius = uint(floor(0.5 + sqrt(12*hex_index-3)/6)); + radius = floor(0.5 + sqrt(12*hex_index-3)/6); ring = hex_index - (3*radius*radius - 3*radius + 1); - side = uint(floor(ring/(radius))); - hex = ring - (radius*side); - } - - vec4 position = vertices[indices[gl_VertexIndex]] + (starts[side]*radius) + (direction[side]*hex) + region_pos; + side = int(floor(ring/(radius))); + } + + vec4 position = vertices[indices[gl_VertexIndex]] + (starts[side]*radius) + (direction[side]*(ring-(radius*side))) + region_pos; if(gl_VertexIndex % 3 == 0) { position.y = (region.hexes[hex_index].heights[0] + region.hexes[hex_index].heights[1] + @@ -112,9 +111,12 @@ void main() { } color = int2color(region.hexes[hex_index].colors[indices[gl_VertexIndex]]); - vec2 hex_qr = start_coords[side]*radius + direction_coords[side]*hex; - /* + vec2 hex_qr = start_coords[side]*radius + direction_coords[side]*(ring-(radius*side)); + vec2 world_qr; + world_qr.x = (x*hex_qr.x + (region_qr.x+region_qr.y/2)*region_width - region_qr.y*x/2)/x; + world_qr.y = -(-z*hex_qr.y - z*hex_qr.x/2 + 0.75*region_qr.y*region_height + 0.25*region_qr.y*z + 0.5*region_qr.x*z)/z - (x*hex_qr.x + (region_qr.x+region_qr.y/2)*region_width - region_qr.y*x/2)/(2*x); + vec4 hex_pos = vec4(0, 0, 0, 1); hex_pos.x = x*hex_qr.x; hex_pos.z = -z*hex_qr.y - z*hex_qr.x/2; @@ -128,11 +130,5 @@ void main() { world_qr.y = -world_pos.z/z - world_pos.x/(2*x); */ - vec2 world_qr; - world_qr.x = (x*hex_qr.x + (region_qr.x+region_qr.y/2)*region_width - region_qr.y*x/2)/x; - world_qr.y = -(-z*hex_qr.y - z*hex_qr.x/2 + 0.75*region_qr.y*region_height + 0.25*region_qr.y*z + 0.5*region_qr.x*z)/z - (x*hex_qr.x + (region_qr.x+region_qr.y/2)*region_width - region_qr.y*x/2)/(2*x); - - color = vec4(world_qr/region_size, 0.0, 1.0); - gl_Position = pc.context.proj * pc.context.view * position; } diff --git a/client/src/hex.c b/client/src/hex.c index 4119024..348fa95 100644 --- a/client/src/hex.c +++ b/client/src/hex.c @@ -202,8 +202,9 @@ VkResult create_hex_context( sizeof(GPUHexContext), &context->context, &context->context_memory)); + fprintf(stderr, "Created hex context %p\n", context->context); context->address = buffer_address(gpu->device, context->context); - VK_RESULT(add_transfer(&context->data, context->context, 0, sizeof(GPUHexContext) - sizeof(VkDeviceAddress)*MAX_LOADED_REGIONS, 0, gpu)); + VK_RESULT(add_transfer(&context->data, context->context, 0, sizeof(GPUHexContext) - (sizeof(GPUHexContext) - offsetof(GPUHexContext, regions)), 0, gpu)); return VK_SUCCESS; } diff --git a/client/src/main.c b/client/src/main.c index 6519e2e..6d6353a 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -12,6 +12,9 @@ #include "vulkan/vulkan_core.h" #include "pthread.h" #include +#include + +#define max(a, b) ((a > b) ? a : b) typedef struct ClientContextStruct { GLFWwindow* window; @@ -21,6 +24,7 @@ typedef struct ClientContextStruct { uint32_t clicked_container; uint32_t clicked_element; + int32_t clicked_hex[2]; vec2 cursor; @@ -62,45 +66,14 @@ VkResult main_thread(ClientContext* context) { .length = 0, .font = 0, }, - { - .pos = {110, 138}, - .size = 32, - .color = {1.0, 1.0, 1.0, 1.0}, - .offset = 20, - .length = 1, - .font = 0, - }, - { - .pos = {210, 138}, - .size = 32, - .color = {1.0, 1.0, 1.0, 1.0}, - .offset = 21, - .length = 1, - .font = 0, - }, }; - GPUDrawable drawables[] = { - { - .pos = {100, 100}, - .size = {50, 50}, - .color = {1, 0, 0, 1}, - .type = 0, - .id = 1, - }, - { - .pos = {200, 100}, - .size = {50, 50}, - .color = {0, 1, 0, 1}, - .type = 0, - .id = 2, - }, - }; + GPUDrawable drawables[] = {}; LayerInput layer = { .strings = strings, .num_strings = sizeof(strings)/sizeof(GPUString), - .max_codes = 24, + .max_codes = 50, .drawables = drawables, .num_drawables = sizeof(drawables)/sizeof(GPUDrawable), @@ -114,57 +87,30 @@ VkResult main_thread(ClientContext* context) { }; create_container(&container, &context->render, &context->ui); - map_string("1234", context->ui.containers[0].layers[0].codes_buffer, 20, 0, &context->ui); - VK_RESULT(add_transfers( - &context->ui.containers[0].layers[0].codes_buffer[20], - context->ui.containers[0].layers[0].codes, - 20*sizeof(uint32_t), - 4*sizeof(uint32_t), - &context->render)); HexRegion* regions[MAX_LOADED_REGIONS]; uint32_t colors[] = { 0xFF0000FF, 0x00FF00FF, - 0x00FF00FF, - 0x00FF00FF, - 0x00FF00FF, - 0x00FF00FF, - 0x00FF00FF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, - 0x0000FFFF, 0x0000FFFF, }; - GPUHex* temp_hexes = malloc(sizeof(GPUHex)*REGION_HEX_COUNT); uint32_t region = 0; - for(int32_t q = -5; q < 5; q++) { - for(int32_t r = -5; r < 5; r++) { + for(int32_t q = -10; q < 10; q++) { + for(int32_t r = -10; r < 10; r++) { VK_RESULT(create_hex_region(q, r, ®ions[region], &context->hex, &context->render)); for(uint32_t i = 0; i < REGION_HEX_COUNT; i++) { for(uint32_t h = 0; h < 6; h++) { - temp_hexes[i].color[h] = colors[r % (sizeof(colors)/sizeof(uint32_t))]; - temp_hexes[i].height[h] = 0; + regions[region]->data[i].color[h] = colors[(q+r+50) % (sizeof(colors)/sizeof(uint32_t))]; + regions[region]->data[i].height[h] = (float)i/REGION_HEX_COUNT; } - temp_hexes[i].color[6] = colors[r % (sizeof(colors)/sizeof(uint32_t))]; + regions[region]->data[i].color[6] = colors[(q+r+50) % (sizeof(colors)/sizeof(uint32_t))]; } - VK_RESULT(add_transfer(temp_hexes, regions[region]->region, offsetof(GPUHexRegion, hexes), sizeof(GPUHex)*REGION_HEX_COUNT, 0, &context->render)); + VK_RESULT(add_transfer(®ions[region]->data, regions[region]->region, offsetof(GPUHexRegion, hexes), sizeof(GPUHex)*REGION_HEX_COUNT, 0, &context->render)); region++; } } - - free(temp_hexes); - context->position[0] = 0; context->position[1] = 0; context->position[2] = 0; @@ -178,7 +124,7 @@ VkResult main_thread(ClientContext* context) { context->key_spin[1] = 0; context->cur_spin[0] = 0; context->cur_spin[1] = 0; - context->distance = 50; + context->distance = 25; context->zoom = 0; context->camera_mode = false; context->key_spin_speed = 1.0; @@ -188,15 +134,18 @@ VkResult main_thread(ClientContext* context) { // uint32_t* mapped_codes = context->ui.containers[0].layers[0].codes_buffer; GPUString* mapped_string = context->ui.containers[0].layers[0].strings_buffer; - char str[21]; + char str[51]; double last_frame_time = 0; + int frame = 0; while(glfwWindowShouldClose(context->window) == 0) { double frame_time = glfwGetTime(); double delta_time = (frame_time - last_frame_time); context->clicked_element = 0x00000000; context->clicked_container = 0x00000000; + context->clicked_hex[0] = 0; + context->clicked_hex[1] = 0; context->zoom = 0; context->cur_spin[0] = 0; context->cur_spin[1] = 0; @@ -205,8 +154,7 @@ VkResult main_thread(ClientContext* context) { if(context->key_spin[0] != 0 || context->key_spin[1] != 0 || context->zoom != 0 || - context->cur_spin[0] != 0 || context->cur_spin[1] != 0 || - last_frame_time == 0) { + context->cur_spin[0] != 0 || context->cur_spin[1] != 0 || frame == 1) { context->rotation[0] += (float)context->key_spin[0]*delta_time*context->key_spin_speed; context->rotation[0] += (float)context->cur_spin[0]*delta_time*context->cur_spin_speed; @@ -237,8 +185,10 @@ VkResult main_thread(ClientContext* context) { vec2 hex_pos = {}; vec2 hex_forward = {}; - hex_pos[0] = camera[0]; - hex_pos[1] = camera[2] - camera[0]/2; + hex_pos[0] = camera[0]; + hex_pos[1] = -camera[2] + camera[0]/2; + hex_forward[0] = 0; + hex_forward[1] = 0; glm_lookat(camera, context->position, up, context->hex.data.view); add_transfer(&context->hex.data, context->hex.context, 0, 2*sizeof(mat4), context->render.current_frame, &context->render); @@ -246,8 +196,8 @@ VkResult main_thread(ClientContext* context) { add_transfer(hex_forward, context->hex.context, offsetof(GPUHexContext, forward), sizeof(vec2), context->render.current_frame, &context->render); } - if(context->clicked_container != 0) { - snprintf(str, 21, "Clicked: %d.%d", context->clicked_container, context->clicked_element); + if(context->clicked_hex[0] != 0 || context->clicked_hex[1] != 0) { + snprintf(str, 50, "Clicked: (%d,%d)", context->clicked_hex[0], context->clicked_hex[1]); map_string(str, mapped_codes, 0, 0, &context->ui); VK_RESULT(add_transfers( context->ui.containers[0].layers[0].codes_buffer, @@ -264,6 +214,7 @@ VkResult main_thread(ClientContext* context) { &context->render)); } + frame += 1; // VkResult result = draw_frame(&context->render, &context->ui, &context->hex, frame_time);