| 1 | #ifndef NORMALS_DEF |
|---|
| 2 | #define NORMALS_DEF |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | #ifdef DEBUG |
|---|
| 6 | #include <iostream> |
|---|
| 7 | #endif |
|---|
| 8 | |
|---|
| 9 | #include <stack> |
|---|
| 10 | |
|---|
| 11 | #include <osg/Geode> |
|---|
| 12 | #include <osg/Geometry> |
|---|
| 13 | #include <osg/NodeVisitor> |
|---|
| 14 | #include <osg/MatrixTransform> |
|---|
| 15 | |
|---|
| 16 | class Normals: public osg::Geode |
|---|
| 17 | { |
|---|
| 18 | public: |
|---|
| 19 | enum Mode { |
|---|
| 20 | SurfaceNormals, |
|---|
| 21 | VertexNormals |
|---|
| 22 | }; |
|---|
| 23 | |
|---|
| 24 | Normals( osg::Node *node, float scale=1.0, Mode mode=SurfaceNormals ); |
|---|
| 25 | |
|---|
| 26 | private: |
|---|
| 27 | |
|---|
| 28 | class MakeNormalsVisitor : public osg::NodeVisitor |
|---|
| 29 | { |
|---|
| 30 | public: |
|---|
| 31 | MakeNormalsVisitor(float normalScale, Mode mode); |
|---|
| 32 | |
|---|
| 33 | void setMode( Mode mode ) { _mode = mode; } |
|---|
| 34 | |
|---|
| 35 | virtual void apply(osg::MatrixTransform& tx); |
|---|
| 36 | |
|---|
| 37 | virtual void apply( osg::Geode &geode ); |
|---|
| 38 | |
|---|
| 39 | osg::Vec3Array *getCoords() { return _local_coords.get(); } |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | private: |
|---|
| 43 | osg::ref_ptr<osg::Vec3Array> _local_coords; |
|---|
| 44 | float _normal_scale; |
|---|
| 45 | Mode _mode; |
|---|
| 46 | osg::Matrix _mat; |
|---|
| 47 | std::stack<osg::Matrix> _matStack; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | void _processPrimitive( unsigned int nv, |
|---|
| 51 | osg::Vec3Array::iterator coords, |
|---|
| 52 | osg::Vec3Array::iterator normals, |
|---|
| 53 | osg::Geometry::AttributeBinding binding ); |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | #ifdef DEBUG |
|---|
| 57 | static void _printPrimitiveType( osg::PrimitiveSet *pset ); |
|---|
| 58 | #endif |
|---|
| 59 | |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | class SurfaceNormals: public Normals |
|---|
| 64 | { |
|---|
| 65 | public: |
|---|
| 66 | SurfaceNormals( osg::Node *node, float scale=1.0 ): |
|---|
| 67 | Normals( node, scale, Normals::SurfaceNormals ) {} |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | class VertexNormals: public Normals |
|---|
| 71 | { |
|---|
| 72 | public: |
|---|
| 73 | VertexNormals( osg::Node *node, float scale=1.0 ): |
|---|
| 74 | Normals( node, scale, Normals::VertexNormals ) {} |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | #endif |
|---|