continuing the avatar class
[laserbrain_demo] / src / app.cc
index b61b0e7..764d248 100644 (file)
@@ -15,6 +15,7 @@
 #include "opt.h"
 #include "post.h"
 #include "renderer.h"
+#include "rtarg.h"
 #include "avatar.h"
 #include "vrinput.h"
 #include "exman.h"
@@ -33,6 +34,7 @@ static Ray calc_pick_ray(int x, int y);
 
 long time_msec;
 int win_width, win_height;
+int vp_width, vp_height;
 float win_aspect;
 bool fb_srgb;
 bool opt_gear_wireframe;
@@ -40,10 +42,10 @@ bool opt_gear_wireframe;
 TextureSet texman;
 SceneSet sceneman;
 
-unsigned int sdr_ltmap, sdr_ltmap_notex;
-
 int fpexcept_enabled;
 
+unsigned int dbg_key_pending;
+
 static Avatar avatar;
 
 static float cam_dist = 0.0;
@@ -81,6 +83,7 @@ static ExhibitSlot exslot_left, exslot_right;
 #define exslot_mouse exslot_right
 
 static Renderer *rend;
+static RenderTarget *goatvr_rtarg;
 
 static Ray last_pick_ray;
 
@@ -122,6 +125,8 @@ bool app_init(int argc, char **argv)
                have_handtracking = goatvr_have_handtracking();
 
                goatvr_recenter();
+
+               goatvr_rtarg = new RenderTarget;
        }
 
        if(fb_srgb) {
@@ -166,34 +171,27 @@ bool app_init(int argc, char **argv)
        avatar.body_rot = rad_to_deg(acos(dot(dir, Vec3(0, 0, 1))));
 
        exman = new ExhibitManager;
-       /*
-       if(!exman->load(mscn, "data/exhibits")) {
-               //return false;
-       }
-       */
+       // exhibits are loaded in post_scene_init, because they need access to the scene graph
+
        if(!exui_init()) {
                error_log("failed to initialize exhibit ui system\n");
                return false;
        }
        exui_setnode(&exslot_left.node);
 
-       if(!(sdr_ltmap_notex = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-notex.p.glsl"))) {
-               return false;
-       }
-       set_uniform_int(sdr_ltmap_notex, "texmap", MTL_TEX_DIFFUSE);
-       set_uniform_int(sdr_ltmap_notex, "lightmap", MTL_TEX_LIGHTMAP);
-
-       if(!(sdr_ltmap = create_program_load("sdr/lightmap.v.glsl", "sdr/lightmap-tex.p.glsl"))) {
-               return false;
-       }
-       set_uniform_int(sdr_ltmap, "texmap", MTL_TEX_DIFFUSE);
-       set_uniform_int(sdr_ltmap, "lightmap", MTL_TEX_LIGHTMAP);
-
        if(!fb_srgb) {
                sdr_post_gamma = create_program_load("sdr/post_gamma.v.glsl", "sdr/post_gamma.p.glsl");
        }
 
        rend = new Renderer;
+       if(!rend->init()) {
+               return false;
+       }
+       if(opt.reflect) {
+               rend->ropt |= RENDER_MIRRORS;
+       } else {
+               rend->ropt &= ~RENDER_MIRRORS;
+       }
        rend->set_scene(mscn);
 
        glUseProgram(0);
@@ -208,8 +206,11 @@ bool app_init(int argc, char **argv)
        return true;
 }
 
+// post_scene_init is called after the scene has completed loading
 static void post_scene_init()
 {
+       mscn->update(0);        // update once to calculate node matrices
+
        int num_mir = mscn->calc_mirror_planes();
        info_log("found %d mirror planes\n", num_mir);
 
@@ -225,6 +226,7 @@ void app_cleanup()
 
        app_grab_mouse(false);
        if(opt.vr) {
+               delete goatvr_rtarg;
                goatvr_shutdown();
        }
        destroy_vrhands();
@@ -276,6 +278,21 @@ static void update(float dt)
        exman->update(dt);
        exui_update(dt);
 
+       // use goatvr sticks for joystick input
+       int num_vr_sticks = goatvr_num_sticks();
+       if(num_vr_sticks > 0) {
+               float p[2];
+               goatvr_stick_pos(0, p);
+               joy_move.x = p[0];
+               joy_move.y = -p[1];
+       }
+       if(num_vr_sticks > 1) {
+               float p[2];
+               goatvr_stick_pos(1, p);
+               joy_look.x = p[0];
+       }
+
+
        float speed = walk_speed * dt;
        Vec3 dir;
 
@@ -414,7 +431,7 @@ void app_display()
                ImGui::GetIOPtr()->DeltaTime = dt;
                ImGui::NewFrame();
 
-               ImGui::ShowTestWindow();
+               //ImGui::ShowTestWindow();
        }
 
        glClearColor(1, 1, 1, 1);
@@ -424,11 +441,23 @@ void app_display()
                goatvr_draw_start();
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
+               unsigned int gfbo = goatvr_get_fbo();
+
                update(dt);
 
                for(int i=0; i<2; i++) {
                        // for each eye
                        goatvr_draw_eye(i);
+                       if(gfbo) {
+                               vp_width = goatvr_get_fb_eye_width(i);
+                               vp_height = goatvr_get_fb_eye_height(i);
+
+                               // this is a lightweight operation
+                               goatvr_rtarg->create_wrap_fbo(gfbo, vp_width, vp_height);
+                               push_render_target(goatvr_rtarg, RT_FAKE);
+                       } else {
+                               vp_width = win_width / 2;
+                       }
 
                        proj_matrix = goatvr_projection_matrix(i, NEAR_CLIP, FAR_CLIP);
                        glMatrixMode(GL_PROJECTION);
@@ -446,9 +475,17 @@ void app_display()
                        if(debug_gui) {
                                ImGui::Render();
                        }
+
+                       if(gfbo) {
+                               pop_render_target(RT_FAKE);
+                       }
                }
+
                goatvr_draw_done();
 
+               vp_width = win_width;
+               vp_height = win_height;
+
                if(should_swap) {
                        app_swap_buffers();
                }
@@ -555,6 +592,9 @@ void app_reshape(int x, int y)
        glViewport(0, 0, x, y);
        goatvr_set_fb_size(x, y, 1.0f);
        debug_gui_reshape(x, y);
+
+       vp_width = x;
+       vp_height = y;
 }
 
 void app_keyboard(int key, bool pressed)
@@ -659,6 +699,13 @@ void app_keyboard(int key, bool pressed)
                case KEY_RIGHT:
                        exui_change_tab(1);
                        break;
+
+               case KEY_F5:
+               case KEY_F6:
+               case KEY_F7:
+               case KEY_F8:
+                       dbg_key_pending |= 1 << (key - KEY_F5);
+                       break;
                }
        }