#include "SphereDrawArray.h" #include using namespace std; namespace X3DTK { namespace GL { SphereDrawArray::refList SphereDrawArray::_refList = SphereDrawArray::refList(); SphereDrawArray *SphereDrawArray::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; } } SphereDrawArray *c = new SphereDrawArray(section); data d; d.count = 1; d.ref = c; _refList.push_back(pair(section, d)); return c; } void SphereDrawArray::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; } } } SphereDrawArray::SphereDrawArray(unsigned int section) { _section = section; unsigned int sub_phi = section; unsigned int sub_theta = section; //filling the vertex array _sphereVertexArray = vector(sub_theta*(sub_phi - 1) + 2); _sphereVertexArray[0].normal = SFVec3f(0.0f, 1.0f, 0.0f); _sphereVertexArray[0].vertex = SFVec3f(0.0f, 1.0f, 0.0f); for (unsigned int i = 1; i < sub_phi; ++i) { for (unsigned int j = 0; j < sub_theta; ++j) { float phi = (float)PI*(float)i/sub_phi; float theta = 2.0f*(float)PI*(float)j/sub_theta; SFVec3f vec(sinf(theta)*sinf(phi), cosf(phi), cosf(theta)*sinf(phi)); _sphereVertexArray[(i - 1)*sub_theta + j + 1].normal = vec; _sphereVertexArray[(i - 1)*sub_theta + j + 1].vertex = vec; } } _sphereVertexArray[sub_theta*(sub_phi - 1) + 1].normal = SFVec3f(0.0f, -1.0f, 0.0f); _sphereVertexArray[sub_theta*(sub_phi - 1) + 1].vertex = SFVec3f(0.0f, -1.0f, 0.0f); //filling the index array _sphereIndexArray = vector(); for (unsigned int j = 0; j < sub_theta - 1; ++j) { _sphereIndexArray.push_back(0); _sphereIndexArray.push_back(j + 2); _sphereIndexArray.push_back(j + 1); } _sphereIndexArray.push_back(0); _sphereIndexArray.push_back(1); _sphereIndexArray.push_back(sub_theta); for (unsigned int k = 0; k < sub_phi - 2; ++k) { for (unsigned int j = 0; j < sub_theta - 1; ++j) { _sphereIndexArray.push_back(1 + (k*sub_theta + j)); _sphereIndexArray.push_back(1 + ((k + 1)*sub_theta + j + 1)); _sphereIndexArray.push_back(1 + ((k + 1)*sub_theta + j)); _sphereIndexArray.push_back(1 + ((k + 1)*sub_theta + j + 1)); _sphereIndexArray.push_back(1 + (k*sub_theta + j)); _sphereIndexArray.push_back(1 + (k*sub_theta + j + 1)); } _sphereIndexArray.push_back(1 + ((k + 1)*sub_theta + sub_theta - 1)); _sphereIndexArray.push_back(1 + (k*sub_theta + sub_theta - 1)); _sphereIndexArray.push_back(1 + ((k + 1)*sub_theta)); _sphereIndexArray.push_back(1 + ((k + 1)*sub_theta)); _sphereIndexArray.push_back(1 + (k*sub_theta + sub_theta - 1)); _sphereIndexArray.push_back(1 + (k*sub_theta)); } for (unsigned int l = 0; l < sub_theta - 1; ++l) { _sphereIndexArray.push_back(1 + ((sub_phi - 2)*sub_theta + l)); _sphereIndexArray.push_back(1 + ((sub_phi - 2)*sub_theta + l + 1)); _sphereIndexArray.push_back(sub_theta*(sub_phi - 1) + 1); } _sphereIndexArray.push_back(1 + ((sub_phi - 2)*sub_theta + sub_theta - 1)); _sphereIndexArray.push_back(1 + ((sub_phi - 2)*sub_theta)); _sphereIndexArray.push_back(sub_theta*(sub_phi - 1) + 1); } unsigned int SphereDrawArray::getSphereSize() const { return _sphereIndexArray.size(); } const void *SphereDrawArray::getSphereVertexArrayAddress() const { return &_sphereVertexArray.front(); } const unsigned int *SphereDrawArray::getSphereIndexArrayAddress() const { return &_sphereIndexArray.front(); } } }