FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
renderer_hmd.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FPLBASE_RENDERER_HMD_H
16 #define FPLBASE_RENDERER_HMD_H
17 
18 #include "fplbase/config.h" // Must come first.
19 
20 #include "fplbase/input.h"
21 #include "fplbase/renderer.h"
22 #include "mathfu/glsl_mappings.h"
23 
24 /// @file fplbase/renderer_hmd.h
25 /// @brief Contains helper functions for interacting with rendering aspects of
26 /// using a Head Mounted Display device, such as Cardboard.
27 
28 namespace fplbase {
29 
30 /// @addtogroup fplbase_renderer
31 /// @{
32 
33 /// @brief Initializes the framebuffer needed for Head Mounted Display
34 /// undistortion.
35 /// @param width The width of the framebuffer.
36 /// @param height The height of the framebuffer.
37 void InitializeUndistortFramebuffer(int width, int height);
38 /// @brief Called before rendering for HMD to set up the framebuffer.
40 /// @brief Called when finished with rendering for HMD, to undistort and render
41 /// the result.
43 
44 /// @brief Called to set whether the Cardboard settings button (gear icon) is
45 /// enabled and rendering.
46 /// @param enabled If the settings button should be enabled.
47 void SetCardboardButtonEnabled(bool enabled);
48 
49 /// Dimensions and transforms for viewport when using stereoscopic rendering.
51  /// Extents of each viewport.
52  mathfu::vec4i viewport_extents[2];
53  /// Transformation matrix for each viewport.
54  mathfu::mat4 viewport_transforms[2];
55 };
56 
57 #if FPLBASE_ANDROID_VR
58 
59 /// @brief Prepare to render to a Head Mounted Display (HMD).
60 /// @param head_mounted_display_input The input object managing the HMD state.
61 /// @param renderer The renderer that is being used to render the scene.
62 /// @param clear_color The color to clear the framebuffer to before rendering.
63 /// @param use_undistortion If undistortion should be applied after rendering.
64 /// @param view_settings The dimensions and transforms for the viewports.
65 void HeadMountedDisplayRenderStart(
66  const HeadMountedDisplayInput& head_mounted_display_input,
67  Renderer* renderer, const mathfu::vec4& clear_color, bool use_undistortion,
68  HeadMountedDisplayViewSettings* view_settings);
69 
70 /// @brief Reset viewport settings, finish applying undistortion effect (if
71 /// enabled) and disable blending.
72 /// @param renderer The renderer that is being used to render the scene.
73 /// @param use_undistortion If the undistortion effect should be used.
74 void HeadMountedDisplayRenderEnd(Renderer* renderer, bool use_undistortion);
75 
76 /// @brief Helper function that wraps the HMD calls, rendering using the given
77 /// callback.
78 ///
79 /// Call render_callback between HeadMountedDisplayRenderStart() and
80 /// HeadMountedDisplayRenderEnd() passing
81 /// HeadMountedDisplayViewSettings.viewport_extents and
82 /// HeadMountedDisplayViewSettings.viewport_transforms as arguments to
83 /// render_callback.
84 ///
85 /// @param input_system The input system managing the game's input.
86 /// @param renderer The renderer that will being used to render the scene.
87 /// @param clear_color The color to clear the framebuffer to before rendering.
88 /// @param render_callback The function to call after setting up each viewport.
89 /// @param use_undistortion If the undistortion effect should be applied.
90 template <typename RenderCallback>
91 void HeadMountedDisplayRender(const InputSystem* input_system,
92  Renderer* renderer,
93  const mathfu::vec4& clear_color,
94  RenderCallback render_callback,
95  bool use_undistortion) {
96  HeadMountedDisplayViewSettings view_settings;
97  HeadMountedDisplayRenderStart(input_system->head_mounted_display_input(),
98  renderer, clear_color, use_undistortion,
99  &view_settings);
100  render_callback(view_settings.viewport_extents,
101  view_settings.viewport_transforms);
102  HeadMountedDisplayRenderEnd(renderer, use_undistortion);
103 }
104 
105 /// @brief Helper function that wraps the HMD calls, rendering using the given
106 /// callback.
107 ///
108 /// Call render_callback between HeadMountedDisplayRenderStart() and
109 /// HeadMountedDisplayRenderEnd() passing
110 /// HeadMountedDisplayViewSettings.viewport_extents and
111 /// HeadMountedDisplayViewSettings.viewport_transforms as arguments to
112 /// render_callback.
113 ///
114 /// @param input_system The input system managing the game's input.
115 /// @param renderer The renderer that will being used to render the scene.
116 /// @param clear_color The color to clear the framebuffer to before rendering.
117 /// @param render_callback The function to call after setting up each viewport.
118 template <typename RenderCallback>
119 void HeadMountedDisplayRender(const InputSystem* input_system,
120  Renderer* renderer,
121  const mathfu::vec4& clear_color,
122  RenderCallback render_callback) {
123  HeadMountedDisplayRender(input_system, renderer, clear_color, render_callback,
124  true);
125 }
126 
127 #endif // FPLBASE_ANDROID_VR
128 
129 /// @}
130 } // namespace fplbase
131 
132 #endif // FPLBASE_RENDERER_CARDBOARD_H
void SetCardboardButtonEnabled(bool enabled)
Called to set whether the Cardboard settings button (gear icon) is enabled and rendering.
mathfu::vec4i viewport_extents[2]
Extents of each viewport.
Definition: renderer_hmd.h:52
void InitializeUndistortFramebuffer(int width, int height)
Initializes the framebuffer needed for Head Mounted Display undistortion.
void BeginUndistortFramebuffer()
Called before rendering for HMD to set up the framebuffer.
Dimensions and transforms for viewport when using stereoscopic rendering.
Definition: renderer_hmd.h:50
Use to handle time, touch/mouse/keyboard/etc input, and lifecyle events.
Definition: input.h:547
mathfu::mat4 viewport_transforms[2]
Transformation matrix for each viewport.
Definition: renderer_hmd.h:54
void FinishUndistortFramebuffer()
Called when finished with rendering for HMD, to undistort and render the result.
Renderer is the main API class for rendering commands.
Definition: renderer.h:310