icosphere.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #include "icosphere.h"
00026 
00027 #include <GL/gl.h>
00028 #include <map>
00029 
00030 using namespace Eigen;
00031 
00032 //--------------------------------------------------------------------------------
00033 // icosahedron data
00034 //--------------------------------------------------------------------------------
00035 #define X .525731112119133606
00036 #define Z .850650808352039932
00037 
00038 static GLfloat vdata[12][3] = {
00039    {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
00040    {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
00041    {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
00042 };
00043 
00044 static GLint tindices[20][3] = {
00045    {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
00046    {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
00047    {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
00048    {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
00049 //--------------------------------------------------------------------------------
00050 
00051 IcoSphere::IcoSphere(unsigned int levels)
00052 {
00053   // init with an icosahedron
00054   for (int i = 0; i < 12; i++)
00055     mVertices.push_back(Map<Vector3f>(vdata[i]));
00056   mIndices.push_back(new std::vector<int>);
00057   std::vector<int>& indices = *mIndices.back();
00058   for (int i = 0; i < 20; i++)
00059   {
00060     for (int k = 0; k < 3; k++)
00061       indices.push_back(tindices[i][k]);
00062   }
00063   mListIds.push_back(0);
00064 
00065   while(mIndices.size()<levels)
00066     _subdivide();
00067 }
00068 
00069 const std::vector<int>& IcoSphere::indices(int level) const
00070 {
00071   while (level>=int(mIndices.size()))
00072     const_cast<IcoSphere*>(this)->_subdivide();
00073   return *mIndices[level];
00074 }
00075 
00076 void IcoSphere::_subdivide(void)
00077 {
00078   typedef unsigned long long Key;
00079   std::map<Key,int> edgeMap;
00080   const std::vector<int>& indices = *mIndices.back();
00081   mIndices.push_back(new std::vector<int>);
00082   std::vector<int>& refinedIndices = *mIndices.back();
00083   int end = indices.size();
00084   for (int i=0; i<end; i+=3)
00085   {
00086     int ids0[3],  // indices of outer vertices
00087         ids1[3];  // indices of edge vertices
00088     for (int k=0; k<3; ++k)
00089     {
00090       int k1 = (k+1)%3;
00091       int e0 = indices[i+k];
00092       int e1 = indices[i+k1];
00093       ids0[k] = e0;
00094       if (e1>e0)
00095         std::swap(e0,e1);
00096       Key edgeKey = Key(e0) | (Key(e1)<<32);
00097       std::map<Key,int>::iterator it = edgeMap.find(edgeKey);
00098       if (it==edgeMap.end())
00099       {
00100         ids1[k] = mVertices.size();
00101         edgeMap[edgeKey] = ids1[k];
00102         mVertices.push_back( (mVertices[e0]+mVertices[e1]).normalized() );
00103       }
00104       else
00105         ids1[k] = it->second;
00106     }
00107     refinedIndices.push_back(ids0[0]); refinedIndices.push_back(ids1[0]); refinedIndices.push_back(ids1[2]);
00108     refinedIndices.push_back(ids0[1]); refinedIndices.push_back(ids1[1]); refinedIndices.push_back(ids1[0]);
00109     refinedIndices.push_back(ids0[2]); refinedIndices.push_back(ids1[2]); refinedIndices.push_back(ids1[1]);
00110     refinedIndices.push_back(ids1[0]); refinedIndices.push_back(ids1[1]); refinedIndices.push_back(ids1[2]);
00111   }
00112   mListIds.push_back(0);
00113 }
00114 
00115 void IcoSphere::draw(int level)
00116 {
00117   while (level>=int(mIndices.size()))
00118     const_cast<IcoSphere*>(this)->_subdivide();
00119   if (mListIds[level]==0)
00120   {
00121     mListIds[level] = glGenLists(1);
00122     glNewList(mListIds[level], GL_COMPILE);
00123       glVertexPointer(3, GL_FLOAT, 0, mVertices[0].data());
00124       glNormalPointer(GL_FLOAT, 0, mVertices[0].data());
00125       glEnableClientState(GL_VERTEX_ARRAY);
00126       glEnableClientState(GL_NORMAL_ARRAY);
00127       glDrawElements(GL_TRIANGLES, mIndices[level]->size(), GL_UNSIGNED_INT, &(mIndices[level]->at(0)));
00128       glDisableClientState(GL_VERTEX_ARRAY);
00129       glDisableClientState(GL_NORMAL_ARRAY);
00130     glEndList();
00131   }
00132   glCallList(mListIds[level]);
00133 }
00134 
00135 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:24