Warning, /include/eigen3/unsupported/Eigen/OpenGLSupport is written in an unsupported language. File is not indexed.
0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009
0010 #ifndef EIGEN_OPENGL_MODULE
0011 #define EIGEN_OPENGL_MODULE
0012
0013 #include "../../Eigen/Geometry"
0014
0015 #if defined(__APPLE_CC__)
0016 #include <OpenGL/gl.h>
0017 #else
0018 #include <GL/gl.h>
0019 #endif
0020
0021 namespace Eigen {
0022
0023 /**
0024 * \defgroup OpenGLSUpport_Module OpenGL Support module
0025 *
0026 * This module provides wrapper functions for a couple of OpenGL functions
0027 * which simplify the way to pass Eigen's object to openGL.
0028 * Here is an example:
0029 *
0030 * \code
0031 * // You need to add path_to_eigen/unsupported to your include path.
0032 * #include <Eigen/OpenGLSupport>
0033 * // ...
0034 * Vector3f x, y;
0035 * Matrix3f rot;
0036 *
0037 * glVertex(y + x * rot);
0038 *
0039 * Quaternion q;
0040 * glRotate(q);
0041 *
0042 * // ...
0043 * \endcode
0044 *
0045 */
0046 //@{
0047
0048 #define EIGEN_GL_FUNC_DECLARATION(FUNC) \
0049 namespace internal { \
0050 template< typename XprType, \
0051 typename Scalar = typename XprType::Scalar, \
0052 int Rows = XprType::RowsAtCompileTime, \
0053 int Cols = XprType::ColsAtCompileTime, \
0054 bool IsGLCompatible = bool(internal::evaluator<XprType>::Flags&LinearAccessBit) \
0055 && bool(XprType::Flags&DirectAccessBit) \
0056 && (XprType::IsVectorAtCompileTime || (XprType::Flags&RowMajorBit)==0)> \
0057 struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl); \
0058 \
0059 template<typename XprType, typename Scalar, int Rows, int Cols> \
0060 struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<XprType,Scalar,Rows,Cols,false> { \
0061 inline static void run(const XprType& p) { \
0062 EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<typename plain_matrix_type_column_major<XprType>::type>::run(p); } \
0063 }; \
0064 } \
0065 \
0066 template<typename Derived> inline void FUNC(const Eigen::DenseBase<Derived>& p) { \
0067 EIGEN_CAT(EIGEN_CAT(internal::gl_,FUNC),_impl)<Derived>::run(p.derived()); \
0068 }
0069
0070
0071 #define EIGEN_GL_FUNC_SPECIALIZATION_MAT(FUNC,SCALAR,ROWS,COLS,SUFFIX) \
0072 namespace internal { \
0073 template< typename XprType> struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<XprType, SCALAR, ROWS, COLS, true> { \
0074 inline static void run(const XprType& p) { FUNC##SUFFIX(p.data()); } \
0075 }; \
0076 }
0077
0078
0079 #define EIGEN_GL_FUNC_SPECIALIZATION_VEC(FUNC,SCALAR,SIZE,SUFFIX) \
0080 namespace internal { \
0081 template< typename XprType> struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<XprType, SCALAR, SIZE, 1, true> { \
0082 inline static void run(const XprType& p) { FUNC##SUFFIX(p.data()); } \
0083 }; \
0084 template< typename XprType> struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<XprType, SCALAR, 1, SIZE, true> { \
0085 inline static void run(const XprType& p) { FUNC##SUFFIX(p.data()); } \
0086 }; \
0087 }
0088
0089
0090 EIGEN_GL_FUNC_DECLARATION (glVertex)
0091 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,int, 2,2iv)
0092 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,short, 2,2sv)
0093 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,float, 2,2fv)
0094 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,double, 2,2dv)
0095 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,int, 3,3iv)
0096 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,short, 3,3sv)
0097 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,float, 3,3fv)
0098 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,double, 3,3dv)
0099 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,int, 4,4iv)
0100 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,short, 4,4sv)
0101 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,float, 4,4fv)
0102 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glVertex,double, 4,4dv)
0103
0104 EIGEN_GL_FUNC_DECLARATION (glTexCoord)
0105 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,int, 2,2iv)
0106 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,short, 2,2sv)
0107 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,float, 2,2fv)
0108 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,double, 2,2dv)
0109 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,int, 3,3iv)
0110 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,short, 3,3sv)
0111 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,float, 3,3fv)
0112 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,double, 3,3dv)
0113 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,int, 4,4iv)
0114 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,short, 4,4sv)
0115 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,float, 4,4fv)
0116 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTexCoord,double, 4,4dv)
0117
0118 EIGEN_GL_FUNC_DECLARATION (glColor)
0119 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,int, 2,2iv)
0120 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,short, 2,2sv)
0121 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,float, 2,2fv)
0122 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,double, 2,2dv)
0123 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,int, 3,3iv)
0124 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,short, 3,3sv)
0125 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,float, 3,3fv)
0126 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,double, 3,3dv)
0127 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,int, 4,4iv)
0128 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,short, 4,4sv)
0129 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,float, 4,4fv)
0130 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glColor,double, 4,4dv)
0131
0132 EIGEN_GL_FUNC_DECLARATION (glNormal)
0133 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glNormal,int, 3,3iv)
0134 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glNormal,short, 3,3sv)
0135 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glNormal,float, 3,3fv)
0136 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glNormal,double, 3,3dv)
0137
0138 inline void glScale2fv(const float* v) { glScalef(v[0], v[1], 1.f); }
0139 inline void glScale2dv(const double* v) { glScaled(v[0], v[1], 1.0); }
0140 inline void glScale3fv(const float* v) { glScalef(v[0], v[1], v[2]); }
0141 inline void glScale3dv(const double* v) { glScaled(v[0], v[1], v[2]); }
0142
0143 EIGEN_GL_FUNC_DECLARATION (glScale)
0144 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glScale,float, 2,2fv)
0145 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glScale,double, 2,2dv)
0146 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glScale,float, 3,3fv)
0147 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glScale,double, 3,3dv)
0148
0149 template<typename Scalar> void glScale(const UniformScaling<Scalar>& s) { glScale(Matrix<Scalar,3,1>::Constant(s.factor())); }
0150
0151 inline void glTranslate2fv(const float* v) { glTranslatef(v[0], v[1], 0.f); }
0152 inline void glTranslate2dv(const double* v) { glTranslated(v[0], v[1], 0.0); }
0153 inline void glTranslate3fv(const float* v) { glTranslatef(v[0], v[1], v[2]); }
0154 inline void glTranslate3dv(const double* v) { glTranslated(v[0], v[1], v[2]); }
0155
0156 EIGEN_GL_FUNC_DECLARATION (glTranslate)
0157 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTranslate,float, 2,2fv)
0158 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTranslate,double, 2,2dv)
0159 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTranslate,float, 3,3fv)
0160 EIGEN_GL_FUNC_SPECIALIZATION_VEC(glTranslate,double, 3,3dv)
0161
0162 template<typename Scalar> void glTranslate(const Translation<Scalar,2>& t) { glTranslate(t.vector()); }
0163 template<typename Scalar> void glTranslate(const Translation<Scalar,3>& t) { glTranslate(t.vector()); }
0164
0165 EIGEN_GL_FUNC_DECLARATION (glMultMatrix)
0166 EIGEN_GL_FUNC_SPECIALIZATION_MAT(glMultMatrix,float, 4,4,f)
0167 EIGEN_GL_FUNC_SPECIALIZATION_MAT(glMultMatrix,double, 4,4,d)
0168
0169 template<typename Scalar> void glMultMatrix(const Transform<Scalar,3,Affine>& t) { glMultMatrix(t.matrix()); }
0170 template<typename Scalar> void glMultMatrix(const Transform<Scalar,3,Projective>& t) { glMultMatrix(t.matrix()); }
0171 template<typename Scalar> void glMultMatrix(const Transform<Scalar,3,AffineCompact>& t) { glMultMatrix(Transform<Scalar,3,Affine>(t).matrix()); }
0172
0173 EIGEN_GL_FUNC_DECLARATION (glLoadMatrix)
0174 EIGEN_GL_FUNC_SPECIALIZATION_MAT(glLoadMatrix,float, 4,4,f)
0175 EIGEN_GL_FUNC_SPECIALIZATION_MAT(glLoadMatrix,double, 4,4,d)
0176
0177 template<typename Scalar> void glLoadMatrix(const Transform<Scalar,3,Affine>& t) { glLoadMatrix(t.matrix()); }
0178 template<typename Scalar> void glLoadMatrix(const Transform<Scalar,3,Projective>& t) { glLoadMatrix(t.matrix()); }
0179 template<typename Scalar> void glLoadMatrix(const Transform<Scalar,3,AffineCompact>& t) { glLoadMatrix(Transform<Scalar,3,Affine>(t).matrix()); }
0180
0181 inline void glRotate(const Rotation2D<float>& rot)
0182 {
0183 glRotatef(rot.angle()*180.f/float(EIGEN_PI), 0.f, 0.f, 1.f);
0184 }
0185 inline void glRotate(const Rotation2D<double>& rot)
0186 {
0187 glRotated(rot.angle()*180.0/double(EIGEN_PI), 0.0, 0.0, 1.0);
0188 }
0189
0190 template<typename Derived> void glRotate(const RotationBase<Derived,3>& rot)
0191 {
0192 Transform<typename Derived::Scalar,3,Projective> tr(rot);
0193 glMultMatrix(tr.matrix());
0194 }
0195
0196 #define EIGEN_GL_MAKE_CONST_const const
0197 #define EIGEN_GL_MAKE_CONST__
0198 #define EIGEN_GL_EVAL(X) X
0199
0200 #define EIGEN_GL_FUNC1_DECLARATION(FUNC,ARG1,CONST) \
0201 namespace internal { \
0202 template< typename XprType, \
0203 typename Scalar = typename XprType::Scalar, \
0204 int Rows = XprType::RowsAtCompileTime, \
0205 int Cols = XprType::ColsAtCompileTime, \
0206 bool IsGLCompatible = bool(internal::evaluator<XprType>::Flags&LinearAccessBit) \
0207 && bool(XprType::Flags&DirectAccessBit) \
0208 && (XprType::IsVectorAtCompileTime || (XprType::Flags&RowMajorBit)==0)> \
0209 struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl); \
0210 \
0211 template<typename XprType, typename Scalar, int Rows, int Cols> \
0212 struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<XprType,Scalar,Rows,Cols,false> { \
0213 inline static void run(ARG1 a,EIGEN_GL_EVAL(EIGEN_GL_MAKE_CONST_##CONST) XprType& p) { \
0214 EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<typename plain_matrix_type_column_major<XprType>::type>::run(a,p); } \
0215 }; \
0216 } \
0217 \
0218 template<typename Derived> inline void FUNC(ARG1 a,EIGEN_GL_EVAL(EIGEN_GL_MAKE_CONST_##CONST) Eigen::DenseBase<Derived>& p) { \
0219 EIGEN_CAT(EIGEN_CAT(internal::gl_,FUNC),_impl)<Derived>::run(a,p.derived()); \
0220 }
0221
0222
0223 #define EIGEN_GL_FUNC1_SPECIALIZATION_MAT(FUNC,ARG1,CONST,SCALAR,ROWS,COLS,SUFFIX) \
0224 namespace internal { \
0225 template< typename XprType> struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<XprType, SCALAR, ROWS, COLS, true> { \
0226 inline static void run(ARG1 a, EIGEN_GL_EVAL(EIGEN_GL_MAKE_CONST_##CONST) XprType& p) { FUNC##SUFFIX(a,p.data()); } \
0227 }; \
0228 }
0229
0230
0231 #define EIGEN_GL_FUNC1_SPECIALIZATION_VEC(FUNC,ARG1,CONST,SCALAR,SIZE,SUFFIX) \
0232 namespace internal { \
0233 template< typename XprType> struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<XprType, SCALAR, SIZE, 1, true> { \
0234 inline static void run(ARG1 a, EIGEN_GL_EVAL(EIGEN_GL_MAKE_CONST_##CONST) XprType& p) { FUNC##SUFFIX(a,p.data()); } \
0235 }; \
0236 template< typename XprType> struct EIGEN_CAT(EIGEN_CAT(gl_,FUNC),_impl)<XprType, SCALAR, 1, SIZE, true> { \
0237 inline static void run(ARG1 a, EIGEN_GL_EVAL(EIGEN_GL_MAKE_CONST_##CONST) XprType& p) { FUNC##SUFFIX(a,p.data()); } \
0238 }; \
0239 }
0240
0241 EIGEN_GL_FUNC1_DECLARATION (glGet,GLenum,_)
0242 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glGet,GLenum,_,float, 4,4,Floatv)
0243 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glGet,GLenum,_,double, 4,4,Doublev)
0244
0245 // glUniform API
0246
0247 #ifdef GL_VERSION_2_0
0248
0249 inline void glUniform2fv_ei (GLint loc, const float* v) { glUniform2fv(loc,1,v); }
0250 inline void glUniform2iv_ei (GLint loc, const int* v) { glUniform2iv(loc,1,v); }
0251
0252 inline void glUniform3fv_ei (GLint loc, const float* v) { glUniform3fv(loc,1,v); }
0253 inline void glUniform3iv_ei (GLint loc, const int* v) { glUniform3iv(loc,1,v); }
0254
0255 inline void glUniform4fv_ei (GLint loc, const float* v) { glUniform4fv(loc,1,v); }
0256 inline void glUniform4iv_ei (GLint loc, const int* v) { glUniform4iv(loc,1,v); }
0257
0258 inline void glUniformMatrix2fv_ei (GLint loc, const float* v) { glUniformMatrix2fv(loc,1,false,v); }
0259 inline void glUniformMatrix3fv_ei (GLint loc, const float* v) { glUniformMatrix3fv(loc,1,false,v); }
0260 inline void glUniformMatrix4fv_ei (GLint loc, const float* v) { glUniformMatrix4fv(loc,1,false,v); }
0261
0262
0263 EIGEN_GL_FUNC1_DECLARATION (glUniform,GLint,const)
0264 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,float, 2,2fv_ei)
0265 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,int, 2,2iv_ei)
0266 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,float, 3,3fv_ei)
0267 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,int, 3,3iv_ei)
0268 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,float, 4,4fv_ei)
0269 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,int, 4,4iv_ei)
0270
0271 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 2,2,Matrix2fv_ei)
0272 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 3,3,Matrix3fv_ei)
0273 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 4,4,Matrix4fv_ei)
0274
0275 #endif
0276
0277 #ifdef GL_VERSION_2_1
0278
0279 inline void glUniformMatrix2x3fv_ei(GLint loc, const float* v) { glUniformMatrix2x3fv(loc,1,false,v); }
0280 inline void glUniformMatrix3x2fv_ei(GLint loc, const float* v) { glUniformMatrix3x2fv(loc,1,false,v); }
0281 inline void glUniformMatrix2x4fv_ei(GLint loc, const float* v) { glUniformMatrix2x4fv(loc,1,false,v); }
0282 inline void glUniformMatrix4x2fv_ei(GLint loc, const float* v) { glUniformMatrix4x2fv(loc,1,false,v); }
0283 inline void glUniformMatrix3x4fv_ei(GLint loc, const float* v) { glUniformMatrix3x4fv(loc,1,false,v); }
0284 inline void glUniformMatrix4x3fv_ei(GLint loc, const float* v) { glUniformMatrix4x3fv(loc,1,false,v); }
0285
0286 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 2,3,Matrix2x3fv_ei)
0287 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 3,2,Matrix3x2fv_ei)
0288 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 2,4,Matrix2x4fv_ei)
0289 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 4,2,Matrix4x2fv_ei)
0290 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 3,4,Matrix3x4fv_ei)
0291 EIGEN_GL_FUNC1_SPECIALIZATION_MAT(glUniform,GLint,const,float, 4,3,Matrix4x3fv_ei)
0292
0293 #endif
0294
0295 #ifdef GL_VERSION_3_0
0296
0297 inline void glUniform2uiv_ei (GLint loc, const unsigned int* v) { glUniform2uiv(loc,1,v); }
0298 inline void glUniform3uiv_ei (GLint loc, const unsigned int* v) { glUniform3uiv(loc,1,v); }
0299 inline void glUniform4uiv_ei (GLint loc, const unsigned int* v) { glUniform4uiv(loc,1,v); }
0300
0301 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,unsigned int, 2,2uiv_ei)
0302 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,unsigned int, 3,3uiv_ei)
0303 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,unsigned int, 4,4uiv_ei)
0304
0305 #endif
0306
0307 #ifdef GL_ARB_gpu_shader_fp64
0308 inline void glUniform2dv_ei (GLint loc, const double* v) { glUniform2dv(loc,1,v); }
0309 inline void glUniform3dv_ei (GLint loc, const double* v) { glUniform3dv(loc,1,v); }
0310 inline void glUniform4dv_ei (GLint loc, const double* v) { glUniform4dv(loc,1,v); }
0311
0312 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,double, 2,2dv_ei)
0313 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,double, 3,3dv_ei)
0314 EIGEN_GL_FUNC1_SPECIALIZATION_VEC(glUniform,GLint,const,double, 4,4dv_ei)
0315 #endif
0316
0317
0318 //@}
0319
0320 }
0321
0322 #endif // EIGEN_OPENGL_MODULE