• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
23   public:
24 
25     GpuHelper();
26 
27     ~GpuHelper();
28 
29     enum ProjectionMode2D { PM_Normalized = 1, PM_Viewport = 2 };
30     void pushProjectionMode2D(ProjectionMode2D pm);
31     void popProjectionMode2D();
32 
33     /** Multiply the OpenGL matrix \a matrixTarget by the matrix \a mat.
34         Essentially, this helper function automatically calls glMatrixMode(matrixTarget) if required
35         and does a proper call to the right glMultMatrix*() function according to the scalar type
36         and storage order.
37         \warning glMatrixMode() must never be called directly. If your're unsure, use forceMatrixMode().
38         \sa Matrix, loadMatrix(), forceMatrixMode()
39     */
40     template<typename Scalar, int _Flags>
41     void multMatrix(const Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget);
42 
43     /** Load the matrix \a mat to the OpenGL matrix \a matrixTarget.
44         Essentially, this helper function automatically calls glMatrixMode(matrixTarget) if required
45         and does a proper call to the right glLoadMatrix*() or glLoadIdentity() function according to the scalar type
46         and storage order.
47         \warning glMatrixMode() must never be called directly. If your're unsure, use forceMatrixMode().
48         \sa Matrix, multMatrix(), forceMatrixMode()
49     */
50     template<typename Scalar, int _Flags>
51     void loadMatrix(const Eigen::Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget);
52 
53     template<typename Scalar, typename Derived>
54     void loadMatrix(
55         const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>,Derived>&,
56         GLenum matrixTarget);
57 
58     /** Make the matrix \a matrixTarget the current OpenGL matrix target.
59         Call this function before loadMatrix() or multMatrix() if you cannot guarantee that glMatrixMode()
60         has never been called after the last loadMatrix() or multMatrix() calls.
61         \todo provides a debug mode checking the sanity of the cached matrix mode.
62     */
forceMatrixTarget(GLenum matrixTarget)63     inline void forceMatrixTarget(GLenum matrixTarget) {glMatrixMode(mCurrentMatrixTarget=matrixTarget);}
64 
65     inline void setMatrixTarget(GLenum matrixTarget);
66 
67     /** Push the OpenGL matrix \a matrixTarget and load \a mat.
68     */
69     template<typename Scalar, int _Flags>
70     inline void pushMatrix(const Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget);
71 
72     template<typename Scalar, typename Derived>
73     void pushMatrix(
74         const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>,Derived>&,
75         GLenum matrixTarget);
76 
77     /** Push and clone the OpenGL matrix \a matrixTarget
78     */
79     inline void pushMatrix(GLenum matrixTarget);
80 
81     /** Pop the OpenGL matrix \a matrixTarget
82     */
83     inline void popMatrix(GLenum matrixTarget);
84 
85     void drawVector(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect = 50.);
86     void drawVectorBox(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect = 50.);
87     void drawUnitCube(void);
88     void drawUnitSphere(int level=0);
89 
90     /// draw the \a nofElement first elements
91     inline void draw(GLenum mode, uint nofElement);
92 
93     /// draw a range of elements
94     inline void draw(GLenum mode, uint start, uint end);
95 
96     /// draw an indexed subset
97     inline void draw(GLenum mode, const std::vector<uint>* pIndexes);
98 
99 protected:
100 
101     void update(void);
102 
103     GLuint mColorBufferId;
104     int mVpWidth, mVpHeight;
105     GLenum mCurrentMatrixTarget;
106     bool mInitialized;
107 };
108 
109 /** Singleton shortcut
110 */
111 extern GpuHelper gpu;
112 
113 
114 /** \internal
115 */
116 template<bool RowMajor, int _Flags> struct GlMatrixHelper;
117 
118 template<int _Flags> struct GlMatrixHelper<false,_Flags>
119 {
120     static void loadMatrix(const Matrix<float, 4,4, _Flags, 4,4>&  mat) { glLoadMatrixf(mat.data()); }
121     static void loadMatrix(const Matrix<double,4,4, _Flags, 4,4>& mat) { glLoadMatrixd(mat.data()); }
122     static void multMatrix(const Matrix<float, 4,4, _Flags, 4,4>&  mat) { glMultMatrixf(mat.data()); }
123     static void multMatrix(const Matrix<double,4,4, _Flags, 4,4>& mat) { glMultMatrixd(mat.data()); }
124 };
125 
126 template<int _Flags> struct GlMatrixHelper<true,_Flags>
127 {
128     static void loadMatrix(const Matrix<float, 4,4, _Flags, 4,4>&  mat) { glLoadMatrixf(mat.transpose().eval().data()); }
129     static void loadMatrix(const Matrix<double,4,4, _Flags, 4,4>& mat) { glLoadMatrixd(mat.transpose().eval().data()); }
130     static void multMatrix(const Matrix<float, 4,4, _Flags, 4,4>&  mat) { glMultMatrixf(mat.transpose().eval().data()); }
131     static void multMatrix(const Matrix<double,4,4, _Flags, 4,4>& mat) { glMultMatrixd(mat.transpose().eval().data()); }
132 };
133 
134 inline void GpuHelper::setMatrixTarget(GLenum matrixTarget)
135 {
136     if (matrixTarget != mCurrentMatrixTarget)
137         glMatrixMode(mCurrentMatrixTarget=matrixTarget);
138 }
139 
140 template<typename Scalar, int _Flags>
141 void GpuHelper::multMatrix(const Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget)
142 {
143     setMatrixTarget(matrixTarget);
144     GlMatrixHelper<_Flags&Eigen::RowMajorBit, _Flags>::multMatrix(mat);
145 }
146 
147 template<typename Scalar, typename Derived>
148 void GpuHelper::loadMatrix(
149     const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>,Derived>&,
150     GLenum matrixTarget)
151 {
152     setMatrixTarget(matrixTarget);
153     glLoadIdentity();
154 }
155 
156 template<typename Scalar, int _Flags>
157 void GpuHelper::loadMatrix(const Eigen::Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget)
158 {
159     setMatrixTarget(matrixTarget);
160     GlMatrixHelper<(_Flags&Eigen::RowMajorBit)!=0, _Flags>::loadMatrix(mat);
161 }
162 
163 inline void GpuHelper::pushMatrix(GLenum matrixTarget)
164 {
165     setMatrixTarget(matrixTarget);
166     glPushMatrix();
167 }
168 
169 template<typename Scalar, int _Flags>
170 inline void GpuHelper::pushMatrix(const Matrix<Scalar,4,4, _Flags, 4,4>& mat, GLenum matrixTarget)
171 {
172     pushMatrix(matrixTarget);
173     GlMatrixHelper<_Flags&Eigen::RowMajorBit,_Flags>::loadMatrix(mat);
174 }
175 
176 template<typename Scalar, typename Derived>
177 void GpuHelper::pushMatrix(
178     const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<Scalar>,Derived>&,
179     GLenum matrixTarget)
180 {
181     pushMatrix(matrixTarget);
182     glLoadIdentity();
183 }
184 
185 inline void GpuHelper::popMatrix(GLenum matrixTarget)
186 {
187     setMatrixTarget(matrixTarget);
188     glPopMatrix();
189 }
190 
191 inline void GpuHelper::draw(GLenum mode, uint nofElement)
192 {
193     glDrawArrays(mode, 0, nofElement);
194 }
195 
196 
197 inline void GpuHelper::draw(GLenum mode, const std::vector<uint>* pIndexes)
198 {
199     glDrawElements(mode, pIndexes->size(), GL_UNSIGNED_INT, &(pIndexes->front()));
200 }
201 
202 inline void GpuHelper::draw(GLenum mode, uint start, uint end)
203 {
204     glDrawArrays(mode, start, end-start);
205 }
206 
207 #endif // EIGEN_GPUHELPER_H
208