Add code to set centre from mouse click

This commit is contained in:
Camden Dixie O'Brien 2025-01-18 13:03:36 +00:00
parent 6b07203881
commit b1025c9f42

93
main.c
View File

@ -45,6 +45,59 @@ typedef struct {
static const char *layers[] = { "VK_LAYER_KHRONOS_validation" }; static const char *layers[] = { "VK_LAYER_KHRONOS_validation" };
static const char *swapchain_ext_name = VK_KHR_SWAPCHAIN_EXTENSION_NAME; 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 VkShaderModule load_shader_module(VkDevice dev, const char *path)
{ {
static char buf[MAX_SHADER_SIZE] static char buf[MAX_SHADER_SIZE]
@ -69,7 +122,7 @@ static VkShaderModule load_shader_module(VkDevice dev, const char *path)
int main(void) int main(void)
{ {
// Set up window and surface // Set up window
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
@ -77,10 +130,11 @@ int main(void)
const GLFWvidmode *mode = glfwGetVideoMode(monitor); const GLFWvidmode *mode = glfwGetVideoMode(monitor);
GLFWwindow *window = glfwCreateWindow( GLFWwindow *window = glfwCreateWindow(
mode->width, mode->height, "GPU Fractal", monitor, NULL); mode->width, mode->height, "GPU Fractal", monitor, NULL);
/* GLFWwindow *window */
/* = glfwCreateWindow(900, 900, "GPU Fractal", NULL, NULL); */
assert(window); assert(window);
// Register mouse click callback
glfwSetMouseButtonCallback(window, mouse_button_callback);
// Initialise Vulkan instance. // Initialise Vulkan instance.
const VkApplicationInfo app_info = { const VkApplicationInfo app_info = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
@ -476,36 +530,9 @@ int main(void)
result = vkCreateFence(dev, &fence_config, NULL, &in_flight); result = vkCreateFence(dev, &fence_config, NULL, &in_flight);
assert(result == VK_SUCCESS); assert(result == VK_SUCCESS);
// Prepare parameters // Initialise shader parameters
params_t params = { params.width = (float)swapchain_extent.width;
.width = (float)swapchain_extent.width, params.height = (float)swapchain_extent.height;
.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
};
#ifdef JULIA_DTHETA #ifdef JULIA_DTHETA
const double dtheta = JULIA_DTHETA; const double dtheta = JULIA_DTHETA;
const double complex rotz = cexp(I * dtheta); const double complex rotz = cexp(I * dtheta);