00001 #include "ConeDrawArray.h"
00002
00003 #include <math.h>
00004
00005 using namespace X3DTK;
00006 using namespace std;
00007
00008 ConeDrawArray::refList ConeDrawArray::_refList = ConeDrawArray::refList();
00009
00010 ConeDrawArray *ConeDrawArray::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 ConeDrawArray *c = new ConeDrawArray(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 ConeDrawArray::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 ConeDrawArray::ConeDrawArray(unsigned int section)
00050 {
00051 _section = section;
00052
00053 _coneSideVertexArray = vector<N3F_V3F>(section + 1);
00054 _coneBottomVertexArray = vector<N3F_V3F>(section + 1);
00055 _coneSideIndexArray = vector<unsigned int>(section + 2);
00056 _coneBottomIndexArray = vector<unsigned int>(section + 2);
00057
00058
00059
00060
00061 float cf = -2.0f*(float)PI/(float)section;
00062
00063
00064 _coneBottomVertexArray[0].normal = SFVec3f(0.0f, -1.0f, 0.0f);
00065 _coneBottomVertexArray[0].vertex = SFVec3f(0.0f, -0.5f, 0.0f);
00066 for (unsigned int k = 0; k < section; ++k)
00067 {
00068 _coneBottomVertexArray[k + 1].normal = SFVec3f(0.0f, -1.0f, 0.0f);
00069 _coneBottomVertexArray[k + 1].vertex = SFVec3f(cosf(cf*(float)k), -0.5f, sinf(cf*(float)k));
00070 }
00071
00072
00073 _coneSideVertexArray[0].normal = SFVec3f(0.0f, 1.0f, 0.0f);
00074 _coneSideVertexArray[0].vertex = SFVec3f(0.0f, 0.5f, 0.0f);
00075 for (unsigned int k = 0; k < section; ++k)
00076 {
00077
00078 SFVec3f OB(cosf(-cf*(float)k), -1.0f, sinf(-cf*(float)k));
00079 SFVec3f Tang(sinf(-cf*(float)k), 0.0f, -cosf(-cf*(float)k));
00080 _coneSideVertexArray[k + 1].normal = crossprod(OB, Tang);
00081 _coneSideVertexArray[k + 1].vertex = SFVec3f(cosf(-cf*(float)k), -0.5f, sinf(-cf*(float)k));
00082 }
00083
00084
00085
00086 for (unsigned int k = 0; k < section + 1; ++k)
00087 {
00088 _coneBottomIndexArray[k] = k;
00089 }
00090 _coneBottomIndexArray[section + 1] = 1;
00091
00092
00093 for (unsigned int k = 0; k < section + 1; ++k)
00094 {
00095 _coneSideIndexArray[k] = k;
00096 }
00097 _coneSideIndexArray[section + 1] = 1;
00098 }
00099
00100 unsigned int ConeDrawArray::getConeSideSize() const
00101 {
00102 return _coneSideIndexArray.size();
00103 }
00104
00105 unsigned int ConeDrawArray::getConeBottomSize() const
00106 {
00107 return _coneBottomIndexArray.size();
00108 }
00109
00110 const void *ConeDrawArray::getConeSideVertexArrayAddress() const
00111 {
00112 return &_coneSideVertexArray.front();
00113 }
00114
00115 const void *ConeDrawArray::getConeBottomVertexArrayAddress() const
00116 {
00117 return &_coneBottomVertexArray.front();
00118 }
00119
00120 const unsigned int *ConeDrawArray::getConeSideIndexArrayAddress() const
00121 {
00122 return &_coneSideIndexArray.front();
00123 }
00124
00125 const unsigned int *ConeDrawArray::getConeBottomIndexArrayAddress() const
00126 {
00127 return &_coneBottomIndexArray.front();
00128 }