#include "CylinderDrawArray.h" #include using namespace std; namespace X3DTK { namespace GL { CylinderDrawArray::refList CylinderDrawArray::_refList = CylinderDrawArray::refList(); CylinderDrawArray *CylinderDrawArray::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; } } CylinderDrawArray *c = new CylinderDrawArray(section); data d; d.count = 1; d.ref = c; _refList.push_back(pair(section, d)); return c; } void CylinderDrawArray::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; } } } CylinderDrawArray::CylinderDrawArray(unsigned int section) : _section(section) { //resize the arrays _cylinderBottomVertexArray = vector(section + 1); _cylinderSideVertexArray = vector(2*section); _cylinderTopVertexArray = vector(section + 1); _cylinderBottomIndexArray = vector(section + 2); _cylinderSideIndexArray = vector(2*section + 2); _cylinderTopIndexArray = 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 _cylinderBottomVertexArray[0].normal = SFVec3f(0.0f, -1.0f, 0.0f); _cylinderBottomVertexArray[0].vertex = SFVec3f(0.0f, -0.5f, 0.0f); for (unsigned int i = 0; i < section; ++i) { _cylinderBottomVertexArray[i + 1].normal = SFVec3f(0.0f, -1.0f, 0.0f); _cylinderBottomVertexArray[i + 1].vertex = SFVec3f(cosf(cf*(float)i), -0.5f, sinf(cf*(float)i)); } //top _cylinderTopVertexArray[0].normal = SFVec3f(0.0f, 1.0f, 0.0f); _cylinderTopVertexArray[0].vertex = SFVec3f(0.0f, 0.5f, 0.0f); for (unsigned int j = 0; j < section; ++j) { _cylinderTopVertexArray[j + 1].normal = SFVec3f(0.0f, 1.0f, 0.0f); _cylinderTopVertexArray[j + 1].vertex = SFVec3f(cosf(-cf*(float)j), 0.5f, sinf(-cf*(float)j)); } //side for (unsigned int k = 0; k < section; ++k) { _cylinderSideVertexArray[2*k].normal = SFVec3f(cosf(cf*(float)k), 0.0f, sinf(cf*(float)k)); _cylinderSideVertexArray[2*k].vertex = SFVec3f(cosf(cf*(float)k), -0.5f, sinf(cf*(float)k)); _cylinderSideVertexArray[2*k + 1].normal = SFVec3f(cosf(cf*(float)k), 0.0f, sinf(cf*(float)k)); _cylinderSideVertexArray[2*k + 1].vertex = SFVec3f(cosf(cf*(float)k), 0.5f, sinf(cf*(float)k)); } //computing the index arrays //bottom for (unsigned int l = 0; l < section + 1; ++l) { _cylinderBottomIndexArray[l] = l; } _cylinderBottomIndexArray[section + 1] = 1;//loop //top for (unsigned int m = 0; m < section + 1; ++m) { _cylinderTopIndexArray[m] = m; } _cylinderTopIndexArray[section + 1] = 1;//loop //side for (unsigned int n = 0; n < section; ++n) { _cylinderSideIndexArray[2*n] = 2*n; _cylinderSideIndexArray[2*n + 1] = 2*n + 1; } _cylinderSideIndexArray[2*section] = 0;//loop _cylinderSideIndexArray[2*section + 1] = 1;//loop } unsigned int CylinderDrawArray::getCylinderBottomSize() const { return _cylinderBottomIndexArray.size(); } unsigned int CylinderDrawArray::getCylinderSideSize() const { return _cylinderSideIndexArray.size(); } unsigned int CylinderDrawArray::getCylinderTopSize() const { return _cylinderTopIndexArray.size(); } const void *CylinderDrawArray::getCylinderBottomVertexArrayAddress() const { return &_cylinderBottomVertexArray.front(); } const void *CylinderDrawArray::getCylinderSideVertexArrayAddress() const { return &_cylinderSideVertexArray.front(); } const void *CylinderDrawArray::getCylinderTopVertexArrayAddress() const { return &_cylinderTopVertexArray.front(); } const unsigned int *CylinderDrawArray::getCylinderBottomIndexArrayAddress() const { return &_cylinderBottomIndexArray.front(); } const unsigned int *CylinderDrawArray::getCylinderSideIndexArrayAddress() const { return &_cylinderSideIndexArray.front(); } const unsigned int *CylinderDrawArray::getCylinderTopIndexArrayAddress() const { return &_cylinderTopIndexArray.front(); } } }