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