5 #include "gmath/gmath.h"
16 static void draw_scene();
18 int win_width, win_height;
24 float cam_height = 1.65;
26 static float cam_theta, cam_phi;
27 static bool should_swap;
29 static bool bnstate[16];
30 static int prev_x, prev_y;
32 static float fov = 60.0;
34 static RenderTarget *rtarg;
35 static bool rtarg_valid;
36 static unsigned int post_sdr;
39 bool app_init(int argc, char **argv)
41 if(!init_options(argc, argv, "vrfileman.conf")) {
44 app_resize(opt.width, opt.height);
45 app_fullscreen(opt.fullscreen);
47 if(init_opengl() == -1) {
51 glEnable(GL_MULTISAMPLE);
54 glGetIntegerv(GL_SAMPLES, &aasamples);
55 printf("got %d samples per pixel\n", aasamples);
57 printf("Max anisotropy: %d\n", glcaps.max_aniso);
59 glEnable(GL_CULL_FACE);
60 glEnable(GL_DEPTH_TEST);
62 if(opt.srgb && GLEW_ARB_framebuffer_sRGB) {
63 printf("enabling sRGB framebuffer\n");
64 glEnable(GL_FRAMEBUFFER_SRGB);
67 rtarg = new RenderTarget;
68 rtarg->create(GL_RGB16F);
71 if(goatvr_init() == -1) {
74 goatvr_set_origin_mode(GOATVR_HEAD);
77 should_swap = goatvr_should_swap() != 0;
78 cam_height = goatvr_get_eye_height();
80 RenderTarget::default_fbo = goatvr_get_fbo();
84 add_shader_header(GL_FRAGMENT_SHADER, "#define set_pixel set_pixel_linear");
86 add_shader_header(GL_FRAGMENT_SHADER, "#define set_pixel set_pixel_srgb");
88 if(!(post_sdr = create_program_load("sdr/post.v.glsl", "sdr/post.p.glsl"))) {
91 clear_shader_header(0);
93 Mesh::use_custom_sdr_attr = false;
95 if(!init_backdrop()) {
99 if(!init_fs(opt.path)) {
112 free_program(post_sdr);
119 rtarg->resize(win_width, win_height);
130 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132 for(int i=0; i<2; i++) {
135 glMatrixMode(GL_PROJECTION);
136 glLoadMatrixf(goatvr_projection_matrix(i, 0.1, 200.0));
138 view_matrix = goatvr_view_matrix(i);
139 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
140 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
141 view_matrix.pre_translate(0, -cam_height, 0);
143 glMatrixMode(GL_MODELVIEW);
144 glLoadMatrixf(view_matrix[0]);
153 app_redraw(); // in VR mode, force continuous redraw
156 // regular monoscopic mode
157 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
160 mat.perspective(deg_to_rad(fov), win_aspect, 0.1, 200.0);
161 glMatrixMode(GL_PROJECTION);
162 glLoadMatrixf(mat[0]);
164 view_matrix = Mat4::identity;
165 view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
166 view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
167 view_matrix.pre_translate(0, -cam_height, 0);
169 glMatrixMode(GL_MODELVIEW);
170 glLoadMatrixf(view_matrix[0]);
175 app_redraw(); // since we added animation we need to redisplay even in non-VR mode
177 assert(glGetError() == GL_NO_ERROR);
180 static void draw_scene()
183 set_render_target(rtarg);
184 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
191 set_render_target(0);
193 glPushAttrib(GL_ENABLE_BIT);
194 glDisable(GL_DEPTH_TEST);
196 glUseProgram(post_sdr);
197 set_uniform_matrix4(post_sdr, "texmat", rtarg->get_texture_matrix()[0]);
199 bind_texture(rtarg->get_texture());
219 void app_reshape(int x, int y)
221 glViewport(0, 0, x, y);
225 goatvr_set_fb_size(x, y, 1.0);
229 void app_keyboard(int key, bool pressed)
238 if(!opt.vr || should_swap) {
239 /* we take the need to swap as a signal that our window is not managed
240 * by some VR compositor, and therefore it's safe to fullscreen without
241 * upsetting the VR rendering output
243 opt.fullscreen = !opt.fullscreen;
244 app_fullscreen(opt.fullscreen);
256 if(fov > 160.0) fov = 160.0;
261 if(fov < 0.0) fov = 0.0;
267 void app_mouse_button(int bn, bool pressed, int x, int y)
269 bnstate[bn] = pressed;
274 void app_mouse_motion(int x, int y)
281 if(!dx && !dy) return;
284 cam_theta += dx * 0.5;
286 if(!opt.vr || !goatvr_have_headtracking()) {
289 if(cam_phi < -90) cam_phi = -90;
290 if(cam_phi > 90) cam_phi = 90;