added a camera class and created a camera that I am not using yet :p
[vkrt] / src / main.cc
index af4277c..893508f 100644 (file)
@@ -5,11 +5,18 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <gmath/gmath.h>
+
+/* extern "C": vulkan framework */
 #include "vk.h"
 #include "util.h"
 
+/* C++ */
+#include "camera.h"
 
+/**************************/
 /* static glfw callbacks */
+/**************************/
 
 static void
 clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
@@ -17,21 +24,51 @@ clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
 static void
 clb_reshape(GLFWwindow *win, int width, int height);
 
+static void
+clb_motion(GLFWwindow *win, double x, double y);
+
+static void
+clb_mouse(GLFWwindow *win, int button, int action, int mods);
+
+/**************************/
 /* static functions */
+/**************************/
+
+/* init, cleanup, display */
 
 static bool
-init(void);
+init();
 
 static void
-cleanup(void);
+cleanup();
 
 static void
-display(void);
+display();
 
+/* vulkan, glfw */
+
+static bool
+vk_init();
+
+static void
+vk_cleanup();
+
+/**************************/
 /* static variables */
+/**************************/
 
 static GLFWwindow *win;
 static bool redraw_pending;
+static bool move_camera;
+
+/* camera */
+static float cam_phi = 25;
+static float cam_theta = 0;
+static float cam_dist = 16;
+static Vec3 cam_pos;
+
+static float aspect;
+static OrbitCamera *camera;
 
 static bool vk_enable_layers = true;
 
@@ -56,6 +93,11 @@ int main(int argc, char** argv)
 {
     atexit(cleanup);
 
+    if (!glfwInit()) {
+        fprintf(stderr, "Failed to initialize GLFW.\n");
+        return 1;
+    }
+
     if (!init()) {
         return 1;
     }
@@ -76,25 +118,27 @@ int main(int argc, char** argv)
         }
     }
 
+#if 0
+       while(!glfwWindowShouldClose(win)) {
+               display();
+
+               glfwSwapBuffers(win);
+               glfwPollEvents();
+       }
+#endif
+
     return 0;
 }
 
 /* static functions */
 
 static bool
-init(void)
+vk_init()
 {
     char *vsdr = 0;
     char *fsdr = 0;
     int vsz, fsz;
 
-    /* initialize GLFW */
-
-    if (!glfwInit()) {
-        fprintf(stderr, "Failed to initialize GLFW.\n");
-        return false;
-    }
-
     if (glfwVulkanSupported() != GLFW_TRUE) {
         fprintf(stderr, "Vulkan is not supported on this device.\n");
         return false;
@@ -199,12 +243,10 @@ init(void)
 
     /* set GLFW callbacks */
 
-    /* glfwSetWindowSizeCallback(win, clb_reshape); */
+    glfwSetWindowSizeCallback(win, clb_reshape);
 
-    /*
     glfwSetCursorPosCallback(win, clb_motion);
     glfwSetMouseButtonCallback(win, clb_mouse);
-    */
 
     return true;
 
@@ -215,8 +257,26 @@ fail:
     return false;
 }
 
+static bool
+init()
+{
+    if (!vk_init()) {
+        fprintf(stderr, "Failed to initialize Vulkan structs.\n");
+        return false;
+    }
+
+    camera = new OrbitCamera;
+    return true;
+}
+
 static void
-display(void)
+cleanup()
+{
+    vk_cleanup();
+}
+
+static void
+display()
 {
     uint32_t img_idx;
     VkSubmitInfo sinfo;
@@ -253,7 +313,7 @@ display(void)
 }
 
 static void
-cleanup(void)
+vk_cleanup()
 {
     vkQueueWaitIdle(vk_core.queue);
 
@@ -291,11 +351,58 @@ clb_key(GLFWwindow *win, int key, int scancode,
         case GLFW_KEY_ESCAPE:
             glfwSetWindowShouldClose(win, GLFW_TRUE);
             return;
+        case ' ':
+            move_camera = !move_camera;
+            break;
+        default:
+            break;
         }
     }
 }
 
 static void
-clb_reshape(GLFWwindow *win, int width, int height)
+clb_reshape(GLFWwindow *win,
+            int width,
+            int height)
+{
+}
+
+static double prev_x, prev_y;
+static bool button[8];
+
+static void
+clb_motion(GLFWwindow *win,
+           double x,
+           double y)
+{
+       double dx = x - prev_x;
+       double dy = y - prev_y;
+
+       prev_x = x;
+       prev_y = y;
+
+       if(button[0]) {
+               cam_theta += dx * 0.5;
+               cam_phi += dy * 0.5;
+
+               if(cam_phi < 0)
+                       cam_phi = 0;
+               if(cam_phi > 90)
+                       cam_phi = 90;
+       }
+
+       if(button[1]) {
+               cam_dist += dy * 0.1;
+               if(cam_dist < 0.0) {
+                       cam_dist = 0.0;
+               }
+       }
+}
+
+static void
+clb_mouse(GLFWwindow *win,
+          int button,
+          int action,
+          int mods)
 {
 }