GpuHelper Class Reference

#include <gpuhelper.h>

Public Types

enum  ProjectionMode2D { PM_Normalized = 1 , PM_Viewport = 2 }
 

Public Member Functions

 GpuHelper ()
 
 ~GpuHelper ()
 
void pushProjectionMode2D (ProjectionMode2D pm)
 
void popProjectionMode2D ()
 
template<typename Scalar , int Flags_>
void multMatrix (const Matrix< Scalar, 4, 4, Flags_, 4, 4 > &mat, GLenum matrixTarget)
 
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)
 
void forceMatrixTarget (GLenum matrixTarget)
 
void setMatrixTarget (GLenum matrixTarget)
 
template<typename Scalar , int Flags_>
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)
 
void pushMatrix (GLenum matrixTarget)
 
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)
 
void draw (GLenum mode, uint nofElement)
 draw the nofElement first elements More...
 
void draw (GLenum mode, uint start, uint end)
 draw a range of elements More...
 
void draw (GLenum mode, const std::vector< uint > *pIndexes)
 draw an indexed subset More...
 

Protected Member Functions

void update (void)
 

Protected Attributes

GLuint mColorBufferId
 
int mVpWidth
 
int mVpHeight
 
GLenum mCurrentMatrixTarget
 
bool mInitialized
 

Member Enumeration Documentation

◆ ProjectionMode2D

Enumerator
PM_Normalized 
PM_Viewport 
27 { PM_Normalized = 1, PM_Viewport = 2 };
@ PM_Viewport
Definition: gpuhelper.h:27
@ PM_Normalized
Definition: gpuhelper.h:27

Constructor & Destructor Documentation

◆ GpuHelper()

GpuHelper::GpuHelper ( )
20  {
21  mVpWidth = mVpHeight = 0;
23  mInitialized = false;
24 }
int mVpHeight
Definition: gpuhelper.h:99
int mVpWidth
Definition: gpuhelper.h:99
bool mInitialized
Definition: gpuhelper.h:101
GLenum mCurrentMatrixTarget
Definition: gpuhelper.h:100

References mCurrentMatrixTarget, mInitialized, mVpHeight, and mVpWidth.

◆ ~GpuHelper()

GpuHelper::~GpuHelper ( )
26 {}

Member Function Documentation

◆ draw() [1/3]

void GpuHelper::draw ( GLenum  mode,
const std::vector< uint > *  pIndexes 
)
inline

draw an indexed subset

181  {
182  glDrawElements(mode, pIndexes->size(), GL_UNSIGNED_INT, &(pIndexes->front()));
183 }

◆ draw() [2/3]

void GpuHelper::draw ( GLenum  mode,
uint  nofElement 
)
inline

draw the nofElement first elements

179 { glDrawArrays(mode, 0, nofElement); }

◆ draw() [3/3]

void GpuHelper::draw ( GLenum  mode,
uint  start,
uint  end 
)
inline

draw a range of elements

185 { glDrawArrays(mode, start, end - start); }
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
void start(const unsigned &i)
(Re-)start i-th timer
Definition: oomph_utilities.cc:243

References Eigen::placeholders::end, and oomph::CumulativeTimings::start().

◆ drawUnitCube()

void GpuHelper::drawUnitCube ( void  )
86  {
87  static float vertices[][3] = {{-0.5, -0.5, -0.5}, {0.5, -0.5, -0.5}, {-0.5, 0.5, -0.5}, {0.5, 0.5, -0.5},
88  {-0.5, -0.5, 0.5}, {0.5, -0.5, 0.5}, {-0.5, 0.5, 0.5}, {0.5, 0.5, 0.5}};
89 
90  glBegin(GL_QUADS);
91  glNormal3f(0, 0, -1);
92  glVertex3fv(vertices[0]);
93  glVertex3fv(vertices[2]);
94  glVertex3fv(vertices[3]);
95  glVertex3fv(vertices[1]);
96  glNormal3f(0, 0, 1);
97  glVertex3fv(vertices[4]);
98  glVertex3fv(vertices[5]);
99  glVertex3fv(vertices[7]);
100  glVertex3fv(vertices[6]);
101  glNormal3f(0, -1, 0);
102  glVertex3fv(vertices[0]);
103  glVertex3fv(vertices[1]);
104  glVertex3fv(vertices[5]);
105  glVertex3fv(vertices[4]);
106  glNormal3f(0, 1, 0);
107  glVertex3fv(vertices[2]);
108  glVertex3fv(vertices[6]);
109  glVertex3fv(vertices[7]);
110  glVertex3fv(vertices[3]);
111  glNormal3f(-1, 0, 0);
112  glVertex3fv(vertices[0]);
113  glVertex3fv(vertices[4]);
114  glVertex3fv(vertices[6]);
115  glVertex3fv(vertices[2]);
116  glNormal3f(1, 0, 0);
117  glVertex3fv(vertices[1]);
118  glVertex3fv(vertices[3]);
119  glVertex3fv(vertices[7]);
120  glVertex3fv(vertices[5]);
121  glEnd();
122 }

Referenced by drawVectorBox().

◆ drawUnitSphere()

void GpuHelper::drawUnitSphere ( int  level = 0)
124  {
125  static IcoSphere sphere;
126  sphere.draw(level);
127 }
Definition: icosphere.h:16
void draw(int level)
Definition: icosphere.cpp:90

References IcoSphere::draw().

◆ drawVector()

void GpuHelper::drawVector ( const Vector3f &  position,
const Vector3f &  vec,
const Color color,
float  aspect = 50. 
)
48  {
49  static GLUquadricObj* cylinder = gluNewQuadric();
50  glColor4fv(color.data());
51  float length = vec.norm();
52  pushMatrix(GL_MODELVIEW);
53  glTranslatef(position.x(), position.y(), position.z());
54  Vector3f ax = Matrix3f::Identity().col(2).cross(vec);
55  ax.normalize();
56  Vector3f tmp = vec;
57  tmp.normalize();
58  float angle = 180.f / M_PI * acos(tmp.z());
59  if (angle > 1e-3) glRotatef(angle, ax.x(), ax.y(), ax.z());
60  gluCylinder(cylinder, length / aspect, length / aspect, 0.8 * length, 10, 10);
61  glTranslatef(0.0, 0.0, 0.8 * length);
62  gluCylinder(cylinder, 2.0 * length / aspect, 0.0, 0.2 * length, 10, 10);
63 
64  popMatrix(GL_MODELVIEW);
65 }
AnnoyingScalar acos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:138
Array< double, 1, 3 > e(1./3., 0.5, 2.)
void pushMatrix(const Matrix< Scalar, 4, 4, Flags_, 4, 4 > &mat, GLenum matrixTarget)
Definition: gpuhelper.h:162
void popMatrix(GLenum matrixTarget)
Definition: gpuhelper.h:174
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
#define M_PI
Definition: main.h:121
double angle(const double &t)
Angular position as a function of time t.
Definition: jeffery_orbit.cc:98
ax
Definition: plotDoE.py:39

References acos(), Jeffery_Solution::angle(), plotDoE::ax, e(), M_PI, popMatrix(), pushMatrix(), and tmp.

Referenced by RenderingWidget::drawScene().

◆ drawVectorBox()

void GpuHelper::drawVectorBox ( const Vector3f &  position,
const Vector3f &  vec,
const Color color,
float  aspect = 50. 
)
67  {
68  static GLUquadricObj* cylinder = gluNewQuadric();
69  glColor4fv(color.data());
70  float length = vec.norm();
71  pushMatrix(GL_MODELVIEW);
72  glTranslatef(position.x(), position.y(), position.z());
73  Vector3f ax = Matrix3f::Identity().col(2).cross(vec);
74  ax.normalize();
75  Vector3f tmp = vec;
76  tmp.normalize();
77  float angle = 180.f / M_PI * acos(tmp.z());
78  if (angle > 1e-3) glRotatef(angle, ax.x(), ax.y(), ax.z());
79  gluCylinder(cylinder, length / aspect, length / aspect, 0.8 * length, 10, 10);
80  glTranslatef(0.0, 0.0, 0.8 * length);
81  glScalef(4.0 * length / aspect, 4.0 * length / aspect, 4.0 * length / aspect);
82  drawUnitCube();
83  popMatrix(GL_MODELVIEW);
84 }
void drawUnitCube(void)
Definition: gpuhelper.cpp:86

References acos(), Jeffery_Solution::angle(), plotDoE::ax, drawUnitCube(), e(), M_PI, popMatrix(), pushMatrix(), and tmp.

◆ forceMatrixTarget()

void GpuHelper::forceMatrixTarget ( GLenum  matrixTarget)
inline

Make the matrix 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.
60 { glMatrixMode(mCurrentMatrixTarget = matrixTarget); }

◆ loadMatrix() [1/2]

template<typename Scalar , typename Derived >
void GpuHelper::loadMatrix ( const Eigen::CwiseNullaryOp< Eigen::internal::scalar_identity_op< Scalar >, Derived > &  ,
GLenum  matrixTarget 
)
145  {
146  setMatrixTarget(matrixTarget);
147  glLoadIdentity();
148 }
void setMatrixTarget(GLenum matrixTarget)
Definition: gpuhelper.h:133

◆ loadMatrix() [2/2]

template<typename Scalar , int Flags_>
void GpuHelper::loadMatrix ( const Eigen::Matrix< Scalar, 4, 4, Flags_, 4, 4 > &  mat,
GLenum  matrixTarget 
)

Load the matrix mat to the OpenGL matrix 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().
See also
Matrix, multMatrix(), forceMatrixMode()
151  {
152  setMatrixTarget(matrixTarget);
153  GlMatrixHelper<(Flags_ & Eigen::RowMajorBit) != 0, Flags_>::loadMatrix(mat);
154 }
void loadMatrix(const Eigen::Matrix< Scalar, 4, 4, Flags_, 4, 4 > &mat, GLenum matrixTarget)
Definition: gpuhelper.h:151
const unsigned int RowMajorBit
Definition: Constants.h:70
Definition: gpuhelper.h:111

References Eigen::RowMajorBit.

Referenced by Camera::activateGL().

◆ multMatrix()

template<typename Scalar , int Flags_>
void GpuHelper::multMatrix ( const Matrix< Scalar, 4, 4, Flags_, 4, 4 > &  mat,
GLenum  matrixTarget 
)

Multiply the OpenGL matrix matrixTarget by the matrix 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().
See also
Matrix, loadMatrix(), forceMatrixMode()

Referenced by FancySpheres::draw().

◆ popMatrix()

void GpuHelper::popMatrix ( GLenum  matrixTarget)
inline

Pop the OpenGL matrix matrixTarget

174  {
175  setMatrixTarget(matrixTarget);
176  glPopMatrix();
177 }

Referenced by FancySpheres::draw(), drawVector(), drawVectorBox(), and popProjectionMode2D().

◆ popProjectionMode2D()

void GpuHelper::popProjectionMode2D ( void  )
43  {
44  popMatrix(GL_PROJECTION);
45  popMatrix(GL_MODELVIEW);
46 }

References popMatrix().

◆ pushMatrix() [1/3]

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

◆ pushMatrix() [2/3]

template<typename Scalar , int Flags_>
void GpuHelper::pushMatrix ( const Matrix< Scalar, 4, 4, Flags_, 4, 4 > &  mat,
GLenum  matrixTarget 
)
inline

Push the OpenGL matrix matrixTarget and load mat.

Referenced by FancySpheres::draw(), drawVector(), drawVectorBox(), and pushProjectionMode2D().

◆ pushMatrix() [3/3]

void GpuHelper::pushMatrix ( GLenum  matrixTarget)
inline

Push and clone the OpenGL matrix matrixTarget

156  {
157  setMatrixTarget(matrixTarget);
158  glPushMatrix();
159 }

◆ pushProjectionMode2D()

void GpuHelper::pushProjectionMode2D ( ProjectionMode2D  pm)
28  {
29  // switch to 2D projection
30  pushMatrix(Matrix4f::Identity(), GL_PROJECTION);
31 
32  if (pm == PM_Normalized) {
33  // glOrtho(-1., 1., -1., 1., 0., 1.);
34  } else if (pm == PM_Viewport) {
35  GLint vp[4];
36  glGetIntegerv(GL_VIEWPORT, vp);
37  glOrtho(0., vp[2], 0., vp[3], -1., 1.);
38  }
39 
40  pushMatrix(Matrix4f::Identity(), GL_MODELVIEW);
41 }
Matrix4d pm
Definition: HessenbergDecomposition_packedMatrix.cpp:4

References pm, PM_Normalized, PM_Viewport, and pushMatrix().

◆ setMatrixTarget()

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

◆ update()

void GpuHelper::update ( void  )
protected

Member Data Documentation

◆ mColorBufferId

GLuint GpuHelper::mColorBufferId
protected

◆ mCurrentMatrixTarget

GLenum GpuHelper::mCurrentMatrixTarget
protected

Referenced by GpuHelper().

◆ mInitialized

bool GpuHelper::mInitialized
protected

Referenced by GpuHelper().

◆ mVpHeight

int GpuHelper::mVpHeight
protected

Referenced by GpuHelper().

◆ mVpWidth

int GpuHelper::mVpWidth
protected

Referenced by GpuHelper().


The documentation for this class was generated from the following files: