CVD 0.8
cvd/gles1_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_GLES1_HELPERS_H
00022 #define CVD_GLES1_HELPERS_H
00023 
00024 #include <iostream>
00025 #include <map>
00026 #include <utility>
00027 
00028 #include <cvd/image_ref.h>
00029 #include <cvd/image.h>
00030 #include <cvd/byte.h>
00031 #include <cvd/rgb.h>
00032 #include <cvd/byte.h>
00033 #include <cvd/rgb8.h>
00034 #include <cvd/rgba.h>
00035 #include <cvd/config.h>
00036 // #include <OpenGLES/ES1/gl.h>
00037 #include <cvd/internal/gles1_types.h>
00038 
00039 #ifdef CVD_HAVE_TOON
00040 #include <TooN/TooN.h>
00041 #include <cvd/se3.h>
00042 #include <cvd/so3.h>
00043 #include <cvd/se2.h>
00044 #include <cvd/so2.h>
00045 #endif
00046 
00047 namespace CVD
00048 {
00049 
00053     inline void glTranslate( const ImageRef & v )
00054     {
00055         glTranslatef(v.x, v.y, 0);
00056     }
00057 
00061     template <int N, typename P, typename A> inline void glTranslate( const TooN::Vector<N, P, A> & v)
00062     {
00063         glTranslatef(v[0], v[1], v[2]);
00064     }
00065 
00070     template <typename P, typename A> inline void glTranslate( const TooN::Vector<2, P, A> & v)
00071     {
00072         glTranslatef(v[0], v[1], 0);
00073     }
00074 
00079     template <typename P, typename A> inline void glTranslate( const TooN::Vector<1, P, A> & v)
00080     {
00081         glTranslatef(v[0], 0, 0);
00082     }
00083 
00089     template <int N, class P, class A> inline void glMultMatrix( const TooN::Matrix<N,N,P,A> & m )
00090     {
00091         GLfloat glm[16];
00092         glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = m[2][0]; glm[3] = m[3][0];
00093         glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = m[2][1]; glm[7] = m[3][1];
00094         glm[8] = m[0][2]; glm[9] = m[1][2]; glm[10] = m[2][2]; glm[11] = m[3][2];
00095         glm[12] = m[0][3]; glm[13] = m[1][3]; glm[14] = m[2][3]; glm[15] = m[3][3];
00096         glMultMatrixf(glm);
00097     }
00098 
00104     template <class P, class A> inline void glMultMatrix( const TooN::Matrix<3,3,P,A> & m )
00105     {
00106         GLfloat glm[16];
00107         glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = m[2][0]; glm[3] = 0;
00108         glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = m[2][1]; glm[7] = 0;
00109         glm[8] = m[0][2]; glm[9] = m[1][2]; glm[10] = m[2][2]; glm[11] = 0;
00110         glm[12] = 0; glm[13] = 0; glm[14] = 0; glm[15] = 1;
00111         glMultMatrixf(glm);
00112     }
00113 
00119     template <class P, class A> inline void glMultMatrix( const TooN::Matrix<2,2,P,A> & m )
00120     {
00121         GLfloat glm[16];
00122         glm[0] = m[0][0]; glm[1] = m[1][0]; glm[2] = 0; glm[3] = 0;
00123         glm[4] = m[0][1]; glm[5] = m[1][1]; glm[6] = 0; glm[7] = 0;
00124         glm[8] = 0; glm[9] = 0; glm[10] = 1; glm[11] = 0;
00125         glm[12] = 0; glm[13] = 0; glm[14] = 0; glm[15] = 1;
00126         glMultMatrixf(glm);
00127     }
00128 
00132     template <typename P>
00133     inline void glMultMatrix( const TooN::SO3<P> & so3 )
00134     {
00135         glMultMatrix( so3.get_matrix());
00136     }
00137 
00142     template <typename P>
00143     inline void glMultMatrix( const TooN::SE3<P> & se3 )
00144     {
00145         glTranslate( se3.get_translation());
00146         glMultMatrix( se3.get_rotation());
00147     }
00148 
00152     template <typename P>
00153     inline void glMultMatrix( const TooN::SO2<P> & so2 )
00154     {
00155         glMultMatrix( so2.get_matrix());
00156     }
00157 
00162     template <typename P>
00163     inline void glMultMatrix( const TooN::SE2<P> & se2 )
00164     {
00165         glTranslate( se2.get_translation());
00166         glMultMatrix( se2.get_rotation());
00167     }
00168 
00176     template <typename P> inline void glOrtho( const CVD::ImageRef & size, const P nearPlane = -1.0, const P farPlane = 1.0)
00177     {
00178         ::glOrthof( -0.375, size.x - 0.375, size.y - 0.375, -0.375, nearPlane, farPlane );
00179     }
00180 
00185     template <typename P, typename A> inline void glOrtho( const TooN::Vector<6, P, A> & param)
00186     {
00187         ::glOrthof( param[0], param[1], param[2], param[3], param[4], param[5]);
00188     }
00189 
00202     template <typename P, typename A>
00203     inline void glFrustum( const TooN::Vector<4, P, A> & params, const float width, const float height, const float nearPlane = 0.1, const float farPlane = 100)
00204     {
00205         GLfloat left, right, bottom, top;
00206         left = -nearPlane * params[2] / params[0];
00207         top = nearPlane * params[3] / params[1];
00208         right = nearPlane * ( width - params[2] ) / params[0];
00209         bottom = - nearPlane * ( height - params[3] ) / params[1];
00210         ::glFrustumf( left, right, bottom, top, nearPlane, farPlane );
00211     }
00212 
00221     template <class CAMERA> inline void glFrustum( const CAMERA & camera, const float width, const float height, const float nearPlane = 0.1, const float farPlane = 100)
00222     {
00223         glFrustum( camera.get_parameters().template slice<0,4>(), width, height, nearPlane, farPlane);
00224     }
00225 
00230     template <typename P, typename A> inline void glFrustum( const TooN::Vector<6, P, A> & param)
00231     {
00232         ::glFrustumf( param[0], param[1], param[2], param[3], param[4], param[5]);
00233     }
00234     
00239     template <typename P, typename A> inline void glColor(const TooN::Vector<3, P, A> & v)
00240     {
00241         glColor4f(v[0], v[1], v[2], 1);
00242     }
00243 
00248     template <typename P, typename A> inline void glColor(const TooN::Vector<4, P, A>& v)
00249     {
00250         glColor4f(v[0], v[1], v[2], v[3]);
00251     }
00252     
00257     template <typename P, typename A> inline void glClearColor(const TooN::Vector<4, P, A> & v)
00258     {
00259 		::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], (GLclampf)v[3]);
00260     }
00261 
00266     template <typename P, typename A> inline void glClearColor(const TooN::Vector<3, P, A>& v)
00267     {
00268 		::glClearColor((GLclampf)v[0], (GLclampf)v[1], (GLclampf)v[2], 1);
00269     }
00270 
00274     template <typename P, typename A> inline void glColor(const TooN::Vector<-1, P, A> & v)
00275     {
00276         switch(v.size()){
00277         case 3: glColor4f(v[0], v[1], v[2], 1);
00278             break;
00279         case 4: glColor4f(v[0], v[1], v[2], v[3]);
00280             break;
00281         }
00282     }
00283 
00288     inline void glColor(const CVD::Rgb<byte>& c)
00289     {
00290         glColor4ub(c.red, c.green, c.blue, 255);
00291     }
00292 
00297     inline void glColor(const CVD::Rgb<float>& c)
00298     {
00299         glColor4f(c.red, c.green, c.blue, 1);
00300     }
00301 
00306     inline void glColor3(const CVD::Rgb8& c)
00307     {
00308         glColor4ub(c.red, c.green, c.blue, 255);
00309     }
00310 
00315     inline void glColor4(const CVD::Rgb8& c)
00316     {
00317         glColor4ub(c.red, c.green, c.blue, c.dummy);
00318     }
00319 
00324     inline void glColor(const CVD::Rgba<unsigned char>& c)
00325     {
00326         glColor4ub(c.red, c.green, c.blue, c.alpha);
00327     }
00328 
00333     inline void glColor(const CVD::Rgba<float>& c)
00334     {
00335         glColor4f(c.red, c.green, c.blue, c.alpha);
00336     }
00337 
00342     template<class C> inline void glReadPixels(BasicImage<C>& i, const ImageRef origin=ImageRef(0,0))
00343     {
00344 		::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00345     }
00346 
00351     template<class C> inline Image<C> glReadPixels(ImageRef size, const ImageRef origin=ImageRef(0,0))
00352     {
00353         Image<C> i(size);
00354 		::glReadPixels(origin.x, origin.y, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00355         return i;
00356     }
00357 
00362     template<class C> inline void glTexSubImage2D( const SubImage<C> &i, GLint xoffset = 0, GLint yoffset = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
00363     {
00364         ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00365 		::glTexSubImage2D(target, level, xoffset, yoffset, i.size().x, i.size().y, gl::data<C>::format, gl::data<C>::type, i.data());
00366     }
00367 
00372     template<class C> inline void glTexImage2D( const SubImage<C> &i, GLint border = 0, GLenum target = GL_TEXTURE_2D, GLint level = 0)
00373     {
00374         ::glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00375 		::glTexImage2D(target, level, gl::data<C>::format, i.size().x, i.size().y, border, gl::data<C>::format, gl::data<C>::type, i.data());
00376     }
00377 
00380     inline void glPrintErrors(void){
00381         GLenum code;
00382         while((code = glGetError()) != GL_NO_ERROR){
00383             std::cout << "GL:" << code << std::endl;
00384         }
00385     }
00386 
00390 
00393     void glSetFont( const std::string & fontname );
00394 
00396     const std::string & glGetFont();
00397 
00399     enum TEXT_STYLE {
00400         FILL = 0,       
00401         OUTLINE = 1,    
00402         NICE = 2        
00403     };
00404 
00414     std::pair<double, double> glDrawText(const std::string & text, enum TEXT_STYLE style = NICE, double spacing = 1.5, double kerning = 0.1);
00415 
00417     std::pair<double, double> glGetExtends(const std::string & text, double spacing = 1.5, double kerning = 0.1);
00418 
00420 
00421 };
00422 
00423 #endif