diff --git a/src/main.c b/src/main.c index 2e9f2b4..75620d3 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -77,7 +78,7 @@ typedef struct MaterialStruct { VkPipelineLayout pipeline_layout; VkPipeline pipeline; - VkDescriptorSet material_descriptors; + VkDescriptorPool descriptor_pool; } Material; typedef struct VulkanContextStruct { @@ -1265,7 +1266,7 @@ Texture load_texture(VkPhysicalDevice physical_device, VkDevice device, VkComman .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, .anisotropyEnable = VK_TRUE, - .maxAnisotropy = 2, + .maxAnisotropy = 2.0f, .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK, .unnormalizedCoordinates = VK_FALSE, .compareEnable = VK_FALSE, @@ -2098,19 +2099,19 @@ VulkanContext* init_vulkan(GLFWwindow* window, uint32_t max_frames_in_flight) { return context; } -vec3 world_position = {0.0f, 0.0f, 0.0f}; -float rotation = 0.0f; struct { - bool forward: 1; - bool backward: 1; - bool left: 1; - bool right: 1; - bool up: 1; - bool down: 1; - bool turn_left: 1; - bool turn_right: 1; - bool turn_up: 1; - bool turn_down: 1; + bool forward; + bool backward; + bool left; + bool right; + bool up; + bool down; + bool turn_left; + bool turn_right; + bool turn_up; + bool turn_down; + bool roll_left; + bool roll_right; } key_flags = { .forward = false, .backward = false, @@ -2121,6 +2122,8 @@ struct { .turn_right = false, .turn_up = false, .turn_down = false, + .roll_left = false, + .roll_right = false, }; void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { @@ -2208,18 +2211,37 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod key_flags.turn_down = false; } break; + + case GLFW_KEY_Q: + if(action == GLFW_PRESS) { + key_flags.roll_left = true; + } else if(action == GLFW_RELEASE) { + key_flags.roll_left = false; + } + break; + + case GLFW_KEY_E: + if(action == GLFW_PRESS) { + key_flags.roll_right = true; + } else if(action == GLFW_RELEASE) { + key_flags.roll_right = false; + } + break; } } -VkResult update_scene_ubo(void** buffers, uint32_t frame_index, vec3 world_position, float* rotation, float aspect_ratio, float time_delta) { +vec3 world_position = {0.0f, 0.0f, 0.0f}; +versor world_rotation = {-1.0f, 0.0f, 0.0f, 0.0f}; + +VkResult update_scene_ubo(void** buffers, uint32_t frame_index, vec3 world_position, versor world_rotation, float aspect_ratio, float time_delta) { vec3 movement_sum = {0.0f, 0.0f, 0.0f}; if(key_flags.forward) { - movement_sum[2] += 1 * time_delta; + movement_sum[2] -= 1 * time_delta; } if(key_flags.backward) { - movement_sum[2] -= 1 * time_delta; + movement_sum[2] += 1 * time_delta; } if(key_flags.left) { @@ -2231,36 +2253,68 @@ VkResult update_scene_ubo(void** buffers, uint32_t frame_index, vec3 world_posit } if(key_flags.up) { - movement_sum[1] += 1 * time_delta; + movement_sum[1] -= 1 * time_delta; } if(key_flags.down) { - movement_sum[1] -= 1 * time_delta; + movement_sum[1] += 1 * time_delta; } + + vec3 eular_rotation = {0.0f, 0.0f, 0.0f}; if(key_flags.turn_right) { - *rotation -= 1 * time_delta; + eular_rotation[0] += 1 * time_delta; } if(key_flags.turn_left) { - *rotation += 1 * time_delta; + eular_rotation[0] -= 1 * time_delta; } - vec3 forward = {0.0f, 0.0f, 1.0f}; + if(key_flags.turn_up) { + eular_rotation[1] -= 1 * time_delta; + } + + if(key_flags.turn_down) { + eular_rotation[1] += 1 * time_delta; + } + + if(key_flags.roll_right) { + eular_rotation[2] += 1 * time_delta; + } + + if(key_flags.roll_left) { + eular_rotation[2] -= 1 * time_delta; + } + + vec3 left = {1.0f, 0.0f, 0.0f}; vec3 up = {0.0f, -1.0f, 0.0f}; + vec3 forward = {0.0f, 0.0f, 1.0f}; + glm_quat_rotatev(world_rotation, left, left); + glm_quat_rotatev(world_rotation, up, up); + glm_quat_rotatev(world_rotation, forward, forward); - vec3 dir; - mat4 rot; - glm_rotate_make(rot, *rotation, up); - glm_mat4_mulv3(rot, forward, 1.0f, dir); + versor relative_rotation_y; + glm_quatv(relative_rotation_y, eular_rotation[1], left); + + versor relative_rotation_x; + glm_quatv(relative_rotation_x, eular_rotation[0], up); + + versor relative_rotation_z; + glm_quatv(relative_rotation_z, eular_rotation[2], forward); + + glm_quat_mul(relative_rotation_x, world_rotation, world_rotation); + glm_quat_mul(relative_rotation_y, world_rotation, world_rotation); + glm_quat_mul(relative_rotation_z, world_rotation, world_rotation); vec3 movement_rot; - glm_mat4_mulv3(rot, movement_sum, 1.0f, movement_rot); + glm_quat_rotatev(world_rotation, movement_sum, movement_rot); glm_vec3_add(movement_rot, world_position, world_position); struct SceneUBO ubo = {}; + glm_perspective(90.0f, aspect_ratio, 0.1, 100, ubo.proj); - glm_look(world_position, dir, up, ubo.view); + glm_quat_look(world_position, world_rotation, ubo.view); + //glm_look(world_position, dir, up, ubo.view); memcpy(buffers[frame_index], (void*)&ubo, sizeof(ubo)); @@ -2268,7 +2322,7 @@ VkResult update_scene_ubo(void** buffers, uint32_t frame_index, vec3 world_posit } VkResult draw_frame(VulkanContext* context) { - update_scene_ubo(context->scene_ubo_ptrs, context->current_frame, world_position, &rotation, (float)context->swapchain_extent.width/(float)context->swapchain_extent.height, 0.01); + update_scene_ubo(context->scene_ubo_ptrs, context->current_frame, world_position, world_rotation, (float)context->swapchain_extent.width/(float)context->swapchain_extent.height, 0.01); VkResult result; result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX);