fixed issues in shaders
[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         // skybox_tex->load("data/cubemap/irradiance.hdr");
189         terrain_rend->set_sky_tex(skybox_tex);
190
191         if(!terrain_rend->create()) {
192                 fprintf(stderr, "terrain fail\n");
193                 return false;
194         }
195
196         return true;
197 }
198
199 static void cleanup()
200 {
201         delete sdr_man;
202         delete camera;
203
204         delete cow_scene;
205         delete cow_rend;
206
207 //TODO
208         delete terrain_tex;
209         delete terrain_rend;
210         gfx_cleanup();
211 }
212
213 static float cow_speed = 10;
214 static Vec3 cow_pos;
215 static bool keystate[256];
216
217 static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mods)
218 {
219         if(action == GLFW_REPEAT) return;
220
221         if(action == GLFW_PRESS) {
222                 switch(key) {
223                 case GLFW_KEY_ESCAPE:
224                         glfwSetWindowShouldClose(win, GLFW_TRUE);
225                         return;
226
227                 case ' ':
228                         move_camera = !move_camera;
229                         break;
230
231                 case 'F':
232                         fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1;
233                         break;
234
235                 case 'U':
236                         fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0;
237                         break;
238
239                 default:
240                         break;
241                 }
242         }
243
244         if(key < 256) {
245                 keystate[key] = action == GLFW_PRESS;
246         }
247 }
248
249 static double prev_x, prev_y;
250 static bool button[8];
251
252 static void clbk_motion(GLFWwindow *win, double x, double y)
253 {
254         double dx = x - prev_x;
255         double dy = y - prev_y;
256
257         prev_x = x;
258         prev_y = y;
259
260         if(button[0]) {
261                 cam_theta += dx * 0.5;
262                 cam_phi += dy * 0.5;
263
264                 if(cam_phi < 0)
265                         cam_phi = 0;
266                 if(cam_phi > 90)
267                         cam_phi = 90;
268         }
269
270         if(button[1]) {
271                 cam_dist += dy * 0.1;
272                 if(cam_dist < 0.0) {
273                         cam_dist = 0.0;
274                 }
275         }
276  }
277
278 static void clbk_mouse(GLFWwindow *win, int bn, int action, int mods)
279 {
280         button[bn] = action == GLFW_PRESS;
281         glfwGetCursorPos(win, &prev_x, &prev_y);
282 }
283
284 static void clbk_reshape(GLFWwindow *win, int width, int height)
285 {
286         gfx_viewport(0, 0, width, height);
287         aspect = (float)width / (float)height;
288         mprojection = calc_projection_matrix(45, aspect, 0.5, 1000.0);
289
290         win_h = height;
291         win_w = width;
292 }
293
294 static void update(float dt)
295 {
296         Vec3 dir;
297
298         if(keystate['D'])
299                 dir.x += cow_speed * dt;
300         if(keystate['A'])
301                 dir.x -= cow_speed * dt;
302         if(keystate['W'])
303                 dir.z -= cow_speed * dt;
304         if(keystate['S'])
305                 dir.z += cow_speed * dt;
306
307         Vec3 *pos = move_camera ? &cam_pos : &cow_pos;
308         float theta = cam_theta / 180.0 * M_PI;
309         pos->x += dir.x * cos(theta) - dir.z * sin(theta);
310         pos->z += dir.x * sin(theta) + dir.z * cos(theta);
311 }
312
313 static void display()
314 {
315         static float prev_tsec;
316         time_sec = glfwGetTime();
317         float dt = time_sec - prev_tsec;
318         prev_tsec = time_sec;
319
320         update(dt);
321
322         cam_pos.y = terrain.get_height(cam_pos) + 0.5;
323         camera->set_orbit_params(cam_theta, cam_phi, cam_dist);
324         camera->set_position(cam_pos.x, cam_pos.y, cam_pos.z);
325
326         gfx_clear(0.1, 0.1, 0.1);
327
328         printf("fog_density: %f\n", fog_density);
329         terrain_rend->fog_density = fog_density;
330         terrain_rend->draw();
331
332         cow_pos.y = terrain.get_height(cow_pos);
333         cow_object->transform.translation(cow_pos);
334         cow_rend->fog_density = fog_density;
335         cow_rend->draw();
336 }