cvd/gl_helpers.h

00001 /*
00002     This file is part of the CVD Library.
00003 
00004     Copyright (C) 2005 The Authors
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2.1 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to the Free Software
00018     Foundation, Inc.,
00019     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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
00074     inline void glMultiTexCoord(GLenum unit, const ImageRef& i)
00075     {
00076             glMultiTexCoord2i(unit,  i.x, i.y);
00077     }
00078 #endif
00082     inline void glRasterPos(const ImageRef& i)
00083     {
00084         glRasterPos2i(i.x, i.y);
00085     }
00086 
00090     template <class P1, class P2> inline void glLine(const P1& x1, const P2& x2) {
00091     glBegin(GL_LINES);
00092     glVertex(x1);
00093     glVertex(x2);
00094     glEnd();
00095     }
00096 
00097     #ifdef CVD_HAVE_TOON
00101     inline void glVertex(const TooN::Vector<2>& v)
00102     {
00103         glVertex2d(v[0], v[1]);
00104     }
00105 
00109     inline void glVertex(const TooN::Vector<3>& v)
00110     {
00111         glVertex3d(v[0], v[1], v[2]);
00112     }
00113 
00117     inline void glVertex(const TooN::Vector<4>& v)
00118     {
00119         glVertex4d(v[0], v[1], v[2], v[3]);
00120     }
00121 
00125     inline void glTexCoord(const TooN::Vector<2>& v)
00126     {
00127         glTexCoord2d(v[0], v[1]);
00128     }
00129 
00133     inline void glTexCoord(const TooN::Vector<3>& v)
00134     {
00135         glTexCoord3d(v[0], v[1], v[2]);
00136     }
00137 
00141     inline void glTexCoord(const TooN::Vector<4>& v)
00142     {
00143         glTexCoord4d(v[0], v[1], v[2], v[3]);
00144     }
00145 
00146 #ifdef GL_GLEXT_PROTOTYPES
00151     inline void glMultiTexCoord(GLenum unit, const TooN::Vector<2>& v)
00152     {
00153             glMultiTexCoord2d(unit, v[0], v[1]);
00154     }
00155 
00160     inline void glMultiTexCoord(GLenum unit, const TooN::Vector<3>& v)
00161     {
00162             glMultiTexCoord3d(unit, v[0], v[1], v[2]);
00163     }
00164 
00169     inline void glMultiTexCoord(GLenum unit, const TooN::Vector<4>& v)
00170     {
00171             glMultiTexCoord4d(unit, v[0], v[1], v[2], v[3]);
00172     }
00173 #endif
00174 
00178     inline void glRasterPos(const TooN::Vector<2>& v)
00179     {
00180         glRasterPos2d(v[0], v[1]);
00181     }
00182 
00186     inline void glRasterPos(const TooN::Vector<3>& v)
00187     {
00188         glRasterPos3d(v[0], v[1], v[2]);
00189     }
00190 
00194     inline void glRasterPos(const TooN::Vector<4>& v)
00195     {
00196         glRasterPos4d(v[0], v[1], v[2], v[3]);
00197     }
00198 
00202     inline void glNormal(const TooN::Vector<3>& n)
00203     {
00204             glNormal3d(n[0], n[1], n[2]);
00205     }
00206 
00210     inline void glTranslate( const ImageRef & v )
00211     {
00212         glTranslatef( static_cast<GLfloat>(v.x), static_cast<GLfloat>(v.y), 0);
00213     }
00214 
00218     template <int N> inline void glTranslate( const TooN::Vector<N> & v)
00219     {
00220         glTranslated(v[0], v[1], v[2]);
00221     }
00222 
00227     template <> inline void glTranslate( const TooN::Vector<2> & v)
00228     {
00229         glTranslated(v[0], v[1], 0);
00230     }
00231 
00236     template <> inline void glTranslate( const TooN::Vector<1> & v)
00237     {
00238         glTranslated(v[0], 0, 0);
00239     }
00240 
00246     template <int N, class A> inline void glMultMatrix( const TooN::FixedMatrix<N,N,A> & m )
00247     {
00248         GLdouble glm[16];
00249         glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = m[2][0]; glm[3] = m[3][0];
00250         glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = m[2][1]; glm[7] = m[3][1];
00251         glm[8] = m[0][2]; glm[9] = m[1][2]; glm[10] = m[2][2]; glm[11] = m[3][2];
00252         glm[12] = m[0][3]; glm[13] = m[1][3]; glm[14] = m[2][3]; glm[15] = m[3][3];
00253         glMultMatrixd(glm);
00254     }
00255 
00261     template <class A> inline void glMultMatrix( const TooN::FixedMatrix<3,3,A> & m )
00262     {
00263         GLdouble glm[16];
00264         glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = m[2][0]; glm[3] = 0;
00265         glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = m[2][1]; glm[7] = 0;
00266         glm[8] = m[0][2]; glm[9] = m[1][2]; glm[10] = m[2][2]; glm[11] = 0;
00267         glm[12] = 0; glm[13] = 0; glm[14] = 0; glm[15] = 1;
00268         glMultMatrixd(glm);
00269     }
00270 
00276     template <class A> inline void glMultMatrix( const TooN::FixedMatrix<2,2,A> & m )
00277     {
00278         GLdouble glm[16];
00279         glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = 0; glm[3] = 0;
00280         glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = 0; glm[7] = 0;
00281         glm[8] = 0; glm[9] = 0; glm[10] = 1; glm[11] = 0;
00282         glm[12] = 0; glm[13] = 0; glm[14] = 0; glm[15] = 1;
00283         glMultMatrixd(glm);
00284     }
00285 
00289     inline void glMultMatrix( const CVD::SO3 & so3 )
00290     {
00291         glMultMatrix( so3.get_matrix());
00292     }
00293 
00298     inline void glMultMatrix( const CVD::SE3 & se3 )
00299     {
00300         glTranslate( se3.get_translation());
00301         glMultMatrix( se3.get_rotation());
00302     }
00303 
00307     inline void glMultMatrix( const CVD::SO2 & so2 )
00308     {
00309         glMultMatrix( so2.get_matrix());
00310     }
00311 
00316     inline void glMultMatrix( const CVD::SE2 & se2 )
00317     {
00318         glTranslate( se2.get_translation());
00319         glMultMatrix( se2.get_rotation());
00320     }
00321 
00329     inline void glOrtho( const CVD::ImageRef & size, const double nearPlane = -1.0, const double farPlane = 1.0)
00330     {
00331 	    ::glOrtho( -0.375, size.x - 0.375, size.y - 0.375, -0.375, nearPlane, farPlane );
00332     }
00333 
00338     inline void glOrtho( const TooN::Vector<6> & param)
00339     {
00340 		::glOrtho( param[0], param[1], param[2], param[3], param[4], param[5]);
00341     }
00342 
00355     template <class A>
00356     inline void glFrustum( const TooN::FixedVector<4,A> & params, double width, double height, double near = 0.1, double far = 100)
00357     {
00358         GLdouble left, right, bottom, top;
00359         left = -near * params[2] / params[0];
00360         top = near * params[3] / params[1];
00361         right = near * ( width - params[2] ) / params[0];
00362         bottom = - near * ( height - params[3] ) / params[1];
00363 		::glFrustum( left, right, bottom, top, near, far );
00364     }
00365 
00374     template <class CAMERA> inline void glFrustum( const CAMERA & camera, double width, double height, double near = 0.1, double far = 100)
00375     {
00376         glFrustum( camera.get_parameters().template slice<0,4>(), width, height, near, far);
00377     }
00378 
00383     inline void glFrustum( const TooN::Vector<6> & param)
00384     {
00385 		::glFrustum( param[0], param[1], param[2], param[3], param[4], param[5]);
00386     }
00387 
00392     inline void glColor(const TooN::Vector<3>& v)
00393     {
00394         glColor3d(v[0], v[1], v[2]);
00395     }
00396 
00401     inline void glColor(const TooN::Vector<4>& v)
00402     {
00403         glColor4d(v[0], v[1], v[2], v[3]);
00404     }
00405 
00410     inline void glClearColor(const TooN::Vector<4>& v)
00411     {
00412 		::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], (GLclampf)v[3]);
00413     }
00414 
00419     inline void glClearColor(const TooN::Vector<3>& v)
00420     {
00421 		::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], 0);
00422     }
00423 
00424 
00425 
00429     inline void glColor(const TooN::Vector<-1> & v)
00430     {
00431         switch(v.size()){
00432         case 3: glColor3d(v[0], v[1], v[2]);
00433             break;
00434         case 4: glColor4d(v[0], v[1], v[2], v[3]);
00435             break;
00436         }
00437     }
00438     #endif
00439 
00444     template<class C> inline void glVertex( const C & list )
00445     {
00446         for(typename C::const_iterator v = list.begin(); v != list.end(); ++v)
00447             glVertex(*v);
00448     }
00449 
00454     inline void glColor(const CVD::Rgb<byte>& c)
00455     {
00456         glColor3ub(c.red, c.green, c.blue);
00457     }
00458 
00463     inline void glColor(const CVD::Rgb<float>& c)
00464     {
00465         glColor3f(c.red, c.green, c.blue);
00466     }
00467 
00472     inline void glColor3(const CVD::Rgb8& c)
00473     {
00474         glColor3ub(c.red, c.green, c.blue);
00475     }
00476 
00481     inline void glColor4(const CVD::Rgb8& c)
00482     {
00483         glColor4ub(c.red, c.green, c.blue, c.dummy);
00484     }
00485 
00490     inline void glColor(const CVD::Rgba<unsigned char>& c)
00491     {
00492         glColor4ub(c.red, c.green, c.blue, c.alpha);
00493     }
00494 
00499     inline void glColor(const CVD::Rgba<float>& c)
00500     {
00501         glColor4f(c.red, c.green, c.blue, c.alpha);
00502     }
00503 
00508     template<class C> inline void glDrawPixels(const SubImage<C>& i)
00509     {
00510         ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00511         ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
00512 		::glDrawPixels(i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00513         ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
00514     }
00515 
00520     template<class C> inline void glReadPixels(BasicImage<C>& i, ImageRef origin=ImageRef(0,0))
00521     {
00522 		::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00523     }
00524 
00529     template<class C> inline Image<C> glReadPixels(ImageRef size, ImageRef origin=ImageRef(0,0))
00530     {
00531         Image<C> i(size);
00532 		::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00533         return i;
00534     }
00535 
00540     template<class C> inline void glTexSubImage2D( const SubImage<C> &i, GLint xoffset = 0, GLint yoffset = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
00541     {
00542         ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00543         ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
00544 		::glTexSubImage2D(target, level, xoffset, yoffset, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00545         ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
00546     }
00547 
00552     template<class C> inline void glTexImage2D( const SubImage<C> &i, GLint border = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
00553     {
00554         ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00555         ::glPixelStorei(GL_UNPACK_ROW_LENGTH, i.row_stride());
00556 		::glTexImage2D(target, level, gl::data<C>::format, i.size().x, i.size().y, border, gl::data<C>::format, gl::data<C>::type, i.data());
00557         ::glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
00558     }
00559 
00562     inline void glPrintErrors(void){
00563         GLenum code;
00564         while((code = glGetError()) != GL_NO_ERROR){
00565             std::cout << "GL:" << code << ":" << gluGetString(code) << std::endl;
00566         }
00567     }
00568 
00572 
00575     void glSetFont( const std::string & fontname );
00576 
00578     const std::string & glGetFont();
00579 
00581     enum TEXT_STYLE {
00582         FILL = 0,       
00583         OUTLINE = 1,    
00584         NICE = 2        
00585     };
00586 
00596     std::pair<double, double> glDrawText(const std::string & text, enum TEXT_STYLE style = NICE, double spacing = 1.5, double kerning = 0.1);
00597 
00599     std::pair<double, double> glGetExtends(const std::string & text, double spacing = 1.5, double kerning = 0.1);
00600 
00602 
00603 };
00604 
00605 #endif

Generated on Wed Feb 18 10:23:01 2009 for CVD by  doxygen 1.5.3