gpuhelper.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "gpuhelper.h"
00026 #include "icosphere.h"
00027 #include <GL/glu.h>
00028 // PLEASE don't look at this old code... ;)
00029 
00030 #include <fstream>
00031 #include <algorithm>
00032 
00033 GpuHelper gpu;
00034 
00035 GpuHelper::GpuHelper()
00036 {
00037     mVpWidth = mVpHeight = 0;
00038     mCurrentMatrixTarget = 0;
00039     mInitialized = false;
00040 }
00041 
00042 GpuHelper::~GpuHelper()
00043 {
00044 }
00045 
00046 void GpuHelper::pushProjectionMode2D(ProjectionMode2D pm)
00047 {
00048     // switch to 2D projection
00049     pushMatrix(Matrix4f::Identity(),GL_PROJECTION);
00050 
00051     if(pm==PM_Normalized)
00052     {
00053         //glOrtho(-1., 1., -1., 1., 0., 1.);
00054     }
00055     else if(pm==PM_Viewport)
00056     {
00057         GLint vp[4];
00058         glGetIntegerv(GL_VIEWPORT, vp);
00059         glOrtho(0., vp[2], 0., vp[3], -1., 1.);
00060     }
00061 
00062     pushMatrix(Matrix4f::Identity(),GL_MODELVIEW);
00063 }
00064 
00065 void GpuHelper::popProjectionMode2D(void)
00066 {
00067     popMatrix(GL_PROJECTION);
00068     popMatrix(GL_MODELVIEW);
00069 }
00070 
00071 void GpuHelper::drawVector(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect /* = 50.*/)
00072 {
00073     static GLUquadricObj *cylindre = gluNewQuadric();
00074     glColor4fv(color.data());
00075     float length = vec.norm();
00076     pushMatrix(GL_MODELVIEW);
00077     glTranslatef(position.x(), position.y(), position.z());
00078     Vector3f ax = Matrix3f::Identity().col(2).cross(vec);
00079     ax.normalize();
00080     Vector3f tmp = vec;
00081     tmp.normalize();
00082     float angle = 180.f/M_PI * acos(tmp.z());
00083     if (angle>1e-3)
00084         glRotatef(angle, ax.x(), ax.y(), ax.z());
00085     gluCylinder(cylindre, length/aspect, length/aspect, 0.8*length, 10, 10);
00086     glTranslatef(0.0,0.0,0.8*length);
00087     gluCylinder(cylindre, 2.0*length/aspect, 0.0, 0.2*length, 10, 10);
00088 
00089     popMatrix(GL_MODELVIEW);
00090 }
00091 
00092 void GpuHelper::drawVectorBox(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect)
00093 {
00094     static GLUquadricObj *cylindre = gluNewQuadric();
00095     glColor4fv(color.data());
00096     float length = vec.norm();
00097     pushMatrix(GL_MODELVIEW);
00098     glTranslatef(position.x(), position.y(), position.z());
00099     Vector3f ax = Matrix3f::Identity().col(2).cross(vec);
00100     ax.normalize();
00101     Vector3f tmp = vec;
00102     tmp.normalize();
00103     float angle = 180.f/M_PI * acos(tmp.z());
00104     if (angle>1e-3)
00105         glRotatef(angle, ax.x(), ax.y(), ax.z());
00106     gluCylinder(cylindre, length/aspect, length/aspect, 0.8*length, 10, 10);
00107     glTranslatef(0.0,0.0,0.8*length);
00108     glScalef(4.0*length/aspect,4.0*length/aspect,4.0*length/aspect);
00109     drawUnitCube();
00110     popMatrix(GL_MODELVIEW);
00111 }
00112 
00113 void GpuHelper::drawUnitCube(void)
00114 {
00115     static float vertices[][3] = {
00116         {-0.5,-0.5,-0.5},
00117         { 0.5,-0.5,-0.5},
00118         {-0.5, 0.5,-0.5},
00119         { 0.5, 0.5,-0.5},
00120         {-0.5,-0.5, 0.5},
00121         { 0.5,-0.5, 0.5},
00122         {-0.5, 0.5, 0.5},
00123         { 0.5, 0.5, 0.5}};
00124 
00125     glBegin(GL_QUADS);
00126     glNormal3f(0,0,-1); glVertex3fv(vertices[0]); glVertex3fv(vertices[2]); glVertex3fv(vertices[3]); glVertex3fv(vertices[1]);
00127     glNormal3f(0,0, 1); glVertex3fv(vertices[4]); glVertex3fv(vertices[5]); glVertex3fv(vertices[7]); glVertex3fv(vertices[6]);
00128     glNormal3f(0,-1,0); glVertex3fv(vertices[0]); glVertex3fv(vertices[1]); glVertex3fv(vertices[5]); glVertex3fv(vertices[4]);
00129     glNormal3f(0, 1,0); glVertex3fv(vertices[2]); glVertex3fv(vertices[6]); glVertex3fv(vertices[7]); glVertex3fv(vertices[3]);
00130     glNormal3f(-1,0,0); glVertex3fv(vertices[0]); glVertex3fv(vertices[4]); glVertex3fv(vertices[6]); glVertex3fv(vertices[2]);
00131     glNormal3f( 1,0,0); glVertex3fv(vertices[1]); glVertex3fv(vertices[3]); glVertex3fv(vertices[7]); glVertex3fv(vertices[5]);
00132     glEnd();
00133 }
00134 
00135 void GpuHelper::drawUnitSphere(int level)
00136 {
00137   static IcoSphere sphere;
00138   sphere.draw(level);
00139 }
00140 
00141 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:16