1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3
/* OpenSceneGraph example, osgstereomatch.
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:
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
21
#include "StereoMultipass.h"
22
#include <osgDB/FileUtils>
25
SubtractPass::SubtractPass(osg::TextureRectangle *left_tex,
26
osg::TextureRectangle *right_tex,
27
int width, int height,
28
int start_disparity) :
30
_TextureHeight(height),
31
_StartDisparity(start_disparity)
33
_RootGroup = new osg::Group;
34
_InTextureLeft = left_tex;
35
_InTextureRight = right_tex;
37
createOutputTextures();
39
_Camera = new osg::Camera;
41
_Camera->addChild(createTexturedQuad().get());
43
_RootGroup->addChild(_Camera.get());
45
setShader("shaders/stereomatch_subtract.frag");
48
SubtractPass::~SubtractPass()
52
osg::ref_ptr<osg::Group> SubtractPass::createTexturedQuad()
54
osg::ref_ptr<osg::Group> top_group = new osg::Group;
56
osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
58
osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
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));
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));
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);
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));
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);
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);
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));
92
quad_geode->addDrawable(quad_geom.get());
94
top_group->addChild(quad_geode.get());
99
void SubtractPass::setupCamera()
102
_Camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
103
_Camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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());
111
_Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
113
_Camera->setRenderOrder(osg::Camera::PRE_RENDER);
114
_Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
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());
122
void SubtractPass::createOutputTextures()
124
for (int i=0; i<4; i++) {
125
_OutTexture[i] = new osg::TextureRectangle;
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);
134
void SubtractPass::setShader(std::string filename)
136
osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT );
137
fshader->loadShaderSourceFromFile(osgDB::findDataFile(filename));
139
_FragmentProgram = 0;
140
_FragmentProgram = new osg::Program;
142
_FragmentProgram->addShader(fshader.get());
144
_StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
147
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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)
162
_RootGroup = new osg::Group;
164
_InTextureDifference[0] = diff_tex0;
165
_InTextureDifference[1] = diff_tex1;
166
_InTextureDifference[2] = diff_tex2;
167
_InTextureDifference[3] = diff_tex3;
169
_InTextureAggregate = agg_tex_in;
170
_OutTextureAggregate = agg_tex_out;
172
_OutTexture = _OutTextureAggregate;
174
_Camera = new osg::Camera;
176
_Camera->addChild(createTexturedQuad().get());
178
_RootGroup->addChild(_Camera.get());
180
setShader("shaders/stereomatch_aggregate.frag");
184
AggregatePass::~AggregatePass()
188
osg::ref_ptr<osg::Group> AggregatePass::createTexturedQuad()
190
osg::ref_ptr<osg::Group> top_group = new osg::Group;
192
osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
194
osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
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));
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));
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);
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));
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);
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);
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));
235
quad_geode->addDrawable(quad_geom.get());
237
top_group->addChild(quad_geode.get());
242
void AggregatePass::setupCamera()
245
_Camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
246
_Camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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());
254
_Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
256
_Camera->setRenderOrder(osg::Camera::PRE_RENDER);
257
_Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
259
_Camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+0), _OutTexture.get());
262
void AggregatePass::setShader(std::string filename)
264
osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT );
265
fshader->loadShaderSourceFromFile(osgDB::findDataFile(filename));
267
_FragmentProgram = 0;
268
_FragmentProgram = new osg::Program;
270
_FragmentProgram->addShader(fshader.get());
272
_StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
275
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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)
285
_RootGroup = new osg::Group;
288
createOutputTextures();
290
_Camera = new osg::Camera;
292
_Camera->addChild(createTexturedQuad().get());
294
_RootGroup->addChild(_Camera.get());
296
setShader("shaders/stereomatch_select.frag");
299
SelectPass::~SelectPass()
303
osg::ref_ptr<osg::Group> SelectPass::createTexturedQuad()
305
osg::ref_ptr<osg::Group> top_group = new osg::Group;
307
osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
309
osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
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));
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));
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);
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));
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);
334
_StateSet = quad_geom->getOrCreateStateSet();
335
_StateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
336
_StateSet->setTextureAttributeAndModes(0, _InTexture.get(), osg::StateAttribute::ON);
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));
342
quad_geode->addDrawable(quad_geom.get());
344
top_group->addChild(quad_geode.get());
349
void SelectPass::setupCamera()
352
_Camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
353
_Camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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());
361
_Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
363
_Camera->setRenderOrder(osg::Camera::PRE_RENDER);
364
_Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
366
_Camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+0), _OutTexture.get());
369
void SelectPass::createOutputTextures()
371
_OutTexture = new osg::TextureRectangle;
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);
379
void SelectPass::setShader(std::string filename)
381
osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT );
382
fshader->loadShaderSourceFromFile(osgDB::findDataFile(filename));
384
_FragmentProgram = 0;
385
_FragmentProgram = new osg::Program;
387
_FragmentProgram->addShader(fshader.get());
389
_StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
392
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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)
401
_RootGroup = new osg::Group;
403
createOutputTextures();
405
_Camera = new osg::Camera;
407
_Camera->addChild(createTexturedQuad().get());
409
_RootGroup->addChild(_Camera.get());
411
setShader("shaders/stereomatch_clear.frag");
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,
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(),
431
_RootGroup->addChild(subp->getRoot().get());
432
_RootGroup->addChild(aggp->getRoot().get());
437
_SelectPass = new SelectPass(_OutTexture[flip].get(),
439
min_disparity, max_disparity);
440
_RootGroup->addChild(_SelectPass->getRoot().get());
443
StereoMultipass::~StereoMultipass()
447
osg::ref_ptr<osg::Group> StereoMultipass::createTexturedQuad()
449
osg::ref_ptr<osg::Group> top_group = new osg::Group;
451
osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
453
osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
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));
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));
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);
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));
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);
478
_StateSet = quad_geom->getOrCreateStateSet();
479
_StateSet->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
481
quad_geode->addDrawable(quad_geom.get());
483
top_group->addChild(quad_geode.get());
488
void StereoMultipass::setupCamera()
491
_Camera->setClearColor(osg::Vec4(10.0f,0.0f,0.0f,1.0f));
492
_Camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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());
500
_Camera->setViewport(0, 0, _TextureWidth, _TextureHeight);
502
_Camera->setRenderOrder(osg::Camera::PRE_RENDER);
503
_Camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
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());
510
void StereoMultipass::createOutputTextures()
512
for (int i=0; i<2; i++) {
513
_OutTexture[i] = new osg::TextureRectangle;
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);
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);
529
void StereoMultipass::setShader(std::string filename)
531
osg::ref_ptr<osg::Shader> fshader = new osg::Shader( osg::Shader::FRAGMENT );
532
fshader->loadShaderSourceFromFile(osgDB::findDataFile(filename));
534
_FragmentProgram = 0;
535
_FragmentProgram = new osg::Program;
537
_FragmentProgram->addShader(fshader.get());
539
_StateSet->setAttributeAndModes(_FragmentProgram.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );