gpuhelper.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_GPUHELPER_H
11 #define EIGEN_GPUHELPER_H
12 
13 #include <Eigen/Geometry>
14 #include <GL/gl.h>
15 #include <vector>
16 
17 using namespace Eigen;
18 
19 typedef Vector4f Color;
20 
21 class GpuHelper {
22  public:
23  GpuHelper();
24 
25  ~GpuHelper();
26 
27  enum ProjectionMode2D { PM_Normalized = 1, PM_Viewport = 2 };
28  void pushProjectionMode2D(ProjectionMode2D pm);
29  void popProjectionMode2D();
30 
38  template <typename Scalar, int Flags_>
39  void multMatrix(const Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget);
40 
48  template <typename Scalar, int Flags_>
49  void loadMatrix(const Eigen::Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget);
50 
51  template <typename Scalar, typename Derived>
53  GLenum matrixTarget);
54 
60  inline void forceMatrixTarget(GLenum matrixTarget) { glMatrixMode(mCurrentMatrixTarget = matrixTarget); }
61 
62  inline void setMatrixTarget(GLenum matrixTarget);
63 
66  template <typename Scalar, int Flags_>
67  inline void pushMatrix(const Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget);
68 
69  template <typename Scalar, typename Derived>
71  GLenum matrixTarget);
72 
75  inline void pushMatrix(GLenum matrixTarget);
76 
79  inline void popMatrix(GLenum matrixTarget);
80 
81  void drawVector(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect = 50.);
82  void drawVectorBox(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect = 50.);
83  void drawUnitCube(void);
84  void drawUnitSphere(int level = 0);
85 
87  inline void draw(GLenum mode, uint nofElement);
88 
90  inline void draw(GLenum mode, uint start, uint end);
91 
93  inline void draw(GLenum mode, const std::vector<uint>* pIndexes);
94 
95  protected:
96  void update(void);
97 
99  int mVpWidth, mVpHeight;
102 };
103 
106 extern GpuHelper gpu;
107 
110 template <bool RowMajor, int Flags_>
112 
113 template <int Flags_>
114 struct GlMatrixHelper<false, Flags_> {
115  static void loadMatrix(const Matrix<float, 4, 4, Flags_, 4, 4>& mat) { glLoadMatrixf(mat.data()); }
116  static void loadMatrix(const Matrix<double, 4, 4, Flags_, 4, 4>& mat) { glLoadMatrixd(mat.data()); }
117  static void multMatrix(const Matrix<float, 4, 4, Flags_, 4, 4>& mat) { glMultMatrixf(mat.data()); }
118  static void multMatrix(const Matrix<double, 4, 4, Flags_, 4, 4>& mat) { glMultMatrixd(mat.data()); }
119 };
120 
121 template <int Flags_>
122 struct GlMatrixHelper<true, Flags_> {
123  static void loadMatrix(const Matrix<float, 4, 4, Flags_, 4, 4>& mat) { glLoadMatrixf(mat.transpose().eval().data()); }
125  glLoadMatrixd(mat.transpose().eval().data());
126  }
127  static void multMatrix(const Matrix<float, 4, 4, Flags_, 4, 4>& mat) { glMultMatrixf(mat.transpose().eval().data()); }
129  glMultMatrixd(mat.transpose().eval().data());
130  }
131 };
132 
133 inline void GpuHelper::setMatrixTarget(GLenum matrixTarget) {
134  if (matrixTarget != mCurrentMatrixTarget) glMatrixMode(mCurrentMatrixTarget = matrixTarget);
135 }
136 
137 template <typename Scalar, int Flags_>
139  setMatrixTarget(matrixTarget);
141 }
142 
143 template <typename Scalar, typename Derived>
145  GLenum matrixTarget) {
146  setMatrixTarget(matrixTarget);
147  glLoadIdentity();
148 }
149 
150 template <typename Scalar, int Flags_>
152  setMatrixTarget(matrixTarget);
153  GlMatrixHelper<(Flags_ & Eigen::RowMajorBit) != 0, Flags_>::loadMatrix(mat);
154 }
155 
156 inline void GpuHelper::pushMatrix(GLenum matrixTarget) {
157  setMatrixTarget(matrixTarget);
158  glPushMatrix();
159 }
160 
161 template <typename Scalar, int Flags_>
162 inline void GpuHelper::pushMatrix(const Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget) {
163  pushMatrix(matrixTarget);
165 }
166 
167 template <typename Scalar, typename Derived>
169  GLenum matrixTarget) {
170  pushMatrix(matrixTarget);
171  glLoadIdentity();
172 }
173 
174 inline void GpuHelper::popMatrix(GLenum matrixTarget) {
175  setMatrixTarget(matrixTarget);
176  glPopMatrix();
177 }
178 
179 inline void GpuHelper::draw(GLenum mode, uint nofElement) { glDrawArrays(mode, 0, nofElement); }
180 
181 inline void GpuHelper::draw(GLenum mode, const std::vector<uint>* pIndexes) {
182  glDrawElements(mode, pIndexes->size(), GL_UNSIGNED_INT, &(pIndexes->front()));
183 }
184 
185 inline void GpuHelper::draw(GLenum mode, uint start, uint end) { glDrawArrays(mode, start, end - start); }
186 
187 #endif // EIGEN_GPUHELPER_H
Matrix4d pm
Definition: HessenbergDecomposition_packedMatrix.cpp:4
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:64
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
TransposeReturnType transpose()
Definition: SparseMatrixBase.h:358
constexpr Storage & data()
Definition: SparseMatrix.h:205
Definition: gpuhelper.h:21
ProjectionMode2D
Definition: gpuhelper.h:27
void pushMatrix(const Matrix< Scalar, 4, 4, Flags_, 4, 4 > &mat, GLenum matrixTarget)
Definition: gpuhelper.h:162
void draw(GLenum mode, uint nofElement)
draw the nofElement first elements
Definition: gpuhelper.h:179
GLuint mColorBufferId
Definition: gpuhelper.h:98
int mVpHeight
Definition: gpuhelper.h:99
void setMatrixTarget(GLenum matrixTarget)
Definition: gpuhelper.h:133
void multMatrix(const Matrix< Scalar, 4, 4, Flags_, 4, 4 > &mat, GLenum matrixTarget)
Definition: gpuhelper.h:138
void forceMatrixTarget(GLenum matrixTarget)
Definition: gpuhelper.h:60
void loadMatrix(const Eigen::Matrix< Scalar, 4, 4, Flags_, 4, 4 > &mat, GLenum matrixTarget)
Definition: gpuhelper.h:151
void update(void)
bool mInitialized
Definition: gpuhelper.h:101
void popMatrix(GLenum matrixTarget)
Definition: gpuhelper.h:174
GLenum mCurrentMatrixTarget
Definition: gpuhelper.h:100
GpuHelper gpu
Definition: gpuhelper.cpp:18
Vector4f Color
Definition: gpuhelper.h:19
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
const unsigned int RowMajorBit
Definition: Constants.h:70
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
void start(const unsigned &i)
(Re-)start i-th timer
Definition: oomph_utilities.cc:243
Definition: NullaryFunctors.h:52
static void loadMatrix(const Matrix< float, 4, 4, Flags_, 4, 4 > &mat)
Definition: gpuhelper.h:115
static void loadMatrix(const Matrix< double, 4, 4, Flags_, 4, 4 > &mat)
Definition: gpuhelper.h:116
static void multMatrix(const Matrix< double, 4, 4, Flags_, 4, 4 > &mat)
Definition: gpuhelper.h:118
static void multMatrix(const Matrix< float, 4, 4, Flags_, 4, 4 > &mat)
Definition: gpuhelper.h:117
static void multMatrix(const Matrix< double, 4, 4, Flags_, 4, 4 > &mat)
Definition: gpuhelper.h:128
static void multMatrix(const Matrix< float, 4, 4, Flags_, 4, 4 > &mat)
Definition: gpuhelper.h:127
static void loadMatrix(const Matrix< double, 4, 4, Flags_, 4, 4 > &mat)
Definition: gpuhelper.h:124
static void loadMatrix(const Matrix< float, 4, 4, Flags_, 4, 4 > &mat)
Definition: gpuhelper.h:123
Definition: gpuhelper.h:111