#include "ConeDrawArray.h" #include using namespace std; namespace X3DTK { namespace GL { ConeDrawArray::refList ConeDrawArray::_refList = ConeDrawArray::refList(); ConeDrawArray *ConeDrawArray::getInstanceOfSection(unsigned int section) { for (refList::iterator it = _refList.begin(); it != _refList.end(); ++it) { if ((*it).first == section) { ++(*it).second.count; return (*it).second.ref; } } ConeDrawArray *c = new ConeDrawArray(section); data d; d.count = 1; d.ref = c; _refList.push_back(pair(section, d)); return c; } void ConeDrawArray::removeInstance() { for (refList::iterator it = _refList.begin(); it != _refList.end(); ++it) { if ((*it).first == _section) { if ((*it).second.count > 0) { --(*it).second.count; if ((*it).second.count == 0) { delete (*it).second.ref; _refList.erase(it); } } return; } } } ConeDrawArray::ConeDrawArray(unsigned int section) { _section = section; //resize the arrays _coneSideVertexArray = vector(section + 1); _coneBottomVertexArray = vector(section + 1); _coneSideIndexArray = vector(section + 2); _coneBottomIndexArray = vector(section + 2); //computing the vertex array //the first point is the center of the bottom face float cf = -2.0f*(float)PI/(float)section; //bottom _coneBottomVertexArray[0].normal = SFVec3f(0.0f, -1.0f, 0.0f); _coneBottomVertexArray[0].vertex = SFVec3f(0.0f, -0.5f, 0.0f); for (unsigned int i = 0; i < section; ++i) { _coneBottomVertexArray[i + 1].normal = SFVec3f(0.0f, -1.0f, 0.0f); _coneBottomVertexArray[i + 1].vertex = SFVec3f(cosf(cf*(float)i), -0.5f, sinf(cf*(float)i)); } //side _coneSideVertexArray[0].normal = SFVec3f(0.0f, 1.0f, 0.0f); _coneSideVertexArray[0].vertex = SFVec3f(0.0f, 0.5f, 0.0f); for (unsigned int j = 0; j < section; ++j) { //computing normal SFVec3f OB(cosf(-cf*(float)j), -1.0f, sinf(-cf*(float)j)); SFVec3f Tang(sinf(-cf*(float)j), 0.0f, -cosf(-cf*(float)j)); _coneSideVertexArray[j + 1].normal = crossprod(OB, Tang); _coneSideVertexArray[j + 1].vertex = SFVec3f(cosf(-cf*(float)j), -0.5f, sinf(-cf*(float)j)); } //computing the index arrays //bottom for (unsigned int k = 0; k < section + 1; ++k) { _coneBottomIndexArray[k] = k; } _coneBottomIndexArray[section + 1] = 1;//loop //side for (unsigned int l = 0; l < section + 1; ++l) { _coneSideIndexArray[l] = l; } _coneSideIndexArray[section + 1] = 1;//loop } unsigned int ConeDrawArray::getConeSideSize() const { return _coneSideIndexArray.size(); } unsigned int ConeDrawArray::getConeBottomSize() const { return _coneBottomIndexArray.size(); } const void *ConeDrawArray::getConeSideVertexArrayAddress() const { return &_coneSideVertexArray.front(); } const void *ConeDrawArray::getConeBottomVertexArrayAddress() const { return &_coneBottomVertexArray.front(); } const unsigned int *ConeDrawArray::getConeSideIndexArrayAddress() const { return &_coneSideIndexArray.front(); } const unsigned int *ConeDrawArray::getConeBottomIndexArrayAddress() const { return &_coneBottomIndexArray.front(); } } }