b3049bc885dae3a4b6a23403214c7b7105c63e67
[vrfileman] / src / app.cc
1 #include <stdio.h>
2 #include <assert.h>
3 #include "opengl.h"
4 #include "app.h"
5 #include "gmath/gmath.h"
6 #include "mesh.h"
7 #include "meshgen.h"
8 #include "backdrop.h"
9 #include "goatvr.h"
10 #include "opt.h"
11 #include "fs.h"
12 #include "rtarg.h"
13 #include "texture.h"
14 #include "sdr.h"
15
16 static void draw_scene();
17
18 int win_width, win_height;
19 float win_aspect;
20 long time_msec;
21 double time_sec;
22 Mat4 view_matrix;
23
24 float cam_height = 1.65;
25
26 static float cam_theta, cam_phi;
27 static bool should_swap;
28
29 static bool bnstate[16];
30 static int prev_x, prev_y;
31
32 static float fov = 60.0;
33
34 static RenderTarget *rtarg;
35 static bool rtarg_valid;
36 static unsigned int post_sdr;
37
38
39 bool app_init(int argc, char **argv)
40 {
41         if(!init_options(argc, argv, "vrfileman.conf")) {
42                 return false;
43         }
44         app_resize(opt.width, opt.height);
45         app_fullscreen(opt.fullscreen);
46
47         if(init_opengl() == -1) {
48                 return false;
49         }
50
51         glEnable(GL_MULTISAMPLE);
52
53         int aasamples = 0;
54         glGetIntegerv(GL_SAMPLES, &aasamples);
55         printf("got %d samples per pixel\n", aasamples);
56
57         printf("Max anisotropy: %d\n", glcaps.max_aniso);
58
59         glEnable(GL_CULL_FACE);
60         glEnable(GL_DEPTH_TEST);
61
62         if(opt.srgb && GLEW_ARB_framebuffer_sRGB) {
63                 printf("enabling sRGB framebuffer\n");
64                 glEnable(GL_FRAMEBUFFER_SRGB);
65         }
66
67         rtarg = new RenderTarget;
68         rtarg->create(GL_RGB16F);
69
70         if(opt.vr) {
71                 if(goatvr_init() == -1) {
72                         return false;
73                 }
74                 goatvr_set_origin_mode(GOATVR_HEAD);
75
76                 goatvr_startvr();
77                 should_swap = goatvr_should_swap() != 0;
78                 cam_height = goatvr_get_eye_height();
79         }
80
81         if(opt.srgb) {
82                 add_shader_header(GL_FRAGMENT_SHADER, "#define set_pixel set_pixel_linear");
83         } else {
84                 add_shader_header(GL_FRAGMENT_SHADER, "#define set_pixel set_pixel_srgb");
85         }
86         if(!(post_sdr = create_program_load("sdr/post.v.glsl", "sdr/post.p.glsl"))) {
87                 return false;
88         }
89         clear_shader_header(0);
90
91         Mesh::use_custom_sdr_attr = false;
92
93         if(!init_backdrop()) {
94                 return false;
95         }
96
97         if(!init_fs(opt.path)) {
98                 return false;
99         }
100
101         return true;
102 }
103
104 void app_cleanup()
105 {
106         if(opt.vr) {
107                 goatvr_shutdown();
108         }
109         delete rtarg;
110         free_program(post_sdr);
111         cleanup_backdrop();
112 }
113
114 static void update()
115 {
116         if(!rtarg_valid) {
117                 rtarg->resize(win_width, win_height);
118         }
119 }
120
121 void app_draw()
122 {
123         update();
124
125         if(opt.vr) {
126                 // VR mode
127                 goatvr_draw_start();
128                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
129
130                 for(int i=0; i<2; i++) {
131                         goatvr_draw_eye(i);
132
133                         glMatrixMode(GL_PROJECTION);
134                         glLoadMatrixf(goatvr_projection_matrix(i, 0.1, 200.0));
135
136                         view_matrix = goatvr_view_matrix(i);
137                         view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
138                         view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
139                         view_matrix.pre_translate(0, -cam_height, 0);
140
141                         glMatrixMode(GL_MODELVIEW);
142                         glLoadMatrixf(view_matrix[0]);
143
144                         draw_scene();
145                 }
146                 goatvr_draw_done();
147
148                 if(should_swap) {
149                         app_swap_buffers();
150                 }
151                 app_redraw();   // in VR mode, force continuous redraw
152
153         } else {
154                 // regular monoscopic mode
155                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
156
157                 Mat4 mat;
158                 mat.perspective(deg_to_rad(fov), win_aspect, 0.1, 200.0);
159                 glMatrixMode(GL_PROJECTION);
160                 glLoadMatrixf(mat[0]);
161
162                 view_matrix = Mat4::identity;
163                 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
164                 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
165                 view_matrix.pre_translate(0, -cam_height, 0);
166
167                 glMatrixMode(GL_MODELVIEW);
168                 glLoadMatrixf(view_matrix[0]);
169
170                 draw_scene();
171
172                 app_swap_buffers();
173                 app_redraw();   // since we added animation we need to redisplay even in non-VR mode
174         }
175         assert(glGetError() == GL_NO_ERROR);
176 }
177
178 static void draw_scene()
179 {
180         set_render_target(rtarg);
181         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
182
183         draw_backdrop();
184         draw_fs();
185
186         set_render_target(0);
187
188         glPushAttrib(GL_ENABLE_BIT);
189         glDisable(GL_DEPTH_TEST);
190
191         glUseProgram(post_sdr);
192         set_uniform_matrix4(post_sdr, "texmat", rtarg->get_texture_matrix()[0]);
193
194         bind_texture(rtarg->get_texture());
195
196         glBegin(GL_QUADS);
197         glTexCoord2f(0, 0);
198         glVertex2f(-1, -1);
199         glTexCoord2f(1, 0);
200         glVertex2f(1, -1);
201         glTexCoord2f(1, 1);
202         glVertex2f(1, 1);
203         glTexCoord2f(0, 1);
204         glVertex2f(-1, 1);
205         glEnd();
206
207         bind_texture(0);
208         glUseProgram(0);
209
210         glPopAttrib();
211 }
212
213 void app_reshape(int x, int y)
214 {
215         glViewport(0, 0, x, y);
216         rtarg_valid = false;
217
218         if(opt.vr) {
219                 goatvr_set_fb_size(x, y, 1.0);
220         }
221 }
222
223 void app_keyboard(int key, bool pressed)
224 {
225         if(pressed) {
226                 switch(key) {
227                 case 27:
228                         app_quit();
229                         break;
230
231                 case 'f':
232                         if(!opt.vr || should_swap) {
233                                 /* we take the need to swap as a signal that our window is not managed
234                                  * by some VR compositor, and therefore it's safe to fullscreen without
235                                  * upsetting the VR rendering output
236                                  */
237                                 opt.fullscreen = !opt.fullscreen;
238                                 app_fullscreen(opt.fullscreen);
239                         }
240                         break;
241
242                 case ' ':
243                         if(opt.vr) {
244                                 goatvr_recenter();
245                         }
246                         break;
247
248                 case '-':
249                         fov += 1.0;
250                         if(fov > 160.0) fov = 160.0;
251                         break;
252
253                 case '=':
254                         fov -= 1.0;
255                         if(fov < 0.0) fov = 0.0;
256                         break;
257                 }
258         }
259 }
260
261 void app_mouse_button(int bn, bool pressed, int x, int y)
262 {
263         bnstate[bn] = pressed;
264         prev_x = x;
265         prev_y = y;
266 }
267
268 void app_mouse_motion(int x, int y)
269 {
270         int dx = x - prev_x;
271         int dy = y - prev_y;
272         prev_x = x;
273         prev_y = y;
274
275         if(!dx && !dy) return;
276
277         if(bnstate[0]) {
278                 cam_theta += dx * 0.5;
279
280                 if(!opt.vr || !goatvr_have_headtracking()) {
281                         cam_phi += dy * 0.5;
282
283                         if(cam_phi < -90) cam_phi = -90;
284                         if(cam_phi > 90) cam_phi = 90;
285                 }
286         }
287         app_redraw();
288 }