implemented a laser pointer master
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Thu, 25 Aug 2016 03:45:52 +0000 (06:45 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Thu, 25 Aug 2016 03:45:52 +0000 (06:45 +0300)
src/app.cc
src/app.h
src/fs.cc
src/main.cc

index f075761..eaa9630 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <assert.h>
+#include <limits.h>
 #include "opengl.h"
 #include "app.h"
 #include "gmath/gmath.h"
 #include "texture.h"
 #include "sdr.h"
 
+#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;
+Mat4 view_matrix, mouse_view_matrix, proj_matrix;
 
 float cam_height = 1.65;
 
-static float cam_theta, cam_phi;
+static Ray mray;
+static float mtheta, mphi;
+static long last_pointer_time = PTIME_INVAL;
+static float laser_alpha;
+
+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;
@@ -76,7 +88,9 @@ bool app_init(int argc, char **argv)
                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();
        }
 
@@ -100,11 +114,15 @@ bool app_init(int argc, char **argv)
                return false;
        }
 
+       if(opt.vr || opt.fullscreen) {
+               app_grab_mouse(true);
+       }
        return true;
 }
 
 void app_cleanup()
 {
+       app_grab_mouse(false);
        if(opt.vr) {
                goatvr_shutdown();
        }
@@ -118,6 +136,46 @@ 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()
@@ -132,13 +190,11 @@ void app_draw()
                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.1, 200.0));
+                       glLoadMatrixf(proj_matrix[0]);
 
-                       view_matrix = goatvr_view_matrix(i);
-                       view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
-                       view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
-                       view_matrix.pre_translate(0, -cam_height, 0);
+                       view_matrix = mouse_view_matrix * Mat4(goatvr_view_matrix(i));
 
                        glMatrixMode(GL_MODELVIEW);
                        glLoadMatrixf(view_matrix[0]);
@@ -156,15 +212,11 @@ void app_draw()
                // regular monoscopic mode
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-               Mat4 mat;
-               mat.perspective(deg_to_rad(fov), win_aspect, 0.1, 200.0);
+               proj_matrix.perspective(deg_to_rad(fov), win_aspect, 0.1, 200.0);
                glMatrixMode(GL_PROJECTION);
-               glLoadMatrixf(mat[0]);
+               glLoadMatrixf(proj_matrix[0]);
 
-               view_matrix = Mat4::identity;
-               view_matrix.pre_rotate_x(deg_to_rad(cam_phi));
-               view_matrix.pre_rotate_y(deg_to_rad(cam_theta));
-               view_matrix.pre_translate(0, -cam_height, 0);
+               view_matrix = mouse_view_matrix;
 
                glMatrixMode(GL_MODELVIEW);
                glLoadMatrixf(view_matrix[0]);
@@ -186,6 +238,9 @@ static void draw_scene()
 
        draw_backdrop();
        draw_fs();
+       if(laser_alpha > 0.0) {
+               draw_laser();
+       }
 
        if(!opt.vr) {
                set_render_target(0);
@@ -216,6 +271,28 @@ static void draw_scene()
        }
 }
 
+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);
@@ -260,6 +337,11 @@ void app_keyboard(int key, bool pressed)
                        fov -= 1.0;
                        if(fov < 0.0) fov = 0.0;
                        break;
+
+               case '`':
+                       app_toggle_grab_mouse();
+                       mtheta = mphi = 0;
+                       break;
                }
        }
 }
@@ -267,28 +349,47 @@ void app_keyboard(int key, bool pressed)
 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(!opt.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;
-               }
+       } else {
+               mtheta += dx * 0.05;
+               mphi = clamp(mphi - dy * 0.05f, -90.0f, 90.0f);
+
+               last_pointer_time = time_msec;
        }
-       app_redraw();
+
+       //app_redraw();
 }
index 97f8f8b..83eaf5a 100644 (file)
--- a/src/app.h
+++ b/src/app.h
@@ -21,10 +21,16 @@ void app_reshape(int x, int y);
 void app_keyboard(int key, bool pressed);
 void app_mouse_button(int bn, bool pressed, int x, int y);
 void app_mouse_motion(int x, int y);
+void app_mouse_delta(int dx, int dy);
 
 // the following functions are implemented by the window system backend
 void app_resize(int x, int y);
 void app_fullscreen(bool fs);
+void app_toggle_fullscreen();
+bool app_is_fullscreen();
+void app_grab_mouse(bool grab);
+void app_toggle_grab_mouse();
+bool app_is_mouse_grabbed();
 void app_quit();
 void app_redraw();
 void app_swap_buffers();
index aedeacd..3db0012 100644 (file)
--- a/src/fs.cc
+++ b/src/fs.cc
@@ -107,6 +107,7 @@ void draw_fs()
        static const float radius = 0.6;
        static const float umax = 0.42;
        static const float max_icon_angle = M_PI * 2.0 * umax;
+       static const float first_row_y = -row_spacing;
 
        int max_ncols = std::max<int>(1, umax * 12);
 
@@ -152,7 +153,7 @@ void draw_fs()
 
                Mat4 xform;
                xform.rotate_y(angle);
-               xform.translate(0, -0.3, 0);
+               xform.translate(0, -0.6, 0);
 
                glPushMatrix();
                glMultMatrixf(xform[0]);
@@ -167,9 +168,9 @@ void draw_fs()
                glTexCoord2f(1, 0);
                glVertex3f(0.2, 0, 0.05);
                glTexCoord2f(1, 1);
-               glVertex3f(0.2, 0, -5.0);
+               glVertex3f(0.2, 0, -10.0);
                glTexCoord2f(0, 1);
-               glVertex3f(-0.2, 0, -5.0);
+               glVertex3f(-0.2, 0, -10.0);
                glColor3f(1, 1, 1);
                glEnd();
                glPopMatrix();
@@ -189,7 +190,7 @@ void draw_fs()
 
                float angle = (float)col++ / (float)(num_dirs - 1) * max_icon_angle - max_icon_angle * 0.5;
 
-               draw_node_name(node, angle, -0.3, radius, false);
+               draw_node_name(node, angle, -0.6, radius * 1.2, false);
        }
 
        // then draw file icons
@@ -209,7 +210,7 @@ void draw_fs()
                float angle = icon_angle(col, ncols, max_icon_angle);
 
                Mat4 xform = rot_xform;
-               xform.translate(0, row * row_spacing, -radius);
+               xform.translate(0, row * row_spacing + first_row_y, -radius);
                xform.rotate_y(angle);
 
                glPushMatrix();
@@ -239,7 +240,7 @@ void draw_fs()
 
                float angle = icon_angle(col, ncols, max_icon_angle);
 
-               draw_node_name(node, angle, row * row_spacing - 0.1, radius, false);
+               draw_node_name(node, angle, row * row_spacing + first_row_y - 0.08, radius, false);
 
                if(++col >= ncols) {
                        col = 0;
@@ -260,11 +261,11 @@ static void draw_node_name(FSNode *node, float angle, float ypos, float dist, bo
                const char *name = full ? node->name_lines[i].c_str() : node->short_name.c_str();
                glPushMatrix();
                Mat4 xform;
-               xform.translate(-dtx_string_width(name) / 2.0, -line_height * i, 0);
+               xform.translate(-dtx_string_width(name) / 2.0, -line_height * i - line_height * 0.5, 0);
                if(node->type == FSTYPE_DIR) {
                        xform.rotate_z(deg_to_rad(90));
                        xform.rotate_x(deg_to_rad(-90));
-                       xform.scale(0.0017);
+                       xform.scale(0.0018);
                } else {
                        xform.scale(0.0012);
                }
index 82454fd..d236a3b 100644 (file)
@@ -8,6 +8,7 @@ static void process_event(SDL_Event *ev);
 static SDL_Window *win;
 static SDL_GLContext ctx;
 static bool redraw_pending = true;
+static bool fullscreen, mouse_grabbed;
 static bool quit;
 
 static int scale_factor = 1;
@@ -96,6 +97,36 @@ void app_resize(int x, int y)
 void app_fullscreen(bool fs)
 {
        SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
+       fullscreen = fs;
+}
+
+void app_toggle_fullscreen()
+{
+       app_fullscreen(!fullscreen);
+}
+
+bool app_is_fullscreen()
+{
+       return fullscreen;
+}
+
+void app_grab_mouse(bool grab)
+{
+       /*SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
+       SDL_ShowCursor(grab ? 1 : 0);
+       */
+       SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
+       mouse_grabbed = grab;
+}
+
+void app_toggle_grab_mouse()
+{
+       app_grab_mouse(!mouse_grabbed);
+}
+
+bool app_is_mouse_grabbed()
+{
+       return mouse_grabbed;
 }
 
 void app_quit()
@@ -137,7 +168,11 @@ static void process_event(SDL_Event *ev)
                break;
 
        case SDL_MOUSEMOTION:
-               app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
+               if(mouse_grabbed) {
+                       app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
+               } else {
+                       app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
+               }
                break;
 
        case SDL_WINDOWEVENT: