78 create(aNumberOfSectors, aHeight, aRadius, anIndexDataType);
89 if (aNumberOfSectors < 3)
91 throw Exception(__FILE__, __FUNCTION__, __LINE__,
"Invalid parameter, the number of sectors should be at least 3");
96 m_external_data_flag =
false;
100 m_local_min_corner =
VEC3(-aRadius, -aHeight / 2.0, -aRadius);
101 m_local_max_corner =
VEC3( aRadius, aHeight / 2.0, aRadius);
103 std::vector<T> p_vertex_set;
104 std::vector<T> p_normal_set;
105 std::vector<unsigned int> p_index_set;
108 double angle(
Pi * 2.0 / aNumberOfSectors);
110 p_vertex_set.push_back(0);
111 p_vertex_set.push_back(-aHeight / 2.0);
112 p_vertex_set.push_back(0);
114 p_vertex_set.push_back(0);
115 p_vertex_set.push_back(aHeight / 2.0);
116 p_vertex_set.push_back(0);
120 for(
unsigned int s(0); s < aNumberOfSectors; ++s, theta += angle)
122 double const x = cos(theta);
123 double const z = sin(theta);
125 p_vertex_set.push_back(x * aRadius);
126 p_vertex_set.push_back(-aHeight / 2.0);
127 p_vertex_set.push_back(z * aRadius);
129 p_normal_set.push_back(x);
130 p_normal_set.push_back(0);
131 p_normal_set.push_back(z);
133 p_vertex_set.push_back(x * aRadius);
134 p_vertex_set.push_back(aHeight / 2.0);
135 p_vertex_set.push_back(z * aRadius);
137 p_normal_set.push_back(x);
138 p_normal_set.push_back(0);
139 p_normal_set.push_back(z);
142 if (s < aNumberOfSectors - 1)
144 p_index_set.push_back(2 + s * 2);
145 p_index_set.push_back(2 + s * 2 + 1);
146 p_index_set.push_back(2 + s * 2 + 2);
148 p_index_set.push_back(2 + s * 2 + 2);
149 p_index_set.push_back(2 + s * 2 + 1);
150 p_index_set.push_back(2 + s * 2 + 3);
154 p_index_set.push_back(2 + s * 2);
155 p_index_set.push_back(2 + s * 2 + 1);
156 p_index_set.push_back(2 + 0);
158 p_index_set.push_back(2 + 0);
159 p_index_set.push_back(2 + s * 2 + 1);
160 p_index_set.push_back(2 + 1);
166 for(
unsigned int s(0); s < aNumberOfSectors; ++s, theta += angle)
168 p_normal_set.push_back(0);
169 p_normal_set.push_back(-1);
170 p_normal_set.push_back(0);
172 p_normal_set.push_back(0);
173 p_normal_set.push_back(1);
174 p_normal_set.push_back(0);
176 if (s < aNumberOfSectors - 1)
178 p_index_set.push_back(0);
179 p_index_set.push_back(2 + s * 2);
180 p_index_set.push_back(2 + s * 2 + 2);
182 p_index_set.push_back(1);
183 p_index_set.push_back(2 + s * 2 + 3);
184 p_index_set.push_back(2 + s * 2 + 1);
188 p_index_set.push_back(0);
189 p_index_set.push_back(2 + s * 2);
190 p_index_set.push_back(2);
192 p_index_set.push_back(1);
193 p_index_set.push_back(2 + 1);
194 p_index_set.push_back(2 + s * 2 + 1);
201 std::vector<T> p_normal_set2;
203 for (
unsigned int vertex_id = 0; vertex_id < p_vertex_set.size() / 3; ++vertex_id)
209 unsigned int counter = 0;
211 for (
unsigned int face_id = 0; face_id < p_index_set.size() / 3; ++face_id)
213 if (p_index_set[face_id * 3] == vertex_id ||
214 p_index_set[face_id * 3 + 1] == vertex_id ||
215 p_index_set[face_id * 3 + 2] == vertex_id)
218 x += p_normal_set[face_id * 3];
219 y += p_normal_set[face_id * 3 + 1];
220 z += p_normal_set[face_id * 3 + 2];
231 p_normal_set2.push_back(x);
232 p_normal_set2.push_back(y);
233 p_normal_set2.push_back(z);
236 switch (anIndexDataType)
238 case GL_UNSIGNED_INT:
240 setInternalData(GL_TRIANGLES,
249 case GL_UNSIGNED_SHORT:
251 std::vector<GLushort> p_ushort_index_set;
253 for (std::vector<GLuint>::const_iterator ite(p_index_set.begin());
254 ite != p_index_set.end();
257 p_ushort_index_set.push_back(*ite);
260 setInternalData(GL_TRIANGLES,
269 case GL_UNSIGNED_BYTE:
271 std::vector<GLubyte> p_ubyte_index_set;
273 for (std::vector<GLuint>::const_iterator ite(p_index_set.begin());
274 ite != p_index_set.end();
277 p_ubyte_index_set.push_back(*ite);
280 setInternalData(GL_TRIANGLES,
290 throw Exception(__FILE__, __FUNCTION__, __LINE__,
291 "Invalid data type.");
296 std::vector<T> p_vertex_set2;
297 std::vector<T> p_normal_set2;
300 for (std::vector<unsigned int>::const_iterator ite = p_index_set.begin();
301 ite != p_index_set.end();
304 p_vertex_set2.push_back(p_vertex_set[*ite * 3 + 0]);
305 p_vertex_set2.push_back(p_vertex_set[*ite * 3 + 1]);
306 p_vertex_set2.push_back(p_vertex_set[*ite * 3 + 2]);
308 p_normal_set2.push_back(p_normal_set[3 * (normal_id / 3) + 0]);
309 p_normal_set2.push_back(p_normal_set[3 * (normal_id / 3) + 1]);
310 p_normal_set2.push_back(p_normal_set[3 * (normal_id / 3) + 2]);
313 setInternalData(GL_TRIANGLES,
CylinderMesh(unsigned int aNumberOfSectors=10, double aHeight=1.0 *cm, double aRadius=0.5 *cm, int anIndexDataType=0)
Default constructor.
void create(unsigned int aNumberOfSectors=10, double aHeight=1.0 *cm, double aRadius=0.5 *cm, int anIndexDataType=0)
Create a new sphere.
Exception is a class to handle exceptions.
PolygonMesh is a class to handle polygon (triangles) meshes.
Vec3< RATIONAL_NUMBER > VEC3
Type of data used to store 3D vectors.