483454827981a8dade869d617717ad2ceb9fff8f
[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 Camera *camera;
34
35 /* variables */
36 // TODO: remove just for test:
37 static Scene scene;
38
39 int main(int argc, char **argv)
40 {
41         for(int i=0; i<argc; ++i) {
42                 if(strcmp(argv[i], "-opengl") == 0) {
43                         use_vulkan = false;
44                         printf("Backend: OpenGL.\n");
45                 }
46                 else if(strcmp(argv[i], "-vulkan") == 0) {
47                         use_vulkan = true;
48                         printf("Backend: Vulkan.\n");
49                 }
50                 else {
51                         printf("No backend specified. Using OpenGL.\n");
52                 }
53         }
54
55         if(!init()) {
56                 fprintf(stderr, "Failed to initialize program.\n");
57                 return 1;
58         }
59
60         glfwSetKeyCallback(win, key_clbk);
61         glfwSetCursorPosCallback(win, motion_clbk);
62         glfwSetMouseButtonCallback(win, mouse_clbk);
63
64         while(!glfwWindowShouldClose(win)) {
65                 display();
66
67                 glfwSwapBuffers(win);
68                 glfwPollEvents();
69         }
70
71         atexit(cleanup);
72         return 0;
73 }
74
75 static bool init()
76 {
77         if(use_vulkan) {
78                 if(!init_vulkan())
79                         return false;
80         }
81         else {
82                 if(!init_opengl())
83                         return false;
84         }
85
86         camera = new Camera(25, 25, 4, 45);
87
88         if(!scene.load("data/spot/spot_control_mesh.obj")) {
89                 fprintf(stderr, "Failed to load scene.\n");
90                 return false;
91         }
92
93         for(size_t i=0; i<scene.objects.size(); ++i) {
94                 printf("object: %d\n", (int)i);
95                 printf("mesh: %s\n", scene.objects[i]->mesh->name.c_str());
96                 printf("material: %s\n", scene.objects[i]->material->name.c_str());
97                 printf("transform:\n");
98                 scene.objects[i]->transform.print();
99         }
100         return true;
101 }
102
103 static void cleanup()
104 {
105         if(use_vulkan) {
106                 cleanup_vulkan();
107         }
108         else {
109                 cleanup_opengl();
110         }
111
112         delete camera;
113 }
114
115 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
116 {
117         if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
118                 glfwSetWindowShouldClose(win, GLFW_TRUE);
119         }
120 }
121
122 static double prev_x, prev_y;
123 static int bnstate[8];
124
125 static void motion_clbk(GLFWwindow *win, double x, double y)
126 {
127         int dx = x - prev_x;
128         int dy = y - prev_y;
129
130         prev_x = x;
131         prev_y = y;
132
133         if(!dx && !dy) return;
134
135         if(bnstate[0]) {
136                 camera->theta += dx * 0.5;
137                 camera->phi += dy * 0.5;
138
139                 if(camera->phi < -90) camera->phi = -90;
140                 if(camera->phi > 90) camera->phi = 90;
141         }
142         if(bnstate[2]) {
143                 camera->distance += dy * 0.1;
144                 if(camera->distance < 0.0) camera->distance = 0.0;
145         }
146 }
147
148 static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods)
149 {
150         bnstate[bn - GLFW_MOUSE_BUTTON_LEFT] = action == GLFW_PRESS ? 1 : 0;
151         glfwGetCursorPos(win, &prev_x, &prev_y);
152 }
153
154 static void display()
155 {
156         if(use_vulkan) {
157                 display_vulkan();
158         }
159         else {
160                 display_opengl();
161         }
162 }