Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DisparityInterpolator.cpp
1 /*
2  * DisparityInterpolator.cpp
3  *
4  * Created on: Jan 29, 2010
5  * Author: africk
6  */
7 
8 #include <OpenGLFramework/SpecializedBatches/DisparityInterpolator.hh>
9 
10 using namespace std;
11 using namespace BIAS;
12 
13 DisparityInterpolator::DisparityInterpolator() {
14  dispMapP_ = NULL;
15  interp_type_ = BG;
16 
17  width_ = 0;
18  height_ = 0;
19 
20  fragmentShaderCode_ = ""
21  "uniform sampler2D dispMap;"
22  "uniform float step;"
23  "uniform int interpType;"
24  ""
25  "float Interpolate(float left_val, float left_x, "
26  "float right_val, float right_x, float x) {"
27  "if (left_val == right_val) {"
28  "return left_val;"
29  "} else {"
30  "return ((x - left_x) / (right_x - left_x)) * right_val + "
31  "((right_x - x) / (right_x - left_x))*left_val;"
32  "}"
33  "}"
34  ""
35  "void main () {"
36  ""
37  "vec2 coord = gl_TexCoord[0];"
38  "float disp = texture2D(dispMap, coord).r;"
39  ""
40  "if (disp == 0 && step > 0) {"
41  ""
42  "vec2 coord_left = coord;"
43  "vec2 coord_right = coord;"
44  "float left_val = 0;"
45  "float right_val = 0;"
46  ""
47  "while (coord_right.x < 1.0 && right_val == 0) {"
48  ""
49  "coord_right.x = coord_right.x+step;"
50  "right_val = texture2D(dispMap, coord_right).r;"
51  ""
52  "}"
53  ""
54  "while (coord_left.x > step && left_val == 0) {"
55  ""
56  "coord_left.x = coord_left.x - step;"
57  "left_val = texture2D(dispMap, coord_left).r;"
58  ""
59  "}"
60  ""
61  "if (left_val!=0 && right_val!=0) {"
62  "if (interpType == 0) {"
63  "disp = Interpolate(left_val,coord_left.x,right_val,coord_right.x,coord.x);"
64  "} else {"
65  "disp = min(left_val,right_val);"
66  "}"
67  ""
68  "} else {"
69  "disp = max(left_val,right_val);"
70  "}"
71  ""
72  "}"
73  "gl_FragColor.r = disp;"
74  "}"
75  "";
76 }
77 
78 DisparityInterpolator::~DisparityInterpolator() {
79 
80 }
81 
82 void DisparityInterpolator::SetDisparityMap(glfTexture2D* dispMap) {
83  dispMapP_ = dispMap;
84 }
85 
86 void DisparityInterpolator::Init(unsigned int width, unsigned int height) {
87  width_ = width;
88  height_ = height;
89  SetViewPort(width_, height_);
90  SetDefaultFloatRenderTarget(width_, height_);
91  InitBatch_();
92  CreateFragmentShaderProgram(fragmentShaderCode_);
93 }
94 
95 void DisparityInterpolator::Draw() {
96 
97  if (dispMapP_ != NULL) {
98  GetRenderTarget()->ClearDepthBuffer(1.0);
99  GetRenderTarget()->ClearColorBuffer(0, 0, 0, 0);
100  GetBatch_()->SetTexture(dispMapP_, 0);
101  GetShaderProgram_()->SetUniform("dispMap", (int) 0);
102  GetShaderProgram_()->SetUniform("step", 1.0f / (float) width_);
103  GetShaderProgram_()->SetUniform("interpType", (int) interp_type_);
104  GetBatch_()->Draw();
105 
106  } else {
107  BIASERR("set disparity map first !");
108  BIASABORT;
109  }
110 }
A 2D texture.
Definition: glfTexture2D.hh:40