You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.9 KiB

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