#include <stdio.h>
#include <assert.h>
+#include <limits.h>
#include "opengl.h"
#include "app.h"
#include "gmath/gmath.h"
#include "meshgen.h"
#include "backdrop.h"
#include "goatvr.h"
+#include "opt.h"
+#include "fs.h"
+#include "rtarg.h"
+#include "texture.h"
+#include "sdr.h"
-static bool parse_args(int argc, char **argv);
+#define LASER_TIMEOUT 2000
+#define PTIME_INVAL -LASER_TIMEOUT
+
+static void draw_scene();
+static void draw_laser();
int win_width, win_height;
float win_aspect;
long time_msec;
+double time_sec;
+Mat4 view_matrix, mouse_view_matrix, proj_matrix;
-static bool use_vr;
-static bool should_swap;
+float cam_height = 1.65;
+
+static Ray mray;
+static float mtheta, mphi;
+static long last_pointer_time = PTIME_INVAL;
+static float laser_alpha;
-static float cam_theta, cam_phi;
-static float cam_height = 1.65;
-static Mesh *mesh_torus;
+static float cam_theta, cam_phi = 15;
+static bool should_swap;
static bool bnstate[16];
-static int prev_x, prev_y;
+static int mouse_x, mouse_y;
+
+static float fov = 60.0;
+
+static bool have_headtracking;
+
+static RenderTarget *rtarg;
+static bool rtarg_valid;
+static unsigned int post_sdr;
+
bool app_init(int argc, char **argv)
{
- if(!parse_args(argc, argv)) {
+ if(!init_options(argc, argv, "vrfileman.conf")) {
return false;
}
+ app_resize(opt.width, opt.height);
+ app_fullscreen(opt.fullscreen);
+
if(init_opengl() == -1) {
return false;
}
glGetIntegerv(GL_SAMPLES, &aasamples);
printf("got %d samples per pixel\n", aasamples);
+ printf("Max anisotropy: %d\n", glcaps.max_aniso);
+
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
- //glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- if(GLEW_ARB_framebuffer_sRGB) {
+ if(opt.srgb && GLEW_ARB_framebuffer_sRGB) {
+ printf("enabling sRGB framebuffer\n");
glEnable(GL_FRAMEBUFFER_SRGB);
}
- if(use_vr) {
+ rtarg = new RenderTarget;
+ rtarg->create(GL_RGB16F);
+
+ if(opt.vr) {
if(goatvr_init() == -1) {
return false;
}
goatvr_startvr();
should_swap = goatvr_should_swap() != 0;
cam_height = goatvr_get_eye_height();
+ have_headtracking = goatvr_have_headtracking();
+
+ goatvr_recenter();
+ RenderTarget::default_fbo = goatvr_get_fbo();
}
- Mesh::use_custom_sdr_attr = false;
+ if(opt.srgb) {
+ add_shader_header(GL_FRAGMENT_SHADER, "#define set_pixel set_pixel_linear");
+ } else {
+ add_shader_header(GL_FRAGMENT_SHADER, "#define set_pixel set_pixel_srgb");
+ }
+ if(!(post_sdr = create_program_load("sdr/post.v.glsl", "sdr/post.p.glsl"))) {
+ return false;
+ }
+ clear_shader_header(0);
- mesh_torus = new Mesh;
- gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
+ Mesh::use_custom_sdr_attr = false;
if(!init_backdrop()) {
return false;
}
+ if(!init_fs(opt.path)) {
+ return false;
+ }
+
+ if(opt.vr || opt.fullscreen) {
+ app_grab_mouse(true);
+ }
return true;
}
void app_cleanup()
{
- if(use_vr) {
+ app_grab_mouse(false);
+ if(opt.vr) {
goatvr_shutdown();
}
- delete mesh_torus;
+ delete rtarg;
+ free_program(post_sdr);
cleanup_backdrop();
}
+static void update()
+{
+ if(!rtarg_valid) {
+ rtarg->resize(win_width, win_height);
+ }
+
+ /* calculate the mouselook view matrix */
+ mouse_view_matrix = Mat4::identity;
+ if(!have_headtracking) {
+ mouse_view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
+ }
+ mouse_view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
+ mouse_view_matrix.pre_translate(0, -cam_height, 0);
+
+ long interval = time_msec - last_pointer_time;
+ if(interval < LASER_TIMEOUT) {
+ Vec3 target;
+
+ if(have_headtracking) {
+ target.x = sin(deg_to_rad(mtheta)) * cos(deg_to_rad(mphi)) * 200.0;
+ target.y = sin(deg_to_rad(mphi)) * 200.0;
+ target.z = -cos(deg_to_rad(mtheta)) * cos(deg_to_rad(mphi)) * 200.0;
+
+ mray.origin = inverse(view_matrix) * Vec3(0.2, -0.4, 0.0);
+ } else {
+ /* pick on a distant sphere to find where the mouse is pointing to
+ * and use that as the other end of the mouse ray
+ */
+ float px = 2.0 * (float)mouse_x / win_width - 1.0;
+ float py = 1.0 - (2.0 * (float)mouse_y / win_height);
+
+ Mat4 mvp_inv = inverse(mouse_view_matrix * proj_matrix);
+ Vec4 vfar = mvp_inv * Vec4(px, py, 1, 1);
+
+ target = vfar.xyz() / vfar.w;
+
+ mray.origin = Vec3(0, cam_height, 0);
+ }
+
+ mray.dir = target - mray.origin;
+
+ laser_alpha = 1.0 - std::max(4.0f * interval / LASER_TIMEOUT - 3.0f, 0.0f);
+ } else {
+ laser_alpha = 0.0f;
+ }
+}
+
void app_draw()
{
- if(use_vr) {
+ update();
+
+ if(opt.vr) {
// VR mode
goatvr_draw_start();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int i=0; i<2; i++) {
goatvr_draw_eye(i);
+ proj_matrix = goatvr_projection_matrix(i, 0.1, 200.0);
glMatrixMode(GL_PROJECTION);
- glLoadMatrixf(goatvr_projection_matrix(i, 0.5, 1000.0));
+ glLoadMatrixf(proj_matrix[0]);
- Mat4 view_mat = goatvr_view_matrix(i);
- view_mat.pre_rotate_x(deg_to_rad(cam_phi));
- view_mat.pre_rotate_y(deg_to_rad(cam_theta));
- view_mat.pre_translate(0, -cam_height, 0);
+ view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
glMatrixMode(GL_MODELVIEW);
- glLoadMatrixf(view_mat[0]);
+ glLoadMatrixf(view_matrix[0]);
- draw_backdrop();
+ draw_scene();
}
goatvr_draw_done();
// regular monoscopic mode
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- Mat4 view_mat;
- view_mat.pre_rotate_x(deg_to_rad(cam_phi));
- view_mat.pre_rotate_y(deg_to_rad(cam_theta));
- view_mat.pre_translate(0, -cam_height, 0);
+ proj_matrix.perspective(deg_to_rad(fov), win_aspect, 0.1, 200.0);
+ glMatrixMode(GL_PROJECTION);
+ glLoadMatrixf(proj_matrix[0]);
+
+ view_matrix = mouse_view_matrix;
glMatrixMode(GL_MODELVIEW);
- glLoadMatrixf(view_mat[0]);
+ glLoadMatrixf(view_matrix[0]);
- draw_backdrop();
+ draw_scene();
app_swap_buffers();
+ app_redraw(); // since we added animation we need to redisplay even in non-VR mode
}
assert(glGetError() == GL_NO_ERROR);
}
+static void draw_scene()
+{
+ if(!opt.vr) {
+ set_render_target(rtarg);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ }
+
+ draw_backdrop();
+ draw_fs();
+ if(laser_alpha > 0.0) {
+ draw_laser();
+ }
+
+ if(!opt.vr) {
+ set_render_target(0);
+
+ glPushAttrib(GL_ENABLE_BIT);
+ glDisable(GL_DEPTH_TEST);
+
+ glUseProgram(post_sdr);
+ set_uniform_matrix4(post_sdr, "texmat", rtarg->get_texture_matrix()[0]);
+
+ bind_texture(rtarg->get_texture());
+
+ glBegin(GL_QUADS);
+ glTexCoord2f(0, 0);
+ glVertex2f(-1, -1);
+ glTexCoord2f(1, 0);
+ glVertex2f(1, -1);
+ glTexCoord2f(1, 1);
+ glVertex2f(1, 1);
+ glTexCoord2f(0, 1);
+ glVertex2f(-1, 1);
+ glEnd();
+
+ bind_texture(0);
+ glUseProgram(0);
+
+ glPopAttrib();
+ }
+}
+
+static void draw_laser()
+{
+ glPushAttrib(GL_ENABLE_BIT);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
+ glDepthMask(0);
+ glUseProgram(0);
+
+ glLineWidth(2.0);
+ glBegin(GL_LINES);
+ glColor4f(1.0, 0.3, 0.2, laser_alpha);
+ glVertex3f(mray.origin.x, mray.origin.y, mray.origin.z);
+ Vec3 end = mray.origin + mray.dir;
+ glVertex3f(end.x, end.y, end.z);
+ glEnd();
+ glLineWidth(1.0);
+
+ glDepthMask(1);
+ glPopAttrib();
+}
+
void app_reshape(int x, int y)
{
glViewport(0, 0, x, y);
+ rtarg_valid = false;
- Mat4 mat;
- mat.perspective(deg_to_rad(50), win_aspect, 0.5, 500.0);
-
- glMatrixMode(GL_PROJECTION);
- glLoadMatrixf(mat[0]);
+ if(opt.vr) {
+ goatvr_set_fb_size(x, y, 1.0);
+ }
}
void app_keyboard(int key, bool pressed)
app_quit();
break;
+ case 'f':
+ if(!opt.vr || should_swap) {
+ /* we take the need to swap as a signal that our window is not managed
+ * by some VR compositor, and therefore it's safe to fullscreen without
+ * upsetting the VR rendering output
+ */
+ opt.fullscreen = !opt.fullscreen;
+ app_fullscreen(opt.fullscreen);
+ }
+ break;
+
case ' ':
- if(use_vr) {
+ if(opt.vr) {
goatvr_recenter();
}
break;
+
+ case '-':
+ fov += 1.0;
+ if(fov > 160.0) fov = 160.0;
+ break;
+
+ case '=':
+ fov -= 1.0;
+ if(fov < 0.0) fov = 0.0;
+ break;
+
+ case '`':
+ app_toggle_grab_mouse();
+ mtheta = mphi = 0;
+ break;
}
}
}
void app_mouse_button(int bn, bool pressed, int x, int y)
{
bnstate[bn] = pressed;
- prev_x = x;
- prev_y = y;
+ mouse_x = x;
+ mouse_y = y;
}
void app_mouse_motion(int x, int y)
{
- int dx = x - prev_x;
- int dy = y - prev_y;
- prev_x = x;
- prev_y = y;
+ int dx = x - mouse_x;
+ int dy = y - mouse_y;
+
+ app_mouse_delta(dx, dy);
+
+ mouse_x = x;
+ mouse_y = y;
+}
+template <typename T>
+static T clamp(T x, T a, T b)
+{
+ return x < a ? a : (b < x ? b : x);
+}
+
+void app_mouse_delta(int dx, int dy)
+{
if(!dx && !dy) return;
+ mouse_x = clamp(mouse_x + dx, 0, win_width);
+ mouse_y = clamp(mouse_y + dy, 0, win_height);
+
if(bnstate[0]) {
cam_theta += dx * 0.5;
+ cam_phi = clamp(cam_phi + dy * 0.5f, -90.0f, 90.0f);
- if(!use_vr || !goatvr_have_headtracking()) {
- cam_phi += dy * 0.5;
+ mtheta = mphi = 0;
+ last_pointer_time = PTIME_INVAL;
- if(cam_phi < -90) cam_phi = -90;
- if(cam_phi > 90) cam_phi = 90;
- }
- }
- app_redraw();
-}
+ } else {
+ mtheta += dx * 0.05;
+ mphi = clamp(mphi - dy * 0.05f, -90.0f, 90.0f);
-static bool parse_args(int argc, char **argv)
-{
- for(int i=1; i<argc; i++) {
- if(argv[i][0] == '-') {
- if(strcmp(argv[i], "-vr") == 0) {
- use_vr = true;
-
- } else if(strcmp(argv[i], "-novr") == 0) {
- use_vr = false;
-
- } else if(strcmp(argv[i], "-help") == 0) {
- printf("usage: %s [options]\noptions:\n", argv[0]);
- printf(" -vr/-novr: enable/disable VR\n");
- printf(" -help: print usage information and exit\n");
- exit(0);
- } else {
- fprintf(stderr, "invalid option: %s\n", argv[i]);
- return false;
- }
- } else {
- fprintf(stderr, "unexpected option: %s\n", argv[i]);
- return false;
- }
+ last_pointer_time = time_msec;
}
- return true;
+
+ //app_redraw();
}