Skip to content

Commit 3d0adb9

Browse files
committed
Merge branch 'rover' into 'master'
Support spheres in DebugDrawer See merge request OpenMW/openmw!5199
2 parents ab77928 + e1ad0de commit 3d0adb9

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

components/debug/debugdraw.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
219270
static 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+
378445
void Debug::DebugDrawer::addDrawCall(const DrawCall& draw)
379446
{
380447
mCustomDebugDrawer[getIndexBufferWriteFromFrame(mCurrentFrame)]->mShapesToDraw.push_back(draw);

components/debug/debugdraw.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace Debug
3939
{
4040
Cube,
4141
Cylinder,
42+
Sphere,
4243
WireCube,
4344
};
4445

@@ -64,6 +65,10 @@ namespace Debug
6465
{
6566
return { pos, dims, color, DrawShape::Cylinder };
6667
}
68+
static DrawCall sphere(osg::Vec3f pos, osg::Vec3 dims = osg::Vec3(50., 50., 50.), osg::Vec3 color = colorWhite)
69+
{
70+
return { pos, dims, color, DrawShape::Sphere };
71+
}
6772
};
6873

6974
class DebugCustomDraw : public osg::Drawable
@@ -79,6 +84,7 @@ namespace Debug
7984

8085
osg::ref_ptr<osg::Geometry> mCubeGeometry;
8186
osg::ref_ptr<osg::Geometry> mCylinderGeometry;
87+
osg::ref_ptr<osg::Geometry> mSphereGeometry;
8288
osg::ref_ptr<osg::Geometry> mWireCubeGeometry;
8389

8490
virtual void drawImplementation(osg::RenderInfo&) const override;
@@ -97,6 +103,7 @@ namespace Debug
97103
void drawCube(
98104
osg::Vec3f mPosition, osg::Vec3f mDims = osg::Vec3(50., 50., 50.), osg::Vec3f mColor = colorWhite);
99105
void drawCubeMinMax(osg::Vec3f min, osg::Vec3f max, osg::Vec3f mColor = colorWhite);
106+
void drawSphere(osg::Vec3f position, float radius = 25., osg::Vec3f color = colorWhite);
100107
void addDrawCall(const DrawCall& draw);
101108
void addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color = colorWhite);
102109

0 commit comments

Comments
 (0)