quick backup:
[demo] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <vector>
5
6 /* TODO: fix those */
7 #include "camera.h"
8 #include "mesh.h"
9 #include "object.h"
10 #include "scene.h"
11
12 #include "opengl/opengl.h"
13 #include "vulkan/vk.h"
14
15 /* static functions */
16
17 static bool init();
18 static void cleanup();
19 static void display();
20
21 /* glfw callbacks */
22 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods);
23 static void motion_clbk(GLFWwindow *win, double x, double y);
24 static void mouse_clbk(GLFWwindow *win, int button, int action, int mods);
25
26 /* global variables */
27 bool use_vulkan;
28
29 GLFWwindow *win;
30 int win_w = 800;
31 int win_h = 600;
32
33 OrbitCamera *camera;
34
35 /* variables */
36 static float phi = 25;
37 static float theta = 0;
38 static float dist = 4;
39
40 // TODO: remove just for test:
41 static Scene scene;
42
43 int main(int argc, char **argv)
44 {
45         for(int i=0; i<argc; ++i) {
46                 if(strcmp(argv[i], "-opengl") == 0) {
47                         use_vulkan = false;
48                         printf("Backend: OpenGL.\n");
49                 }
50                 else if(strcmp(argv[i], "-vulkan") == 0) {
51                         use_vulkan = true;
52                         printf("Backend: Vulkan.\n");
53                 }
54                 else {
55                         printf("No backend specified. Using OpenGL.\n");
56                 }
57         }
58
59         if(!init()) {
60                 fprintf(stderr, "Failed to initialize program.\n");
61                 return 1;
62         }
63
64         glfwSetKeyCallback(win, key_clbk);
65         glfwSetCursorPosCallback(win, motion_clbk);
66         glfwSetMouseButtonCallback(win, mouse_clbk);
67
68         while(!glfwWindowShouldClose(win)) {
69                 display();
70
71                 glfwSwapBuffers(win);
72                 glfwPollEvents();
73         }
74
75         atexit(cleanup);
76         return 0;
77 }
78
79 static bool init()
80 {
81         /* TODO */
82         /*
83                 TODO changes:
84                 1- create cam
85                 2- scene
86                 3- renderers
87         */
88         if(use_vulkan) {
89                 if(!init_vulkan())
90                         return false;
91         }
92         else {
93                 if(!init_opengl())
94                         return false;
95         }
96
97         camera = new OrbitCamera;
98         camera->set_orbit_params(phi, theta, dist);
99
100         if(!scene.load("data/spot/spot_control_mesh.obj")) {
101                 fprintf(stderr, "Failed to load scene.\n");
102                 return false;
103         }
104
105         for(size_t i=0; i<scene.objects.size(); ++i) {
106                 printf("object: %d\n", (int)i);
107                 printf("mesh: %s\n", scene.objects[i]->mesh->name.c_str());
108                 printf("material: %s\n", scene.objects[i]->material->name.c_str());
109                 printf("transform:\n");
110                 scene.objects[i]->transform.print();
111         }
112         return true;
113 }
114
115 static void cleanup()
116 {
117         if(use_vulkan) {
118                 cleanup_vulkan();
119         }
120         else {
121                 cleanup_opengl();
122         }
123
124         delete camera;
125 }
126
127 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
128 {
129         if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
130                 glfwSetWindowShouldClose(win, GLFW_TRUE);
131         }
132 }
133
134 static double prev_x, prev_y;
135 static int button;
136
137 static void motion_clbk(GLFWwindow *win, double x, double y)
138 {
139         switch(button) {
140         case GLFW_MOUSE_BUTTON_LEFT:
141                 theta += x - prev_x;
142                 phi += y - prev_y;
143
144                 if(phi < -90)
145                         phi = -90;
146                 if(phi > 90)
147                         phi = 90;
148
149                 break;
150
151         case GLFW_MOUSE_BUTTON_RIGHT:
152                 dist *= (y - prev_y) * 0.01 + 1;
153                 if(dist < 0.0) {
154                         dist = 0.0;
155                 }
156                 break;
157         }
158
159         prev_x = x;
160         prev_y = y;
161 }
162
163 static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods)
164 {
165         button = bn;
166         glfwGetCursorPos(win, &prev_x, &prev_y);
167 }
168
169 static void display()
170 {
171         if(use_vulkan) {
172                 display_vulkan();
173         }
174         else {
175                 display_opengl();
176         }
177 }