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#include "gpuhelper.h" 11#include "icosphere.h" 12#include <GL/glu.h> 13// PLEASE don't look at this old code... ;) 14 15#include <fstream> 16#include <algorithm> 17 18GpuHelper gpu; 19 20GpuHelper::GpuHelper() 21{ 22 mVpWidth = mVpHeight = 0; 23 mCurrentMatrixTarget = 0; 24 mInitialized = false; 25} 26 27GpuHelper::~GpuHelper() 28{ 29} 30 31void GpuHelper::pushProjectionMode2D(ProjectionMode2D pm) 32{ 33 // switch to 2D projection 34 pushMatrix(Matrix4f::Identity(),GL_PROJECTION); 35 36 if(pm==PM_Normalized) 37 { 38 //glOrtho(-1., 1., -1., 1., 0., 1.); 39 } 40 else if(pm==PM_Viewport) 41 { 42 GLint vp[4]; 43 glGetIntegerv(GL_VIEWPORT, vp); 44 glOrtho(0., vp[2], 0., vp[3], -1., 1.); 45 } 46 47 pushMatrix(Matrix4f::Identity(),GL_MODELVIEW); 48} 49 50void GpuHelper::popProjectionMode2D(void) 51{ 52 popMatrix(GL_PROJECTION); 53 popMatrix(GL_MODELVIEW); 54} 55 56void GpuHelper::drawVector(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect /* = 50.*/) 57{ 58 static GLUquadricObj *cylindre = gluNewQuadric(); 59 glColor4fv(color.data()); 60 float length = vec.norm(); 61 pushMatrix(GL_MODELVIEW); 62 glTranslatef(position.x(), position.y(), position.z()); 63 Vector3f ax = Matrix3f::Identity().col(2).cross(vec); 64 ax.normalize(); 65 Vector3f tmp = vec; 66 tmp.normalize(); 67 float angle = 180.f/M_PI * acos(tmp.z()); 68 if (angle>1e-3) 69 glRotatef(angle, ax.x(), ax.y(), ax.z()); 70 gluCylinder(cylindre, length/aspect, length/aspect, 0.8*length, 10, 10); 71 glTranslatef(0.0,0.0,0.8*length); 72 gluCylinder(cylindre, 2.0*length/aspect, 0.0, 0.2*length, 10, 10); 73 74 popMatrix(GL_MODELVIEW); 75} 76 77void GpuHelper::drawVectorBox(const Vector3f& position, const Vector3f& vec, const Color& color, float aspect) 78{ 79 static GLUquadricObj *cylindre = gluNewQuadric(); 80 glColor4fv(color.data()); 81 float length = vec.norm(); 82 pushMatrix(GL_MODELVIEW); 83 glTranslatef(position.x(), position.y(), position.z()); 84 Vector3f ax = Matrix3f::Identity().col(2).cross(vec); 85 ax.normalize(); 86 Vector3f tmp = vec; 87 tmp.normalize(); 88 float angle = 180.f/M_PI * acos(tmp.z()); 89 if (angle>1e-3) 90 glRotatef(angle, ax.x(), ax.y(), ax.z()); 91 gluCylinder(cylindre, length/aspect, length/aspect, 0.8*length, 10, 10); 92 glTranslatef(0.0,0.0,0.8*length); 93 glScalef(4.0*length/aspect,4.0*length/aspect,4.0*length/aspect); 94 drawUnitCube(); 95 popMatrix(GL_MODELVIEW); 96} 97 98void GpuHelper::drawUnitCube(void) 99{ 100 static float vertices[][3] = { 101 {-0.5,-0.5,-0.5}, 102 { 0.5,-0.5,-0.5}, 103 {-0.5, 0.5,-0.5}, 104 { 0.5, 0.5,-0.5}, 105 {-0.5,-0.5, 0.5}, 106 { 0.5,-0.5, 0.5}, 107 {-0.5, 0.5, 0.5}, 108 { 0.5, 0.5, 0.5}}; 109 110 glBegin(GL_QUADS); 111 glNormal3f(0,0,-1); glVertex3fv(vertices[0]); glVertex3fv(vertices[2]); glVertex3fv(vertices[3]); glVertex3fv(vertices[1]); 112 glNormal3f(0,0, 1); glVertex3fv(vertices[4]); glVertex3fv(vertices[5]); glVertex3fv(vertices[7]); glVertex3fv(vertices[6]); 113 glNormal3f(0,-1,0); glVertex3fv(vertices[0]); glVertex3fv(vertices[1]); glVertex3fv(vertices[5]); glVertex3fv(vertices[4]); 114 glNormal3f(0, 1,0); glVertex3fv(vertices[2]); glVertex3fv(vertices[6]); glVertex3fv(vertices[7]); glVertex3fv(vertices[3]); 115 glNormal3f(-1,0,0); glVertex3fv(vertices[0]); glVertex3fv(vertices[4]); glVertex3fv(vertices[6]); glVertex3fv(vertices[2]); 116 glNormal3f( 1,0,0); glVertex3fv(vertices[1]); glVertex3fv(vertices[3]); glVertex3fv(vertices[7]); glVertex3fv(vertices[5]); 117 glEnd(); 118} 119 120void GpuHelper::drawUnitSphere(int level) 121{ 122 static IcoSphere sphere; 123 sphere.draw(level); 124} 125 126 127