OpenCOVER
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MultiChannelDrawer.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef MULTICHANNELDRAWER_H
9 #define MULTICHANNELDRAWER_H
10 
11 #include <osg/Matrix>
12 #include <osg/Array>
13 #include <osg/Geode>
14 #include <osg/Camera>
15 #include <osg/TextureRectangle>
16 
17 #include <util/coExport.h>
18 
19 #include <memory>
20 #include <vector>
21 
22 namespace opencover
23 {
24 
25 class MultiChannelDrawer;
26 struct ViewChannelData;
27 
28 enum ViewEye {
33 };
34 
35 // Store data associated with one view (image rendered for a viewport)
36 struct ViewData {
37  static const int NumImages = 1;
38 
40  int viewNum = -1;
41  int geoWidth = 0, geoHeight = 0;
45  osg::Matrix imgProj, imgView, imgModel;
46  osg::Matrix newProj, newView, newModel;
47 
48  // geometry for mapping depth image
49  osg::ref_ptr<osg::TextureRectangle> colorTex;
50  osg::ref_ptr<osg::TextureRectangle> depthTex;
51  osg::ref_ptr<osg::Image> colorImg[NumImages];
52  osg::ref_ptr<osg::Image> depthImg[NumImages];
53  osg::ref_ptr<osg::Vec2Array> texcoord;
54  osg::ref_ptr<osg::Geometry> fixedGeo;
55  osg::ref_ptr<osg::Geometry> reprojGeo;
56  osg::ref_ptr<osg::Vec2Array> pointCoord, quadCoord;
57  osg::ref_ptr<osg::DrawArrays> pointArr, quadArr;
58  osg::ref_ptr<osg::Uniform> size;
59  osg::ref_ptr<osg::Uniform> pixelOffset;
60  osg::ref_ptr<osg::Uniform> withNeighbors;
61  osg::ref_ptr<osg::Uniform> withHoles;
62  osg::ref_ptr<osg::Program> reprojConstProgram;
63  osg::ref_ptr<osg::Program> reprojAdaptProgram;
64  osg::ref_ptr<osg::Program> reprojMeshProgram;
65 
66  std::vector<ViewChannelData *> viewChan;
67 
68  ViewData(int view=-1)
69  : viewNum(view)
70  {
71  for (int i=0; i<NumImages; ++i) {
72  width[i] = 0;
73  height[i] = 0;
74  depthWidth[i] = 0;
75  depthHeight[i] = 0;
76  colorFormat[i] = 0;
77  depthFormat[i] = 0;
78  }
79  }
80 
81  ~ViewData();
82 };
83 
85 struct ChannelData {
87  int channelNum = -1;
89  bool second;
90  int width;
91  int height;
92  osg::Matrix curProj, curView, curModel;
93 
94  // geometry for mapping depth image
95  osg::ref_ptr<osg::Camera> camera;
96  osg::ref_ptr<osg::Group> scene;
97 
98  std::vector<std::shared_ptr<ViewChannelData>> viewChan;
99 
101  : drawer(drawer)
102  , channelNum(channel)
103  , eye(Middle)
104  , second(false)
105  , width(0)
106  , height(0)
107  {
108  }
109 
110  ~ChannelData();
111 
112  void addView(std::shared_ptr<ViewData> vd);
113  void enableView(std::shared_ptr<ViewData> vd, bool enable);
114  void clearViews();
115  void updateViews();
116 };
117 
120 
121  ViewChannelData(std::shared_ptr<ViewData> view, ChannelData *chan);
123  void setThis(std::shared_ptr<ViewChannelData> vcd);
124 
125  osg::ref_ptr<osg::Geometry> fixedGeo;
126  osg::ref_ptr<osg::Geometry> reprojGeo;
127  osg::ref_ptr<osg::Uniform> reprojMat;
128  osg::ref_ptr<osg::StateSet> state;
129  osg::ref_ptr<osg::Geode> geode;
130  osg::ref_ptr<osg::Drawable::DrawCallback> drawCallback;
131 
132  ChannelData *chan = nullptr;
133  std::shared_ptr<ViewData> view;
134 
135  void update();
136 
137 };
138 
139 class PLUGIN_UTILEXPORT MultiChannelDrawer: public osg::Camera {
140  friend struct SingleScreenCB;
141 public:
145 
146  MultiChannelDrawer(bool flipped=false, bool useCuda=false);
148  int numViews() const;
149  void update();
150  const osg::Matrix &modelMatrix(int idx) const;
151  const osg::Matrix &viewMatrix(int idx) const;
152  const osg::Matrix &projectionMatrix(int idx) const;
153 
155  enum Mode {
156  AsIs, //< as is, without reprojection
157  Reproject, //< reproject every pixel as single pixel-sized point
158  ReprojectAdaptive, //< reproject pixels and adapt their size based on viewer distance and reprojection matrix
159  ReprojectAdaptiveWithNeighbors, //< reproject pixels and adapt their size so that gaps to neighbor pixels are filled
160  ReprojectMesh, //< reproject as rectilinear mesh
161  ReprojectMeshWithHoles //< reprjoct as rectilinear mesh, but keep holes where pixels become heavily deformed
162  };
163  Mode mode() const;
164  void setMode(Mode mode);
165 
167  Same, //< only render the view created for this viewport
168  MatchingEye, //< render all views for the same (left/right) eye
169  All, //< render all views
170  };
171 
173  ViewSelection viewsToRender() const;
175  void setViewsToRender(ViewSelection views);
177  void setNumViews(int nv=-1);
179  void setViewEye(int view, ViewEye eye);
180 
182  void swapFrame();
184  void updateMatrices(int idx, const osg::Matrix &model, const osg::Matrix &view, const osg::Matrix &proj);
186  void resizeView(int idx, int w, int h, GLenum depthFormat=0, GLenum colorFormat=GL_UNSIGNED_BYTE);
188  std::shared_ptr<ViewData> getViewData(int idx);
190  unsigned char *rgba(int idx) const;
192  unsigned char *depth(int idx) const;
194  void clearColor(int idx);
196  void clearDepth(int idx);
197 
198 private:
199  void initViewData(ViewData &cd);
200  void initChannelData(ChannelData &cd);
201  void createGeometry(ViewData &cd);
202  void createGeometry(ChannelData &cd);
203  void clearViewData();
204  std::vector<std::shared_ptr<ViewData>> m_viewData;
205  std::vector<std::shared_ptr<ChannelData>> m_channelData;
206  bool m_flipped;
207  Mode m_mode;
208  ViewSelection m_viewsToRender = Same;
209  int renderTex=0, writeTex=0;
210  struct AvailableEyes {
211  bool middle=false;
212  bool left=false;
213  bool right=false;
214  };
215  AvailableEyes m_availableEyes;
216  bool haveEye(ViewEye eye);
217 
218  const bool m_useCuda;
219  void updateGeoForView(ViewData &vd);
220  int m_numViews = 0;
221 };
222 
223 }
224 #endif
opencover::ViewData ViewData
Definition: MultiChannelDrawer.h:143
osg::ref_ptr< osg::Geometry > fixedGeo
Definition: MultiChannelDrawer.h:125
int channelNum
Definition: MultiChannelDrawer.h:87
osg::ref_ptr< osg::Geometry > fixedGeo
Definition: MultiChannelDrawer.h:54
Definition: MultiChannelDrawer.h:32
int height
Definition: MultiChannelDrawer.h:91
int geoWidth
Definition: MultiChannelDrawer.h:41
int width
Definition: MultiChannelDrawer.h:90
Definition: MultiChannelDrawer.h:157
MultiChannelDrawer * drawer
Definition: MultiChannelDrawer.h:86
Definition: MultiChannelDrawer.h:156
osg::ref_ptr< osg::Geometry > reprojGeo
Definition: MultiChannelDrawer.h:55
osg::Matrix curView
Definition: MultiChannelDrawer.h:92
osg::ref_ptr< osg::TextureRectangle > depthTex
Definition: MultiChannelDrawer.h:50
osg::Matrix imgModel
Definition: MultiChannelDrawer.h:45
std::vector< std::shared_ptr< ViewChannelData > > viewChan
Definition: MultiChannelDrawer.h:98
GLenum depthFormat[NumImages]
Definition: MultiChannelDrawer.h:44
ViewEye eye
Definition: MultiChannelDrawer.h:39
int viewNum
Definition: MultiChannelDrawer.h:40
std::vector< ViewChannelData * > viewChan
Definition: MultiChannelDrawer.h:66
Definition: MultiChannelDrawer.h:169
osg::ref_ptr< osg::Drawable::DrawCallback > drawCallback
Definition: MultiChannelDrawer.h:130
Definition: MultiChannelDrawer.h:168
opencover::ChannelData ChannelData
Definition: MultiChannelDrawer.h:142
ViewChannelData(std::shared_ptr< ViewData > view, ChannelData *chan)
Definition: MultiChannelDrawer.h:30
data for rendering a View into a Channel
Definition: MultiChannelDrawer.h:119
ChannelData(MultiChannelDrawer *drawer, int channel)
Definition: MultiChannelDrawer.h:100
osg::ref_ptr< osg::Image > colorImg[NumImages]
Definition: MultiChannelDrawer.h:51
osg::ref_ptr< osg::Vec2Array > quadCoord
Definition: MultiChannelDrawer.h:56
osg::ref_ptr< osg::Vec2Array > texcoord
Definition: MultiChannelDrawer.h:53
osg::Matrix newProj
Definition: MultiChannelDrawer.h:46
ChannelData * chan
Definition: MultiChannelDrawer.h:132
osg::ref_ptr< osg::TextureRectangle > colorTex
Definition: MultiChannelDrawer.h:49
osg::Matrix curModel
Definition: MultiChannelDrawer.h:92
Definition: MultiChannelDrawer.h:158
Definition: MultiChannelDrawer.h:31
ViewEye
Definition: MultiChannelDrawer.h:28
osg::ref_ptr< osg::Uniform > size
Definition: MultiChannelDrawer.h:58
ViewSelection
Definition: MultiChannelDrawer.h:166
osg::ref_ptr< osg::DrawArrays > quadArr
Definition: MultiChannelDrawer.h:57
Definition: MultiChannelDrawer.h:160
GLenum colorFormat[NumImages]
Definition: MultiChannelDrawer.h:44
osg::ref_ptr< osg::Uniform > withHoles
Definition: MultiChannelDrawer.h:61
osg::ref_ptr< osg::Uniform > pixelOffset
Definition: MultiChannelDrawer.h:59
Definition: MultiChannelDrawer.h:139
osg::ref_ptr< osg::Uniform > withNeighbors
Definition: MultiChannelDrawer.h:60
int geoHeight
Definition: MultiChannelDrawer.h:41
osg::ref_ptr< osg::Geometry > reprojGeo
Definition: MultiChannelDrawer.h:126
Definition: MultiChannelDrawer.h:36
ViewData(int view=-1)
Definition: MultiChannelDrawer.h:68
ViewEye eye
Definition: MultiChannelDrawer.h:88
int height[NumImages]
Definition: MultiChannelDrawer.h:42
osg::ref_ptr< osg::Group > scene
Definition: MultiChannelDrawer.h:96
osg::ref_ptr< osg::DrawArrays > pointArr
Definition: MultiChannelDrawer.h:57
osg::ref_ptr< osg::Vec2Array > pointCoord
Definition: MultiChannelDrawer.h:56
osg::ref_ptr< osg::Image > depthImg[NumImages]
Definition: MultiChannelDrawer.h:52
osg::ref_ptr< osg::Program > reprojAdaptProgram
Definition: MultiChannelDrawer.h:63
opencover::ViewEye ViewEye
Definition: MultiChannelDrawer.h:144
osg::Matrix curProj
Definition: MultiChannelDrawer.h:92
osg::ref_ptr< osg::Uniform > reprojMat
Definition: MultiChannelDrawer.h:127
void setThis(std::shared_ptr< ViewChannelData > vcd)
Definition: MultiChannelDrawer.h:167
int depthWidth[NumImages]
Definition: MultiChannelDrawer.h:43
Definition: MultiChannelDrawer.h:29
osg::ref_ptr< osg::StateSet > state
Definition: MultiChannelDrawer.h:128
osg::Matrix newModel
Definition: MultiChannelDrawer.h:46
static const int NumImages
Definition: MultiChannelDrawer.h:37
osg::ref_ptr< osg::Geode > geode
Definition: MultiChannelDrawer.h:129
osg::ref_ptr< osg::Program > reprojMeshProgram
Definition: MultiChannelDrawer.h:64
store data associated with one channel (output viewport)
Definition: MultiChannelDrawer.h:85
osg::Matrix newView
Definition: MultiChannelDrawer.h:46
int width[NumImages]
Definition: MultiChannelDrawer.h:42
osg::Matrix imgProj
Definition: MultiChannelDrawer.h:45
osg::Matrix imgView
Definition: MultiChannelDrawer.h:45
int depthHeight[NumImages]
Definition: MultiChannelDrawer.h:43
osg::ref_ptr< osg::Program > reprojConstProgram
Definition: MultiChannelDrawer.h:62
void enableView(std::shared_ptr< ViewData > vd, bool enable)
osg::ref_ptr< osg::Camera > camera
Definition: MultiChannelDrawer.h:95
Mode
reprojection mode
Definition: MultiChannelDrawer.h:155
bool second
Definition: MultiChannelDrawer.h:89
void addView(std::shared_ptr< ViewData > vd)
std::shared_ptr< ViewData > view
Definition: MultiChannelDrawer.h:133