1 /***********************************************************************
2 DepthImageRenderer - Class to centralize storage of raw or filtered
3 depth images on the GPU, and perform simple repetitive rendering tasks
4 such as rendering elevation values into a frame buffer.
5 Copyright (c) 2014-2018 Oliver Kreylos
7 This file is part of the Augmented Reality Sandbox (SARndbox).
9 The Augmented Reality Sandbox is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 The Augmented Reality Sandbox is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with the Augmented Reality Sandbox; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 ***********************************************************************/
24 #ifndef DEPTHIMAGERENDERER_INCLUDED
25 #define DEPTHIMAGERENDERER_INCLUDED
28 #include <GL/Extensions/GLARBShaderObjects.h>
29 #include <GL/GLObject.h>
30 #include <GL/GLGeometryVertex.h>
31 #include <Kinect/FrameBuffer.h>
32 #include <Kinect/FrameSource.h>
36 class DepthImageRenderer
: public GLObject
{
37 /* Embedded classes: */
39 typedef GLGeometry::Vertex
<void, 0, void, 0, void, GLfloat
, 2>
40 Vertex
; // Type for template vertices
42 struct DataItem
: public GLObject::DataItem
{ // Structure storing per-context OpenGL state
46 /* OpenGL state management: */
47 GLuint vertexBuffer
; // ID of vertex buffer object holding surface's template vertices
48 GLuint indexBuffer
; // ID of index buffer object holding surface's triangles
49 GLuint depthTexture
; // ID of texture object holding surface's vertex elevations in depth image space
50 unsigned int depthTextureVersion
; // Version number of the depth image texture
52 /* GLSL shader management: */
53 GLhandleARB depthShader
; // Shader program to render the surface's depth only
54 GLint depthShaderUniforms
[2]; // Locations of the depth shader's uniform variables
56 elevationShader
; // Shader program to render the surface's elevation relative to a plane
57 GLint elevationShaderUniforms
[4]; // Locations of the elevation shader's uniform variables
59 /* Constructors and destructors: */
61 virtual ~DataItem(void);
65 unsigned int depthImageSize
[2]; // Size of depth image texture
66 Kinect::LensDistortion lensDistortion
; // 2D lens distortion parameters
67 PTransform depthProjection
; // Projection matrix from depth image space into 3D camera space
68 GLfloat depthProjectionMatrix
[16]; // Same, in GLSL-compatible format
69 GLfloat weightDicEq
[4]; // Equation to calculate the weight of a depth image-space point in 3D camera space
70 Plane basePlane
; // Base plane to calculate surface elevation
71 GLfloat basePlaneDicEq
[4]; // Base plane equation in depth image space in GLSL-compatible format
73 /* Transient state: */
74 Kinect::FrameBuffer depthImage
; // The most recent float-pixel depth image
75 unsigned int depthImageVersion
; // Version number of the depth image
77 /* Constructors and destructors: */
79 DepthImageRenderer(const unsigned int
80 sDepthImageSize
[2]); // Creates an elevation renderer for the given depth image size
82 /* Methods from GLObject: */
83 virtual void initContext(GLContextData
& contextData
) const;
86 const unsigned int* getDepthImageSize(void) const { // Returns the depth image size
87 return depthImageSize
;
89 unsigned int getDepthImageSize(int index
) const { // Returns one component of the depth image size
90 return depthImageSize
[index
];
92 const PTransform
& getDepthProjection(void) const { // Returns the depth unprojection matrix
93 return depthProjection
;
95 const Plane
& getBasePlane(void) const { // Returns the elevation base plane
98 void setDepthProjection(const PTransform
&
99 newDepthProjection
); // Sets a new depth unprojection matrix
100 void setIntrinsics(const Kinect::FrameSource::IntrinsicParameters
&
101 ips
); // Sets a new depth unprojection matrix and, if present, 2D lens distortion parameters
102 void setBasePlane(const Plane
& newBasePlane
); // Sets a new base plane for elevation rendering
103 void setDepthImage(const Kinect::FrameBuffer
&
104 newDepthImage
); // Sets a new depth image for subsequent surface rendering
105 Scalar
intersectLine(const Point
& p0
, const Point
& p1
, Scalar elevationMin
,
107 const; // Intersects a line segment with the current depth image in camera space; returns intersection point's parameter along line
108 unsigned int getDepthImageVersion(void)
109 const { // Returns the version number of the current depth image
110 return depthImageVersion
;
112 void uploadDepthProjection(GLint location
)
113 const; // Uploads the depth unprojection matrix into the GLSL 4x4 matrix at the given uniform location
114 void bindDepthTexture(GLContextData
& contextData
)
115 const; // Binds the up-to-date depth texture image to the currently active texture unit
116 void renderSurfaceTemplate(GLContextData
& contextData
)
117 const; // Renders the template quad strip mesh using current OpenGL settings
118 void renderDepth(const PTransform
& projectionModelview
,
119 GLContextData
& contextData
)
120 const; // Renders the surface into a pure depth buffer, for early z culling or shadow passes etc.
121 void renderElevation(const PTransform
& projectionModelview
,
122 GLContextData
& contextData
)
123 const; // Renders the surface's elevation relative to the base plane into the current one-component floating-point valued frame buffer