Changed hex shader to be on the x/z plane instead of x/y, added camera orbit logic

main
noah metz 2024-11-01 12:55:56 -06:00
parent 43c5ff71ad
commit 07b0a46dfc
2 changed files with 141 additions and 39 deletions

@ -8,21 +8,21 @@ float w = 0.5;
vec4 vertices[] = { vec4 vertices[] = {
vec4(0, 0, 0, 1), vec4(0, 0, 0, 1),
vec4(w*cos(0*PI/3), w*sin(0*PI/3), 0, 1), vec4(w*cos(0*PI/3), 0, w*sin(0*PI/3), 1),
vec4(w*cos(1*PI/3), w*sin(1*PI/3), 0, 1), vec4(w*cos(1*PI/3), 0, w*sin(1*PI/3), 1),
vec4(w*cos(2*PI/3), w*sin(2*PI/3), 0, 1), vec4(w*cos(2*PI/3), 0, w*sin(2*PI/3), 1),
vec4(w*cos(3*PI/3), w*sin(3*PI/3), 0, 1), vec4(w*cos(3*PI/3), 0, w*sin(3*PI/3), 1),
vec4(w*cos(4*PI/3), w*sin(4*PI/3), 0, 1), vec4(w*cos(4*PI/3), 0, w*sin(4*PI/3), 1),
vec4(w*cos(5*PI/3), w*sin(5*PI/3), 0, 1), vec4(w*cos(5*PI/3), 0, w*sin(5*PI/3), 1),
}; };
uint indices[] = { uint indices[] = {
0, 1, 2, 0, 2, 1,
0, 2, 3, 0, 3, 2,
0, 3, 4, 0, 4, 3,
0, 4, 5, 0, 5, 4,
0, 5, 6, 0, 6, 5,
0, 6, 1, 0, 1, 6,
}; };
void main() { void main() {

@ -1,4 +1,5 @@
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "cglm/cam.h"
#include "cglm/mat4.h" #include "cglm/mat4.h"
#include "ui.h" #include "ui.h"
#include "gpu.h" #include "gpu.h"
@ -10,6 +11,7 @@
#include "vulkan/vk_enum_string_helper.h" #include "vulkan/vk_enum_string_helper.h"
#include "vulkan/vulkan_core.h" #include "vulkan/vulkan_core.h"
#include "pthread.h" #include "pthread.h"
#include <math.h>
typedef struct ClientContextStruct { typedef struct ClientContextStruct {
GLFWwindow* window; GLFWwindow* window;
@ -19,12 +21,21 @@ typedef struct ClientContextStruct {
uint32_t clicked_container; uint32_t clicked_container;
uint32_t clicked_element; uint32_t clicked_element;
vec3 pos; vec2 cursor;
vec4 rot;
double delta_time; vec3 position;
VkBuffer hex_context;
VmaAllocation hex_context_memory; vec2 rotation;
GPUHexContext hex; int32_t key_spin[2];
vec2 cur_spin;
double distance;
int32_t zoom;
bool camera_mode;
float key_spin_speed;
float cur_spin_speed;
float zoom_speed;
} ClientContext; } ClientContext;
void* network_thread(void* data) { void* network_thread(void* data) {
@ -34,6 +45,9 @@ void* network_thread(void* data) {
return NULL; return NULL;
} }
vec3 zero = {0, 0, 0};
vec3 up = {0, 1, 0};
VkResult main_thread(ClientContext* context) { VkResult main_thread(ClientContext* context) {
VkResult result; VkResult result;
@ -110,12 +124,31 @@ VkResult main_thread(ClientContext* context) {
4*sizeof(uint32_t), 4*sizeof(uint32_t),
&context->render)); &context->render));
VK_RESULT(create_storage_buffer(context->render.allocator, 0, sizeof(GPUHexContext), &context->hex_context, &context->hex_context_memory)); VkBuffer hex_context;
VkDeviceAddress hex_context_address = buffer_address(context->render.device, context->hex_context); VmaAllocation hex_context_memory;
glm_ortho_default((float)context->render.swapchain_extent.width/(float)context->render.swapchain_extent.height, context->hex.proj); GPUHexContext hex;
glm_translate_make(context->hex.view, context->pos); VK_RESULT(create_storage_buffer(context->render.allocator, 0, sizeof(GPUHexContext), &hex_context, &hex_context_memory));
add_transfer(&context->hex, context->hex_context, 0, sizeof(GPUHexContext), 0, &context->render); VkDeviceAddress hex_context_address = buffer_address(context->render.device, hex_context);
add_transfer(&context->hex, context->hex_context, 0, sizeof(GPUHexContext), 1, &context->render); glm_perspective(30.0, (float)context->render.swapchain_extent.width/(float)context->render.swapchain_extent.height, 0.01, 99.9, hex.proj);
context->position[0] = 0;
context->position[1] = 0;
context->position[2] = 0;
context->rotation[0] = 0;
context->rotation[1] = 0;
context->cur_spin[0] = 0;
context->cur_spin[1] = 0;
context->key_spin[0] = 0;
context->key_spin[1] = 0;
context->cur_spin[0] = 0;
context->cur_spin[1] = 0;
context->distance = 1;
context->zoom = 0;
context->camera_mode = false;
context->key_spin_speed = 1.0;
context->cur_spin_speed = 1.0;
context->zoom_speed = 1.0;
// //
uint32_t* mapped_codes = context->ui.containers[0].layers[0].codes_buffer; uint32_t* mapped_codes = context->ui.containers[0].layers[0].codes_buffer;
@ -126,13 +159,50 @@ VkResult main_thread(ClientContext* context) {
double last_frame_time = glfwGetTime(); double last_frame_time = glfwGetTime();
while(glfwWindowShouldClose(context->window) == 0) { while(glfwWindowShouldClose(context->window) == 0) {
double frame_time = glfwGetTime(); double frame_time = glfwGetTime();
context->delta_time = (frame_time - last_frame_time); double delta_time = (frame_time - last_frame_time);
context->clicked_element = 0x00000000; context->clicked_element = 0x00000000;
context->clicked_container = 0x00000000; context->clicked_container = 0x00000000;
context->zoom = 0;
context->cur_spin[0] = 0;
context->cur_spin[1] = 0;
glfwPollEvents(); glfwPollEvents();
if(context->key_spin[0] != 0 || context->key_spin[1] != 0 ||
context->zoom != 0 ||
context->cur_spin[0] != 0 || context->cur_spin[1] != 0) {
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;
if(context->rotation[0] > 2*M_PI) {
context->rotation[0] -= 2*M_PI;
} else if(context->rotation[0] < 0) {
context->rotation[0] += 2*M_PI;
}
context->rotation[1] += (float)context->key_spin[1]*delta_time*context->key_spin_speed;
context->rotation[1] += (float)context->cur_spin[1]*delta_time*context->cur_spin_speed;
if(context->rotation[1] > M_PI/2) {
context->rotation[1] = M_PI/2;
} else if(context->rotation[1] < 0) {
context->rotation[1] = 0;
}
context->distance += context->zoom*delta_time*context->zoom_speed;
if(context->distance < 1) {
context->distance = 1;
}
vec3 camera;
camera[0] = context->position[0] + context->distance*cos(context->rotation[1])*cos(context->rotation[0]);
camera[1] = context->position[1] + context->distance*sin(context->rotation[1]);
camera[2] = context->position[2] + context->distance*cos(context->rotation[1])*sin(context->rotation[0]);
glm_lookat(camera, context->position, up, hex.view);
add_transfer(&hex, hex_context, 0, sizeof(GPUHexContext), (context->render.current_frame + 1) % MAX_FRAMES_IN_FLIGHT, &context->render);
}
if(context->clicked_container != 0) { if(context->clicked_container != 0) {
snprintf(str, 21, "Clicked: %d.%d", context->clicked_container, context->clicked_element); snprintf(str, 21, "Clicked: %d.%d", context->clicked_container, context->clicked_element);
map_string(str, mapped_codes, 0, 0, &context->ui); map_string(str, mapped_codes, 0, 0, &context->ui);
@ -166,11 +236,40 @@ VkResult main_thread(ClientContext* context) {
} }
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
(void)window; ClientContext* context = (ClientContext*)glfwGetWindowUserPointer(window);
(void)key;
(void)scancode; (void)scancode;
(void)action;
(void)mods; (void)mods;
switch(key) {
case GLFW_KEY_A:
if(action == GLFW_PRESS) {
context->key_spin[0] -= 1;
} else if(action == GLFW_RELEASE) {
context->key_spin[0] += 1;
}
break;
case GLFW_KEY_D:
if(action == GLFW_PRESS) {
context->key_spin[0] += 1;
} else if(action == GLFW_RELEASE) {
context->key_spin[0] -= 1;
}
break;
case GLFW_KEY_W:
if(action == GLFW_PRESS) {
context->key_spin[1] += 1;
} else if(action == GLFW_RELEASE) {
context->key_spin[1] -= 1;
}
break;
case GLFW_KEY_S:
if(action == GLFW_PRESS) {
context->key_spin[1] -= 1;
} else if(action == GLFW_RELEASE) {
context->key_spin[1] += 1;
}
break;
}
} }
bool contains(double* point, GPUContainer* container, GPUDrawable* rect) { bool contains(double* point, GPUContainer* container, GPUDrawable* rect) {
@ -193,11 +292,13 @@ void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
glfwGetCursorPos(window, &cursor[0], &cursor[1]); glfwGetCursorPos(window, &cursor[0], &cursor[1]);
switch(button) { switch(button) {
// Handle camera hover // Handle camera hover
case GLFW_MOUSE_BUTTON_MIDDLE: case GLFW_MOUSE_BUTTON_RIGHT:
if(action == GLFW_PRESS) { if(action == GLFW_PRESS) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
context->camera_mode = true;
} else { } else {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
context->camera_mode = false;
} }
break; break;
case GLFW_MOUSE_BUTTON_LEFT: case GLFW_MOUSE_BUTTON_LEFT:
@ -225,23 +326,24 @@ void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
// 3. Based on what was clicked, start the mouse click animation at the target area // 3. Based on what was clicked, start the mouse click animation at the target area
} }
break; break;
case GLFW_MOUSE_BUTTON_RIGHT:
if(action == GLFW_PRESS) {
}
break;
} }
} }
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
(void)window; ClientContext* context = (ClientContext*)glfwGetWindowUserPointer(window);
(void)xoffset; (void)xoffset;
(void)yoffset; context->zoom = -yoffset;
} }
void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) { void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) {
(void)window; ClientContext* context = (ClientContext*)glfwGetWindowUserPointer(window);
(void)xpos; if(context->camera_mode == true) {
(void)ypos; context->cur_spin[0] = (xpos - context->cursor[0])/context->render.swapchain_extent.width*-100;
context->cur_spin[1] = (ypos - context->cursor[1])/context->render.swapchain_extent.height*100;
context->cursor[0] = xpos;
context->cursor[1] = ypos;
}
} }
int main() { int main() {