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