00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "gpuhelper.h"
00026 #include "icosphere.h"
00027 #include <GL/glu.h>
00028
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
00049 pushMatrix(Matrix4f::Identity(),GL_PROJECTION);
00050
00051 if(pm==PM_Normalized)
00052 {
00053
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 )
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