4e6677adbe09132acd32c9cc8c8bd65e3ce0ef4b
[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         /* TODO */
78         /*
79                 TODO changes:
80                 1- create cam
81                 2- scene
82                 3- renderers
83         */
84         if(use_vulkan) {
85                 if(!init_vulkan())
86                         return false;
87         }
88         else {
89                 if(!init_opengl())
90                         return false;
91         }
92
93         camera = new Camera(25, 25, 4, 45);
94
95         if(!scene.load("data/spot/spot_control_mesh.obj")) {
96                 fprintf(stderr, "Failed to load scene.\n");
97                 return false;
98         }
99
100         for(size_t i=0; i<scene.objects.size(); ++i) {
101                 printf("object: %d\n", (int)i);
102                 printf("mesh: %s\n", scene.objects[i]->mesh->name.c_str());
103                 printf("material: %s\n", scene.objects[i]->material->name.c_str());
104                 printf("transform:\n");
105                 scene.objects[i]->transform.print();
106         }
107         return true;
108 }
109
110 static void cleanup()
111 {
112         if(use_vulkan) {
113                 cleanup_vulkan();
114         }
115         else {
116                 cleanup_opengl();
117         }
118
119         delete camera;
120 }
121
122 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
123 {
124         if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
125                 glfwSetWindowShouldClose(win, GLFW_TRUE);
126         }
127 }
128
129 static double prev_x, prev_y;
130 static int bnstate[8];
131
132 static void motion_clbk(GLFWwindow *win, double x, double y)
133 {
134         int dx = x - prev_x;
135         int dy = y - prev_y;
136
137         prev_x = x;
138         prev_y = y;
139
140         if(!dx && !dy) return;
141
142         if(bnstate[0]) {
143                 camera->theta += dx * 0.5;
144                 camera->phi += dy * 0.5;
145
146                 if(camera->phi < -90) camera->phi = -90;
147                 if(camera->phi > 90) camera->phi = 90;
148         }
149         if(bnstate[2]) {
150                 camera->distance += dy * 0.1;
151                 if(camera->distance < 0.0) camera->distance = 0.0;
152         }
153 }
154
155 static void mouse_clbk(GLFWwindow *win, int bn, int action, int mods)
156 {
157         bnstate[bn - GLFW_MOUSE_BUTTON_LEFT] = action == GLFW_PRESS ? 1 : 0;
158         glfwGetCursorPos(win, &prev_x, &prev_y);
159 }
160
161 static void display()
162 {
163         if(use_vulkan) {
164                 display_vulkan();
165         }
166         else {
167                 display_opengl();
168         }
169 }