Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
OutputLensDistortion.cpp
1 #include <GLviewer/OutputAttachments/OutputLensDistortion.hh>
2 #include <Base/Image/Image.hh>
3 #include <Base/Image/ImageIO.hh>
4 
5 using namespace std;
6 using namespace BIAS;
7 
8 OutputLensDistortion::OutputLensDistortion(BIAS::glfRenderTarget *out)
10 {
11  InitShader_();
12 }
13 
15 
16 }
17 
20  string vertexCode =
21  "void main()\r\n"
22  "{\r\n"
23  " gl_TexCoord[0] = gl_MultiTexCoord0;\r\n"
24  " gl_Position = ftransform();\r\n"
25  "}\r\n";
26  string fragmentCode =
27  "uniform sampler2D tex;\r\n"
28  "uniform sampler2D dep;\r\n"
29  "uniform float w;\r\n"
30  "uniform float h;\r\n"
31  "uniform sampler2D lookup;\r\n"
32  ""
33  " void main() {\r\n"
34  ""
35  " vec2 indirect = texture2D(lookup, gl_TexCoord[0].st).st;\r\n"
36  " gl_FragColor = texture2D(tex, indirect);\r\n"
37  " gl_FragDepth = texture2D(dep, indirect).r;\r\n"
38  " }\r\n";
39 
40  return GLProjectionOutputAttachment::InitShader(vertexCode, fragmentCode);
41 }
42 
46  if (!ppp) {
47  BIASERR("lens distortion only works on ProjectionParametersPerspective");
48  BIASABORT;
49  }
50  KMatrix K, Kinv, idealK;
51  K = ppp->GetK();
52  Kinv = K.Invert();
53  ppp->GetIdealK(idealK);
54  unsigned int width, height;
55  ppp->GetImageSize(width,height);
56  Image<float> lookupTexture(width, height, 3);
57  float* tex = lookupTexture.GetImageData();
58  unsigned int c=0;
59  HomgPoint2D source(0,0,1);
60 
61  for (unsigned int y=0; y<height; y++) {
62  for (unsigned int x=0; x<width; x++) {
63  source[0] = x;
64  source[1] = y;
65  ppp->Undistort(source);
66  source = idealK * Kinv * source;
67  source.Homogenize();
68 //
69  source[0] /= width;
70  source[1] /= height;
71 
72  tex[c++] = source[0];
73  tex[c++] = source[1];
74  tex[c++] = source[2];
75  }
76  }
77 
78  distortionMap_.Create();
79  distortionMap_.SetMinFilter(GL_NEAREST);
80  distortionMap_.SetMagFilter(GL_NEAREST);
81  distortionMap_.SetWrapT(GL_CLAMP);
82  distortionMap_.SetWrapS(GL_CLAMP);
83  distortionMap_.SetTextureNr(GL_TEXTURE2);
84  distortionMap_.UploadImage(lookupTexture, GL_RGB32F_ARB);
85 
86 }
87 
89 
90  if(target_ == NULL){
91  BIASWARN("Target is NULL, not drawing anything.");
92  return -1;
93  }
94 
95  Vector4<int> vp;
96  glGetIntegerv(GL_VIEWPORT, vp.GetData());
97 
98  unsigned int imgWidth = vp[2] - vp[0];
99  unsigned int imgHeight = vp[3] - vp[1];
100 
103  // bind render target
104  target_->Bind();
105  shaderProg_.Bind();
108  shaderProg_.SetUniform("lookup", distortionMap_.GetTextureNrInUniformFormat());
109  shaderProg_.SetUniform("w", (float)imgWidth);
110  shaderProg_.SetUniform("h", (float)imgHeight);
111 
112  glMatrixMode(GL_PROJECTION);
113  glPushMatrix();
114  glLoadIdentity();
115  gluOrtho2D(0, imgWidth, 0, imgHeight);
116  glMatrixMode(GL_MODELVIEW);
117  glPushMatrix();
118  glLoadIdentity();
119 
120  // quad to render texture on
121  glPushAttrib(GL_ENABLE_BIT);
122  glEnable(GL_TEXTURE_2D);
125  distortionMap_.BindTU();
126  glBegin(GL_QUADS);
127  glColor3f(1.0, 0.0, 0.0);
128  glTexCoord2f(0.0, 0.0);
129  glVertex2f(0.0, 0.0);
130 
131  glColor3f(0.0, 1.0, 0.0);
132  glTexCoord2f(0.0, 1.0);
133  glVertex2f(0.0, imgHeight);
134 
135  glColor3f(0.0, 0.0, 1.0);
136  glTexCoord2f(1.0, 1.0);
137  glVertex2f(imgWidth, imgHeight);
138 
139  glColor3f(1.0, 1.0, 1.0);
140  glTexCoord2f(1.0, 0.0);
141  glVertex2f(imgWidth, 0.0);
142  glEnd();
143  glFlush();
144  glPopAttrib();
145  glMatrixMode(GL_MODELVIEW);
146  glPopMatrix();
147  glMatrixMode(GL_PROJECTION);
148  glPopMatrix();
149  glUseProgram(0);
150 
151  GLF_THROW_ON_OPENGL_ERROR
152 
153  return 0;
154 }
155 
void SetUniform(const std::string &varName, float value)
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
int InitShader_()
creates strings with shader code and calls InitShader(vertex, fragment) from superclass ...
void Create()
Creates the texture but does not upload any data yet.
Definition: glfTexture.cpp:80
int Draw_()
main draw function
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
void SetWrapS(GLenum wrapS)
Sets the wrapping mode for the 1st texture coordinate.
Definition: glfTexture.cpp:105
void UploadImage(const BIAS::ImageBase &image, GLenum internalFormat=0, int mipmap=0)
Uploads a BIAS::Image to a mipmap of the texture.
int InitShader(std::string vertexCode, std::string fragmentCode)
can be called to create output attachment without having to derive new subclass will also create FBO ...
int GetTextureNrInUniformFormat()
Returns the OpenGL texture number in uniform format or -1 for error.
Definition: glfTexture.hh:183
void BindTU() const
Binds the texture.
Definition: glfTexture.cpp:249
void SetMinFilter(GLenum minFilter)
Sets the minifying function.
Definition: glfTexture.cpp:89
void Bind() const
Binds the shader program.
virtual void Bind() const =0
Makes this render target the currently used render target.
void SetWrapT(GLenum wrapT)
Sets the wrapping mode for the 2nd texture coordinate.
Definition: glfTexture.cpp:113
void ClearDepthBuffer(float depth=1.0f)
Clears the depth buffer of the render target with the given value.
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
virtual bool Undistort(BIAS::HomgPoint2D &point2d) const
Using the Matlab camera calibration toolbox parameters for an undistortion of distorted coordinates...
const T * GetData() const
get the data pointer the member function itself is const (before {..}) because it doesn&#39;t change the...
Definition: Vector4.hh:177
void CreateLookupTable(BIAS::ProjectionParametersBase *pp)
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
void SetTextureNr(GLint textureNr)
Sets the OpenGL texture number (GL_TEXTURE0 - GL_TEXTURE[MaxTextureUnit-1]).
Definition: glfTexture.cpp:207
void ClearColorBuffer(float red=0.0f, float green=0.0f, float blue=0.0f, float alpha=0.0f)
Clears the color buffer of the render target with the given color.
Class for extra rendering pass in GLProjection (for now only used in glutGLviewer) ...
KMatrix Invert() const
returns analyticaly inverted matrix
Definition: KMatrix.cpp:31
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
Interface for render targets.
void SetMagFilter(GLenum magFilter)
Sets the magnification function.
Definition: glfTexture.cpp:97