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