diff --git a/main.c b/main.c index f26a460..5de8a9c 100644 --- a/main.c +++ b/main.c @@ -45,6 +45,59 @@ typedef struct { static const char *layers[] = { "VK_LAYER_KHRONOS_validation" }; static const char *swapchain_ext_name = VK_KHR_SWAPCHAIN_EXTENSION_NAME; +// Initial parameters +static params_t params = { +#ifdef JULIA + .julia = { +#ifdef JULIA_C_RE + .re = JULIA_C_RE, +#endif +#ifdef JULIA_C_IM + .im = JULIA_C_IM, +#endif + }, +#endif +#if defined(CENTRE_RE) || defined(CENTRE_IM) + .centre = { +#ifdef CENTRE_RE + .re = CENTRE_RE, +#endif +#ifdef CENTRE_IM + .im = CENTRE_IM, +#endif + }, +#endif + .scale = 1.0, +#ifdef POWRATE + .zpow = 2.0, +#endif +}; + +static void +mouse_button_callback(GLFWwindow *window, int button, int action, int mods) +{ + (void)window; + (void)button; + (void)mods; + + if (action == GLFW_RELEASE) + return; + + double x, y; + glfwGetCursorPos(window, &x, &y); + + double zx = 4.0 * (x / params.width - 0.5); + double zy = 4.0 * (y / params.height - 0.5); + zx *= params.width / params.height; + + zx = params.scale * zx + params.centre.re; + zy = params.scale * zy + params.centre.im; + + printf("CLICKED: %.8f + %.8fi\n", zx, zy); + params.centre.re = zx; + params.centre.im = zy; +} + static VkShaderModule load_shader_module(VkDevice dev, const char *path) { static char buf[MAX_SHADER_SIZE] @@ -69,7 +122,7 @@ static VkShaderModule load_shader_module(VkDevice dev, const char *path) int main(void) { - // Set up window and surface + // Set up window glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); @@ -77,10 +130,11 @@ int main(void) const GLFWvidmode *mode = glfwGetVideoMode(monitor); GLFWwindow *window = glfwCreateWindow( mode->width, mode->height, "GPU Fractal", monitor, NULL); - /* GLFWwindow *window */ - /* = glfwCreateWindow(900, 900, "GPU Fractal", NULL, NULL); */ assert(window); + // Register mouse click callback + glfwSetMouseButtonCallback(window, mouse_button_callback); + // Initialise Vulkan instance. const VkApplicationInfo app_info = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, @@ -476,36 +530,9 @@ int main(void) result = vkCreateFence(dev, &fence_config, NULL, &in_flight); assert(result == VK_SUCCESS); - // Prepare parameters - params_t params = { - .width = (float)swapchain_extent.width, - .height = (float)swapchain_extent.height, -#ifdef JULIA - .julia = { -#ifdef JULIA_C_RE - .re = JULIA_C_RE, -#endif -#ifdef JULIA_C_IM - .im = JULIA_C_IM, -#endif - }, -#endif -#if defined(CENTRE_RE) || defined(CENTRE_IM) - .centre = { -#ifdef CENTRE_RE - .re = CENTRE_RE, -#endif -#ifdef CENTRE_IM - .im = CENTRE_IM, -#endif - }, -#endif - .scale = 1.0, -#ifdef POWRATE - .zpow = 2.0, -#endif - }; - + // Initialise shader parameters + params.width = (float)swapchain_extent.width; + params.height = (float)swapchain_extent.height; #ifdef JULIA_DTHETA const double dtheta = JULIA_DTHETA; const double complex rotz = cexp(I * dtheta);