1 /***********************************************************************
2 Sandbox - Vrui application to drive an augmented reality sandbox.
3 Copyright (c) 2012-2018 Oliver Kreylos
4 Copyright (c) 2020 Scottsdale Community College
6 This file is part of the Augmented Reality Sandbox (SARndbox).
8 The Augmented Reality Sandbox is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The Augmented Reality Sandbox is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with the Augmented Reality Sandbox; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ***********************************************************************/
23 #ifndef SANDBOX_INCLUDED
24 #define SANDBOX_INCLUDED
26 #include <Threads/TripleBuffer.h>
27 #include <Geometry/Box.h>
28 #include <Geometry/Rotation.h>
29 #include <Geometry/OrthonormalTransformation.h>
30 #include <Geometry/ProjectiveTransformation.h>
32 #include <GL/GLColorMap.h>
33 #include <GL/GLMaterial.h>
34 #include <GL/GLObject.h>
35 #include <GL/GLGeometryVertex.h>
36 #include <GLMotif/ToggleButton.h>
37 #include <GLMotif/TextFieldSlider.h>
38 #include <Vrui/Tool.h>
39 #include <Vrui/GenericToolFactory.h>
40 #include <Vrui/TransparentObject.h>
41 #include <Vrui/Application.h>
42 #include <Kinect/FrameBuffer.h>
43 #include <Kinect/FrameSource.h>
47 /* Forward declarations: */
49 template <class ParameterParam
>
65 class DepthImageRenderer
;
66 class ElevationColorMap
;
68 class SurfaceRenderer
;
70 class ContourLineExtractor
;
72 typedef Misc::FunctionCall
<GLContextData
&> AddWaterFunction
;
75 class Sandbox
: public Vrui::Application
, public GLObject
{
76 /* Embedded classes: */
78 typedef Geometry::Box
<Scalar
, 3> Box
; // Type for bounding boxes
79 typedef Geometry::OrthonormalTransformation
<Scalar
, 3> ONTransform
; // Type for rigid body transformations
80 typedef Kinect::FrameSource::DepthCorrection::PixelCorrection PixelDepthCorrection
; // Type for per-pixel depth correction factors
82 struct DataItem
: public GLObject::DataItem
{
85 double waterTableTime
; // Simulation time stamp of the water table in this OpenGL context
86 GLsizei shadowBufferSize
[2]; // Size of the shadow rendering frame buffer
87 GLuint shadowFramebufferObject
; // Frame buffer object to render shadow maps
88 GLuint shadowDepthTextureObject
; // Depth texture for the shadow rendering frame buffer
90 /* Constructors and destructors: */
92 virtual ~DataItem(void);
95 struct RenderSettings
{ // Structure to hold per-window rendering settings
98 bool fixProjectorView
; // Flag whether to allow viewpoint navigation or always render from the projector's point of view
100 projectorTransform
; // The calibrated projector transformation matrix for fixed-projection rendering
101 bool projectorTransformValid
; // Flag whether the projector transformation is valid
102 bool hillshade
; // Flag whether to use augmented reality hill shading
103 GLMaterial surfaceMaterial
; // Material properties to render the surface in hill shading mode
104 bool useShadows
; // Flag whether to use shadows in augmented reality hill shading
105 ElevationColorMap
* elevationColorMap
; // Pointer to an elevation color map
106 bool useContourLines
; // Flag whether to draw elevation contour lines
107 GLfloat contourLineSpacing
; // Spacing between adjacent contour lines in cm
108 bool renderWaterSurface
; // Flag whether to render the water surface as a geometric surface
109 GLfloat waterOpacity
; // Opacity factor for water when rendered as texture
110 SurfaceRenderer
* surfaceRenderer
; // Surface rendering object for this window
111 WaterRenderer
* waterRenderer
; // A renderer to render the water surface as geometry
113 /* Constructors and destructors: */
114 RenderSettings(void); // Creates default rendering settings
115 RenderSettings(const RenderSettings
& source
); // Copy constructor
116 ~RenderSettings(void); // Destroys rendering settings
119 void loadProjectorTransform(const char*
120 projectorTransformName
); // Loads a projector transformation from the given file
121 void loadHeightMap(const char* heightMapName
); // Loads the selected height map
124 friend class GlobalWaterTool
;
125 friend class LocalWaterTool
;
126 friend class DEMTool
;
130 Kinect::FrameSource
* camera
; // The Kinect camera device
131 unsigned int frameSize
[2]; // Width and height of the camera's depth frames
132 PixelDepthCorrection
* pixelDepthCorrection
; // Buffer of per-pixel depth correction coefficients
133 Kinect::FrameSource::IntrinsicParameters cameraIps
; // Intrinsic parameters of the Kinect camera
134 FrameFilter
* frameFilter
; // Processing object to filter raw depth frames from the Kinect camera
135 bool pauseUpdates
; // Pauses updates of the topography
136 Threads::TripleBuffer
<Kinect::FrameBuffer
>
137 filteredFrames
; // Triple buffer for incoming filtered depth frames
138 DepthImageRenderer
* depthImageRenderer
; // Object managing the current filtered depth image
139 ONTransform boxTransform
; // Transformation from camera space to baseplane space (x along long sandbox axis, z up)
140 Scalar boxSize
; // Radius of sphere around sandbox area
141 Box bbox
; // Bounding box around all potential surfaces
142 WaterTable2
* waterTable
; // Water flow simulation object
143 double waterSpeed
; // Relative speed of water flow simulation
144 unsigned int waterMaxSteps
; // Maximum number of water simulation steps per frame
145 GLfloat rainStrength
; // Amount of water deposited by rain tools and objects on each water simulation step
146 ContourLineExtractor
* contourLineExtractor
; // Object to find major contour lines for labelling
147 HandExtractor
* handExtractor
; // Object to detect splayed hands above the sand surface to make rain
148 const AddWaterFunction
* addWaterFunction
; // Render function registered with the water table
149 bool addWaterFunctionRegistered
; // Flag if the water adding function is currently registered with the water table
150 std::vector
<RenderSettings
> renderSettings
; // List of per-window rendering settings
151 Vrui::Lightsource
* sun
; // An external fixed light source
152 DEM
* activeDem
; // The currently active DEM
153 GLMotif::PopupMenu
* mainMenu
;
154 GLMotif::ToggleButton
* pauseUpdatesToggle
;
155 GLMotif::PopupWindow
* waterControlDialog
;
156 GLMotif::TextFieldSlider
* waterSpeedSlider
;
157 GLMotif::TextFieldSlider
* waterMaxStepsSlider
;
158 GLMotif::TextField
* frameRateTextField
;
159 GLMotif::TextFieldSlider
* waterAttenuationSlider
;
160 int controlPipeFd
; // File descriptor of an optional named pipe to send control commands to a running AR Sandbox
162 /* Private methods: */
163 void rawDepthFrameDispatcher(const Kinect::FrameBuffer
&
164 frameBuffer
); // Callback receiving raw depth frames from the Kinect camera; forwards them to the frame filter and rain maker objects
165 void receiveFilteredFrame(const Kinect::FrameBuffer
&
166 frameBuffer
); // Callback receiving filtered depth frames from the filter object
167 void toggleDEM(DEM
* dem
); // Sets or toggles the currently active DEM
168 void addWater(GLContextData
& contextData
) const; // Function to render geometry that adds water to the water table
169 void addContourLabel(GLContextData
& contextData
) const; // Function to render geometry that adds a contour label
170 void pauseUpdatesCallback(GLMotif::ToggleButton::ValueChangedCallbackData
* cbData
);
171 void showWaterControlDialogCallback(Misc::CallbackData
* cbData
);
172 void waterSpeedSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData
* cbData
);
173 void waterMaxStepsSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData
* cbData
);
174 void waterAttenuationSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData
* cbData
);
175 GLMotif::PopupMenu
* createMainMenu(void);
176 GLMotif::PopupWindow
* createWaterControlDialog(void);
178 /* Constructors and destructors: */
180 Sandbox(int& argc
, char**& argv
);
181 virtual ~Sandbox(void);
183 /* Methods from Vrui::Application: */
184 virtual void toolDestructionCallback(Vrui::ToolManager::ToolDestructionCallbackData
* cbData
);
185 virtual void frame(void);
186 virtual void display(GLContextData
& contextData
) const;
187 virtual void resetNavigation(void);
188 virtual void eventCallback(EventID eventId
, Vrui::InputDevice::ButtonCallbackData
* cbData
);
190 /* Methods from GLObject: */
191 virtual void initContext(GLContextData
& contextData
) const;