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