gpuhelper.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen 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 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_GPUHELPER_H
00026 #define EIGEN_GPUHELPER_H
00027 
00028 #include <Eigen/Geometry>
00029 #include <GL/gl.h>
00030 #include <vector>
00031 
00032 using namespace Eigen;
00033 
00034 typedef Vector4f Color;
00035 
00036 class GpuHelper
00037 {
00038   public:
00039 
00040     GpuHelper();
00041 
00042     ~GpuHelper();
00043 
00044     enum ProjectionMode2D { PM_Normalized = 1, PM_Viewport = 2 };
00045     void pushProjectionMode2D(ProjectionMode2D pm);
00046     void popProjectionMode2D();
00047 
00055     template<typename Scalar, int _Flags>
00056     void multMatrix(const Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget);
00057 
00065     template<typename Scalar, int _Flags>
00066     void loadMatrix(const Eigen::Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget);
00067 
00068     template<typename Scalar, typename Derived>
00069     void loadMatrix(
00070         const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>,Derived>&,
00071         GLenum matrixTarget);
00072 
00078     inline void forceMatrixTarget(GLenum matrixTarget) {glMatrixMode(mCurrentMatrixTarget=matrixTarget);}
00079 
00080     inline void setMatrixTarget(GLenum matrixTarget);
00081 
00084     template<typename Scalar, int _Flags>
00085     inline void pushMatrix(const Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget);
00086 
00087     template<typename Scalar, typename Derived>
00088     void pushMatrix(
00089         const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>,Derived>&,
00090         GLenum matrixTarget);
00091 
00094     inline void pushMatrix(GLenum matrixTarget);
00095 
00098     inline void popMatrix(GLenum matrixTarget);
00099 
00100     void drawVector(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect = 50.);
00101     void drawVectorBox(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect = 50.);
00102     void drawUnitCube(void);
00103     void drawUnitSphere(int level=0);
00104 
00106     inline void draw(GLenum mode, uint nofElement);
00107 
00109     inline void draw(GLenum mode, uint start, uint end);
00110 
00112     inline void draw(GLenum mode, const std::vector<uint>* pIndexes);
00113 
00114 protected:
00115 
00116     void update(void);
00117 
00118     GLuint mColorBufferId;
00119     int mVpWidth, mVpHeight;
00120     GLenum mCurrentMatrixTarget;
00121     bool mInitialized;
00122 };
00123 
00126 extern GpuHelper gpu;
00127 
00128 
00131 template<bool RowMajor, int _Flags> struct GlMatrixHelper;
00132 
00133 template<int _Flags> struct GlMatrixHelper<false,_Flags>
00134 {
00135     static void loadMatrix(const Matrix<float, 4,4, _Flags, 4,4>&  mat) { glLoadMatrixf(mat.data()); }
00136     static void loadMatrix(const Matrix<double,4,4, _Flags, 4,4>& mat) { glLoadMatrixd(mat.data()); }
00137     static void multMatrix(const Matrix<float, 4,4, _Flags, 4,4>&  mat) { glMultMatrixf(mat.data()); }
00138     static void multMatrix(const Matrix<double,4,4, _Flags, 4,4>& mat) { glMultMatrixd(mat.data()); }
00139 };
00140 
00141 template<int _Flags> struct GlMatrixHelper<true,_Flags>
00142 {
00143     static void loadMatrix(const Matrix<float, 4,4, _Flags, 4,4>&  mat) { glLoadMatrixf(mat.transpose().eval().data()); }
00144     static void loadMatrix(const Matrix<double,4,4, _Flags, 4,4>& mat) { glLoadMatrixd(mat.transpose().eval().data()); }
00145     static void multMatrix(const Matrix<float, 4,4, _Flags, 4,4>&  mat) { glMultMatrixf(mat.transpose().eval().data()); }
00146     static void multMatrix(const Matrix<double,4,4, _Flags, 4,4>& mat) { glMultMatrixd(mat.transpose().eval().data()); }
00147 };
00148 
00149 inline void GpuHelper::setMatrixTarget(GLenum matrixTarget)
00150 {
00151     if (matrixTarget != mCurrentMatrixTarget)
00152         glMatrixMode(mCurrentMatrixTarget=matrixTarget);
00153 }
00154 
00155 template<typename Scalar, int _Flags>
00156 void GpuHelper::multMatrix(const Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget)
00157 {
00158     setMatrixTarget(matrixTarget);
00159     GlMatrixHelper<_Flags&Eigen::RowMajorBit, _Flags>::multMatrix(mat);
00160 }
00161 
00162 template<typename Scalar, typename Derived>
00163 void GpuHelper::loadMatrix(
00164     const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>,Derived>&,
00165     GLenum matrixTarget)
00166 {
00167     setMatrixTarget(matrixTarget);
00168     glLoadIdentity();
00169 }
00170 
00171 template<typename Scalar, int _Flags>
00172 void GpuHelper::loadMatrix(const Eigen::Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget)
00173 {
00174     setMatrixTarget(matrixTarget);
00175     GlMatrixHelper<(_Flags&Eigen::RowMajorBit)!=0, _Flags>::loadMatrix(mat);
00176 }
00177 
00178 inline void GpuHelper::pushMatrix(GLenum matrixTarget)
00179 {
00180     setMatrixTarget(matrixTarget);
00181     glPushMatrix();
00182 }
00183 
00184 template<typename Scalar, int _Flags>
00185 inline void GpuHelper::pushMatrix(const Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget)
00186 {
00187     pushMatrix(matrixTarget);
00188     GlMatrixHelper<_Flags&Eigen::RowMajorBit,_Flags>::loadMatrix(mat);
00189 }
00190 
00191 template<typename Scalar, typename Derived>
00192 void GpuHelper::pushMatrix(
00193     const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>,Derived>&,
00194     GLenum matrixTarget)
00195 {
00196     pushMatrix(matrixTarget);
00197     glLoadIdentity();
00198 }
00199 
00200 inline void GpuHelper::popMatrix(GLenum matrixTarget)
00201 {
00202     setMatrixTarget(matrixTarget);
00203     glPopMatrix();
00204 }
00205 
00206 inline void GpuHelper::draw(GLenum mode, uint nofElement)
00207 {
00208     glDrawArrays(mode, 0, nofElement);
00209 }
00210 
00211 
00212 inline void GpuHelper::draw(GLenum mode, const std::vector<uint>* pIndexes)
00213 {
00214     glDrawElements(mode, pIndexes->size(), GL_UNSIGNED_INT, &(pIndexes->front()));
00215 }
00216 
00217 inline void GpuHelper::draw(GLenum mode, uint start, uint end)
00218 {
00219     glDrawArrays(mode, start, end-start);
00220 }
00221 
00222 #endif // EIGEN_GPUHELPER_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:16