00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef CVD_GL_HELPERS_H
00022 #define CVD_GL_HELPERS_H
00023
00024 #include <iostream>
00025 #include <map>
00026
00027 #include <cvd/image_ref.h>
00028 #include <cvd/image.h>
00029 #include <cvd/byte.h>
00030 #include <cvd/rgb.h>
00031 #include <cvd/byte.h>
00032 #include <cvd/rgb8.h>
00033 #include <cvd/rgba.h>
00034 #include <cvd/config.h>
00035 #ifdef WIN32
00036 #include <windows.h>
00037 #endif
00038 #include <GL/gl.h>
00039 #include <GL/glu.h>
00040 #include <cvd/internal/gl_types.h>
00041
00042 #ifdef CVD_HAVE_TOON
00043 #include <TooN/TooN.h>
00044 #include <cvd/se3.h>
00045 #include <cvd/so3.h>
00046 #include <cvd/se2.h>
00047 #include <cvd/so2.h>
00048 #endif
00049
00050 namespace CVD
00051 {
00052
00056 inline void glVertex(const ImageRef& i)
00057 {
00058 glVertex2i(i.x, i.y);
00059 }
00060
00064 inline void glTexCoord(const ImageRef& i)
00065 {
00066 glTexCoord2i(i.x, i.y);
00067 }
00068
00069 #ifdef GL_GLEXT_PROTOTYPES
00070
00071
00072
00073
00074 inline void glMultiTexCoord(GLenum unit, const ImageRef& i)
00075 {
00076 glMultiTexCoord2i(unit, i.x, i.y);
00077 }
00078 #endif
00079
00080
00081
00082 inline void glRasterPos(const ImageRef& i)
00083 {
00084 glRasterPos2i(i.x, i.y);
00085 }
00086
00091 inline void glRect( const ImageRef & p, const ImageRef & q)
00092 {
00093 glRecti(p.x, p.y, q.x, q.y);
00094 }
00095
00096 #ifdef CVD_HAVE_TOON
00097
00098
00099
00100 inline void glVertex(const TooN::Vector<2>& v)
00101 {
00102 glVertex2d(v[0], v[1]);
00103 }
00104
00108 inline void glVertex(const TooN::Vector<3>& v)
00109 {
00110 glVertex3d(v[0], v[1], v[2]);
00111 }
00112
00116 inline void glVertex(const TooN::Vector<4>& v)
00117 {
00118 glVertex4d(v[0], v[1], v[2], v[3]);
00119 }
00120
00124 inline void glTexCoord(const TooN::Vector<2>& v)
00125 {
00126 glTexCoord2d(v[0], v[1]);
00127 }
00128
00132 inline void glTexCoord(const TooN::Vector<3>& v)
00133 {
00134 glTexCoord3d(v[0], v[1], v[2]);
00135 }
00136
00140 inline void glTexCoord(const TooN::Vector<4>& v)
00141 {
00142 glTexCoord4d(v[0], v[1], v[2], v[3]);
00143 }
00144
00149 inline void glRect( const TooN::Vector<2> & p, const TooN::Vector<2> & q)
00150 {
00151 glRectd(p[0], p[1], q[0], q[1]);
00152 }
00153
00154 #ifdef GL_GLEXT_PROTOTYPES
00155
00156
00157
00158
00159 inline void glMultiTexCoord(GLenum unit, const TooN::Vector<2>& v)
00160 {
00161 glMultiTexCoord2d(unit, v[0], v[1]);
00162 }
00163
00168 inline void glMultiTexCoord(GLenum unit, const TooN::Vector<3>& v)
00169 {
00170 glMultiTexCoord3d(unit, v[0], v[1], v[2]);
00171 }
00172
00177 inline void glMultiTexCoord(GLenum unit, const TooN::Vector<4>& v)
00178 {
00179 glMultiTexCoord4d(unit, v[0], v[1], v[2], v[3]);
00180 }
00181 #endif
00182
00186 inline void glRasterPos(const TooN::Vector<2>& v)
00187 {
00188 glRasterPos2d(v[0], v[1]);
00189 }
00190
00194 inline void glRasterPos(const TooN::Vector<3>& v)
00195 {
00196 glRasterPos3d(v[0], v[1], v[2]);
00197 }
00198
00202 inline void glRasterPos(const TooN::Vector<4>& v)
00203 {
00204 glRasterPos4d(v[0], v[1], v[2], v[3]);
00205 }
00206
00210 inline void glNormal(const TooN::Vector<3>& n)
00211 {
00212 glNormal3d(n[0], n[1], n[2]);
00213 }
00214
00218 inline void glTranslate( const ImageRef & v )
00219 {
00220 glTranslatef( static_cast<GLfloat>(v.x), static_cast<GLfloat>(v.y), 0);
00221 }
00222
00226 template <int N> inline void glTranslate( const TooN::Vector<N> & v)
00227 {
00228 glTranslated(v[0], v[1], v[2]);
00229 }
00230
00235 template <> inline void glTranslate( const TooN::Vector<2> & v)
00236 {
00237 glTranslated(v[0], v[1], 0);
00238 }
00239
00244 template <> inline void glTranslate( const TooN::Vector<1> & v)
00245 {
00246 glTranslated(v[0], 0, 0);
00247 }
00248
00254 template <int N, class P, class A> inline void glMultMatrix( const TooN::Matrix<N,N,P,A> & m )
00255 {
00256 GLdouble glm[16];
00257 glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = m[2][0]; glm[3] = m[3][0];
00258 glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = m[2][1]; glm[7] = m[3][1];
00259 glm[8] = m[0][2]; glm[9] = m[1][2]; glm[10] = m[2][2]; glm[11] = m[3][2];
00260 glm[12] = m[0][3]; glm[13] = m[1][3]; glm[14] = m[2][3]; glm[15] = m[3][3];
00261 glMultMatrixd(glm);
00262 }
00263
00269 template <class P, class A> inline void glMultMatrix( const TooN::Matrix<3,3,P,A> & m )
00270 {
00271 GLdouble glm[16];
00272 glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = m[2][0]; glm[3] = 0;
00273 glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = m[2][1]; glm[7] = 0;
00274 glm[8] = m[0][2]; glm[9] = m[1][2]; glm[10] = m[2][2]; glm[11] = 0;
00275 glm[12] = 0; glm[13] = 0; glm[14] = 0; glm[15] = 1;
00276 glMultMatrixd(glm);
00277 }
00278
00284 template <class P, class A> inline void glMultMatrix( const TooN::Matrix<2,2,P,A> & m )
00285 {
00286 GLdouble glm[16];
00287 glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = 0; glm[3] = 0;
00288 glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = 0; glm[7] = 0;
00289 glm[8] = 0; glm[9] = 0; glm[10] = 1; glm[11] = 0;
00290 glm[12] = 0; glm[13] = 0; glm[14] = 0; glm[15] = 1;
00291 glMultMatrixd(glm);
00292 }
00293
00297 template <typename P>
00298 inline void glMultMatrix( const TooN::SO3<P> & so3 )
00299 {
00300 glMultMatrix( so3.get_matrix());
00301 }
00302
00307 template <typename P>
00308 inline void glMultMatrix( const TooN::SE3<P> & se3 )
00309 {
00310 glTranslate( se3.get_translation());
00311 glMultMatrix( se3.get_rotation());
00312 }
00313
00317 template <typename P>
00318 inline void glMultMatrix( const TooN::SO2<P> & so2 )
00319 {
00320 glMultMatrix( so2.get_matrix());
00321 }
00322
00327 template <typename P>
00328 inline void glMultMatrix( const TooN::SE2<P> & se2 )
00329 {
00330 glTranslate( se2.get_translation());
00331 glMultMatrix( se2.get_rotation());
00332 }
00333
00341 inline void glOrtho( const CVD::ImageRef & size, const double nearPlane = -1.0, const double farPlane = 1.0)
00342 {
00343 ::glOrtho( -0.375, size.x - 0.375, size.y - 0.375, -0.375, nearPlane, farPlane );
00344 }
00345
00350 inline void glOrtho( const TooN::Vector<6> & param)
00351 {
00352 ::glOrtho( param[0], param[1], param[2], param[3], param[4], param[5]);
00353 }
00354
00367 template <typename P, typename A>
00368 inline void glFrustum( const TooN::Vector<4,P,A> & params, double width, double height, double nearPlane = 0.1, double farPlane = 100)
00369 {
00370 GLdouble left, right, bottom, top;
00371 left = -nearPlane * params[2] / params[0];
00372 top = nearPlane * params[3] / params[1];
00373 right = nearPlane * ( width - params[2] ) / params[0];
00374 bottom = - nearPlane * ( height - params[3] ) / params[1];
00375 ::glFrustum( left, right, bottom, top, nearPlane, farPlane );
00376 }
00377
00386 template <class CAMERA> inline void glFrustum( const CAMERA & camera, double width, double height, double nearPlane = 0.1, double farPlane = 100)
00387 {
00388 glFrustum( camera.get_parameters().template slice<0,4>(), width, height, nearPlane, farPlane);
00389 }
00390
00395 inline void glFrustum( const TooN::Vector<6> & param)
00396 {
00397 ::glFrustum( param[0], param[1], param[2], param[3], param[4], param[5]);
00398 }
00399
00404 inline void glColor(const TooN::Vector<3>& v)
00405 {
00406 glColor3d(v[0], v[1], v[2]);
00407 }
00408
00413 inline void glColor(const TooN::Vector<4>& v)
00414 {
00415 glColor4d(v[0], v[1], v[2], v[3]);
00416 }
00417
00422 inline void glClearColor(const TooN::Vector<4>& v)
00423 {
00424 ::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], (GLclampf)v[3]);
00425 }
00426
00431 inline void glClearColor(const TooN::Vector<3>& v)
00432 {
00433 ::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], 0);
00434 }
00435
00436
00437
00441 inline void glColor(const TooN::Vector<-1> & v)
00442 {
00443 switch(v.size()){
00444 case 3: glColor3d(v[0], v[1], v[2]);
00445 break;
00446 case 4: glColor4d(v[0], v[1], v[2], v[3]);
00447 break;
00448 }
00449 }
00450 #endif
00451
00455 template <class P1, class P2> inline void glLine(const P1& x1, const P2& x2) {
00456 glBegin(GL_LINES);
00457 glVertex(x1);
00458 glVertex(x2);
00459 glEnd();
00460 }
00461
00466 template<class C> inline void glVertex( const C & list )
00467 {
00468 for(typename C::const_iterator v = list.begin(); v != list.end(); ++v)
00469 glVertex(*v);
00470 }
00471
00476 inline void glColor(const CVD::Rgb<byte>& c)
00477 {
00478 glColor3ub(c.red, c.green, c.blue);
00479 }
00480
00485 inline void glColor(const CVD::Rgb<float>& c)
00486 {
00487 glColor3f(c.red, c.green, c.blue);
00488 }
00489
00494 inline void glColor3(const CVD::Rgb8& c)
00495 {
00496 glColor3ub(c.red, c.green, c.blue);
00497 }
00498
00503 inline void glColor4(const CVD::Rgb8& c)
00504 {
00505 glColor4ub(c.red, c.green, c.blue, c.dummy);
00506 }
00507
00512 inline void glColor(const CVD::Rgba<unsigned char>& c)
00513 {
00514 glColor4ub(c.red, c.green, c.blue, c.alpha);
00515 }
00516
00521 inline void glColor(const CVD::Rgba<float>& c)
00522 {
00523 glColor4f(c.red, c.green, c.blue, c.alpha);
00524 }
00525
00530 template<class C> inline void glDrawPixels(const SubImage<C>& i)
00531 {
00532 ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00533 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
00534 ::glDrawPixels(i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00535 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
00536 }
00537
00542 template<class C> inline void glReadPixels(BasicImage<C>& i, ImageRef origin=ImageRef(0,0))
00543 {
00544 ::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00545 }
00546
00551 template<class C> inline Image<C> glReadPixels(ImageRef size, ImageRef origin=ImageRef(0,0))
00552 {
00553 Image<C> i(size);
00554 ::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00555 return i;
00556 }
00557
00562 template<class C> inline void glTexSubImage2D( const SubImage<C> &i, GLint xoffset = 0, GLint yoffset = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
00563 {
00564 ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00565 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
00566 ::glTexSubImage2D(target, level, xoffset, yoffset, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00567 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
00568 }
00569
00574 template<class C> inline void glTexImage2D( const SubImage<C> &i, GLint border = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
00575 {
00576 ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00577 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
00578 ::glTexImage2D(target, level, gl::data<C>::format, i.size().x, i.size().y, border, gl::data<C>::format, gl::data<C>::type, i.data());
00579 ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
00580 }
00581
00584 inline void glPrintErrors(void){
00585 GLenum code;
00586 while((code = glGetError()) != GL_NO_ERROR){
00587 std::cout << "GL:" << code << ":" << gluGetString(code) << std::endl;
00588 }
00589 }
00590
00594
00597 void glSetFont( const std::string & fontname );
00598
00600 const std::string & glGetFont();
00601
00603 enum TEXT_STYLE {
00604 FILL = 0,
00605 OUTLINE = 1,
00606 NICE = 2
00607 };
00608
00618 std::pair<double, double> glDrawText(const std::string & text, enum TEXT_STYLE style = NICE, double spacing = 1.5, double kerning = 0.1);
00619
00621 std::pair<double, double> glGetExtends(const std::string & text, double spacing = 1.5, double kerning = 0.1);
00622
00624
00625 };
00626
00627 #endif