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.

120 lines
3.8 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 "icosphere.h"
  10. #include <GL/gl.h>
  11. #include <map>
  12. using namespace Eigen;
  13. //--------------------------------------------------------------------------------
  14. // icosahedron data
  15. //--------------------------------------------------------------------------------
  16. #define X .525731112119133606
  17. #define Z .850650808352039932
  18. static GLfloat vdata[12][3] = {
  19. {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
  20. {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
  21. {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
  22. };
  23. static GLint tindices[20][3] = {
  24. {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
  25. {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
  26. {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
  27. {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
  28. //--------------------------------------------------------------------------------
  29. IcoSphere::IcoSphere(unsigned int levels)
  30. {
  31. // init with an icosahedron
  32. for (int i = 0; i < 12; i++)
  33. mVertices.push_back(Map<Vector3f>(vdata[i]));
  34. mIndices.push_back(new std::vector<int>);
  35. std::vector<int>& indices = *mIndices.back();
  36. for (int i = 0; i < 20; i++)
  37. {
  38. for (int k = 0; k < 3; k++)
  39. indices.push_back(tindices[i][k]);
  40. }
  41. mListIds.push_back(0);
  42. while(mIndices.size()<levels)
  43. _subdivide();
  44. }
  45. const std::vector<int>& IcoSphere::indices(int level) const
  46. {
  47. while (level>=int(mIndices.size()))
  48. const_cast<IcoSphere*>(this)->_subdivide();
  49. return *mIndices[level];
  50. }
  51. void IcoSphere::_subdivide(void)
  52. {
  53. typedef unsigned long long Key;
  54. std::map<Key,int> edgeMap;
  55. const std::vector<int>& indices = *mIndices.back();
  56. mIndices.push_back(new std::vector<int>);
  57. std::vector<int>& refinedIndices = *mIndices.back();
  58. int end = indices.size();
  59. for (int i=0; i<end; i+=3)
  60. {
  61. int ids0[3], // indices of outer vertices
  62. ids1[3]; // indices of edge vertices
  63. for (int k=0; k<3; ++k)
  64. {
  65. int k1 = (k+1)%3;
  66. int e0 = indices[i+k];
  67. int e1 = indices[i+k1];
  68. ids0[k] = e0;
  69. if (e1>e0)
  70. std::swap(e0,e1);
  71. Key edgeKey = Key(e0) | (Key(e1)<<32);
  72. std::map<Key,int>::iterator it = edgeMap.find(edgeKey);
  73. if (it==edgeMap.end())
  74. {
  75. ids1[k] = mVertices.size();
  76. edgeMap[edgeKey] = ids1[k];
  77. mVertices.push_back( (mVertices[e0]+mVertices[e1]).normalized() );
  78. }
  79. else
  80. ids1[k] = it->second;
  81. }
  82. refinedIndices.push_back(ids0[0]); refinedIndices.push_back(ids1[0]); refinedIndices.push_back(ids1[2]);
  83. refinedIndices.push_back(ids0[1]); refinedIndices.push_back(ids1[1]); refinedIndices.push_back(ids1[0]);
  84. refinedIndices.push_back(ids0[2]); refinedIndices.push_back(ids1[2]); refinedIndices.push_back(ids1[1]);
  85. refinedIndices.push_back(ids1[0]); refinedIndices.push_back(ids1[1]); refinedIndices.push_back(ids1[2]);
  86. }
  87. mListIds.push_back(0);
  88. }
  89. void IcoSphere::draw(int level)
  90. {
  91. while (level>=int(mIndices.size()))
  92. const_cast<IcoSphere*>(this)->_subdivide();
  93. if (mListIds[level]==0)
  94. {
  95. mListIds[level] = glGenLists(1);
  96. glNewList(mListIds[level], GL_COMPILE);
  97. glVertexPointer(3, GL_FLOAT, 0, mVertices[0].data());
  98. glNormalPointer(GL_FLOAT, 0, mVertices[0].data());
  99. glEnableClientState(GL_VERTEX_ARRAY);
  100. glEnableClientState(GL_NORMAL_ARRAY);
  101. glDrawElements(GL_TRIANGLES, mIndices[level]->size(), GL_UNSIGNED_INT, &(mIndices[level]->at(0)));
  102. glDisableClientState(GL_VERTEX_ARRAY);
  103. glDisableClientState(GL_NORMAL_ARRAY);
  104. glEndList();
  105. }
  106. glCallList(mListIds[level]);
  107. }