1246d2f6edec544c5188f56eff50ad09c2e5986c
[demo] / src / main.cc
1 #include <GL/glew.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <vector>
7
8 #include <gmath/gmath.h>
9
10 #include "gfxapi.h"
11 #include "global.h"
12
13 /* TODO: fix those */
14 #include "camera.h"
15 #include "mesh.h"
16 #include "morph_renderer.h"
17 #include "object.h"
18 #include "scene.h"
19 #include "terrain.h"
20 #include "texture.h"
21
22 #include "opengl/opengl.h"
23 #include "vulkan/vk.h"
24
25 /* static functions */
26
27 static bool init(Gfx_API api);
28 static void cleanup();
29 static void display();
30
31 /* glfw callbacks */
32
33 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods);
34 static void clbk_motion(GLFWwindow *win, double x, double y);
35 static void clbk_mouse(GLFWwindow *win, int button, int action, int mods);
36 static void clbk_reshape(GLFWwindow *win, int width, int height);
37
38 /* global variables */
39
40 Mat4 mprojection;
41
42 GLFWwindow *win;
43 int win_w = 800;
44 int win_h = 600;
45
46 ShaderManager *sdr_man;
47
48 double time_sec;
49
50 /* variables */
51 static bool move_camera;
52
53 static float cam_phi = 25;
54 static float cam_theta = 0;
55 static float cam_dist = 16;
56 static Vec3 cam_pos;
57
58 static float aspect;
59 static OrbitCamera *camera;
60
61 static Scene *cow_scene;
62 static Object *cow_object;
63 static MorphRenderer *cow_rend;
64
65 static Terrain terrain;
66 static Texture *skybox_tex;
67 static Texture *terrain_tex;
68 static Material terrain_mat;
69 static Renderer *terrain_rend;
70
71 /* *** */
72
73 int main(int argc, char **argv)
74 {
75         Gfx_API api;
76
77         for(int i=0; i<argc; ++i) {
78                 if(strcmp(argv[i], "-opengl") == 0) {
79                         api = GFX_GL;
80                         printf("Backend: OpenGL.\n");
81                 }
82                 else if(strcmp(argv[i], "-vulkan") == 0) {
83                         api = GFX_VK;
84                         printf("Backend: Vulkan.\n");
85                 }
86                 else {
87                         api = GFX_GL;
88                         printf("No backend specified. Using OpenGL.\n");
89                 }
90         }
91
92         if(!init(api)) {
93                 fprintf(stderr, "Failed to initialize program.\n");
94                 return 1;
95         }
96
97         glfwSetKeyCallback(win, clbk_key);
98         glfwSetCursorPosCallback(win, clbk_motion);
99         glfwSetMouseButtonCallback(win, clbk_mouse);
100         glfwSetWindowSizeCallback(win, clbk_reshape);
101
102         glfwGetWindowSize(win, &win_w, &win_h);
103         clbk_reshape(win, win_w, win_h);
104
105         while(!glfwWindowShouldClose(win)) {
106                 display();
107
108                 glfwSwapBuffers(win);
109                 glfwPollEvents();
110         }
111
112         cleanup();
113         // atexit(cleanup);
114         return 0;
115 }
116
117 static bool init(Gfx_API api)
118 {
119         if(!gfx_init(api))
120                 return false;
121
122         sdr_man = new ShaderManager;
123
124         camera = new OrbitCamera;
125
126         cow_scene = new Scene;
127         if(!cow_scene->load("data/spot/spot.obj")) {
128                 fprintf(stderr, "Failed to load scene: spot.obj.\n");
129                 return false;
130         }
131
132         cow_rend = new MorphRenderer;
133         cow_rend->camera = camera;
134         cow_rend->scene = cow_scene;
135
136         if(!cow_rend->create()) {
137                 fprintf(stderr, "Failed to create renderer for cows.\n");
138                 return false;
139         }
140
141         cow_object = cow_scene->objects[0];
142
143         terrain_tex = gfx_create_texture();
144         if(!terrain_tex->load("data/grass.jpeg")) {
145                 fprintf(stderr, "Failed to load ground texture.\n");
146                 return false;
147         }
148
149         Image ter_hmap;
150         if(!ter_hmap.load("data/terhmap.png")) {
151                 fprintf(stderr, "Failed to load terrain heightmap.\n");
152                 return false;
153         }
154
155         TerrainParams p;
156         p.xsz = 200;
157         p.ysz = 200;
158         p.max_height = 30;
159         p.xtiles = 40;
160         p.ytiles = 40;
161         p.tile_usub = 10;
162         p.tile_vsub = 10;
163         p.num_octaves = 3;
164         p.noise_freq = 5;
165         p.coarse_heightmap = ter_hmap;
166
167         terrain.init();
168         terrain.generate(p);
169
170         terrain_mat.diffuse = Vec3(1, 1, 1);
171         terrain_mat.specular = Vec3(0, 0, 0);
172         terrain_mat.shininess = 40;
173         terrain_mat.dtex = terrain_tex;
174         terrain_mat.name = "tt";
175
176         terrain.material = terrain_mat;
177
178         terrain_rend = new Renderer;
179         terrain_rend->camera = camera;
180         terrain_rend->scene = terrain.get_visible(camera);
181
182         skybox_tex = gfx_create_texture();
183         skybox_tex->load("data/cubemap/cubemap.hdr");
184         terrain_rend->set_sky_tex(skybox_tex);
185
186         if(!terrain_rend->create()) {
187                 fprintf(stderr, "terrain fail\n");
188                 return false;
189         }
190
191         return true;
192 }
193
194 static void cleanup()
195 {
196         delete sdr_man;
197         delete camera;
198
199         delete cow_scene;
200         delete cow_rend;
201
202 //TODO
203         delete terrain_tex;
204         delete terrain_rend;
205         gfx_cleanup();
206 }
207
208 static float cow_speed = 10;
209 static Vec3 cow_pos;
210 static bool keystate[256];
211
212 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
213 {
214         if(action == GLFW_REPEAT) return;
215
216         if(action == GLFW_PRESS) {
217                 switch(key) {
218                 case GLFW_KEY_ESCAPE:
219                         glfwSetWindowShouldClose(win, GLFW_TRUE);
220                         return;
221
222                 case ' ':
223                         move_camera = !move_camera;
224                         break;
225
226                 default:
227                         break;
228                 }
229         }
230
231         if(key < 256) {
232                 keystate[key] = action == GLFW_PRESS;
233         }
234 }
235
236 static double prev_x, prev_y;
237 static bool button[8];
238
239 static void clbk_motion(GLFWwindow *win, double x, double y)
240 {
241         double dx = x - prev_x;
242         double dy = y - prev_y;
243
244         prev_x = x;
245         prev_y = y;
246
247         if(button[0]) {
248                 cam_theta += dx * 0.5;
249                 cam_phi += dy * 0.5;
250
251                 if(cam_phi < 0)
252                         cam_phi = 0;
253                 if(cam_phi > 90)
254                         cam_phi = 90;
255         }
256
257         if(button[1]) {
258                 cam_dist += dy * 0.1;
259                 if(cam_dist < 0.0) {
260                         cam_dist = 0.0;
261                 }
262         }
263  }
264
265 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
266 {
267         button[bn] = action == GLFW_PRESS;
268         glfwGetCursorPos(win, &prev_x, &prev_y);
269 }
270
271 static void clbk_reshape(GLFWwindow *win, int width, int height)
272 {
273         gfx_viewport(0, 0, width, height);
274         aspect = (float)width / (float)height;
275         mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
276
277         win_h = height;
278         win_w = width;
279 }
280
281 static void update(float dt)
282 {
283         Vec3 dir;
284
285         if(keystate['D'])
286                 dir.x += cow_speed * dt;
287         if(keystate['A'])
288                 dir.x -= cow_speed * dt;
289         if(keystate['W'])
290                 dir.z -= cow_speed * dt;
291         if(keystate['S'])
292                 dir.z += cow_speed * dt;
293
294         Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
295         float theta = cam_theta / 180.0 * M_PI;
296         pos->x += dir.x * cos(theta) - dir.z * sin(theta);
297         pos->z += dir.x * sin(theta) + dir.z * cos(theta);
298 }
299
300 static void display()
301 {
302         static float prev_tsec;
303         time_sec = glfwGetTime();
304         float dt = time_sec - prev_tsec;
305         prev_tsec = time_sec;
306
307         update(dt);
308
309         cam_pos.y = terrain.get_height(cam_pos) + 0.5;
310         camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
311         camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
312
313         gfx_clear(0.1, 0.1, 0.1);
314
315         terrain_rend->draw();
316
317         cow_pos.y = terrain.get_height(cow_pos);
318         cow_object->transform.translation(cow_pos);
319         cow_rend->draw();
320 }