Shape 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_SHAPE
  14. #define OSG_SHAPE 1
  15. #include <osg/Object>
  16. #include <osg/Vec3>
  17. #include <osg/Quat>
  18. #include <osg/Plane>
  19. #include <osg/Array>
  20. namespace osg {
  21. // forward declare visitors.
  22. class ShapeVisitor;
  23. class ConstShapeVisitor;
  24. /** META_StateAttribute macro define the standard clone, isSameKindAs,
  25. * className and getType methods.
  26. * Use when subclassing from Object to make it more convenient to define
  27. * the standard pure virtual methods which are required for all Object
  28. * subclasses.*/
  29. #define META_Shape(library,name) \
  30. virtual Object* cloneType() const { return new name(); } \
  31. virtual Object* clone(const CopyOp& copyop) const { return new name (*this,copyop); } \
  32. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const name *>(obj)!=NULL; } \
  33. virtual const char* libraryName() const { return #library; } \
  34. virtual const char* className() const { return #name; } \
  35. virtual void accept(ShapeVisitor& sv) { sv.apply(*this); } \
  36. virtual void accept(ConstShapeVisitor& csv) const { csv.apply(*this); }
  37. /** Base class for all shape types.
  38. * Shapes are used to either for culling and collision detection or
  39. * to define the geometric shape of procedurally generate Geometry.
  40. */
  41. class OSG_EXPORT Shape : public Object
  42. {
  43. public:
  44. Shape() {}
  45. Shape(const Shape& sa,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  46. Object(sa,copyop) {}
  47. /** Clone the type of an attribute, with Object* return type.
  48. Must be defined by derived classes.*/
  49. virtual Object* cloneType() const = 0;
  50. /** Clone an attribute, with Object* return type.
  51. Must be defined by derived classes.*/
  52. virtual Object* clone(const CopyOp&) const = 0;
  53. /** return true if this and obj are of the same kind of object.*/
  54. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Shape*>(obj)!=NULL; }
  55. /** return the name of the attribute's library.*/
  56. virtual const char* libraryName() const { return "osg"; }
  57. /** return the name of the attribute's class type.*/
  58. virtual const char* className() const { return "Shape"; }
  59. /** accept a non const shape visitor which can be used on non const shape objects.
  60. Must be defined by derived classes.*/
  61. virtual void accept(ShapeVisitor&)=0;
  62. /** accept a const shape visitor which can be used on const shape objects.
  63. Must be defined by derived classes.*/
  64. virtual void accept(ConstShapeVisitor&) const =0;
  65. protected:
  66. virtual ~Shape();
  67. };
  68. // forward declarations of Shape types.
  69. class Sphere;
  70. class Box;
  71. class Cone;
  72. class Cylinder;
  73. class Capsule;
  74. class InfinitePlane;
  75. class TriangleMesh;
  76. class ConvexHull;
  77. class HeightField;
  78. class CompositeShape;
  79. class OSG_EXPORT ShapeVisitor
  80. {
  81. public:
  82. ShapeVisitor() {}
  83. virtual ~ShapeVisitor();
  84. virtual void apply(Shape&) {}
  85. virtual void apply(Sphere&) {}
  86. virtual void apply(Box&) {}
  87. virtual void apply(Cone&) {}
  88. virtual void apply(Cylinder&) {}
  89. virtual void apply(Capsule&) {}
  90. virtual void apply(InfinitePlane&) {}
  91. virtual void apply(TriangleMesh&) {}
  92. virtual void apply(ConvexHull&) {}
  93. virtual void apply(HeightField&) {}
  94. virtual void apply(CompositeShape&) {}
  95. };
  96. class OSG_EXPORT ConstShapeVisitor
  97. {
  98. public:
  99. ConstShapeVisitor() {}
  100. virtual ~ConstShapeVisitor();
  101. virtual void apply(const Shape&) {}
  102. virtual void apply(const Sphere&) {}
  103. virtual void apply(const Box&) {}
  104. virtual void apply(const Cone&) {}
  105. virtual void apply(const Cylinder&) {}
  106. virtual void apply(const Capsule&) {}
  107. virtual void apply(const InfinitePlane&) {}
  108. virtual void apply(const TriangleMesh&) {}
  109. virtual void apply(const ConvexHull&) {}
  110. virtual void apply(const HeightField&) {}
  111. virtual void apply(const CompositeShape&) {}
  112. };
  113. class OSG_EXPORT Sphere : public Shape
  114. {
  115. public:
  116. Sphere():
  117. _center(0.0f,0.0f,0.0f),
  118. _radius(1.0f) {}
  119. Sphere(const Vec3& center,float radius):
  120. _center(center),
  121. _radius(radius) {}
  122. Sphere(const Sphere& sphere,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  123. Shape(sphere,copyop),
  124. _center(sphere._center),
  125. _radius(sphere._radius) {}
  126. META_Shape(osg, Sphere);
  127. inline bool valid() const { return _radius>=0.0f; }
  128. inline void set(const Vec3& center,float radius)
  129. {
  130. _center = center;
  131. _radius = radius;
  132. }
  133. inline void setCenter(const Vec3& center) { _center = center; }
  134. inline const Vec3& getCenter() const { return _center; }
  135. inline void setRadius(float radius) { _radius = radius; }
  136. inline float getRadius() const { return _radius; }
  137. protected:
  138. virtual ~Sphere();
  139. Vec3 _center;
  140. float _radius;
  141. };
  142. class OSG_EXPORT Box : public Shape
  143. {
  144. public:
  145. Box():
  146. _center(0.0f,0.0f,0.0f),
  147. _halfLengths(0.5f,0.5f,0.5f) {}
  148. Box(const Vec3& center,float width):
  149. _center(center),
  150. _halfLengths(width*0.5f,width*0.5f,width*0.5f) {}
  151. Box(const Vec3& center,float lengthX,float lengthY, float lengthZ):
  152. _center(center),
  153. _halfLengths(lengthX*0.5f,lengthY*0.5f,lengthZ*0.5f) {}
  154. Box(const Box& box,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  155. Shape(box,copyop),
  156. _center(box._center),
  157. _halfLengths(box._halfLengths),
  158. _rotation(box._rotation) {}
  159. META_Shape(osg, Box);
  160. inline bool valid() const { return _halfLengths.x()>=0.0f; }
  161. inline void set(const Vec3& center,const Vec3& halfLengths)
  162. {
  163. _center = center;
  164. _halfLengths = halfLengths;
  165. }
  166. inline void setCenter(const Vec3& center) { _center = center; }
  167. inline const Vec3& getCenter() const { return _center; }
  168. inline void setHalfLengths(const Vec3& halfLengths) { _halfLengths = halfLengths; }
  169. inline const Vec3& getHalfLengths() const { return _halfLengths; }
  170. inline void setRotation(const Quat& quat) { _rotation = quat; }
  171. inline const Quat& getRotation() const { return _rotation; }
  172. inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
  173. inline bool zeroRotation() const { return _rotation.zeroRotation(); }
  174. protected:
  175. virtual ~Box();
  176. Vec3 _center;
  177. Vec3 _halfLengths;
  178. Quat _rotation;
  179. };
  180. class OSG_EXPORT Cone : public Shape
  181. {
  182. public:
  183. Cone():
  184. _center(0.0f,0.0f,0.0f),
  185. _radius(1.0f),
  186. _height(1.0f) {}
  187. Cone(const Vec3& center,float radius,float height):
  188. _center(center),
  189. _radius(radius),
  190. _height(height) {}
  191. Cone(const Cone& cone,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  192. Shape(cone,copyop),
  193. _center(cone._center),
  194. _radius(cone._radius),
  195. _height(cone._height),
  196. _rotation(cone._rotation) {}
  197. META_Shape(osg, Cone);
  198. inline bool valid() const { return _radius>=0.0f; }
  199. inline void set(const Vec3& center,float radius, float height)
  200. {
  201. _center = center;
  202. _radius = radius;
  203. _height = height;
  204. }
  205. inline void setCenter(const Vec3& center) { _center = center; }
  206. inline const Vec3& getCenter() const { return _center; }
  207. inline void setRadius(float radius) { _radius = radius; }
  208. inline float getRadius() const { return _radius; }
  209. inline void setHeight(float height) { _height = height; }
  210. inline float getHeight() const { return _height; }
  211. inline void setRotation(const Quat& quat) { _rotation = quat; }
  212. inline const Quat& getRotation() const { return _rotation; }
  213. inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
  214. inline bool zeroRotation() const { return _rotation.zeroRotation(); }
  215. inline float getBaseOffsetFactor() const { return 0.25f; }
  216. inline float getBaseOffset() const { return -getBaseOffsetFactor()*getHeight(); }
  217. protected:
  218. virtual ~Cone();
  219. Vec3 _center;
  220. float _radius;
  221. float _height;
  222. Quat _rotation;
  223. };
  224. class OSG_EXPORT Cylinder : public Shape
  225. {
  226. public:
  227. Cylinder():
  228. _center(0.0f,0.0f,0.0f),
  229. _radius(1.0f),
  230. _height(1.0f) {}
  231. Cylinder(const Vec3& center,float radius,float height):
  232. _center(center),
  233. _radius(radius),
  234. _height(height) {}
  235. Cylinder(const Cylinder& cylinder,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  236. Shape(cylinder,copyop),
  237. _center(cylinder._center),
  238. _radius(cylinder._radius),
  239. _height(cylinder._height),
  240. _rotation(cylinder._rotation) {}
  241. META_Shape(osg, Cylinder);
  242. inline bool valid() const { return _radius>=0.0f; }
  243. inline void set(const Vec3& center,float radius, float height)
  244. {
  245. _center = center;
  246. _radius = radius;
  247. _height = height;
  248. }
  249. inline void setCenter(const Vec3& center) { _center = center; }
  250. inline const Vec3& getCenter() const { return _center; }
  251. inline void setRadius(float radius) { _radius = radius; }
  252. inline float getRadius() const { return _radius; }
  253. inline void setHeight(float height) { _height = height; }
  254. inline float getHeight() const { return _height; }
  255. inline void setRotation(const Quat& quat) { _rotation = quat; }
  256. inline const Quat& getRotation() const { return _rotation; }
  257. inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
  258. bool zeroRotation() const { return _rotation.zeroRotation(); }
  259. protected:
  260. virtual ~Cylinder();
  261. Vec3 _center;
  262. float _radius;
  263. float _height;
  264. Quat _rotation;
  265. };
  266. class OSG_EXPORT Capsule : public Shape
  267. {
  268. public:
  269. Capsule():
  270. _center(0.0f,0.0f,0.0f),
  271. _radius(1.0f),
  272. _height(1.0f) {}
  273. Capsule(const Vec3& center,float radius,float height):
  274. _center(center),
  275. _radius(radius),
  276. _height(height) {}
  277. Capsule(const Capsule& capsule,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  278. Shape(capsule,copyop),
  279. _center(capsule._center),
  280. _radius(capsule._radius),
  281. _height(capsule._height),
  282. _rotation(capsule._rotation) {}
  283. META_Shape(osg, Capsule);
  284. inline bool valid() const { return _radius>=0.0f; }
  285. inline void set(const Vec3& center,float radius, float height)
  286. {
  287. _center = center;
  288. _radius = radius;
  289. _height = height;
  290. }
  291. inline void setCenter(const Vec3& center) { _center = center; }
  292. inline const Vec3& getCenter() const { return _center; }
  293. inline void setRadius(float radius) { _radius = radius; }
  294. inline float getRadius() const { return _radius; }
  295. inline void setHeight(float height) { _height = height; }
  296. inline float getHeight() const { return _height; }
  297. inline void setRotation(const Quat& quat) { _rotation = quat; }
  298. inline const Quat& getRotation() const { return _rotation; }
  299. inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
  300. bool zeroRotation() const { return _rotation.zeroRotation(); }
  301. protected:
  302. virtual ~Capsule();
  303. Vec3 _center;
  304. float _radius;
  305. float _height;
  306. Quat _rotation;
  307. };
  308. /** Deprecated. */
  309. class OSG_EXPORT InfinitePlane : public Shape, public Plane
  310. {
  311. public:
  312. InfinitePlane() {}
  313. InfinitePlane(const InfinitePlane& plane,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  314. Shape(plane,copyop),
  315. Plane(plane) {}
  316. META_Shape(osg, InfinitePlane);
  317. protected:
  318. virtual ~InfinitePlane();
  319. };
  320. /** Deprecated. */
  321. class OSG_EXPORT TriangleMesh : public Shape
  322. {
  323. public:
  324. TriangleMesh() {}
  325. TriangleMesh(const TriangleMesh& mesh,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  326. Shape(mesh,copyop),
  327. _vertices(mesh._vertices),
  328. _indices(mesh._indices) {}
  329. META_Shape(osg, TriangleMesh);
  330. void setVertices(Vec3Array* vertices) { _vertices = vertices; }
  331. Vec3Array* getVertices() { return _vertices.get(); }
  332. const Vec3Array* getVertices() const { return _vertices.get(); }
  333. void setIndices(IndexArray* indices) { _indices = indices; }
  334. IndexArray* getIndices() { return _indices.get(); }
  335. const IndexArray* getIndices() const { return _indices.get(); }
  336. protected:
  337. virtual ~TriangleMesh();
  338. ref_ptr<Vec3Array> _vertices;
  339. ref_ptr<IndexArray> _indices;
  340. };
  341. /** Deprecated. */
  342. class OSG_EXPORT ConvexHull : public TriangleMesh
  343. {
  344. public:
  345. ConvexHull() {}
  346. ConvexHull(const ConvexHull& hull,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  347. TriangleMesh(hull,copyop) {}
  348. META_Shape(osg, ConvexHull);
  349. protected:
  350. virtual ~ConvexHull();
  351. };
  352. class OSG_EXPORT HeightField : public Shape
  353. {
  354. public:
  355. HeightField();
  356. HeightField(const HeightField& mesh,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  357. META_Shape(osg, HeightField);
  358. typedef std::vector<float> HeightList;
  359. void allocate(unsigned int numColumns,unsigned int numRows);
  360. inline unsigned int getNumColumns() const { return _columns; }
  361. inline unsigned int getNumRows() const { return _rows; }
  362. inline void setOrigin(const Vec3& origin) { _origin = origin; }
  363. inline const Vec3& getOrigin() const { return _origin; }
  364. inline void setXInterval(float dx) { _dx = dx; }
  365. inline float getXInterval() const { return _dx; }
  366. inline void setYInterval(float dy) { _dy = dy; }
  367. inline float getYInterval() const { return _dy; }
  368. /** Get the FloatArray height data.*/
  369. FloatArray* getFloatArray() { return _heights.get(); }
  370. /** Get the const FloatArray height data.*/
  371. const FloatArray* getFloatArray() const { return _heights.get(); }
  372. HeightList& getHeightList() { return _heights->asVector(); }
  373. const HeightList& getHeightList() const { return _heights->asVector(); }
  374. /** Set the height of the skirt to render around the edge of HeightField.
  375. * The skirt is used as a means of disguising edge boundaries between adjacent HeightField,
  376. * particularly of ones with different resolutions.*/
  377. void setSkirtHeight(float skirtHeight) { _skirtHeight = skirtHeight; }
  378. /** Get the height of the skirt to render around the edge of HeightField.*/
  379. float getSkirtHeight() const { return _skirtHeight; }
  380. /** Set the width in number of cells in from the edge that the height field should be rendered from.
  381. * This exists to allow gradient and curvature continutity to be maintained between adjacent HeightField, where
  382. * the border cells will overlap adjacent HeightField.*/
  383. void setBorderWidth(unsigned int borderWidth) { _borderWidth = borderWidth; }
  384. /** Get the width in number of cells in from the edge that the height field should be rendered from.*/
  385. unsigned int getBorderWidth() const { return _borderWidth; }
  386. inline void setRotation(const Quat& quat) { _rotation = quat; }
  387. inline const Quat& getRotation() const { return _rotation; }
  388. inline Matrix computeRotationMatrix() const { return Matrix(_rotation); }
  389. inline bool zeroRotation() const { return _rotation.zeroRotation(); }
  390. /* set a single height point in the height field */
  391. inline void setHeight(unsigned int c,unsigned int r,float value)
  392. {
  393. (*_heights)[c+r*_columns] = value;
  394. }
  395. /* Get address of single height point in the height field, allows user to change. */
  396. inline float& getHeight(unsigned int c,unsigned int r)
  397. {
  398. return (*_heights)[c+r*_columns];
  399. }
  400. /* Get value of single height point in the height field, not editable. */
  401. inline float getHeight(unsigned int c,unsigned int r) const
  402. {
  403. return (*_heights)[c+r*_columns];
  404. }
  405. inline Vec3 getVertex(unsigned int c,unsigned int r) const
  406. {
  407. return Vec3(_origin.x()+getXInterval()*(float)c,
  408. _origin.y()+getYInterval()*(float)r,
  409. _origin.z()+(*_heights)[c+r*_columns]);
  410. }
  411. Vec3 getNormal(unsigned int c,unsigned int r) const;
  412. Vec2 getHeightDelta(unsigned int c,unsigned int r) const;
  413. protected:
  414. virtual ~HeightField();
  415. unsigned int _columns,_rows;
  416. Vec3 _origin; // _origin is the min value of the X and Y coordinates.
  417. float _dx;
  418. float _dy;
  419. float _skirtHeight;
  420. unsigned int _borderWidth;
  421. Quat _rotation;
  422. ref_ptr<FloatArray> _heights;
  423. };
  424. typedef HeightField Grid;
  425. /** Deprecated. */
  426. class OSG_EXPORT CompositeShape : public Shape
  427. {
  428. public:
  429. typedef std::vector< ref_ptr<Shape> > ChildList;
  430. CompositeShape() {}
  431. CompositeShape(const CompositeShape& cs,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  432. Shape(cs,copyop),
  433. _children(cs._children) {}
  434. META_Shape(osg, CompositeShape);
  435. /** Set the shape that encloses all of the children.*/
  436. void setShape(Shape* shape) { _shape = shape; }
  437. /** Get the shape that encloses all of the children.*/
  438. Shape* getShape() { return _shape.get(); }
  439. /** Get the const shape that encloses all of the children.*/
  440. const Shape* getShape() const { return _shape.get(); }
  441. /** Get the number of children of this composite shape.*/
  442. unsigned int getNumChildren() const { return static_cast<unsigned int>(_children.size()); }
  443. /** Get a child.*/
  444. Shape* getChild(unsigned int i) { return _children[i].get(); }
  445. /** Get a const child.*/
  446. const Shape* getChild(unsigned int i) const { return _children[i].get(); }
  447. /** Add a child to the list.*/
  448. void addChild(Shape* shape) { _children.push_back(shape); }
  449. template<class T> void addChild( const ref_ptr<T>& child ) { addChild(child.get()); }
  450. /** remove a child from the list.*/
  451. void removeChild(unsigned int i) { _children.erase(_children.begin()+i); }
  452. /** find the index number of child, if child is not found then it returns getNumChildren(),
  453. * so should be used in similar style to STL's result!=end().*/
  454. unsigned int findChildNo(Shape* shape) const
  455. {
  456. for (unsigned int childNo=0;childNo<_children.size();++childNo)
  457. {
  458. if (_children[childNo]==shape) return childNo;
  459. }
  460. return static_cast<unsigned int>(_children.size()); // node not found.
  461. }
  462. protected:
  463. virtual ~CompositeShape();
  464. ref_ptr<Shape> _shape;
  465. ChildList _children;
  466. };
  467. /** Describe several hints that can be passed to a Tessellator (like the one used
  468. * by \c ShapeDrawable) as a mean to try to influence the way it works.
  469. */
  470. class TessellationHints : public Object
  471. {
  472. public:
  473. TessellationHints():
  474. _TessellationMode(USE_SHAPE_DEFAULTS),
  475. _detailRatio(1.0f),
  476. _targetNumFaces(100),
  477. _createFrontFace(true),
  478. _createBackFace(false),
  479. _createNormals(true),
  480. _createTextureCoords(false),
  481. _createTop(true),
  482. _createBody(true),
  483. _createBottom(true) {}
  484. TessellationHints(const TessellationHints& tess, const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  485. Object(tess,copyop),
  486. _TessellationMode(tess._TessellationMode),
  487. _detailRatio(tess._detailRatio),
  488. _targetNumFaces(tess._targetNumFaces),
  489. _createFrontFace(tess._createFrontFace),
  490. _createBackFace(tess._createBackFace),
  491. _createNormals(tess._createNormals),
  492. _createTextureCoords(tess._createTextureCoords),
  493. _createTop(tess._createTop),
  494. _createBody(tess._createBody),
  495. _createBottom(tess._createBottom) {}
  496. META_Object(osg,TessellationHints);
  497. enum TessellationMode
  498. {
  499. USE_SHAPE_DEFAULTS,
  500. USE_TARGET_NUM_FACES
  501. };
  502. inline void setTessellationMode(TessellationMode mode) { _TessellationMode=mode; }
  503. inline TessellationMode getTessellationMode() const { return _TessellationMode; }
  504. inline void setDetailRatio(float ratio) { _detailRatio = ratio; }
  505. inline float getDetailRatio() const { return _detailRatio; }
  506. inline void setTargetNumFaces(unsigned int target) { _targetNumFaces=target; }
  507. inline unsigned int getTargetNumFaces() const { return _targetNumFaces; }
  508. inline void setCreateFrontFace(bool on) { _createFrontFace=on; }
  509. inline bool getCreateFrontFace() const { return _createFrontFace; }
  510. inline void setCreateBackFace(bool on) { _createBackFace=on; }
  511. inline bool getCreateBackFace() const { return _createBackFace; }
  512. inline void setCreateNormals(bool on) { _createNormals=on; }
  513. inline bool getCreateNormals() const { return _createNormals; }
  514. inline void setCreateTextureCoords(bool on) { _createTextureCoords=on; }
  515. inline bool getCreateTextureCoords() const { return _createTextureCoords; }
  516. inline void setCreateTop(bool on) { _createTop=on; }
  517. inline bool getCreateTop() const { return _createTop; }
  518. inline void setCreateBody(bool on) { _createBody=on; }
  519. inline bool getCreateBody() const { return _createBody; }
  520. inline void setCreateBottom(bool on) { _createBottom=on; }
  521. inline bool getCreateBottom() const { return _createBottom; }
  522. protected:
  523. ~TessellationHints() {}
  524. TessellationMode _TessellationMode;
  525. float _detailRatio;
  526. unsigned int _targetNumFaces;
  527. bool _createFrontFace;
  528. bool _createBackFace;
  529. bool _createNormals;
  530. bool _createTextureCoords;
  531. bool _createTop;
  532. bool _createBody;
  533. bool _createBottom;
  534. };
  535. // forward declare;
  536. class Geometry;
  537. /** Convenience class for populating an Geometry with vertex, normals, texture coords and primitives that can render a Shape. */
  538. class OSG_EXPORT BuildShapeGeometryVisitor : public ConstShapeVisitor
  539. {
  540. public:
  541. BuildShapeGeometryVisitor(Geometry* geometry, const TessellationHints* hints);
  542. virtual void apply(const Sphere&);
  543. virtual void apply(const Box&);
  544. virtual void apply(const Cone&);
  545. virtual void apply(const Cylinder&);
  546. virtual void apply(const Capsule&);
  547. virtual void apply(const InfinitePlane&);
  548. virtual void apply(const TriangleMesh&);
  549. virtual void apply(const ConvexHull&);
  550. virtual void apply(const HeightField&);
  551. virtual void apply(const CompositeShape&);
  552. void Normal(const Vec3f& v) { _normals->push_back(v); }
  553. void Normal3f(float x, float y, float z) { Normal(Vec3(x,y,z)); }
  554. void TexCoord(const Vec2f& tc) { _texcoords->push_back(tc); }
  555. void TexCoord2f(float x, float y) { TexCoord(Vec2(x,y)); }
  556. void Vertex(const Vec3f& v);
  557. void Vertex3f(float x, float y, float z) { Vertex(Vec3(x,y,z)); }
  558. void setMatrix(const Matrixd& m);
  559. void Begin(GLenum mode);
  560. void End();
  561. protected:
  562. BuildShapeGeometryVisitor& operator = (const BuildShapeGeometryVisitor&) { return *this; }
  563. enum SphereHalf { SphereTopHalf, SphereBottomHalf };
  564. // helpers for apply( Cylinder | Sphere | Capsule )
  565. void drawCylinderBody(unsigned int numSegments, float radius, float height);
  566. void drawHalfSphere(unsigned int numSegments, unsigned int numRows, float radius, SphereHalf which, float zOffset = 0.0f);
  567. Geometry* _geometry;
  568. const TessellationHints* _hints;
  569. ref_ptr<Vec3Array> _vertices;
  570. ref_ptr<Vec3Array> _normals;
  571. ref_ptr<Vec2Array> _texcoords;
  572. GLenum _mode;
  573. unsigned int _start_index;
  574. Matrixd _matrix;
  575. Matrixd _inverse;
  576. };
  577. extern OSG_EXPORT Geometry* convertShapeToGeometry(const Shape& shape, const TessellationHints* hints);
  578. extern OSG_EXPORT Geometry* convertShapeToGeometry(const Shape& shape, const TessellationHints* hints, const Vec4& color, Array::Binding colorBinding=Array::BIND_OVERALL);
  579. }
  580. #endif