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