@@ -216,6 +216,57 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in
216216 geom.addPrimitiveSet (indices);
217217}
218218
219+ static 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+
219270static int getIndexBufferReadFromFrame (const unsigned int & nFrame)
220271{
221272 return nFrame % 2 ;
@@ -255,6 +306,7 @@ namespace Debug
255306 , mLinesToDraw (copy.mLinesToDraw )
256307 , mCubeGeometry (copy.mCubeGeometry )
257308 , mCylinderGeometry (copy.mCylinderGeometry )
309+ , mSphereGeometry (copy.mSphereGeometry )
258310 , mWireCubeGeometry (copy.mWireCubeGeometry )
259311 {
260312 }
@@ -294,6 +346,9 @@ namespace Debug
294346 case DrawShape::Cylinder:
295347 geometry = mCylinderGeometry ;
296348 break ;
349+ case DrawShape::Sphere:
350+ geometry = mSphereGeometry ;
351+ break ;
297352 case DrawShape::WireCube:
298353 geometry = mWireCubeGeometry ;
299354 break ;
@@ -341,6 +396,11 @@ Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager)
341396 cylinderGeom->setUseVertexBufferObjects (true );
342397 generateCylinder (*cylinderGeom, .5 , 1 ., 20 );
343398
399+ auto sphereGeom = new osg::Geometry;
400+ sphereGeom->setSupportsDisplayList (false );
401+ sphereGeom->setUseVertexBufferObjects (true );
402+ generateSphere (*sphereGeom, .5 , 20 , 10 );
403+
344404 auto wireCube = new osg::Geometry;
345405 wireCube->setSupportsDisplayList (false );
346406 wireCube->setUseVertexBufferObjects (true );
@@ -352,6 +412,7 @@ Debug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager)
352412 mCustomDebugDrawer [i]->setStateSet (stateset);
353413 mCustomDebugDrawer [i]->mWireCubeGeometry = wireCube;
354414 mCustomDebugDrawer [i]->mCubeGeometry = cubeGeometry;
415+ mCustomDebugDrawer [i]->mSphereGeometry = sphereGeom;
355416 mCustomDebugDrawer [i]->mCylinderGeometry = cylinderGeom;
356417 }
357418}
@@ -375,6 +436,12 @@ void Debug::DebugDrawer::drawCubeMinMax(osg::Vec3f min, osg::Vec3f max, osg::Vec
375436 drawCube (pos, dims, color);
376437}
377438
439+ void Debug::DebugDrawer::drawSphere (osg::Vec3f position, float radius, osg::Vec3f color)
440+ {
441+ mCustomDebugDrawer [getIndexBufferWriteFromFrame (mCurrentFrame )]->mShapesToDraw .push_back (
442+ { position, osg::Vec3f (radius, radius, radius) * 2 , color, DrawShape::Sphere });
443+ }
444+
378445void Debug::DebugDrawer::addDrawCall (const DrawCall& draw)
379446{
380447 mCustomDebugDrawer [getIndexBufferWriteFromFrame (mCurrentFrame )]->mShapesToDraw .push_back (draw);
0 commit comments