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