dc3440d8cb72359ce54f46dccd31658d464de52b
[demo] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <vector>
5
6 #include "object.h"
7 #include "mesh.h"
8 #include "scene.h"
9
10 #include "opengl/opengl.h"
11 #include "vulkan/vk.h"
12
13 /* static functions */
14
15 static bool init();
16 static void cleanup();
17 static void display();
18
19 /* glfw callbacks */
20 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods);
21
22 /* global variables */
23 bool use_vulkan;
24 GLFWwindow *win;
25
26 /* variables */
27 static Scene scene;
28
29 int main(int argc, char **argv)
30 {
31         for(int i=0; i<argc; ++i) {
32                 if(strcmp(argv[i], "-opengl") == 0) {
33                         use_vulkan = false;
34                         printf("Backend: OpenGL.\n");
35                 }
36                 else if(strcmp(argv[i], "-vulkan") == 0) {
37                         use_vulkan = true;
38                         printf("Backend: Vulkan.\n");
39                 }
40                 else {
41                         printf("No backend specified. Using OpenGL.\n");
42                 }
43         }
44
45         if(!init()) {
46                 fprintf(stderr, "Failed to initialize program.\n");
47                 return 1;
48         }
49
50         glfwSetKeyCallback(win, key_clbk);
51
52         while(!glfwWindowShouldClose(win)) {
53                 display();
54
55                 glfwSwapBuffers(win);
56                 glfwPollEvents();
57         }
58
59         atexit(cleanup);
60         return 0;
61 }
62
63 static bool init()
64 {
65         if(use_vulkan) {
66                 if(!init_vulkan())
67                         return false;
68         }
69         else {
70                 if(!init_opengl())
71                         return false;
72         }
73
74         if(!scene.load("data/spot/spot_control_mesh.obj")) {
75                 fprintf(stderr, "Failed to load scene.\n");
76                 return false;
77         }
78
79         for(size_t i=0; i<scene.objects.size(); ++i) {
80                 printf("object: %d\n", (int)i);
81                 printf("mesh: %s\n", scene.objects[i]->mesh->name.c_str());
82                 printf("material: %s\n", scene.objects[i]->material->name.c_str());
83                 printf("transform:\n");
84                 scene.objects[i]->transform.print();
85         }
86         return true;
87 }
88
89 static void cleanup()
90 {
91         if(use_vulkan) {
92                 cleanup_vulkan();
93         }
94         else {
95                 cleanup_opengl();
96         }
97 }
98
99 static void key_clbk(GLFWwindow *win, int key, int scancode, int action, int mods)
100 {
101         if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
102                 glfwSetWindowShouldClose(win, GLFW_TRUE);
103         }
104 }
105
106 static void display()
107 {
108         if(use_vulkan) {
109                 display_vulkan();
110         }
111         else {
112                 display_opengl();
113         }
114 }