SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
gl_helpers.h
Go to the documentation of this file.
1 /*
2  This file is part of the CVD Library.
3 
4  Copyright (C) 2005 The Authors
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #ifndef CVD_GL_HELPERS_H
22 #define CVD_GL_HELPERS_H
23 
24 #include <iostream>
25 #include <map>
26 
27 #include "image_ref.h"
28 #include "image.h"
29 #include "byte.h"
30 #include "rgb.h"
31 #include "byte.h"
32 #include "rgb8.h"
33 #include "rgba.h"
34 #import <OpenGLES/ES1/gl.h>
35 #import <OpenGLES/ES1/glext.h>
36 #include "gl_types.h"
37 
38 #include "TooN.h"
39 #include "se3.h"
40 #include "so3.h"
41 #include "se2.h"
42 #include "so2.h"
43 
44 namespace CVD
45 {
46 
50  inline void glVertex(const ImageRef& i)
51  {
52  glVertex2i(i.x, i.y);
53  }
54 
58  inline void glTexCoord(const ImageRef& i)
59  {
60  glTexCoord2i(i.x, i.y);
61  }
62 
66  inline void glRasterPos(const ImageRef& i)
67  {
68  glRasterPos2i(i.x, i.y);
69  }
70 
75  inline void glRect( const ImageRef & p, const ImageRef & q)
76  {
77  glRecti(p.x, p.y, q.x, q.y);
78  }
79 
83  inline void glVertex(const TooN::Vector<2>& v)
84  {
85  glVertex2d(v[0], v[1]);
86  }
87 
91  inline void glVertex(const TooN::Vector<3>& v)
92  {
93  glVertex3d(v[0], v[1], v[2]);
94  }
95 
99  inline void glVertex(const TooN::Vector<4>& v)
100  {
101  glVertex4d(v[0], v[1], v[2], v[3]);
102  }
103 
107  inline void glTexCoord(const TooN::Vector<2>& v)
108  {
109  glTexCoord2d(v[0], v[1]);
110  }
111 
115  inline void glTexCoord(const TooN::Vector<3>& v)
116  {
117  glTexCoord3d(v[0], v[1], v[2]);
118  }
119 
123  inline void glTexCoord(const TooN::Vector<4>& v)
124  {
125  glTexCoord4d(v[0], v[1], v[2], v[3]);
126  }
127 
132  inline void glRect( const TooN::Vector<2> & p, const TooN::Vector<2> & q)
133  {
134  glRectd(p[0], p[1], q[0], q[1]);
135  }
136 
137 #ifdef GL_GLEXT_PROTOTYPES
138  inline void glMultiTexCoord(GLenum unit, const TooN::Vector<2>& v)
143  {
144  glMultiTexCoord2d(unit, v[0], v[1]);
145  }
146 
151  inline void glMultiTexCoord(GLenum unit, const TooN::Vector<3>& v)
152  {
153  glMultiTexCoord3d(unit, v[0], v[1], v[2]);
154  }
155 
160  inline void glMultiTexCoord(GLenum unit, const TooN::Vector<4>& v)
161  {
162  glMultiTexCoord4d(unit, v[0], v[1], v[2], v[3]);
163  }
164 #endif
165 
169  inline void glRasterPos(const TooN::Vector<2>& v)
170  {
171  glRasterPos2d(v[0], v[1]);
172  }
173 
177  inline void glRasterPos(const TooN::Vector<3>& v)
178  {
179  glRasterPos3d(v[0], v[1], v[2]);
180  }
181 
185  inline void glRasterPos(const TooN::Vector<4>& v)
186  {
187  glRasterPos4d(v[0], v[1], v[2], v[3]);
188  }
189 
193  inline void glNormal(const TooN::Vector<3>& n)
194  {
195  glNormal3d(n[0], n[1], n[2]);
196  }
197 
201  inline void glTranslate( const ImageRef & v )
202  {
203  glTranslatef( static_cast<GLfloat>(v.x), static_cast<GLfloat>(v.y), 0);
204  }
205 
209  template <int N> inline void glTranslate( const TooN::Vector<N> & v)
210  {
211  glTranslated(v[0], v[1], v[2]);
212  }
213 
218  template <> inline void glTranslate( const TooN::Vector<2> & v)
219  {
220  glTranslated(v[0], v[1], 0);
221  }
222 
227  template <> inline void glTranslate( const TooN::Vector<1> & v)
228  {
229  glTranslated(v[0], 0, 0);
230  }
231 
237  template <int N, class P, class A> inline void glMultMatrix( const TooN::Matrix<N,N,P,A> & m )
238  {
239  GLdouble glm[16];
240  glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = m[2][0]; glm[3] = m[3][0];
241  glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = m[2][1]; glm[7] = m[3][1];
242  glm[8] = m[0][2]; glm[9] = m[1][2]; glm[10] = m[2][2]; glm[11] = m[3][2];
243  glm[12] = m[0][3]; glm[13] = m[1][3]; glm[14] = m[2][3]; glm[15] = m[3][3];
244  glMultMatrixd(glm);
245  }
246 
252  template <class P, class A> inline void glMultMatrix( const TooN::Matrix<3,3,P,A> & m )
253  {
254  GLdouble glm[16];
255  glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = m[2][0]; glm[3] = 0;
256  glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = m[2][1]; glm[7] = 0;
257  glm[8] = m[0][2]; glm[9] = m[1][2]; glm[10] = m[2][2]; glm[11] = 0;
258  glm[12] = 0; glm[13] = 0; glm[14] = 0; glm[15] = 1;
259  glMultMatrixd(glm);
260  }
261 
267  template <class P, class A> inline void glMultMatrix( const TooN::Matrix<2,2,P,A> & m )
268  {
269  GLdouble glm[16];
270  glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = 0; glm[3] = 0;
271  glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = 0; glm[7] = 0;
272  glm[8] = 0; glm[9] = 0; glm[10] = 1; glm[11] = 0;
273  glm[12] = 0; glm[13] = 0; glm[14] = 0; glm[15] = 1;
274  glMultMatrixd(glm);
275  }
276 
280  template <typename P>
281  inline void glMultMatrix( const TooN::SO3<P> & so3 )
282  {
283  glMultMatrix( so3.get_matrix());
284  }
285 
290  template <typename P>
291  inline void glMultMatrix( const TooN::SE3<P> & se3 )
292  {
294  glMultMatrix( se3.get_rotation());
295  }
296 
300  template <typename P>
301  inline void glMultMatrix( const TooN::SO2<P> & so2 )
302  {
303  glMultMatrix( so2.get_matrix());
304  }
305 
310  template <typename P>
311  inline void glMultMatrix( const TooN::SE2<P> & se2 )
312  {
314  glMultMatrix( se2.get_rotation());
315  }
316 
324  inline void glOrtho( const CVD::ImageRef & size, const double nearPlane = -1.0, const double farPlane = 1.0)
325  {
326  ::glOrtho( -0.375, size.x - 0.375, size.y - 0.375, -0.375, nearPlane, farPlane );
327  }
328 
333  inline void glOrtho( const TooN::Vector<6> & param)
334  {
335  ::glOrtho( param[0], param[1], param[2], param[3], param[4], param[5]);
336  }
337 
350  template <typename P, typename A>
351  inline void glFrustum( const TooN::Vector<4,P,A> & params, double width, double height, double nearPlane = 0.1, double farPlane = 100)
352  {
353  GLdouble left, right, bottom, top;
354  left = -nearPlane * params[2] / params[0];
355  top = nearPlane * params[3] / params[1];
356  right = nearPlane * ( width - params[2] ) / params[0];
357  bottom = - nearPlane * ( height - params[3] ) / params[1];
358  ::glFrustum( left, right, bottom, top, nearPlane, farPlane );
359  }
360 
369  template <class CAMERA> inline void glFrustum( const CAMERA & camera, double width, double height, double nearPlane = 0.1, double farPlane = 100)
370  {
371  glFrustum( camera.get_parameters().template slice<0,4>(), width, height, nearPlane, farPlane);
372  }
373 
378  inline void glFrustum( const TooN::Vector<6> & param)
379  {
380  ::glFrustum( param[0], param[1], param[2], param[3], param[4], param[5]);
381  }
382 
387  inline void glColor(const TooN::Vector<3>& v)
388  {
389  glColor3d(v[0], v[1], v[2]);
390  }
391 
396  inline void glColor(const TooN::Vector<4>& v)
397  {
398  glColor4d(v[0], v[1], v[2], v[3]);
399  }
400 
405  inline void glClearColor(const TooN::Vector<4>& v)
406  {
407  ::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], (GLclampf)v[3]);
408  }
409 
414  inline void glClearColor(const TooN::Vector<3>& v)
415  {
416  ::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], 0);
417  }
418 
419 
420 
424  inline void glColor(const TooN::Vector<-1> & v)
425  {
426  switch(v.size()){
427  case 3: glColor3d(v[0], v[1], v[2]);
428  break;
429  case 4: glColor4d(v[0], v[1], v[2], v[3]);
430  break;
431  }
432  }
433 
434 
438  template <class P1, class P2> inline void glLine(const P1& x1, const P2& x2) {
439  glBegin(GL_LINES);
440  glVertex(x1);
441  glVertex(x2);
442  glEnd();
443  }
444 
449  template<class C> inline void glVertex( const C & list )
450  {
451  for(typename C::const_iterator v = list.begin(); v != list.end(); ++v)
452  glVertex(*v);
453  }
454 
459  inline void glColor(const CVD::Rgb<byte>& c)
460  {
461  glColor3ub(c.red, c.green, c.blue);
462  }
463 
468  inline void glColor(const CVD::Rgb<float>& c)
469  {
470  glColor3f(c.red, c.green, c.blue);
471  }
472 
477  inline void glColor3(const CVD::Rgb8& c)
478  {
479  glColor3ub(c.red, c.green, c.blue);
480  }
481 
486  inline void glColor4(const CVD::Rgb8& c)
487  {
488  glColor4ub(c.red, c.green, c.blue, c.dummy);
489  }
490 
495  inline void glColor(const CVD::Rgba<unsigned char>& c)
496  {
497  glColor4ub(c.red, c.green, c.blue, c.alpha);
498  }
499 
504  inline void glColor(const CVD::Rgba<float>& c)
505  {
506  glColor4f(c.red, c.green, c.blue, c.alpha);
507  }
508 
513  template<class C> inline void glDrawPixels(const SubImage<C>& i)
514  {
515  ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
516  ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
518  ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
519  }
520 
525  template<class C> inline void glReadPixels(BasicImage<C>& i, ImageRef origin=ImageRef(0,0))
526  {
527  ::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
528  }
529 
534  template<class C> inline Image<C> glReadPixels(ImageRef size, ImageRef origin=ImageRef(0,0))
535  {
536  Image<C> i(size);
537  ::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
538  return i;
539  }
540 
545  template<class C> inline void glTexSubImage2D( const SubImage<C> &i, GLint xoffset = 0, GLint yoffset = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
546  {
547  ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
548  ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
549  ::glTexSubImage2D(target, level, xoffset, yoffset, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
550  ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
551  }
552 
557  template<class C> inline void glTexImage2D( const SubImage<C> &i, GLint border = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
558  {
559  ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
560  ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
561  ::glTexImage2D(target, level, gl::data<C>::format, i.size().x, i.size().y, border, gl::data<C>::format, gl::data<C>::type, i.data());
562  ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
563  }
564 
567  inline void glPrintErrors(void){
568  GLenum code;
569  while((code = glGetError()) != GL_NO_ERROR){
570  std::cout << "GL:" << code << ":" << gluGetString(code) << std::endl;
571  }
572  }
573 
577 
580  void glSetFont( const std::string & fontname );
581 
583  const std::string & glGetFont();
584 
586  enum TEXT_STYLE {
587  FILL = 0,
588  OUTLINE = 1,
589  NICE = 2
590  };
591 
601  std::pair<double, double> glDrawText(const std::string & text, enum TEXT_STYLE style = NICE, double spacing = 1.5, double kerning = 0.1);
602 
604  std::pair<double, double> glGetExtends(const std::string & text, double spacing = 1.5, double kerning = 0.1);
605 
607 
608 };
609 
610 #endif
int y
The y co-ordinate.
Definition: image_ref.h:180
int x
The x co-ordinate.
Definition: image_ref.h:179
T red
The red component.
Definition: rgb.h:57
Definition: rgb.h:45
SO3< Precision > & get_rotation()
Returns the rotation part of the transformation as a SO3.
Definition: se3.h:64
void glDrawPixels(const SubImage< C > &i)
Definition: gl_helpers.h:513
unsigned char dummy
The 4th byte, usually either ignored or used to represent the alpha value.
Definition: rgb8.h:38
Definition: rgba.h:38
const std::string & glGetFont()
returns the name of the currently active font
void glClearColor(const TooN::Vector< 4 > &v)
Definition: gl_helpers.h:405
void glReadPixels(BasicImage< C > &i, ImageRef origin=ImageRef(0, 0))
Definition: gl_helpers.h:525
void glColor3(const CVD::Rgb8 &c)
Definition: gl_helpers.h:477
T green
The green component.
Definition: rgba.h:51
std::pair< double, double > glGetExtends(const std::string &text, double spacing=1.5, double kerning=0.1)
returns the size of the bounding box of a text to be rendered, similar to glDrawText but without any ...
std::pair< double, double > glDrawText(const std::string &text, enum TEXT_STYLE style=NICE, double spacing=1.5, double kerning=0.1)
Definition: abs.h:24
SO2< Precision > & get_rotation()
Returns the rotation part of the transformation as a SO2.
Definition: se2.h:60
renders glyphs as filled polygons
Definition: gl_helpers.h:587
int row_stride() const
What is the row stride of the image?
Definition: image.h:376
T blue
The blue component.
Definition: rgb.h:59
void glTexSubImage2D(const SubImage< C > &i, GLint xoffset=0, GLint yoffset=0, GLenum target=GL_TEXTURE_2D, GLint level=0)
Definition: gl_helpers.h:545
void glNormal(const TooN::Vector< 3 > &n)
Definition: gl_helpers.h:193
void glOrtho(const CVD::ImageRef &size, const double nearPlane=-1.0, const double farPlane=1.0)
Definition: gl_helpers.h:324
void glColor(const TooN::Vector< 3 > &v)
Definition: gl_helpers.h:387
void glVertex(const ImageRef &i)
Definition: gl_helpers.h:50
void glSetFont(const std::string &fontname)
unsigned char green
The green component.
Definition: rgb8.h:36
void glRect(const ImageRef &p, const ImageRef &q)
Definition: gl_helpers.h:75
void glRasterPos(const ImageRef &i)
Definition: gl_helpers.h:66
void glTranslate(const ImageRef &v)
Definition: gl_helpers.h:201
ImageRef size() const
What is the size of this image?
Definition: image.h:370
void glFrustum(const TooN::Vector< 4, P, A > &params, double width, double height, double nearPlane=0.1, double farPlane=100)
Definition: gl_helpers.h:351
T red
The red component.
Definition: rgba.h:50
T alpha
The alpha component.
Definition: rgba.h:53
const T * data() const
Returns the raw image data.
Definition: image.h:326
unsigned char red
The red component.
Definition: rgb8.h:35
Vector< 2, Precision > & get_translation()
Returns the translation part of the transformation as a Vector.
Definition: se2.h:64
Vector< 3, Precision > & get_translation()
Returns the translation part of the transformation as a Vector.
Definition: se3.h:69
void glTexCoord(const ImageRef &i)
Definition: gl_helpers.h:58
T green
The green component.
Definition: rgb.h:58
Vector< Size, Precision > unit(const Vector< Size, Precision, Base > &v)
Definition: helpers.h:126
void glPrintErrors(void)
Definition: gl_helpers.h:567
Definition: rgb8.h:33
TEXT_STYLE
different style for font rendering
Definition: gl_helpers.h:586
void glTexImage2D(const SubImage< C > &i, GLint border=0, GLenum target=GL_TEXTURE_2D, GLint level=0)
Definition: gl_helpers.h:557
unsigned char blue
The blue component.
Definition: rgb8.h:37
void glMultMatrix(const TooN::Matrix< N, N, P, A > &m)
Definition: gl_helpers.h:237
renders glyphs filled with antialiased outlines
Definition: gl_helpers.h:589
const Matrix< 3, 3, Precision > & get_matrix() const
Returns the SO3 as a Matrix<3>
Definition: so3.h:143
const Matrix< 2, 2, Precision > & get_matrix() const
Returns the SO2 as a Matrix<2>
Definition: so2.h:112
void glLine(const P1 &x1, const P2 &x2)
Definition: gl_helpers.h:438
#define cout
Definition: Bundle.cpp:16
renders glyphs as outlines with GL_LINES
Definition: gl_helpers.h:588
void glColor4(const CVD::Rgb8 &c)
Definition: gl_helpers.h:486
T blue
The blue component.
Definition: rgba.h:52