gpuhelper.h 6.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef EIGEN_GPUHELPER_H
#define EIGEN_GPUHELPER_H

#include <Eigen/Geometry>
#include <GL/gl.h>
#include <vector>

using namespace Eigen;

typedef Vector4f Color;

class GpuHelper {
 public:
  GpuHelper();

  ~GpuHelper();

  enum ProjectionMode2D { PM_Normalized = 1, PM_Viewport = 2 };
  void pushProjectionMode2D(ProjectionMode2D pm);
  void popProjectionMode2D();

  /** Multiply the OpenGL matrix \a matrixTarget by the matrix \a mat.
      Essentially, this helper function automatically calls glMatrixMode(matrixTarget) if required
      and does a proper call to the right glMultMatrix*() function according to the scalar type
      and storage order.
      \warning glMatrixMode() must never be called directly. If you are unsure, use forceMatrixMode().
      \sa Matrix, loadMatrix(), forceMatrixMode()
  */
  template <typename Scalar, int Flags_>
  void multMatrix(const Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget);

  /** Load the matrix \a mat to the OpenGL matrix \a matrixTarget.
      Essentially, this helper function automatically calls glMatrixMode(matrixTarget) if required
      and does a proper call to the right glLoadMatrix*() or glLoadIdentity() function according to the scalar type
      and storage order.
      \warning glMatrixMode() must never be called directly. If you are unsure, use forceMatrixMode().
      \sa Matrix, multMatrix(), forceMatrixMode()
  */
  template <typename Scalar, int Flags_>
  void loadMatrix(const Eigen::Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget);

  template <typename Scalar, typename Derived>
  void loadMatrix(const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>, Derived>&,
                  GLenum matrixTarget);

  /** Make the matrix \a matrixTarget the current OpenGL matrix target.
      Call this function before loadMatrix() or multMatrix() if you cannot guarantee that glMatrixMode()
      has never been called after the last loadMatrix() or multMatrix() calls.
      \todo provides a debug mode checking the sanity of the cached matrix mode.
  */
  inline void forceMatrixTarget(GLenum matrixTarget) { glMatrixMode(mCurrentMatrixTarget = matrixTarget); }

  inline void setMatrixTarget(GLenum matrixTarget);

  /** Push the OpenGL matrix \a matrixTarget and load \a mat.
   */
  template <typename Scalar, int Flags_>
  inline void pushMatrix(const Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget);

  template <typename Scalar, typename Derived>
  void pushMatrix(const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>, Derived>&,
                  GLenum matrixTarget);

  /** Push and clone the OpenGL matrix \a matrixTarget
   */
  inline void pushMatrix(GLenum matrixTarget);

  /** Pop the OpenGL matrix \a matrixTarget
   */
  inline void popMatrix(GLenum matrixTarget);

  void drawVector(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect = 50.);
  void drawVectorBox(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect = 50.);
  void drawUnitCube(void);
  void drawUnitSphere(int level = 0);

  /// draw the \a nofElement first elements
  inline void draw(GLenum mode, uint nofElement);

  /// draw a range of elements
  inline void draw(GLenum mode, uint start, uint end);

  /// draw an indexed subset
  inline void draw(GLenum mode, const std::vector<uint>* pIndexes);

 protected:
  void update(void);

  GLuint mColorBufferId;
  int mVpWidth, mVpHeight;
  GLenum mCurrentMatrixTarget;
  bool mInitialized;
};

/** Singleton shortcut
 */
extern GpuHelper gpu;

/** \internal
 */
template <bool RowMajor, int Flags_>
struct GlMatrixHelper;

template <int Flags_>
struct GlMatrixHelper<false, Flags_> {
  static void loadMatrix(const Matrix<float, 4, 4, Flags_, 4, 4>& mat) { glLoadMatrixf(mat.data()); }
  static void loadMatrix(const Matrix<double, 4, 4, Flags_, 4, 4>& mat) { glLoadMatrixd(mat.data()); }
  static void multMatrix(const Matrix<float, 4, 4, Flags_, 4, 4>& mat) { glMultMatrixf(mat.data()); }
  static void multMatrix(const Matrix<double, 4, 4, Flags_, 4, 4>& mat) { glMultMatrixd(mat.data()); }
};

template <int Flags_>
struct GlMatrixHelper<true, Flags_> {
  static void loadMatrix(const Matrix<float, 4, 4, Flags_, 4, 4>& mat) { glLoadMatrixf(mat.transpose().eval().data()); }
  static void loadMatrix(const Matrix<double, 4, 4, Flags_, 4, 4>& mat) {
    glLoadMatrixd(mat.transpose().eval().data());
  }
  static void multMatrix(const Matrix<float, 4, 4, Flags_, 4, 4>& mat) { glMultMatrixf(mat.transpose().eval().data()); }
  static void multMatrix(const Matrix<double, 4, 4, Flags_, 4, 4>& mat) {
    glMultMatrixd(mat.transpose().eval().data());
  }
};

inline void GpuHelper::setMatrixTarget(GLenum matrixTarget) {
  if (matrixTarget != mCurrentMatrixTarget) glMatrixMode(mCurrentMatrixTarget = matrixTarget);
}

template <typename Scalar, int Flags_>
void GpuHelper::multMatrix(const Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget) {
  setMatrixTarget(matrixTarget);
  GlMatrixHelper<Flags_ & Eigen::RowMajorBit, Flags_>::multMatrix(mat);
}

template <typename Scalar, typename Derived>
void GpuHelper::loadMatrix(const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>, Derived>&,
                           GLenum matrixTarget) {
  setMatrixTarget(matrixTarget);
  glLoadIdentity();
}

template <typename Scalar, int Flags_>
void GpuHelper::loadMatrix(const Eigen::Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget) {
  setMatrixTarget(matrixTarget);
  GlMatrixHelper<(Flags_ & Eigen::RowMajorBit) != 0, Flags_>::loadMatrix(mat);
}

inline void GpuHelper::pushMatrix(GLenum matrixTarget) {
  setMatrixTarget(matrixTarget);
  glPushMatrix();
}

template <typename Scalar, int Flags_>
inline void GpuHelper::pushMatrix(const Matrix<Scalar, 4, 4, Flags_, 4, 4>& mat, GLenum matrixTarget) {
  pushMatrix(matrixTarget);
  GlMatrixHelper<Flags_ & Eigen::RowMajorBit, Flags_>::loadMatrix(mat);
}

template <typename Scalar, typename Derived>
void GpuHelper::pushMatrix(const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>, Derived>&,
                           GLenum matrixTarget) {
  pushMatrix(matrixTarget);
  glLoadIdentity();
}

inline void GpuHelper::popMatrix(GLenum matrixTarget) {
  setMatrixTarget(matrixTarget);
  glPopMatrix();
}

inline void GpuHelper::draw(GLenum mode, uint nofElement) { glDrawArrays(mode, 0, nofElement); }

inline void GpuHelper::draw(GLenum mode, const std::vector<uint>* pIndexes) {
  glDrawElements(mode, pIndexes->size(), GL_UNSIGNED_INT, &(pIndexes->front()));
}

inline void GpuHelper::draw(GLenum mode, uint start, uint end) { glDrawArrays(mode, start, end - start); }

#endif  // EIGEN_GPUHELPER_H