backup:
[demo] / src / main.cc
index 4e6677a..1e45c4b 100644 (file)
@@ -3,11 +3,16 @@
 #include <string.h>
 #include <vector>
 
+#include <gmath/gmath.h>
+
+#include "global.h"
+
 /* TODO: fix those */
 #include "camera.h"
 #include "mesh.h"
 #include "object.h"
 #include "scene.h"
+#include "shader_manager.h"
 
 #include "opengl/opengl.h"
 #include "vulkan/vk.h"
@@ -19,20 +24,31 @@ static void cleanup();
 static void display();
 
 /* glfw callbacks */
-static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods);
-static void motion_clbk(GLFWwindow *win, double x, double y);
-static void mouse_clbk(GLFWwindow *win, int button, int action, int mods);
+
+static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
+static void clbk_motion(GLFWwindow *win, double x, double y);
+static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
+static void clbk_reshape(GLFWwindow *win, int width, int height);
 
 /* global variables */
+
 bool use_vulkan;
+Mat4 mprojection;
 
 GLFWwindow *win;
 int win_w = 800;
 int win_h = 600;
 
-Camera *camera;
+OrbitCamera *camera;
+float phi = 25;
+float theta = 0;
+float dist = 4;
+
+ShaderManager *sdr_man;
 
 /* variables */
+static float aspect;
+
 // TODO: remove just for test:
 static Scene scene;
 
@@ -57,9 +73,10 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       glfwSetKeyCallback(win, key_clbk);
-       glfwSetCursorPosCallback(win, motion_clbk);
-       glfwSetMouseButtonCallback(win, mouse_clbk);
+       glfwSetKeyCallback(win, clbk_key);
+       glfwSetCursorPosCallback(win, clbk_motion);
+       glfwSetMouseButtonCallback(win, clbk_mouse);
+       glfwSetWindowSizeCallback(win, clbk_reshape);
 
        while(!glfwWindowShouldClose(win)) {
                display();
@@ -74,13 +91,6 @@ int main(int argc, char **argv)
 
 static bool init()
 {
-       /* TODO */
-       /*
-               TODO changes:
-               1- create cam
-               2- scene
-               3- renderers
-       */
        if(use_vulkan) {
                if(!init_vulkan())
                        return false;
@@ -90,13 +100,17 @@ static bool init()
                        return false;
        }
 
-       camera = new Camera(25, 25, 4, 45);
+       sdr_man = new ShaderManager;
+
+       camera = new OrbitCamera;
+       camera->set_orbit_params(phi, theta, dist);
 
        if(!scene.load("data/spot/spot_control_mesh.obj")) {
                fprintf(stderr, "Failed to load scene.\n");
                return false;
        }
 
+// TODO delete: debugging
        for(size_t i=0; i<scene.objects.size(); ++i) {
                printf("object: %d\n", (int)i);
                printf("mesh: %s\n", scene.objects[i]->mesh->name.c_str());
@@ -109,17 +123,18 @@ static bool init()
 
 static void cleanup()
 {
+       delete sdr_man;
+       delete camera;
+
        if(use_vulkan) {
                cleanup_vulkan();
        }
        else {
                cleanup_opengl();
        }
-
-       delete camera;
 }
 
-static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
+static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
 {
        if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
                glfwSetWindowShouldClose(win, GLFW_TRUE);
@@ -127,37 +142,53 @@ static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mod
 }
 
 static double prev_x, prev_y;
-static int bnstate[8];
+static int button;
 
-static void motion_clbk(GLFWwindow *win, double x, double y)
+static void clbk_motion(GLFWwindow *win, double x, double y)
 {
-       int dx = x - prev_x;
-       int dy = y - prev_y;
+       switch(button) {
+       case GLFW_MOUSE_BUTTON_LEFT:
+               theta += x - prev_x;
+               phi += y - prev_y;
+
+               if(phi < -90)
+                       phi = -90;
+               if(phi > 90)
+                       phi = 90;
+
+               break;
+
+       case GLFW_MOUSE_BUTTON_RIGHT:
+               dist *= (y - prev_y) * 0.01 + 1;
+               if(dist < 0.0) {
+                       dist = 0.0;
+               }
+               break;
+       }
 
        prev_x = x;
        prev_y = y;
-
-       if(!dx && !dy) return;
-
-       if(bnstate[0]) {
-               camera->theta += dx * 0.5;
-               camera->phi += dy * 0.5;
-
-               if(camera->phi < -90) camera->phi = -90;
-               if(camera->phi > 90) camera->phi = 90;
-       }
-       if(bnstate[2]) {
-               camera->distance += dy * 0.1;
-               if(camera->distance < 0.0) camera->distance = 0.0;
-       }
 }
 
-static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods)
+static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
 {
-       bnstate[bn - GLFW_MOUSE_BUTTON_LEFT] = action == GLFW_PRESS ? 1 : 0;
+       button = bn;
        glfwGetCursorPos(win, &prev_x, &prev_y);
 }
 
+static void clbk_reshape(GLFWwindow *win, int width, int height)
+{
+       if(use_vulkan) {
+               //TODO
+               return;
+       }
+       else {
+               glViewport(0, 0, width, height);
+               aspect = (float)width / (float)height;
+               mprojection = calc_projection_matrix(1000, 0.5, aspect, 45);
+       }
+}
+
 static void display()
 {
        if(use_vulkan) {
@@ -165,5 +196,6 @@ static void display()
        }
        else {
                display_opengl();
+               scene.objects[0]->mesh->draw();
        }
 }
\ No newline at end of file