// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008 Gael Guennebaud // // 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 #include #include using namespace StormEigen; 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 your're unsure, use forceMatrixMode(). \sa Matrix, loadMatrix(), forceMatrixMode() */ template void multMatrix(const Matrix& 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 your're unsure, use forceMatrixMode(). \sa Matrix, multMatrix(), forceMatrixMode() */ template void loadMatrix(const StormEigen::Matrix& mat, GLenum matrixTarget); template void loadMatrix( const StormEigen::CwiseNullaryOp,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 inline void pushMatrix(const Matrix& mat, GLenum matrixTarget); template void pushMatrix( const StormEigen::CwiseNullaryOp,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* pIndexes); protected: void update(void); GLuint mColorBufferId; int mVpWidth, mVpHeight; GLenum mCurrentMatrixTarget; bool mInitialized; }; /** Singleton shortcut */ extern GpuHelper gpu; /** \internal */ template struct GlMatrixHelper; template struct GlMatrixHelper { static void loadMatrix(const Matrix& mat) { glLoadMatrixf(mat.data()); } static void loadMatrix(const Matrix& mat) { glLoadMatrixd(mat.data()); } static void multMatrix(const Matrix& mat) { glMultMatrixf(mat.data()); } static void multMatrix(const Matrix& mat) { glMultMatrixd(mat.data()); } }; template struct GlMatrixHelper { static void loadMatrix(const Matrix& mat) { glLoadMatrixf(mat.transpose().eval().data()); } static void loadMatrix(const Matrix& mat) { glLoadMatrixd(mat.transpose().eval().data()); } static void multMatrix(const Matrix& mat) { glMultMatrixf(mat.transpose().eval().data()); } static void multMatrix(const Matrix& mat) { glMultMatrixd(mat.transpose().eval().data()); } }; inline void GpuHelper::setMatrixTarget(GLenum matrixTarget) { if (matrixTarget != mCurrentMatrixTarget) glMatrixMode(mCurrentMatrixTarget=matrixTarget); } template void GpuHelper::multMatrix(const Matrix& mat, GLenum matrixTarget) { setMatrixTarget(matrixTarget); GlMatrixHelper<_Flags&StormEigen::RowMajorBit, _Flags>::multMatrix(mat); } template void GpuHelper::loadMatrix( const StormEigen::CwiseNullaryOp,Derived>&, GLenum matrixTarget) { setMatrixTarget(matrixTarget); glLoadIdentity(); } template void GpuHelper::loadMatrix(const StormEigen::Matrix& mat, GLenum matrixTarget) { setMatrixTarget(matrixTarget); GlMatrixHelper<(_Flags&StormEigen::RowMajorBit)!=0, _Flags>::loadMatrix(mat); } inline void GpuHelper::pushMatrix(GLenum matrixTarget) { setMatrixTarget(matrixTarget); glPushMatrix(); } template inline void GpuHelper::pushMatrix(const Matrix& mat, GLenum matrixTarget) { pushMatrix(matrixTarget); GlMatrixHelper<_Flags&StormEigen::RowMajorBit,_Flags>::loadMatrix(mat); } template void GpuHelper::pushMatrix( const StormEigen::CwiseNullaryOp,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* 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