Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CylinderDrawArray.cpp

Go to the documentation of this file.
00001 #include "CylinderDrawArray.h"
00002 
00003 #include <math.h>
00004 
00005 using namespace X3DTK;
00006 using namespace std;
00007 
00008 CylinderDrawArray::refList CylinderDrawArray::_refList = CylinderDrawArray::refList();
00009 
00010 CylinderDrawArray *CylinderDrawArray::getInstanceOfSection(unsigned int section)
00011 {
00012   for (refList::iterator it = _refList.begin(); it != _refList.end(); ++it)
00013   {
00014     if ((*it).first == section)
00015     {
00016       ++(*it).second.count;
00017       return (*it).second.ref;
00018     }  
00019   }
00020   
00021   CylinderDrawArray *c = new CylinderDrawArray(section);
00022   data d;
00023   d.count = 1;
00024   d.ref = c;
00025   _refList.push_back(pair<unsigned int, data>(section, d));
00026   return c;
00027 }
00028 
00029 void CylinderDrawArray::removeInstance()
00030 {
00031   for (refList::iterator it = _refList.begin(); it != _refList.end(); ++it)
00032   {
00033     if ((*it).first == _section)
00034     {
00035       if ((*it).second.count > 0)
00036       {
00037         --(*it).second.count;
00038         if ((*it).second.count == 0)
00039         {
00040           delete (*it).second.ref;
00041           _refList.erase(it);
00042         }  
00043       }  
00044       return;
00045     }  
00046   }
00047 }
00048 
00049 CylinderDrawArray::CylinderDrawArray(unsigned int section)
00050 : _section(section)
00051 {
00052   //resize the arrays
00053   _cylinderBottomVertexArray = vector<N3F_V3F>(section + 1);
00054   _cylinderSideVertexArray = vector<N3F_V3F>(2*section);
00055   _cylinderTopVertexArray = vector<N3F_V3F>(section + 1);
00056   _cylinderBottomIndexArray = vector<unsigned int>(section + 2);
00057   _cylinderSideIndexArray = vector<unsigned int>(2*section + 2);
00058   _cylinderTopIndexArray = vector<unsigned int>(section + 2);
00059   
00060   //computing the vertex array
00061   //the first point is the center of the bottom face
00062   float cf = -2.0f*(float)PI/(float)section;
00063 
00064   //bottom
00065   _cylinderBottomVertexArray[0].normal = SFVec3f(0.0f, -1.0f, 0.0f);
00066   _cylinderBottomVertexArray[0].vertex = SFVec3f(0.0f, -0.5f, 0.0f);
00067   for (unsigned int k = 0; k < section; ++k)
00068   {
00069     _cylinderBottomVertexArray[k + 1].normal = SFVec3f(0.0f, -1.0f, 0.0f);
00070     _cylinderBottomVertexArray[k + 1].vertex = SFVec3f(cosf(cf*(float)k), -0.5f, sinf(cf*(float)k));
00071   }
00072       
00073   //top
00074   _cylinderTopVertexArray[0].normal = SFVec3f(0.0f, 1.0f, 0.0f);
00075   _cylinderTopVertexArray[0].vertex = SFVec3f(0.0f, 0.5f, 0.0f);
00076   for (unsigned int k = 0; k < section; ++k)
00077   {
00078     _cylinderTopVertexArray[k + 1].normal = SFVec3f(0.0f, 1.0f, 0.0f);
00079     _cylinderTopVertexArray[k + 1].vertex = SFVec3f(cosf(-cf*(float)k), 0.5f, sinf(-cf*(float)k));
00080   }
00081   
00082   //side
00083   for (unsigned int k = 0; k < section; ++k)
00084   {
00085     _cylinderSideVertexArray[2*k].normal = SFVec3f(cosf(cf*(float)k), 0.0f, sinf(cf*(float)k));
00086     _cylinderSideVertexArray[2*k].vertex = SFVec3f(cosf(cf*(float)k), -0.5f, sinf(cf*(float)k));
00087 
00088     _cylinderSideVertexArray[2*k + 1].normal = SFVec3f(cosf(cf*(float)k), 0.0f, sinf(cf*(float)k));
00089     _cylinderSideVertexArray[2*k + 1].vertex = SFVec3f(cosf(cf*(float)k), 0.5f, sinf(cf*(float)k));
00090   }
00091   
00092   //computing the index arrays
00093   //bottom
00094   for (unsigned int k = 0; k < section + 1; ++k)
00095   {
00096     _cylinderBottomIndexArray[k] = k;
00097   }
00098   _cylinderBottomIndexArray[section + 1] = 1;//loop
00099   
00100   //top
00101   for (unsigned int k = 0; k < section + 1; ++k)
00102   {
00103     _cylinderTopIndexArray[k] = k;
00104   }
00105   _cylinderTopIndexArray[section + 1] = 1;//loop
00106 
00107   //side
00108   for (unsigned int k = 0; k < section; ++k)
00109   {
00110     _cylinderSideIndexArray[2*k] = 2*k;
00111     _cylinderSideIndexArray[2*k + 1] = 2*k + 1;
00112   }
00113   _cylinderSideIndexArray[2*section] = 0;//loop
00114   _cylinderSideIndexArray[2*section + 1] = 1;//loop
00115 }
00116 
00117 unsigned int CylinderDrawArray::getCylinderBottomSize() const
00118 {
00119   return _cylinderBottomIndexArray.size();
00120 }
00121 
00122 unsigned int CylinderDrawArray::getCylinderSideSize() const
00123 {
00124   return _cylinderSideIndexArray.size();
00125 }
00126 
00127 unsigned int CylinderDrawArray::getCylinderTopSize() const
00128 {
00129   return _cylinderTopIndexArray.size();
00130 }
00131 
00132 const void *CylinderDrawArray::getCylinderBottomVertexArrayAddress() const
00133 {
00134   return &_cylinderBottomVertexArray.front();
00135 }
00136 
00137 const void *CylinderDrawArray::getCylinderSideVertexArrayAddress() const
00138 {
00139   return &_cylinderSideVertexArray.front();
00140 }
00141 
00142 const void *CylinderDrawArray::getCylinderTopVertexArrayAddress() const
00143 {
00144   return &_cylinderTopVertexArray.front();
00145 }
00146 
00147 const unsigned int *CylinderDrawArray::getCylinderBottomIndexArrayAddress() const
00148 {
00149   return &_cylinderBottomIndexArray.front();
00150 }
00151 
00152 const unsigned int *CylinderDrawArray::getCylinderSideIndexArrayAddress() const
00153 {
00154   return &_cylinderSideIndexArray.front();
00155 }
00156 
00157 const unsigned int *CylinderDrawArray::getCylinderTopIndexArrayAddress() const
00158 {
00159   return &_cylinderTopIndexArray.front();  
00160 }

Generated on Wed May 14 10:03:08 2003 for X3DToolKit by doxygen1.3