~ubuntu-branches/ubuntu/trusty/openscenegraph/trusty

« back to all changes in this revision

Viewing changes to OpenSceneGraph/examples/osgstereomatch/StereoMultipass.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-07-29 04:34:38 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20080729043438-no1h9h0dpsrlzp1y
* Non-maintainer upload.
* No longer try to detect (using /proc/cpuinfo when available) how many
  CPUs are available, fixing the FTBFS (due to -j0) on various platforms
  (Closes: #477353). The right way to do it is to support parallel=n in
  DEB_BUILD_OPTIONS (see Debian Policy §4.9.1), and adequate support has
  been implemented.
* Add patch to fix FTBFS due to the build system now refusing to handle
  whitespaces (Policy CMP0004 say the logs), thanks to Andreas Putzo who
  provided it (Closes: #482239):
   - debian/patches/fix-cmp0004-build-failure.dpatch
* Remove myself from Uploaders, as requested a while ago, done by Luk in
  his 2.2.0-2.1 NMU, which was never acknowledged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 
2
 
 
3
/* OpenSceneGraph example, osgstereomatch.
 
4
*
 
5
*  Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
*  of this software and associated documentation files (the "Software"), to deal
 
7
*  in the Software without restriction, including without limitation the rights
 
8
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
*  copies of the Software, and to permit persons to whom the Software is
 
10
*  furnished to do so, subject to the following conditions:
 
11
*
 
12
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
13
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
14
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
15
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
16
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
17
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
18
*  THE SOFTWARE.
 
19
*/
 
20
 
 
21
#include "StereoMultipass.h"
 
22
#include <osgDB/FileUtils>
 
23
#include <iostream>
 
24
 
 
25
SubtractPass::SubtractPass(osg::TextureRectangle *left_tex, 
 
26
                                                   osg::TextureRectangle *right_tex,
 
27
                                                   int width, int height,
 
28
                                                   int start_disparity) :
 
29
    _TextureWidth(width),
 
30
    _TextureHeight(height),
 
31
    _StartDisparity(start_disparity)
 
32
{
 
33
    _RootGroup = new osg::Group;
 
34
    _InTextureLeft = left_tex;
 
35
    _InTextureRight = right_tex;
 
36
   
 
37
    createOutputTextures();
 
38
 
 
39
    _Camera = new osg::Camera;
 
40
    setupCamera();
 
41
    _Camera->addChild(createTexturedQuad().get());
 
42
 
 
43
    _RootGroup->addChild(_Camera.get());
 
44
 
 
45
    setShader("shaders/stereomatch_subtract.frag");
 
46
}
 
47
 
 
48
SubtractPass::~SubtractPass()
 
49
{
 
50
}
 
51
 
 
52
osg::ref_ptr<osg::Group> SubtractPass::createTexturedQuad()
 
53
{
 
54
    osg::ref_ptr<osg::Group> top_group = new osg::Group;
 
55
    
 
56
    osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
 
57
 
 
58
    osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
 
59
    // counter-clockwise
 
60
    quad_coords->push_back(osg::Vec3d(0, 0, -1));
 
61
    quad_coords->push_back(osg::Vec3d(1, 0, -1));
 
62
    quad_coords->push_back(osg::Vec3d(1, 1, -1));
 
63
    quad_coords->push_back(osg::Vec3d(0, 1, -1));
 
64
 
 
65
    osg::ref_ptr<osg::Vec2Array> quad_tcoords = new osg::Vec2Array; // texture coords
 
66
    quad_tcoords->push_back(osg::Vec2(0, 0));
 
67
    quad_tcoords->push_back(osg::Vec2(_TextureWidth, 0));
 
68
    quad_tcoords->push_back(osg::Vec2(_TextureWidth, _TextureHeight));
 
69
    quad_tcoords->push_back(osg::Vec2(0, _TextureHeight));
 
70
 
 
71
    osg::ref_ptr<osg::Geometry> quad_geom = new osg::Geometry;
 
72
    osg::ref_ptr<osg::DrawArrays> quad_da = new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4);
 
73
 
 
74
    osg::ref_ptr<osg::Vec4Array> quad_colors = new osg::Vec4Array;
 
75
    quad_colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
 
76
 
 
77
    quad_geom->setVertexArray(quad_coords.get());
 
78
    quad_geom->setTexCoordArray(0, quad_tcoords.get());
 
79
    quad_geom->addPrimitiveSet(quad_da.get());
 
80
    quad_geom->setColorArray(quad_colors.get());
 
81
    quad_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
 
82
 
 
83
    _StateSet = quad_geom->getOrCreateStateSet();
 
84
    _StateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
 
85
    _StateSet->setTextureAttributeAndModes(0, _InTextureLeft.get(), osg::StateAttribute::ON);
 
86
    _StateSet->setTextureAttributeAndModes(1, _InTextureRight.get(), osg::StateAttribute::ON);
 
87
 
 
88
    _StateSet->addUniform(new osg::Uniform("textureLeft", 0));
 
89
    _StateSet->addUniform(new osg::Uniform("textureRight", 1));
 
90
    _StateSet->addUniform(new osg::Uniform("start_disparity", _StartDisparity));
 
91
    
 
92
    quad_geode->addDrawable(quad_geom.get());
 
93
    
 
94
    top_group->addChild(quad_geode.get());
 
95
 
 
96
    return top_group;
 
97
}
 
98
 
 
99
void SubtractPass::setupCamera()
 
100
{
 
101
    // clearing
 
102
    _Camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
 
103
    _Camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
104
 
 
105
    // projection and view
 
106
    _Camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
 
107
    _Camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
 
108
    _Camera->setViewMatrix(osg::Matrix::identity());
 
109
 
 
110
    // viewport
 
111
    _Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
 
112
 
 
113
    _Camera->setRenderOrder(osg::Camera::PRE_RENDER);
 
114
    _Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
 
115
    
 
116
    // attach the 4 textures
 
117
    for (int i=0; i<4; i++) {
 
118
                _Camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+i), _OutTexture[i].get());
 
119
    }
 
120
}
 
121
 
 
122
void SubtractPass::createOutputTextures()
 
123
{
 
124
    for (int i=0; i<4; i++) {
 
125
                _OutTexture[i] = new osg::TextureRectangle;
 
126
    
 
127
                _OutTexture[i]->setTextureSize(_TextureWidth, _TextureHeight);
 
128
                _OutTexture[i]->setInternalFormat(GL_RGBA);
 
129
                _OutTexture[i]->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
 
130
                _OutTexture[i]->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
 
131
    }
 
132
}
 
133
 
 
134
void SubtractPass::setShader(std::string filename)
 
135
{
 
136
    osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT ); 
 
137
    fshader->loadShaderSourceFromFile(osgDB::findDataFile(filename));
 
138
 
 
139
    _FragmentProgram = 0;
 
140
    _FragmentProgram = new osg::Program;
 
141
 
 
142
    _FragmentProgram->addShader(fshader.get());
 
143
 
 
144
    _StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
 
145
}
 
146
 
 
147
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
148
 
 
149
AggregatePass::AggregatePass(osg::TextureRectangle *diff_tex0,
 
150
                                                         osg::TextureRectangle *diff_tex1,
 
151
                                                         osg::TextureRectangle *diff_tex2,
 
152
                                                         osg::TextureRectangle *diff_tex3,
 
153
                                                         osg::TextureRectangle *agg_tex_in,
 
154
                                                         osg::TextureRectangle *agg_tex_out,
 
155
                                                         int width, int height,
 
156
                                                         int start_disparity, int window_size):
 
157
    _TextureWidth(width),
 
158
    _TextureHeight(height),
 
159
    _StartDisparity(start_disparity),
 
160
    _WindowSize(window_size)
 
161
{
 
162
    _RootGroup = new osg::Group;
 
163
    
 
164
    _InTextureDifference[0] = diff_tex0;
 
165
    _InTextureDifference[1] = diff_tex1;
 
166
    _InTextureDifference[2] = diff_tex2;
 
167
    _InTextureDifference[3] = diff_tex3;
 
168
 
 
169
    _InTextureAggregate = agg_tex_in;
 
170
    _OutTextureAggregate = agg_tex_out;
 
171
 
 
172
    _OutTexture = _OutTextureAggregate;
 
173
    
 
174
    _Camera = new osg::Camera;
 
175
    setupCamera();
 
176
    _Camera->addChild(createTexturedQuad().get());
 
177
 
 
178
    _RootGroup->addChild(_Camera.get());
 
179
 
 
180
    setShader("shaders/stereomatch_aggregate.frag");
 
181
 
 
182
}
 
183
 
 
184
AggregatePass::~AggregatePass()
 
185
{
 
186
}
 
187
 
 
188
osg::ref_ptr<osg::Group> AggregatePass::createTexturedQuad()
 
189
{
 
190
    osg::ref_ptr<osg::Group> top_group = new osg::Group;
 
191
    
 
192
    osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
 
193
 
 
194
    osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
 
195
    // counter-clockwise
 
196
    quad_coords->push_back(osg::Vec3d(0, 0, -1));
 
197
    quad_coords->push_back(osg::Vec3d(1, 0, -1));
 
198
    quad_coords->push_back(osg::Vec3d(1, 1, -1));
 
199
    quad_coords->push_back(osg::Vec3d(0, 1, -1));
 
200
 
 
201
    osg::ref_ptr<osg::Vec2Array> quad_tcoords = new osg::Vec2Array; // texture coords
 
202
    quad_tcoords->push_back(osg::Vec2(0, 0));
 
203
    quad_tcoords->push_back(osg::Vec2(_TextureWidth, 0));
 
204
    quad_tcoords->push_back(osg::Vec2(_TextureWidth, _TextureHeight));
 
205
    quad_tcoords->push_back(osg::Vec2(0, _TextureHeight));
 
206
 
 
207
    osg::ref_ptr<osg::Geometry> quad_geom = new osg::Geometry;
 
208
    osg::ref_ptr<osg::DrawArrays> quad_da = new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4);
 
209
 
 
210
    osg::ref_ptr<osg::Vec4Array> quad_colors = new osg::Vec4Array;
 
211
    quad_colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
 
212
 
 
213
    quad_geom->setVertexArray(quad_coords.get());
 
214
    quad_geom->setTexCoordArray(0, quad_tcoords.get());
 
215
    quad_geom->addPrimitiveSet(quad_da.get());
 
216
    quad_geom->setColorArray(quad_colors.get());
 
217
    quad_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
 
218
 
 
219
    _StateSet = quad_geom->getOrCreateStateSet();
 
220
    _StateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
 
221
    _StateSet->setTextureAttributeAndModes(0, _InTextureDifference[0].get(), osg::StateAttribute::ON);
 
222
    _StateSet->setTextureAttributeAndModes(1, _InTextureDifference[1].get(), osg::StateAttribute::ON);
 
223
    _StateSet->setTextureAttributeAndModes(2, _InTextureDifference[2].get(), osg::StateAttribute::ON);
 
224
    _StateSet->setTextureAttributeAndModes(3, _InTextureDifference[3].get(), osg::StateAttribute::ON);
 
225
    _StateSet->setTextureAttributeAndModes(4, _InTextureAggregate.get(), osg::StateAttribute::ON);
 
226
    
 
227
    _StateSet->addUniform(new osg::Uniform("textureDiff0", 0));
 
228
    _StateSet->addUniform(new osg::Uniform("textureDiff1", 1));
 
229
    _StateSet->addUniform(new osg::Uniform("textureDiff2", 2));
 
230
    _StateSet->addUniform(new osg::Uniform("textureDiff3", 3));
 
231
    _StateSet->addUniform(new osg::Uniform("textureAggIn", 4));
 
232
    _StateSet->addUniform(new osg::Uniform("start_disparity", _StartDisparity));
 
233
    _StateSet->addUniform(new osg::Uniform("window_size", _WindowSize));
 
234
 
 
235
    quad_geode->addDrawable(quad_geom.get());
 
236
    
 
237
    top_group->addChild(quad_geode.get());
 
238
 
 
239
    return top_group;
 
240
}
 
241
 
 
242
void AggregatePass::setupCamera()
 
243
{
 
244
    // clearing
 
245
    _Camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
 
246
    _Camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
247
 
 
248
    // projection and view
 
249
    _Camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
 
250
    _Camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
 
251
    _Camera->setViewMatrix(osg::Matrix::identity());
 
252
 
 
253
    // viewport
 
254
    _Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
 
255
 
 
256
    _Camera->setRenderOrder(osg::Camera::PRE_RENDER);
 
257
    _Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
 
258
 
 
259
    _Camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+0), _OutTexture.get());
 
260
}
 
261
 
 
262
void AggregatePass::setShader(std::string filename)
 
263
{
 
264
    osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT ); 
 
265
    fshader->loadShaderSourceFromFile(osgDB::findDataFile(filename));
 
266
 
 
267
    _FragmentProgram = 0;
 
268
    _FragmentProgram = new osg::Program;
 
269
 
 
270
    _FragmentProgram->addShader(fshader.get());
 
271
 
 
272
    _StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
 
273
}
 
274
 
 
275
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
276
 
 
277
SelectPass::SelectPass(osg::TextureRectangle *in_tex, 
 
278
                                           int width, int height,
 
279
                                           int min_disparity, int max_disparity) :
 
280
    _TextureWidth(width),
 
281
    _TextureHeight(height),
 
282
    _MinDisparity(min_disparity),
 
283
    _MaxDisparity(max_disparity)
 
284
{
 
285
    _RootGroup = new osg::Group;
 
286
    _InTexture = in_tex;
 
287
   
 
288
    createOutputTextures();
 
289
 
 
290
    _Camera = new osg::Camera;
 
291
    setupCamera();
 
292
    _Camera->addChild(createTexturedQuad().get());
 
293
 
 
294
    _RootGroup->addChild(_Camera.get());
 
295
 
 
296
    setShader("shaders/stereomatch_select.frag");
 
297
}
 
298
 
 
299
SelectPass::~SelectPass()
 
300
{
 
301
}
 
302
 
 
303
osg::ref_ptr<osg::Group> SelectPass::createTexturedQuad()
 
304
{
 
305
    osg::ref_ptr<osg::Group> top_group = new osg::Group;
 
306
    
 
307
    osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
 
308
 
 
309
    osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
 
310
    // counter-clockwise
 
311
    quad_coords->push_back(osg::Vec3d(0, 0, -1));
 
312
    quad_coords->push_back(osg::Vec3d(1, 0, -1));
 
313
    quad_coords->push_back(osg::Vec3d(1, 1, -1));
 
314
    quad_coords->push_back(osg::Vec3d(0, 1, -1));
 
315
 
 
316
    osg::ref_ptr<osg::Vec2Array> quad_tcoords = new osg::Vec2Array; // texture coords
 
317
    quad_tcoords->push_back(osg::Vec2(0, 0));
 
318
    quad_tcoords->push_back(osg::Vec2(_TextureWidth, 0));
 
319
    quad_tcoords->push_back(osg::Vec2(_TextureWidth, _TextureHeight));
 
320
    quad_tcoords->push_back(osg::Vec2(0, _TextureHeight));
 
321
 
 
322
    osg::ref_ptr<osg::Geometry> quad_geom = new osg::Geometry;
 
323
    osg::ref_ptr<osg::DrawArrays> quad_da = new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4);
 
324
 
 
325
    osg::ref_ptr<osg::Vec4Array> quad_colors = new osg::Vec4Array;
 
326
    quad_colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
 
327
 
 
328
    quad_geom->setVertexArray(quad_coords.get());
 
329
    quad_geom->setTexCoordArray(0, quad_tcoords.get());
 
330
    quad_geom->addPrimitiveSet(quad_da.get());
 
331
    quad_geom->setColorArray(quad_colors.get());
 
332
    quad_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
 
333
 
 
334
    _StateSet = quad_geom->getOrCreateStateSet();
 
335
    _StateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
 
336
    _StateSet->setTextureAttributeAndModes(0, _InTexture.get(), osg::StateAttribute::ON);
 
337
    
 
338
    _StateSet->addUniform(new osg::Uniform("textureIn", 0));
 
339
    _StateSet->addUniform(new osg::Uniform("min_disparity", _MinDisparity));
 
340
    _StateSet->addUniform(new osg::Uniform("max_disparity", _MaxDisparity));
 
341
 
 
342
    quad_geode->addDrawable(quad_geom.get());
 
343
    
 
344
    top_group->addChild(quad_geode.get());
 
345
 
 
346
    return top_group;
 
347
}
 
348
 
 
349
void SelectPass::setupCamera()
 
350
{
 
351
    // clearing
 
352
    _Camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
 
353
    _Camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
354
 
 
355
    // projection and view
 
356
    _Camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
 
357
    _Camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
 
358
    _Camera->setViewMatrix(osg::Matrix::identity());
 
359
 
 
360
    // viewport
 
361
    _Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
 
362
 
 
363
    _Camera->setRenderOrder(osg::Camera::PRE_RENDER);
 
364
    _Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
 
365
 
 
366
        _Camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+0), _OutTexture.get());
 
367
}
 
368
 
 
369
void SelectPass::createOutputTextures()
 
370
{
 
371
    _OutTexture = new osg::TextureRectangle;
 
372
    
 
373
    _OutTexture->setTextureSize(_TextureWidth, _TextureHeight);
 
374
    _OutTexture->setInternalFormat(GL_RGBA);
 
375
    _OutTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
 
376
    _OutTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
 
377
}
 
378
 
 
379
void SelectPass::setShader(std::string filename)
 
380
{
 
381
    osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT ); 
 
382
    fshader->loadShaderSourceFromFile(osgDB::findDataFile(filename));
 
383
 
 
384
    _FragmentProgram = 0;
 
385
    _FragmentProgram = new osg::Program;
 
386
 
 
387
    _FragmentProgram->addShader(fshader.get());
 
388
 
 
389
    _StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
 
390
}
 
391
 
 
392
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
393
 
 
394
StereoMultipass::StereoMultipass(osg::TextureRectangle *left_tex, 
 
395
                                                                 osg::TextureRectangle *right_tex,
 
396
                                                                 int width, int height, 
 
397
                                                                 int min_disparity, int max_disparity, int window_size) :
 
398
    _TextureWidth(width),
 
399
    _TextureHeight(height)
 
400
{
 
401
    _RootGroup = new osg::Group;
 
402
 
 
403
    createOutputTextures();
 
404
    
 
405
    _Camera = new osg::Camera;
 
406
    setupCamera();
 
407
    _Camera->addChild(createTexturedQuad().get());
 
408
 
 
409
    _RootGroup->addChild(_Camera.get());
 
410
 
 
411
    setShader("shaders/stereomatch_clear.frag");
 
412
 
 
413
    flip=1;
 
414
    flop=0;
 
415
        // we can do 16 differences in one pass,
 
416
        // but we must ping-pong the aggregate textures between passes
 
417
        // add passes until we cover the disparity range
 
418
        for (int i=min_disparity; i<=max_disparity; i+=16) {
 
419
                SubtractPass *subp = new SubtractPass(left_tex, right_tex,
 
420
                                                                                          width, height,
 
421
                                                                                          i);
 
422
                AggregatePass *aggp = new AggregatePass(subp->getOutputTexture(0).get(),
 
423
                                                                                                subp->getOutputTexture(1).get(),
 
424
                                                                                                subp->getOutputTexture(2).get(),
 
425
                                                                                                subp->getOutputTexture(3).get(),
 
426
                                                                                                _OutTexture[flip].get(),
 
427
                                                                                                _OutTexture[flop].get(),
 
428
                                                                                                width, height,
 
429
                                                                                                i, window_size);
 
430
 
 
431
                _RootGroup->addChild(subp->getRoot().get());
 
432
                _RootGroup->addChild(aggp->getRoot().get());
 
433
                flip = flip ? 0 : 1;
 
434
                flop = flop ? 0 : 1;
 
435
    }
 
436
    // add select pass
 
437
    _SelectPass = new SelectPass(_OutTexture[flip].get(),
 
438
                                                                 width, height,
 
439
                                                                 min_disparity, max_disparity);
 
440
    _RootGroup->addChild(_SelectPass->getRoot().get());
 
441
}
 
442
 
 
443
StereoMultipass::~StereoMultipass()
 
444
{
 
445
}
 
446
 
 
447
osg::ref_ptr<osg::Group> StereoMultipass::createTexturedQuad()
 
448
{
 
449
    osg::ref_ptr<osg::Group> top_group = new osg::Group;
 
450
    
 
451
    osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
 
452
 
 
453
    osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
 
454
    // counter-clockwise
 
455
    quad_coords->push_back(osg::Vec3d(0, 0, -1));
 
456
    quad_coords->push_back(osg::Vec3d(1, 0, -1));
 
457
    quad_coords->push_back(osg::Vec3d(1, 1, -1));
 
458
    quad_coords->push_back(osg::Vec3d(0, 1, -1));
 
459
 
 
460
    osg::ref_ptr<osg::Vec2Array> quad_tcoords = new osg::Vec2Array; // texture coords
 
461
    quad_tcoords->push_back(osg::Vec2(0, 0));
 
462
    quad_tcoords->push_back(osg::Vec2(_TextureWidth, 0));
 
463
    quad_tcoords->push_back(osg::Vec2(_TextureWidth, _TextureHeight));
 
464
    quad_tcoords->push_back(osg::Vec2(0, _TextureHeight));
 
465
 
 
466
    osg::ref_ptr<osg::Geometry> quad_geom = new osg::Geometry;
 
467
    osg::ref_ptr<osg::DrawArrays> quad_da = new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4);
 
468
 
 
469
    osg::ref_ptr<osg::Vec4Array> quad_colors = new osg::Vec4Array;
 
470
    quad_colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
 
471
 
 
472
    quad_geom->setVertexArray(quad_coords.get());
 
473
    quad_geom->setTexCoordArray(0, quad_tcoords.get());
 
474
    quad_geom->addPrimitiveSet(quad_da.get());
 
475
    quad_geom->setColorArray(quad_colors.get());
 
476
    quad_geom->setColorBinding(osg::Geometry::BIND_OVERALL);
 
477
    
 
478
    _StateSet = quad_geom->getOrCreateStateSet();
 
479
    _StateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
 
480
 
 
481
    quad_geode->addDrawable(quad_geom.get());
 
482
    
 
483
    top_group->addChild(quad_geode.get());
 
484
 
 
485
    return top_group;
 
486
}
 
487
 
 
488
void StereoMultipass::setupCamera()
 
489
{
 
490
    // clearing
 
491
    _Camera->setClearColor(osg::Vec4(10.0f,0.0f,0.0f,1.0f));
 
492
    _Camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
493
 
 
494
    // projection and view
 
495
    _Camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
 
496
    _Camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
 
497
    _Camera->setViewMatrix(osg::Matrix::identity());
 
498
 
 
499
    // viewport
 
500
    _Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
 
501
 
 
502
    _Camera->setRenderOrder(osg::Camera::PRE_RENDER);
 
503
    _Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
 
504
 
 
505
        // attach two textures for aggregating results
 
506
    _Camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+0), _OutTexture[0].get());
 
507
    _Camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+1), _OutTexture[1].get());
 
508
}
 
509
 
 
510
void StereoMultipass::createOutputTextures()
 
511
{
 
512
    for (int i=0; i<2; i++) {
 
513
                _OutTexture[i] = new osg::TextureRectangle;
 
514
        
 
515
                _OutTexture[i]->setTextureSize(_TextureWidth, _TextureHeight);
 
516
                _OutTexture[i]->setInternalFormat(GL_RGBA);
 
517
                _OutTexture[i]->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
 
518
                _OutTexture[i]->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
 
519
        
 
520
                // hdr, we want to store floats
 
521
                _OutTexture[i]->setInternalFormat(GL_RGBA16F_ARB);
 
522
                //_OutTexture[i]->setInternalFormat(GL_FLOAT_RGBA32_NV);
 
523
                //_OutTexture[i]->setInternalFormat(GL_FLOAT_RGBA16_NV);
 
524
                _OutTexture[i]->setSourceFormat(GL_RGBA);
 
525
                _OutTexture[i]->setSourceType(GL_FLOAT);
 
526
    }
 
527
}
 
528
 
 
529
void StereoMultipass::setShader(std::string filename)
 
530
{
 
531
    osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT ); 
 
532
    fshader->loadShaderSourceFromFile(osgDB::findDataFile(filename));
 
533
 
 
534
    _FragmentProgram = 0;
 
535
    _FragmentProgram = new osg::Program;
 
536
 
 
537
    _FragmentProgram->addShader(fshader.get());
 
538
 
 
539
    _StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
 
540
}
 
541