This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
forked from KeckCAVES/SARndbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sandbox.h
259 lines (231 loc) · 11.9 KB
/
Sandbox.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/***********************************************************************
Sandbox - Vrui application to drive an augmented reality sandbox.
Copyright (c) 2012-2019 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Augmented Reality Sandbox is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#ifndef SANDBOX_INCLUDED
#define SANDBOX_INCLUDED
#include <Threads/Mutex.h>
#include <Threads/TripleBuffer.h>
#include <Geometry/Box.h>
#include <Geometry/Rotation.h>
#include <Geometry/OrthonormalTransformation.h>
#include <Geometry/ProjectiveTransformation.h>
#include <GL/gl.h>
#include <GL/GLColorMap.h>
#include <GL/GLMaterial.h>
#include <GL/GLObject.h>
#include <GL/GLGeometryVertex.h>
#include <GLMotif/ToggleButton.h>
#include <GLMotif/TextFieldSlider.h>
#include <Vrui/Tool.h>
#include <Vrui/GenericToolFactory.h>
#include <Vrui/TransparentObject.h>
#include <Vrui/Application.h>
#include <Kinect/FrameBuffer.h>
#include <Kinect/FrameSource.h>
#include "Types.h"
/* Forward declarations: */
namespace Misc {
template <class ParameterParam>
class FunctionCall;
}
class GLContextData;
namespace GLMotif {
class PopupMenu;
class PopupWindow;
class TextField;
}
namespace Vrui {
class Lightsource;
}
namespace Kinect {
class Camera;
}
class FrameFilter;
class DepthImageRenderer;
class ElevationColorMap;
class DEM;
class SurfaceRenderer;
class WaterTable2;
class HandExtractor;
typedef Misc::FunctionCall<GLContextData&> AddWaterFunction;
class RemoteServer;
class WaterRenderer;
class Sandbox: public Vrui::Application, public GLObject {
/* Embedded classes: */
private:
typedef Geometry::Box<Scalar, 3> Box; // Type for bounding boxes
typedef Geometry::OrthonormalTransformation<Scalar, 3>
ONTransform; // Type for rigid body transformations
typedef Kinect::FrameSource::DepthCorrection::PixelCorrection
PixelDepthCorrection; // Type for per-pixel depth correction factors
struct DataItem: public GLObject::DataItem {
/* Elements: */
public:
double waterTableTime; // Simulation time stamp of the water table in this OpenGL context
GLsizei shadowBufferSize[2]; // Size of the shadow rendering frame buffer
GLuint shadowFramebufferObject; // Frame buffer object to render shadow maps
GLuint shadowDepthTextureObject; // Depth texture for the shadow rendering frame buffer
/* Constructors and destructors: */
DataItem(void);
virtual ~DataItem(void);
};
struct GridRequest { // Structure representing a request to read back bathymetry and/or water level grids from the GPU
/* Embedded classes: */
public:
typedef void (*CallbackFunction)(GLfloat*, GLfloat*, void*); // Type for callback functions
struct Request { // Structure holding a request's parameters
/* Elements: */
public:
GLfloat* bathymetryBuffer; // Pointer to a buffer to hold the requested bathymetry grid if requested
GLfloat* waterLevelBuffer; // Pointer to a buffer to hold the requested water level grid if requested
CallbackFunction callback; // Function to call when the grid(s) has/have been read back
void* callbackData; // Additional data element to pass to callback function
/* Constructors and destructors: */
Request(void) // Creates an inactive request
: bathymetryBuffer(0), waterLevelBuffer(0), callback(0), callbackData(0) {
}
/* Methods: */
bool isActive(void) const { // Returns true if there is a pending request
return callback != 0;
}
void complete(void) { // Calls the read-back callback
(*callback)(bathymetryBuffer, waterLevelBuffer, callbackData);
}
};
/* Elements: */
Threads::Mutex mutex; // Mutex serializing access to the request structure
Request currentRequest; // The currently pending grid request
/* Constructors and destructors: */
GridRequest(void) { // Creates an inactive grid request
}
/* Methods: */
bool requestGrids(GLfloat* newBathymetryBuffer, GLfloat* newWaterLevelBuffer,
CallbackFunction newCallback, void*
newCallbackData) { // Requests a grid read-back; returns true if request has been granted
Threads::Mutex::Lock lock(mutex);
if (currentRequest.callback == 0) {
currentRequest.bathymetryBuffer = newBathymetryBuffer;
currentRequest.waterLevelBuffer = newWaterLevelBuffer;
currentRequest.callback = newCallback;
currentRequest.callbackData = newCallbackData;
return true;
} else {
return false;
}
}
Request getRequest(void) { // Returns the current grid request and deactivates it
Threads::Mutex::Lock lock(mutex);
Request result = currentRequest;
currentRequest.callback = 0;
return result;
}
};
struct RenderSettings { // Structure to hold per-window rendering settings
/* Elements: */
public:
bool fixProjectorView; // Flag whether to allow viewpoint navigation or always render from the projector's point of view
PTransform
projectorTransform; // The calibrated projector transformation matrix for fixed-projection rendering
bool projectorTransformValid; // Flag whether the projector transformation is valid
bool hillshade; // Flag whether to use augmented reality hill shading
GLMaterial surfaceMaterial; // Material properties to render the surface in hill shading mode
bool useShadows; // Flag whether to use shadows in augmented reality hill shading
ElevationColorMap* elevationColorMap; // Pointer to an elevation color map
bool useContourLines; // Flag whether to draw elevation contour lines
GLfloat contourLineSpacing; // Spacing between adjacent contour lines in cm
bool renderWaterSurface; // Flag whether to render the water surface as a geometric surface
GLfloat waterOpacity; // Opacity factor for water when rendered as texture
SurfaceRenderer* surfaceRenderer; // Surface rendering object for this window
WaterRenderer* waterRenderer; // A renderer to render the water surface as geometry
/* Constructors and destructors: */
RenderSettings(void); // Creates default rendering settings
RenderSettings(const RenderSettings& source); // Copy constructor
~RenderSettings(void); // Destroys rendering settings
/* Methods: */
void loadProjectorTransform(const char*
projectorTransformName); // Loads a projector transformation from the given file
void loadHeightMap(const char* heightMapName); // Loads the selected height map
};
friend class GlobalWaterTool;
friend class LocalWaterTool;
friend class DEMTool;
friend class BathymetrySaverTool;
friend class RemoteServer;
/* Elements: */
private:
RemoteServer*
remoteServer; // A server to stream bathymetry and water level grids to remote clients
Kinect::FrameSource* camera; // The Kinect camera device
unsigned int frameSize[2]; // Width and height of the camera's depth frames
PixelDepthCorrection* pixelDepthCorrection; // Buffer of per-pixel depth correction coefficients
Kinect::FrameSource::IntrinsicParameters cameraIps; // Intrinsic parameters of the Kinect camera
FrameFilter* frameFilter; // Processing object to filter raw depth frames from the Kinect camera
bool pauseUpdates; // Pauses updates of the topography
Threads::TripleBuffer<Kinect::FrameBuffer>
filteredFrames; // Triple buffer for incoming filtered depth frames
DepthImageRenderer* depthImageRenderer; // Object managing the current filtered depth image
ONTransform
boxTransform; // Transformation from camera space to baseplane space (x along long sandbox axis, z up)
Scalar boxSize; // Radius of sphere around sandbox area
Box bbox; // Bounding box around all potential surfaces
WaterTable2* waterTable; // Water flow simulation object
double waterSpeed; // Relative speed of water flow simulation
unsigned int waterMaxSteps; // Maximum number of water simulation steps per frame
GLfloat rainStrength; // Amount of water deposited by rain tools and objects on each water simulation step
HandExtractor* handExtractor; // Object to detect splayed hands above the sand surface to make rain
const AddWaterFunction* addWaterFunction; // Render function registered with the water table
bool addWaterFunctionRegistered; // Flag if the water adding function is currently registered with the water table
mutable GridRequest gridRequest; // Structure holding pending grid read-back requests
std::vector<RenderSettings> renderSettings; // List of per-window rendering settings
Vrui::Lightsource* sun; // An external fixed light source
DEM* activeDem; // The currently active DEM
GLMotif::PopupMenu* mainMenu;
GLMotif::ToggleButton* pauseUpdatesToggle;
GLMotif::PopupWindow* waterControlDialog;
GLMotif::TextFieldSlider* waterSpeedSlider;
GLMotif::TextFieldSlider* waterMaxStepsSlider;
GLMotif::TextField* frameRateTextField;
GLMotif::TextFieldSlider* waterAttenuationSlider;
int controlPipeFd; // File descriptor of an optional named pipe to send control commands to a running AR Sandbox
/* Private methods: */
void rawDepthFrameDispatcher(const Kinect::FrameBuffer&
frameBuffer); // Callback receiving raw depth frames from the Kinect camera; forwards them to the frame filter and rain maker objects
void receiveFilteredFrame(const Kinect::FrameBuffer&
frameBuffer); // Callback receiving filtered depth frames from the filter object
void toggleDEM(DEM* dem); // Sets or toggles the currently active DEM
void addWater(GLContextData& contextData)
const; // Function to render geometry that adds water to the water table
void pauseUpdatesCallback(GLMotif::ToggleButton::ValueChangedCallbackData* cbData);
void showWaterControlDialogCallback(Misc::CallbackData* cbData);
void waterSpeedSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
void waterMaxStepsSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
void waterAttenuationSliderCallback(GLMotif::TextFieldSlider::ValueChangedCallbackData* cbData);
GLMotif::PopupMenu* createMainMenu(void);
GLMotif::PopupWindow* createWaterControlDialog(void);
/* Constructors and destructors: */
public:
Sandbox(int& argc, char**& argv);
virtual ~Sandbox(void);
/* Methods from Vrui::Application: */
virtual void toolDestructionCallback(Vrui::ToolManager::ToolDestructionCallbackData* cbData);
virtual void frame(void);
virtual void display(GLContextData& contextData) const;
virtual void resetNavigation(void);
virtual void eventCallback(EventID eventId, Vrui::InputDevice::ButtonCallbackData* cbData);
/* Methods from GLObject: */
virtual void initContext(GLContextData& contextData) const;
};
#endif