MCPcopy Create free account
hub / github.com/OpenMW/openmw / generateSphere

Function generateSphere

components/debug/debugdraw.cpp:219–268  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

217}
218
219static void generateSphere(osg::Geometry& geom, float radius, int segments, int rings)
220{
221 osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
222 osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
223 osg::ref_ptr<osg::DrawElementsUShort> indices = new osg::DrawElementsUShort(osg::DrawElementsUShort::TRIANGLES, 0);
224 int vertexCount = segments * (rings + 1);
225 vertices->reserve(vertexCount);
226 normals->reserve(vertexCount);
227 int indexCount = segments * rings * 2 * 3;
228 indices->reserve(indexCount);
229
230 for (int i = 0; i <= rings; ++i)
231 {
232 float phi = (float(i) / float(rings)) * osg::PIf;
233 for (int j = 0; j < segments; ++j)
234 {
235 float theta = (float(j) / float(segments)) * osg::PIf * 2.f;
236 auto pos = sphereCoordToCartesian(theta, phi, radius);
237 vertices->push_back(pos);
238 pos.normalize();
239 normals->push_back(pos);
240 }
241 }
242
243 for (int i = 0; i < rings; ++i)
244 {
245 int ringTopBase = i * segments;
246 int ringBottomBase = ringTopBase + segments;
247
248 for (int j = 0; j < segments; ++j)
249 {
250 auto v1 = static_cast<GLushort>(ringTopBase + j);
251 auto v2 = static_cast<GLushort>(ringTopBase + (j + 1) % segments);
252 auto v3 = static_cast<GLushort>(ringBottomBase + j);
253 auto v4 = static_cast<GLushort>(ringBottomBase + (j + 1) % segments);
254
255 indices->push_back(v1);
256 indices->push_back(v4);
257 indices->push_back(v2);
258
259 indices->push_back(v4);
260 indices->push_back(v1);
261 indices->push_back(v3);
262 }
263 }
264
265 geom.setVertexArray(vertices);
266 geom.setNormalArray(normals, osg::Array::BIND_PER_VERTEX);
267 geom.addPrimitiveSet(indices);
268}
269
270static int getIndexBufferReadFromFrame(const unsigned int& nFrame)
271{

Callers 1

DebugDrawerMethod · 0.85

Calls 2

sphereCoordToCartesianFunction · 0.85
reserveMethod · 0.45

Tested by

no test coverage detected