From e5309127449ef27d992f2d3b7b04c82d11910e8f Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 2 Sep 2003 17:19:18 +0000 Subject: [PATCH] Made Matrix a typedef to Matrixf, and converted the old Matrix to Matrixf, as part of prep for supporting both Matrixf (float) and Matrixd (double). Added osg::Matrixf::glLoadMatrix() and osg::Matrixf::glMultiMatrix() methods and changed corresponding usage of glLoad/MultMatrixf() calls across to use these methods. Again prep for support Matrixd. Fixes for VisualStudio 6.0 compile. --- include/osg/Matrix | 95 ++-- include/osg/State | 4 +- include/osgUtil/CullVisitor | 8 +- src/osg/ColorMatrix.cpp | 2 +- src/osg/FragmentProgram.cpp | 2 +- src/osg/Matrix.cpp | 69 ++- src/osg/ShapeDrawable.cpp | 8 +- src/osg/TexMat.cpp | 4 +- src/osg/VertexProgram.cpp | 2 +- src/osgFX/BumpMapping.cpp | 842 +++++++++++++++-------------- src/osgFX/SpecularHighlights.cpp | 2 +- src/osgProducer/OsgCameraGroup.cpp | 6 +- src/osgSim/ScalarBar.cpp | 291 +++++----- src/osgSim/SphereSegment.cpp | 20 +- src/osgUtil/CullVisitor.cpp | 84 +-- 15 files changed, 753 insertions(+), 686 deletions(-) diff --git a/include/osg/Matrix b/include/osg/Matrix index f1df1a264..1cba17625 100644 --- a/include/osg/Matrix +++ b/include/osg/Matrix @@ -27,26 +27,35 @@ namespace osg { class Quat; -class SG_EXPORT Matrix +class SG_EXPORT Matrixf { public: - Matrix(); - Matrix( const Matrix& other); - explicit Matrix( float const * const def ); - Matrix( float a00, float a01, float a02, float a03, + Matrixf(); + + Matrixf( const Matrixf& other); + + explicit Matrixf( float const * const def ); + + explicit Matrixf(double const * const ptr ) + { + for(int i=0;i<16;++i) + ((float*)_mat)[i] = ptr[i]; + } + + Matrixf( float a00, float a01, float a02, float a03, float a10, float a11, float a12, float a13, float a20, float a21, float a22, float a23, float a30, float a31, float a32, float a33); - ~Matrix() {} + ~Matrixf() {} - int compare(const Matrix& m) const { return memcmp(_mat,m._mat,sizeof(_mat)); } + int compare(const Matrixf& m) const { return memcmp(_mat,m._mat,sizeof(_mat)); } - bool operator < (const Matrix& m) const { return compare(m)<0; } - bool operator == (const Matrix& m) const { return compare(m)==0; } - bool operator != (const Matrix& m) const { return compare(m)!=0; } + bool operator < (const Matrixf& m) const { return compare(m)<0; } + bool operator == (const Matrixf& m) const { return compare(m)==0; } + bool operator != (const Matrixf& m) const { return compare(m)!=0; } inline float& operator()(int row, int col) { return _mat[row][col]; } inline float operator()(int row, int col) const { return _mat[row][col]; } @@ -59,14 +68,14 @@ class SG_EXPORT Matrix - inline Matrix& operator = (const Matrix& other) + inline Matrixf& operator = (const Matrixf& other) { if( &other == this ) return *this; std::copy((float*)other._mat,(float*)other._mat+16,(float*)(_mat)); return *this; } - inline void set(const Matrix& other) + inline void set(const Matrixf& other) { std::copy((float*)other._mat,(float*)other._mat+16,(float*)(_mat)); } @@ -82,7 +91,7 @@ class SG_EXPORT Matrix float a30, float a31, float a32, float a33); float * ptr() { return (float *)_mat; } - float * ptr() const { return (float *)_mat; } + const float * ptr() const { return (const float *)_mat; } void makeIdentity(); @@ -143,44 +152,44 @@ class SG_EXPORT Matrix /** Get to the position and orientation of a modelview matrix, using the same convention as gluLookAt. */ void getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance=1.0f); - bool invert( const Matrix& ); + bool invert( const Matrixf& ); //basic utility functions to create new matrices - inline static Matrix identity( void ); - inline static Matrix scale( const Vec3& sv); - inline static Matrix scale( float sx, float sy, float sz); - inline static Matrix translate( const Vec3& dv); - inline static Matrix translate( float x, float y, float z); - inline static Matrix rotate( const Vec3& from, const Vec3& to); - inline static Matrix rotate( float angle, float x, float y, float z); - inline static Matrix rotate( float angle, const Vec3& axis); - inline static Matrix rotate( float angle1, const Vec3& axis1, + inline static Matrixf identity( void ); + inline static Matrixf scale( const Vec3& sv); + inline static Matrixf scale( float sx, float sy, float sz); + inline static Matrixf translate( const Vec3& dv); + inline static Matrixf translate( float x, float y, float z); + inline static Matrixf rotate( const Vec3& from, const Vec3& to); + inline static Matrixf rotate( float angle, float x, float y, float z); + inline static Matrixf rotate( float angle, const Vec3& axis); + inline static Matrixf rotate( float angle1, const Vec3& axis1, float angle2, const Vec3& axis2, float angle3, const Vec3& axis3); - inline static Matrix rotate( const Quat& quat); - inline static Matrix inverse( const Matrix& matrix); + inline static Matrixf rotate( const Quat& quat); + inline static Matrixf inverse( const Matrixf& matrix); /** Create a orthographic projection. See glOrtho for further details.*/ - inline static Matrix ortho(double left, double right, + inline static Matrixf ortho(double left, double right, double bottom, double top, double zNear, double zFar); /** Create a 2D orthographic projection. See glOrtho for further details.*/ - inline static Matrix ortho2D(double left, double right, + inline static Matrixf ortho2D(double left, double right, double bottom, double top); /** Create a perspective projection. See glFrustum for further details.*/ - inline static Matrix frustum(double left, double right, + inline static Matrixf frustum(double left, double right, double bottom, double top, double zNear, double zFar); /** Create a symmetrical perspective projection, See gluPerspective for further details. * Aspect ratio is defined as width/height.*/ - inline static Matrix perspective(double fovy,double aspectRatio, + inline static Matrixf perspective(double fovy,double aspectRatio, double zNear, double zFar); /** Create the position and orientation as per a camera, using the same convention as gluLookAt. */ - inline static Matrix lookAt(const Vec3& eye,const Vec3& center,const Vec3& up); + inline static Matrixf lookAt(const Vec3& eye,const Vec3& center,const Vec3& up); @@ -199,36 +208,44 @@ class SG_EXPORT Matrix inline Vec3 getScale() const { return Vec3(_mat[0][0],_mat[1][1],_mat[2][2]); } /** apply apply an 3x3 transform of v*M[0..2,0..2] */ - inline static Vec3 transform3x3(const Vec3& v,const Matrix& m); + inline static Vec3 transform3x3(const Vec3& v,const Matrixf& m); /** apply apply an 3x3 transform of M[0..2,0..2]*v */ - inline static Vec3 transform3x3(const Matrix& m,const Vec3& v); + inline static Vec3 transform3x3(const Matrixf& m,const Vec3& v); // basic Matrix multiplication, our workhorse methods. - void mult( const Matrix&, const Matrix& ); - void preMult( const Matrix& ); - void postMult( const Matrix& ); + void mult( const Matrixf&, const Matrixf& ); + void preMult( const Matrixf& ); + void postMult( const Matrixf& ); - inline void operator *= ( const Matrix& other ) + inline void operator *= ( const Matrixf& other ) { if( this == &other ) { - Matrix temp(other); + Matrixf temp(other); postMult( temp ); } else postMult( other ); } - inline Matrix operator * ( const Matrix &m ) const + inline Matrixf operator * ( const Matrixf &m ) const { - osg::Matrix r; + osg::Matrixf r; r.mult(*this,m); return r; } + + /** call glLoadMatixf with this matrix.*/ + void glLoadMatrix() const; + + /** call glMultMatixf with this matrix.*/ + void glMultMatrix() const; protected: float _mat[4][4]; }; +typedef Matrixf Matrix; + class RefMatrix : public Object, public Matrix { public: diff --git a/include/osg/State b/include/osg/State index e07543739..0abab3253 100644 --- a/include/osg/State +++ b/include/osg/State @@ -100,7 +100,7 @@ class SG_EXPORT State : public Referenced if (matrix) { _projection=matrix; - glLoadMatrixf(matrix->ptr()); + matrix->glLoadMatrix(); } else { @@ -123,7 +123,7 @@ class SG_EXPORT State : public Referenced if (matrix) { _modelView=matrix; - glLoadMatrixf(matrix->ptr()); + matrix->glLoadMatrix(); } else { diff --git a/include/osgUtil/CullVisitor b/include/osgUtil/CullVisitor index 1066aea7a..0fb4b9b78 100644 --- a/include/osgUtil/CullVisitor +++ b/include/osgUtil/CullVisitor @@ -215,6 +215,7 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor, public osg::CullStac osg::State* getState() { return _state.get(); } const osg::State* getState() const { return _state.get(); } + typedef double NearFarReal; protected: @@ -251,9 +252,10 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor, public osg::CullStac RenderBin* _currentRenderBin; ComputeNearFarMode _computeNearFar; - float _nearFarRatio; - float _computed_znear; - float _computed_zfar; + + NearFarReal _nearFarRatio; + NearFarReal _computed_znear; + NearFarReal _computed_zfar; osg::ref_ptr _clearNode; diff --git a/src/osg/ColorMatrix.cpp b/src/osg/ColorMatrix.cpp index 8e64b0119..f4c9741f9 100644 --- a/src/osg/ColorMatrix.cpp +++ b/src/osg/ColorMatrix.cpp @@ -32,7 +32,7 @@ void ColorMatrix::apply(State&) const if (s_ARB_imaging) { glMatrixMode( GL_COLOR ); - glLoadMatrixf( _matrix.ptr() ); + _matrix.glLoadMatrix(); glMatrixMode( GL_MODELVIEW ); } } diff --git a/src/osg/FragmentProgram.cpp b/src/osg/FragmentProgram.cpp index 371a2bcaa..82c384dca 100644 --- a/src/osg/FragmentProgram.cpp +++ b/src/osg/FragmentProgram.cpp @@ -171,7 +171,7 @@ void FragmentProgram::apply(State& state) const ++itr) { glMatrixMode((*itr).first); - glLoadMatrixf((*itr).second.ptr()); + (*itr).second.glLoadMatrix(); } glMatrixMode(GL_MODELVIEW); // restore matrix mode } diff --git a/src/osg/Matrix.cpp b/src/osg/Matrix.cpp index 079d75aa6..f2a8af5d2 100644 --- a/src/osg/Matrix.cpp +++ b/src/osg/Matrix.cpp @@ -15,6 +15,8 @@ #include #include +#include + #include using namespace osg; @@ -35,22 +37,22 @@ using namespace osg; -Matrix::Matrix() +Matrixf::Matrixf() { makeIdentity(); } -Matrix::Matrix( const Matrix& other) +Matrixf::Matrixf( const Matrixf& other) { set( (const float *) other._mat ); } -Matrix::Matrix( const float * const def ) +Matrixf::Matrixf( const float * const def ) { set( def ); } -Matrix::Matrix( float a00, float a01, float a02, float a03, +Matrixf::Matrixf( float a00, float a01, float a02, float a03, float a10, float a11, float a12, float a13, float a20, float a21, float a22, float a23, float a30, float a31, float a32, float a33) @@ -61,7 +63,7 @@ Matrix::Matrix( float a00, float a01, float a02, float a03, SET_ROW(3, a30, a31, a32, a33 ) } -void Matrix::set( float a00, float a01, float a02, float a03, +void Matrixf::set( float a00, float a01, float a02, float a03, float a10, float a11, float a12, float a13, float a20, float a21, float a22, float a23, float a30, float a31, float a32, float a33) @@ -72,7 +74,7 @@ void Matrix::set( float a00, float a01, float a02, float a03, SET_ROW(3, a30, a31, a32, a33 ) } -void Matrix::setTrans( float tx, float ty, float tz ) +void Matrixf::setTrans( float tx, float ty, float tz ) { _mat[3][0] = tx; _mat[3][1] = ty; @@ -80,14 +82,14 @@ void Matrix::setTrans( float tx, float ty, float tz ) } -void Matrix::setTrans( const Vec3& v ) +void Matrixf::setTrans( const Vec3& v ) { _mat[3][0] = v[0]; _mat[3][1] = v[1]; _mat[3][2] = v[2]; } -void Matrix::makeIdentity() +void Matrixf::makeIdentity() { SET_ROW(0, 1, 0, 0, 0 ) SET_ROW(1, 0, 1, 0, 0 ) @@ -95,12 +97,12 @@ void Matrix::makeIdentity() SET_ROW(3, 0, 0, 0, 1 ) } -void Matrix::makeScale( const Vec3& v ) +void Matrixf::makeScale( const Vec3& v ) { makeScale(v[0], v[1], v[2] ); } -void Matrix::makeScale( float x, float y, float z ) +void Matrixf::makeScale( float x, float y, float z ) { SET_ROW(0, x, 0, 0, 0 ) SET_ROW(1, 0, y, 0, 0 ) @@ -108,12 +110,12 @@ void Matrix::makeScale( float x, float y, float z ) SET_ROW(3, 0, 0, 0, 1 ) } -void Matrix::makeTranslate( const Vec3& v ) +void Matrixf::makeTranslate( const Vec3& v ) { makeTranslate( v[0], v[1], v[2] ); } -void Matrix::makeTranslate( float x, float y, float z ) +void Matrixf::makeTranslate( float x, float y, float z ) { SET_ROW(0, 1, 0, 0, 0 ) SET_ROW(1, 0, 1, 0, 0 ) @@ -121,33 +123,33 @@ void Matrix::makeTranslate( float x, float y, float z ) SET_ROW(3, x, y, z, 1 ) } -void Matrix::makeRotate( const Vec3& from, const Vec3& to ) +void Matrixf::makeRotate( const Vec3& from, const Vec3& to ) { Quat quat; quat.makeRotate(from,to); quat.get(*this); } -void Matrix::makeRotate( float angle, const Vec3& axis ) +void Matrixf::makeRotate( float angle, const Vec3& axis ) { Quat quat; quat.makeRotate( angle, axis); quat.get(*this); } -void Matrix::makeRotate( float angle, float x, float y, float z ) +void Matrixf::makeRotate( float angle, float x, float y, float z ) { Quat quat; quat.makeRotate( angle, x, y, z); quat.get(*this); } -void Matrix::makeRotate( const Quat& q ) +void Matrixf::makeRotate( const Quat& q ) { q.get(*this); } -void Matrix::makeRotate( float angle1, const Vec3& axis1, +void Matrixf::makeRotate( float angle1, const Vec3& axis1, float angle2, const Vec3& axis2, float angle3, const Vec3& axis3) { @@ -158,7 +160,7 @@ void Matrix::makeRotate( float angle1, const Vec3& axis1, quat.get(*this); } -void Matrix::mult( const Matrix& lhs, const Matrix& rhs ) +void Matrixf::mult( const Matrix& lhs, const Matrix& rhs ) { if (&lhs==this) { @@ -191,7 +193,7 @@ void Matrix::mult( const Matrix& lhs, const Matrix& rhs ) _mat[3][3] = INNER_PRODUCT(lhs, rhs, 3, 3); } -void Matrix::preMult( const Matrix& other ) +void Matrixf::preMult( const Matrix& other ) { // brute force method requiring a copy //Matrix tmp(other* *this); @@ -212,7 +214,7 @@ void Matrix::preMult( const Matrix& other ) } -void Matrix::postMult( const Matrix& other ) +void Matrixf::postMult( const Matrix& other ) { // brute force method requiring a copy //Matrix tmp(*this * other); @@ -243,7 +245,7 @@ inline T SGL_ABS(T a) #define SGL_SWAP(a,b,temp) ((temp)=(a),(a)=(b),(b)=(temp)) #endif -bool Matrix::invert( const Matrix& mat ) +bool Matrixf::invert( const Matrix& mat ) { if (&mat==this) { Matrix tm(mat); @@ -312,7 +314,7 @@ bool Matrix::invert( const Matrix& mat ) return true; } -void Matrix::makeOrtho(double left, double right, +void Matrixf::makeOrtho(double left, double right, double bottom, double top, double zNear, double zFar) { @@ -326,7 +328,7 @@ void Matrix::makeOrtho(double left, double right, SET_ROW(3, tx, ty, tz, 1.0f ) } -void Matrix::getOrtho(double& left, double& right, +void Matrixf::getOrtho(double& left, double& right, double& bottom, double& top, double& zNear, double& zFar) { @@ -341,7 +343,7 @@ void Matrix::getOrtho(double& left, double& right, } -void Matrix::makeFrustum(double left, double right, +void Matrixf::makeFrustum(double left, double right, double bottom, double top, double zNear, double zFar) { @@ -356,7 +358,7 @@ void Matrix::makeFrustum(double left, double right, SET_ROW(3, 0.0f, 0.0f, D, 0.0f ) } -void Matrix::getFrustum(double& left, double& right, +void Matrixf::getFrustum(double& left, double& right, double& bottom, double& top, double& zNear, double& zFar) { @@ -371,7 +373,7 @@ void Matrix::getFrustum(double& left, double& right, } -void Matrix::makePerspective(double fovy,double aspectRatio, +void Matrixf::makePerspective(double fovy,double aspectRatio, double zNear, double zFar) { // calculate the appropriate left, right etc. @@ -384,7 +386,7 @@ void Matrix::makePerspective(double fovy,double aspectRatio, } -void Matrix::makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up) +void Matrixf::makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up) { Vec3 f(center-eye); f.normalize(); @@ -399,10 +401,10 @@ void Matrix::makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up) s[2], u[2], -f[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); - preMult(Matrix::translate(-eye)); + preMult(Matrixf::translate(-eye)); } -void Matrix::getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance) +void Matrixf::getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance) { Matrix inv; inv.invert(*this); @@ -413,4 +415,13 @@ void Matrix::getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance) center = eye + center*lookDistance; } +void Matrixf::glLoadMatrix() const +{ + glLoadMatrixf((GLfloat*)_mat); +} + +void Matrixf::glMultMatrix() const +{ + glMultMatrixf((GLfloat*)_mat); +} #undef SET_ROW diff --git a/src/osg/ShapeDrawable.cpp b/src/osg/ShapeDrawable.cpp index ecfa4c76b..9f8211fb5 100644 --- a/src/osg/ShapeDrawable.cpp +++ b/src/osg/ShapeDrawable.cpp @@ -168,7 +168,7 @@ void DrawShapeVisitor::apply(const Box& box) if (!box.zeroRotation()) { Matrix rotation(box.getRotationMatrix()); - glMultMatrixf(rotation.ptr()); + rotation.glMultMatrix(); } glBegin(GL_QUADS); @@ -284,7 +284,7 @@ void DrawShapeVisitor::apply(const Cone& cone) if (!cone.zeroRotation()) { Matrix rotation(cone.getRotationMatrix()); - glMultMatrixf(rotation.ptr()); + rotation.glMultMatrix(); } // evaluate hints @@ -402,7 +402,7 @@ void DrawShapeVisitor::apply(const Cylinder& cylinder) if (!cylinder.zeroRotation()) { Matrix rotation(cylinder.getRotationMatrix()); - glMultMatrixf(rotation.ptr()); + rotation.glMultMatrix(); } // evaluate hints @@ -568,7 +568,7 @@ void DrawShapeVisitor::apply(const HeightField& field) if (!field.zeroRotation()) { Matrix rotation(field.getRotationMatrix()); - glMultMatrixf(rotation.ptr()); + rotation.glMultMatrix(); } float dx = field.getXInterval(); diff --git a/src/osg/TexMat.cpp b/src/osg/TexMat.cpp index 43ba649a6..1a480c8e4 100644 --- a/src/osg/TexMat.cpp +++ b/src/osg/TexMat.cpp @@ -27,6 +27,6 @@ TexMat::~TexMat() void TexMat::apply(State&) const { glMatrixMode( GL_TEXTURE ); - glLoadMatrixf( _matrix.ptr() ); - glMatrixMode( GL_MODELVIEW ); // fix! GWM Aug 2001 + _matrix.glLoadMatrix(); + glMatrixMode( GL_MODELVIEW ); } diff --git a/src/osg/VertexProgram.cpp b/src/osg/VertexProgram.cpp index f27d9b0ac..dc29f4a4c 100644 --- a/src/osg/VertexProgram.cpp +++ b/src/osg/VertexProgram.cpp @@ -171,7 +171,7 @@ void VertexProgram::apply(State& state) const ++itr) { glMatrixMode((*itr).first); - glLoadMatrixf((*itr).second.ptr()); + (*itr).second.glLoadMatrix(); } glMatrixMode(GL_MODELVIEW); // restore matrix mode } diff --git a/src/osgFX/BumpMapping.cpp b/src/osgFX/BumpMapping.cpp index 3ee56fb22..c06646311 100644 --- a/src/osgFX/BumpMapping.cpp +++ b/src/osgFX/BumpMapping.cpp @@ -22,116 +22,122 @@ using namespace osgFX; namespace { - // this is a visitor class that prepares all geometries in a subgraph - // by calling prepareGeometry() which in turn generates tangent-space - // basis vectors - class TsgVisitor: public osg::NodeVisitor { - public: - TsgVisitor(BumpMapping *bm): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), bm_(bm) {} - void apply(osg::Geode &geode) - { - for (unsigned i=0; i(geode.getDrawable(i)); - if (geo) { - bm_->prepareGeometry(geo); - } - } - osg::NodeVisitor::apply(geode); - } - private: - osg::ref_ptr bm_; - }; + using osg::NodeVisitor; + + // this is a visitor class that prepares all geometries in a subgraph + // by calling prepareGeometry() which in turn generates tangent-space + // basis vectors + class TsgVisitor: public NodeVisitor { + public: + TsgVisitor(BumpMapping *bm): NodeVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN), bm_(bm) {} + void apply(osg::Geode &geode) + { + for (unsigned i=0; i(geode.getDrawable(i)); + if (geo) { + bm_->prepareGeometry(geo); + } + } + NodeVisitor::apply(geode); + } + private: + osg::ref_ptr bm_; + }; - // this visitor generates texture coordinates for all geometries in a - // subgraph. It is used only for demo purposes. - class TexCoordGenerator: public osg::NodeVisitor { - public: - TexCoordGenerator(int du, int nu): osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), du_(du), nu_(nu) {} - void apply(osg::Geode &geode) - { - const osg::BoundingSphere &bsphere = geode.getBound(); - float scale = 10; - if (bsphere.radius() != 0) { - scale = 5 / bsphere.radius(); - } - for (unsigned i=0; i(geode.getDrawable(i)); - if (geo) { - osg::ref_ptr tc = generate_coords(geo->getVertexArray(), geo->getNormalArray(), scale); - geo->setTexCoordArray(du_, tc.get()); - geo->setTexCoordArray(nu_, tc.get()); - } - } - osg::NodeVisitor::apply(geode); - } + // this visitor generates texture coordinates for all geometries in a + // subgraph. It is used only for demo purposes. + class TexCoordGenerator: public osg::NodeVisitor { + public: + TexCoordGenerator(int du, int nu): NodeVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN), du_(du), nu_(nu) {} + void apply(osg::Geode &geode) + { + const osg::BoundingSphere &bsphere = geode.getBound(); + float scale = 10; + if (bsphere.radius() != 0) { + scale = 5 / bsphere.radius(); + } + for (unsigned i=0; i(geode.getDrawable(i)); + if (geo) { + osg::ref_ptr tc = generate_coords(geo->getVertexArray(), geo->getNormalArray(), scale); + geo->setTexCoordArray(du_, tc.get()); + geo->setTexCoordArray(nu_, tc.get()); + } + } + NodeVisitor::apply(geode); + } - protected: - osg::Vec2Array *generate_coords(osg::Array *vx, osg::Array *nx, float scale) - { - osg::Vec2Array *v2a = dynamic_cast(vx); - osg::Vec3Array *v3a = dynamic_cast(vx); - osg::Vec4Array *v4a = dynamic_cast(vx); - osg::Vec2Array *n2a = dynamic_cast(nx); - osg::Vec3Array *n3a = dynamic_cast(nx); - osg::Vec4Array *n4a = dynamic_cast(nx); + protected: + osg::Vec2Array *generate_coords(osg::Array *vx, osg::Array *nx, float scale) + { + osg::Vec2Array *v2a = dynamic_cast(vx); + osg::Vec3Array *v3a = dynamic_cast(vx); + osg::Vec4Array *v4a = dynamic_cast(vx); + osg::Vec2Array *n2a = dynamic_cast(nx); + osg::Vec3Array *n3a = dynamic_cast(nx); + osg::Vec4Array *n4a = dynamic_cast(nx); - osg::ref_ptr tc = new osg::Vec2Array; - for (unsigned i=0; igetNumElements(); ++i) { + osg::ref_ptr tc = new osg::Vec2Array; + for (unsigned i=0; igetNumElements(); ++i) { - osg::Vec3 P; - if (v2a) P.set(v2a->at(i).x(), v2a->at(i).y(), 0); - if (v3a) P.set(v3a->at(i).x(), v3a->at(i).y(), v3a->at(i).z()); - if (v4a) P.set(v4a->at(i).x(), v4a->at(i).y(), v4a->at(i).z()); + osg::Vec3 P; + if (v2a) P.set(v2a->at(i).x(), v2a->at(i).y(), 0); + if (v3a) P.set(v3a->at(i).x(), v3a->at(i).y(), v3a->at(i).z()); + if (v4a) P.set(v4a->at(i).x(), v4a->at(i).y(), v4a->at(i).z()); - osg::Vec3 N(0, 0, 1); - if (n2a) N.set(n2a->at(i).x(), n2a->at(i).y(), 0); - if (n3a) N.set(n3a->at(i).x(), n3a->at(i).y(), n3a->at(i).z()); - if (n4a) N.set(n4a->at(i).x(), n4a->at(i).y(), n4a->at(i).z()); + osg::Vec3 N(0, 0, 1); + if (n2a) N.set(n2a->at(i).x(), n2a->at(i).y(), 0); + if (n3a) N.set(n3a->at(i).x(), n3a->at(i).y(), n3a->at(i).z()); + if (n4a) N.set(n4a->at(i).x(), n4a->at(i).y(), n4a->at(i).z()); - int axis = 0; - if (N.y() > N.x() && N.y() > N.z()) axis = 1; - if (-N.y() > N.x() && -N.y() > N.z()) axis = 1; - if (N.z() > N.x() && N.z() > N.y()) axis = 2; - if (-N.z() > N.x() && -N.z() > N.y()) axis = 2; + int axis = 0; + if (N.y() > N.x() && N.y() > N.z()) axis = 1; + if (-N.y() > N.x() && -N.y() > N.z()) axis = 1; + if (N.z() > N.x() && N.z() > N.y()) axis = 2; + if (-N.z() > N.x() && -N.z() > N.y()) axis = 2; - osg::Vec2 uv; + osg::Vec2 uv; - switch (axis) { - case 0: uv.set(P.y(), P.z()); break; - case 1: uv.set(P.x(), P.z()); break; - case 2: uv.set(P.x(), P.y()); break; - default: ; - } + switch (axis) { + case 0: uv.set(P.y(), P.z()); break; + case 1: uv.set(P.x(), P.z()); break; + case 2: uv.set(P.x(), P.y()); break; + default: ; + } - tc->push_back(uv * scale); - } - return tc.take(); - } + tc->push_back(uv * scale); + } + return tc.take(); + } - private: - int du_; - int nu_; - }; + private: + int du_; + int nu_; + }; } namespace { - // a state attribute class that grabs the initial inverse view matrix - // and sends it to a VertexProgram. - // NOTE: due to lack of support for per-context parameters in VertexProgram, - // this class will send the matrix to the vp only while the first context - // is being rendered. All subsequent contexts will use the first context's - // matrix. + const unsigned int NO_VALID_CONTEXT = 0xffffffff; + + // a state attribute class that grabs the initial inverse view matrix + // and sends it to a VertexProgram. + // NOTE: due to lack of support for per-context parameters in VertexProgram, + // this class will send the matrix to the vp only while the first context + // is being rendered. All subsequent contexts will use the first context's + // matrix. class ViewMatrixExtractor: public osg::StateAttribute { public: + + ViewMatrixExtractor() : osg::StateAttribute(), vp_(0), param_(0), - first_context_(-1) + first_context_(NO_VALID_CONTEXT) { } @@ -139,7 +145,7 @@ namespace : osg::StateAttribute(copy, copyop), vp_(static_cast(copyop(copy.vp_.get()))), param_(copy.param_), - first_context_(-1) + first_context_(NO_VALID_CONTEXT) { } @@ -147,7 +153,7 @@ namespace : osg::StateAttribute(), vp_(vp), param_(param), - first_context_(-1) + first_context_(NO_VALID_CONTEXT) { } @@ -164,23 +170,23 @@ namespace void apply(osg::State &state) const { - if (first_context_ == -1) { - first_context_ = state.getContextID(); - } - if (state.getContextID() == first_context_) { - if (vp_.valid()) { - osg::Matrix M = state.getInitialInverseViewMatrix(); - for (int i=0; i<4; ++i) { - vp_->setProgramLocalParameter(param_+i, osg::Vec4(M(0, i), M(1, i), M(2, i), M(3, i))); - } - } - } + if (first_context_ == NO_VALID_CONTEXT) { + first_context_ = state.getContextID(); + } + if (state.getContextID() == first_context_) { + if (vp_.valid()) { + osg::Matrix M = state.getInitialInverseViewMatrix(); + for (int i=0; i<4; ++i) { + vp_->setProgramLocalParameter(param_+i, osg::Vec4(M(0, i), M(1, i), M(2, i), M(3, i))); + } + } + } } private: mutable osg::ref_ptr vp_; int param_; - mutable int first_context_; + mutable unsigned int first_context_; }; } @@ -188,7 +194,7 @@ namespace namespace { - // let's register this cool effect! :) + // let's register this cool effect! :) Registry::Proxy proxy(new BumpMapping); } @@ -196,168 +202,168 @@ namespace namespace { - // "Full ARB" technique uses ARB vertex program and fragment program. - // Handles ambient, diffuse and specular lighting transparently. A texture - // for the diffuse component is required as well as a normal map texture. + // "Full ARB" technique uses ARB vertex program and fragment program. + // Handles ambient, diffuse and specular lighting transparently. A texture + // for the diffuse component is required as well as a normal map texture. class FullArbTechnique: public Technique { public: - FullArbTechnique(int lightnum, int diffuseunit, int normalunit, osg::Texture2D *diffuse_tex, osg::Texture2D *normal_tex) + FullArbTechnique(int lightnum, int diffuseunit, int normalunit, osg::Texture2D *diffuse_tex, osg::Texture2D *normal_tex) : Technique(), lightnum_(lightnum), diffuseunit_(diffuseunit), - normalunit_(normalunit), - diffuse_tex_(diffuse_tex), - normal_tex_(normal_tex) + normalunit_(normalunit), + diffuse_tex_(diffuse_tex), + normal_tex_(normal_tex) { } - META_Technique( - "FullArbTechnique", - "Single-pass technique, requires ARB_vertex_program and ARB_fragment_program." - ); + META_Technique( + "FullArbTechnique", + "Single-pass technique, requires ARB_vertex_program and ARB_fragment_program." + ); void getRequiredExtensions(std::vector &extensions) const { extensions.push_back("GL_ARB_vertex_program"); - extensions.push_back("GL_ARB_fragment_program"); + extensions.push_back("GL_ARB_fragment_program"); } protected: void define_passes() { - // vertex program - std::ostringstream vp_oss; - vp_oss << - "!!ARBvp1.0\n" - "OPTION ARB_position_invariant;" - "PARAM c4 = { 0, 0, 0, 1 };" - "PARAM c5 = { 0.5, 4, 0, 0 };" - "TEMP R0, R1, R2, R3, R4, R5, R6, R7, R8;" - "ATTRIB v5 = vertex.attrib[15];" - "ATTRIB v4 = vertex.attrib[7];" - "ATTRIB v3 = vertex.attrib[6];" - "ATTRIB v25 = vertex.texcoord[1];" - "ATTRIB v24 = vertex.texcoord[0];" - "ATTRIB v18 = vertex.normal;" - "ATTRIB v16 = vertex.position;" - "PARAM s259[4] = { state.matrix.mvp };" - "PARAM s18 = state.light[0].position;" - "PARAM s77 = state.lightprod[0].specular;" - "PARAM s4 = state.material.shininess;" - "PARAM s75 = state.lightprod[0].ambient;" - "PARAM s223[4] = { state.matrix.modelview[0] };" - "PARAM c0[4] = { program.local[0..3] };" - " MOV result.texcoord[2].xyz, s75.xyzx;" - " MOV result.texcoord[2].w, s4.x;" - " MOV result.texcoord[0].zw, s77.xyxy;" - " MOV result.texcoord[0].xy, v24;" - " MOV result.texcoord[1].zw, s77.zwzw;" - " MOV result.texcoord[1].xy, v25;" - " MOV R5, c0[0];" - " MUL R0, R5.y, s223[1];" - " MAD R0, R5.x, s223[0], R0;" - " MAD R0, R5.z, s223[2], R0;" - " MAD R0, R5.w, s223[3], R0;" - " DP4 R1.x, R0, v16;" - " MOV R4, c0[1];" - " MUL R2, R4.y, s223[1];" - " MAD R2, R4.x, s223[0], R2;" - " MAD R2, R4.z, s223[2], R2;" - " MAD R7, R4.w, s223[3], R2;" - " DP4 R1.y, R7, v16;" - " MOV R3, c0[2];" - " MUL R2, R3.y, s223[1];" - " MAD R2, R3.x, s223[0], R2;" - " MAD R2, R3.z, s223[2], R2;" - " MAD R6, R3.w, s223[3], R2;" - " DP4 R1.z, R6, v16;" - " MOV R2, c0[3];" - " MUL R8, R2.y, s223[1];" - " MAD R8, R2.x, s223[0], R8;" - " MAD R8, R2.z, s223[2], R8;" - " MAD R8, R2.w, s223[3], R8;" - " MOV R8.x, R5.w;" - " MOV R8.y, R4.w;" - " MOV R8.z, R3.w;" - " ADD R1.yzw, R8.xxyz, -R1.xxyz;" - " DP3 R1.x, R1.yzwy, R1.yzwy;" - " RSQ R1.x, R1.x;" - " DP4 R5.x, R5, s18;" - " DP4 R5.y, R4, s18;" - " DP4 R5.z, R3, s18;" - " DP3 R2.x, R5.xyzx, R5.xyzx;" - " RSQ R2.x, R2.x;" - " MUL R5.xyz, R2.x, R5.xyzx;" - " MAD R1.yzw, R1.x, R1.yyzw, R5.xxyz;" - " DP3 R1.x, R1.yzwy, R1.yzwy;" - " RSQ R1.x, R1.x;" - " MUL R4.xyz, R1.x, R1.yzwy;" - " DP3 R3.x, R0.xyzx, v3.xyzx;" - " DP3 R3.y, R7.xyzx, v3.xyzx;" - " DP3 R3.z, R6.xyzx, v3.xyzx;" - " DP3 R8.x, R3.xyzx, R4.xyzx;" - " DP3 R2.x, R0.xyzx, v4.xyzx;" - " DP3 R2.y, R7.xyzx, v4.xyzx;" - " DP3 R2.z, R6.xyzx, v4.xyzx;" - " DP3 R8.y, R2.xyzx, R4.xyzx;" - " DP3 R1.x, R0.xyzx, v5.xyzx;" - " DP3 R1.y, R7.xyzx, v5.xyzx;" - " DP3 R1.z, R6.xyzx, v5.xyzx;" - " DP3 R8.z, R1.xyzx, R4.xyzx;" - " MAD result.color.front.secondary.xyz, c5.x, R8.xyzx, c5.x;" - " DP3 R0.y, R0.xyzx, v18.xyzx;" - " DP3 R0.z, R7.xyzx, v18.xyzx;" - " DP3 R0.w, R6.xyzx, v18.xyzx;" - " DP3 R0.x, R0.yzwy, R0.yzwy;" - " RSQ R0.x, R0.x;" - " MUL R6.xyz, R0.x, R0.yzwy;" - " DP3 R0.x, R6.xyzx, R4.xyzx;" - " MUL result.color.front.secondary.w, c5.y, R0.x;" - " DP3 R0.x, R3.xyzx, R5.xyzx;" - " DP3 R0.y, R2.xyzx, R5.xyzx;" - " DP3 R0.z, R1.xyzx, R5.xyzx;" - " MAD result.color.front.primary.xyz, c5.x, R0.xyzx, c5.x;" - " DP3 R0.x, R6.xyzx, R5.xyzx;" - " MUL result.color.front.primary.w, c5.y, R0.x;" - "END\n"; + // vertex program + std::ostringstream vp_oss; + vp_oss << + "!!ARBvp1.0\n" + "OPTION ARB_position_invariant;" + "PARAM c4 = { 0, 0, 0, 1 };" + "PARAM c5 = { 0.5, 4, 0, 0 };" + "TEMP R0, R1, R2, R3, R4, R5, R6, R7, R8;" + "ATTRIB v5 = vertex.attrib[15];" + "ATTRIB v4 = vertex.attrib[7];" + "ATTRIB v3 = vertex.attrib[6];" + "ATTRIB v25 = vertex.texcoord[1];" + "ATTRIB v24 = vertex.texcoord[0];" + "ATTRIB v18 = vertex.normal;" + "ATTRIB v16 = vertex.position;" + "PARAM s259[4] = { state.matrix.mvp };" + "PARAM s18 = state.light[0].position;" + "PARAM s77 = state.lightprod[0].specular;" + "PARAM s4 = state.material.shininess;" + "PARAM s75 = state.lightprod[0].ambient;" + "PARAM s223[4] = { state.matrix.modelview[0] };" + "PARAM c0[4] = { program.local[0..3] };" + " MOV result.texcoord[2].xyz, s75.xyzx;" + " MOV result.texcoord[2].w, s4.x;" + " MOV result.texcoord[0].zw, s77.xyxy;" + " MOV result.texcoord[0].xy, v24;" + " MOV result.texcoord[1].zw, s77.zwzw;" + " MOV result.texcoord[1].xy, v25;" + " MOV R5, c0[0];" + " MUL R0, R5.y, s223[1];" + " MAD R0, R5.x, s223[0], R0;" + " MAD R0, R5.z, s223[2], R0;" + " MAD R0, R5.w, s223[3], R0;" + " DP4 R1.x, R0, v16;" + " MOV R4, c0[1];" + " MUL R2, R4.y, s223[1];" + " MAD R2, R4.x, s223[0], R2;" + " MAD R2, R4.z, s223[2], R2;" + " MAD R7, R4.w, s223[3], R2;" + " DP4 R1.y, R7, v16;" + " MOV R3, c0[2];" + " MUL R2, R3.y, s223[1];" + " MAD R2, R3.x, s223[0], R2;" + " MAD R2, R3.z, s223[2], R2;" + " MAD R6, R3.w, s223[3], R2;" + " DP4 R1.z, R6, v16;" + " MOV R2, c0[3];" + " MUL R8, R2.y, s223[1];" + " MAD R8, R2.x, s223[0], R8;" + " MAD R8, R2.z, s223[2], R8;" + " MAD R8, R2.w, s223[3], R8;" + " MOV R8.x, R5.w;" + " MOV R8.y, R4.w;" + " MOV R8.z, R3.w;" + " ADD R1.yzw, R8.xxyz, -R1.xxyz;" + " DP3 R1.x, R1.yzwy, R1.yzwy;" + " RSQ R1.x, R1.x;" + " DP4 R5.x, R5, s18;" + " DP4 R5.y, R4, s18;" + " DP4 R5.z, R3, s18;" + " DP3 R2.x, R5.xyzx, R5.xyzx;" + " RSQ R2.x, R2.x;" + " MUL R5.xyz, R2.x, R5.xyzx;" + " MAD R1.yzw, R1.x, R1.yyzw, R5.xxyz;" + " DP3 R1.x, R1.yzwy, R1.yzwy;" + " RSQ R1.x, R1.x;" + " MUL R4.xyz, R1.x, R1.yzwy;" + " DP3 R3.x, R0.xyzx, v3.xyzx;" + " DP3 R3.y, R7.xyzx, v3.xyzx;" + " DP3 R3.z, R6.xyzx, v3.xyzx;" + " DP3 R8.x, R3.xyzx, R4.xyzx;" + " DP3 R2.x, R0.xyzx, v4.xyzx;" + " DP3 R2.y, R7.xyzx, v4.xyzx;" + " DP3 R2.z, R6.xyzx, v4.xyzx;" + " DP3 R8.y, R2.xyzx, R4.xyzx;" + " DP3 R1.x, R0.xyzx, v5.xyzx;" + " DP3 R1.y, R7.xyzx, v5.xyzx;" + " DP3 R1.z, R6.xyzx, v5.xyzx;" + " DP3 R8.z, R1.xyzx, R4.xyzx;" + " MAD result.color.front.secondary.xyz, c5.x, R8.xyzx, c5.x;" + " DP3 R0.y, R0.xyzx, v18.xyzx;" + " DP3 R0.z, R7.xyzx, v18.xyzx;" + " DP3 R0.w, R6.xyzx, v18.xyzx;" + " DP3 R0.x, R0.yzwy, R0.yzwy;" + " RSQ R0.x, R0.x;" + " MUL R6.xyz, R0.x, R0.yzwy;" + " DP3 R0.x, R6.xyzx, R4.xyzx;" + " MUL result.color.front.secondary.w, c5.y, R0.x;" + " DP3 R0.x, R3.xyzx, R5.xyzx;" + " DP3 R0.y, R2.xyzx, R5.xyzx;" + " DP3 R0.z, R1.xyzx, R5.xyzx;" + " MAD result.color.front.primary.xyz, c5.x, R0.xyzx, c5.x;" + " DP3 R0.x, R6.xyzx, R5.xyzx;" + " MUL result.color.front.primary.w, c5.y, R0.x;" + "END\n"; - // fragment program - std::ostringstream fp_oss; - fp_oss << - "!!ARBfp1.0\n" - "PARAM c0 = {1, 2, 0.5, 0};" - "PARAM c1 = {0, 0, 0, 1};" - "TEMP R0;" - "TEMP R1;" - "TEMP R2;" - "TEX R0, fragment.texcoord[0], texture[0], 2D;" - "TEX R1, fragment.texcoord[1], texture[1], 2D;" - "ADD R0, R0, -c0.z;" - "MUL R0.xyz, c0.y, R0;" - "ADD R2.xyz, fragment.color.primary, -c0.z;" - "MUL R2.xyz, c0.y, R2;" - "DP3_SAT R0.w, R0, R2;" - "ADD R2, fragment.color.secondary, -c0.z;" - "MUL R2.xyz, c0.y, R2;" - "DP3_SAT R0.x, R0, R2;" - "POW R0.x, R0.x, fragment.texcoord[2].w;" - "MOV R2.xyz, fragment.texcoord[2].xyyx;" - "MOV R2.w, c1.w;" - "MOV_SAT R0.y, fragment.color.primary.w;" - "MUL R0.w, R0.y, R0.w;" - "ADD R2, R2, R0.w;" - "MUL R1.xyz, R1, R2;" - "MOV_SAT R0.y, fragment.color.secondary.w;" - "MUL R0.xyz, R0.y, R0.x;" - "MOV R2.xy, fragment.texcoord[1].zwzz;" - "MOV R2.z, fragment.texcoord[0].z;" - "MUL R2.xyz, R0, R2;" - "ADD R2.xyz, R1, R2;" - "MOV result.color.xyz, R2;" - "MOV result.color.w, c0.x;" - "END\n"; + // fragment program + std::ostringstream fp_oss; + fp_oss << + "!!ARBfp1.0\n" + "PARAM c0 = {1, 2, 0.5, 0};" + "PARAM c1 = {0, 0, 0, 1};" + "TEMP R0;" + "TEMP R1;" + "TEMP R2;" + "TEX R0, fragment.texcoord[0], texture[0], 2D;" + "TEX R1, fragment.texcoord[1], texture[1], 2D;" + "ADD R0, R0, -c0.z;" + "MUL R0.xyz, c0.y, R0;" + "ADD R2.xyz, fragment.color.primary, -c0.z;" + "MUL R2.xyz, c0.y, R2;" + "DP3_SAT R0.w, R0, R2;" + "ADD R2, fragment.color.secondary, -c0.z;" + "MUL R2.xyz, c0.y, R2;" + "DP3_SAT R0.x, R0, R2;" + "POW R0.x, R0.x, fragment.texcoord[2].w;" + "MOV R2.xyz, fragment.texcoord[2].xyyx;" + "MOV R2.w, c1.w;" + "MOV_SAT R0.y, fragment.color.primary.w;" + "MUL R0.w, R0.y, R0.w;" + "ADD R2, R2, R0.w;" + "MUL R1.xyz, R1, R2;" + "MOV_SAT R0.y, fragment.color.secondary.w;" + "MUL R0.xyz, R0.y, R0.x;" + "MOV R2.xy, fragment.texcoord[1].zwzz;" + "MOV R2.z, fragment.texcoord[0].z;" + "MUL R2.xyz, R0, R2;" + "ADD R2.xyz, R1, R2;" + "MOV result.color.xyz, R2;" + "MOV result.color.w, c0.x;" + "END\n"; osg::ref_ptr ss = new osg::StateSet; @@ -365,19 +371,19 @@ namespace vp->setVertexProgram(vp_oss.str()); ss->setAttributeAndModes(vp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - osg::ref_ptr fp = new osg::FragmentProgram; + osg::ref_ptr fp = new osg::FragmentProgram; fp->setFragmentProgram(fp_oss.str()); ss->setAttributeAndModes(fp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); ss->setAttributeAndModes(new ViewMatrixExtractor(vp.get(), 0), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - if (diffuse_tex_.valid()) { - ss->setTextureAttributeAndModes(diffuseunit_, diffuse_tex_.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - } + if (diffuse_tex_.valid()) { + ss->setTextureAttributeAndModes(diffuseunit_, diffuse_tex_.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + } - if (normal_tex_.valid()) { - ss->setTextureAttributeAndModes(normalunit_, normal_tex_.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - } + if (normal_tex_.valid()) { + ss->setTextureAttributeAndModes(normalunit_, normal_tex_.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + } addPass(ss.get()); } @@ -385,9 +391,9 @@ namespace private: int lightnum_; int diffuseunit_; - int normalunit_; - osg::ref_ptr diffuse_tex_; - osg::ref_ptr normal_tex_; + int normalunit_; + osg::ref_ptr diffuse_tex_; + osg::ref_ptr normal_tex_; }; } @@ -395,166 +401,166 @@ namespace namespace { - // "ARB Vp" technique uses ARB vertex program and DOT3 texture environment. - // Ambient and specular components are not handled. A texture for the diffuse - // component is required as well as a normal map texture. - class ArbVpTechnique: public Technique { - public: - ArbVpTechnique(int lightnum, int diffuseunit, int normalunit, osg::Texture2D *diffuse_tex, osg::Texture2D *normal_tex) + // "ARB Vp" technique uses ARB vertex program and DOT3 texture environment. + // Ambient and specular components are not handled. A texture for the diffuse + // component is required as well as a normal map texture. + class ArbVpTechnique: public Technique { + public: + ArbVpTechnique(int lightnum, int diffuseunit, int normalunit, osg::Texture2D *diffuse_tex, osg::Texture2D *normal_tex) : Technique(), lightnum_(lightnum), diffuseunit_(diffuseunit), - normalunit_(normalunit), - diffuse_tex_(diffuse_tex), - normal_tex_(normal_tex) + normalunit_(normalunit), + diffuse_tex_(diffuse_tex), + normal_tex_(normal_tex) { } - META_Technique( - "ArbVpTechnique", - "Two-passes technique, requires ARB_vertex_program and ARB_texture_env_dot3." - "Only diffuse lighting, no ambient, no specularity." - ); + META_Technique( + "ArbVpTechnique", + "Two-passes technique, requires ARB_vertex_program and ARB_texture_env_dot3." + "Only diffuse lighting, no ambient, no specularity." + ); void getRequiredExtensions(std::vector &extensions) const { extensions.push_back("GL_ARB_vertex_program"); - extensions.push_back("GL_ARB_texture_env_dot3"); + extensions.push_back("GL_ARB_texture_env_dot3"); } - void define_passes() - { - if (diffuseunit_ != (normalunit_ + 1)) { - osg::notify(osg::WARN) << "Warning: osgFX::BumpMapping: this technique (ArbVpTechnique) requires that diffuse_unit == (normal_unit + 1). Effect may not show up properly.\n"; - } + void define_passes() + { + if (diffuseunit_ != (normalunit_ + 1)) { + osg::notify(osg::WARN) << "Warning: osgFX::BumpMapping: this technique (ArbVpTechnique) requires that diffuse_unit == (normal_unit + 1). Effect may not show up properly.\n"; + } - // first pass, diffuse bump - { - std::ostringstream vp_oss; - vp_oss << - "!!ARBvp1.0\n" - "OPTION ARB_position_invariant;" - "PARAM c0 = { 0.5, 1, 0, 0 };" - "TEMP R0, R1, R2;" - "ATTRIB v5 = vertex.attrib[15];" - "ATTRIB v4 = vertex.attrib[7];" - "ATTRIB v3 = vertex.attrib[6];" - "ATTRIB v24 = vertex.texcoord[" << normalunit_ << "];" - "ATTRIB v25 = vertex.texcoord[" << diffuseunit_ << "];" - "ATTRIB v18 = vertex.normal;" - "ATTRIB v16 = vertex.position;" - "PARAM s259[4] = { state.matrix.mvp };" - "PARAM s18 = state.light[" << lightnum_ << "].position;" - "PARAM s223[4] = { state.matrix.modelview[0] };" - " MOV result.texcoord[" << diffuseunit_ << "].xy, v25;" - " MOV result.texcoord[" << normalunit_ << "].xy, v24;" - " DP3 R0.y, s223[0].xyzx, v3.xyzx;" - " DP3 R0.z, s223[1].xyzx, v3.xyzx;" - " DP3 R0.w, s223[2].xyzx, v3.xyzx;" - " DP3 R0.x, s18.xyzx, s18.xyzx;" - " RSQ R0.x, R0.x;" - " MUL R2.xyz, R0.x, s18.xyzx;" - " DP3 R1.x, R0.yzwy, R2.xyzx;" - " DP3 R0.x, s223[0].xyzx, v4.xyzx;" - " DP3 R0.y, s223[1].xyzx, v4.xyzx;" - " DP3 R0.z, s223[2].xyzx, v4.xyzx;" - " DP3 R1.y, R0.xyzx, R2.xyzx;" - " DP3 R0.x, s223[0].xyzx, v5.xyzx;" - " DP3 R0.y, s223[1].xyzx, v5.xyzx;" - " DP3 R0.z, s223[2].xyzx, v5.xyzx;" - " DP3 R1.z, R0.xyzx, R2.xyzx;" - " MAD result.color.front.primary.xyz, c0.x, R1.xyzx, c0.x;" - " MOV result.color.front.primary.w, c0.y;" - "END\n"; + // first pass, diffuse bump + { + std::ostringstream vp_oss; + vp_oss << + "!!ARBvp1.0\n" + "OPTION ARB_position_invariant;" + "PARAM c0 = { 0.5, 1, 0, 0 };" + "TEMP R0, R1, R2;" + "ATTRIB v5 = vertex.attrib[15];" + "ATTRIB v4 = vertex.attrib[7];" + "ATTRIB v3 = vertex.attrib[6];" + "ATTRIB v24 = vertex.texcoord[" << normalunit_ << "];" + "ATTRIB v25 = vertex.texcoord[" << diffuseunit_ << "];" + "ATTRIB v18 = vertex.normal;" + "ATTRIB v16 = vertex.position;" + "PARAM s259[4] = { state.matrix.mvp };" + "PARAM s18 = state.light[" << lightnum_ << "].position;" + "PARAM s223[4] = { state.matrix.modelview[0] };" + " MOV result.texcoord[" << diffuseunit_ << "].xy, v25;" + " MOV result.texcoord[" << normalunit_ << "].xy, v24;" + " DP3 R0.y, s223[0].xyzx, v3.xyzx;" + " DP3 R0.z, s223[1].xyzx, v3.xyzx;" + " DP3 R0.w, s223[2].xyzx, v3.xyzx;" + " DP3 R0.x, s18.xyzx, s18.xyzx;" + " RSQ R0.x, R0.x;" + " MUL R2.xyz, R0.x, s18.xyzx;" + " DP3 R1.x, R0.yzwy, R2.xyzx;" + " DP3 R0.x, s223[0].xyzx, v4.xyzx;" + " DP3 R0.y, s223[1].xyzx, v4.xyzx;" + " DP3 R0.z, s223[2].xyzx, v4.xyzx;" + " DP3 R1.y, R0.xyzx, R2.xyzx;" + " DP3 R0.x, s223[0].xyzx, v5.xyzx;" + " DP3 R0.y, s223[1].xyzx, v5.xyzx;" + " DP3 R0.z, s223[2].xyzx, v5.xyzx;" + " DP3 R1.z, R0.xyzx, R2.xyzx;" + " MAD result.color.front.primary.xyz, c0.x, R1.xyzx, c0.x;" + " MOV result.color.front.primary.w, c0.y;" + "END\n"; - osg::ref_ptr ss = new osg::StateSet; + osg::ref_ptr ss = new osg::StateSet; - osg::ref_ptr vp = new osg::VertexProgram; - vp->setVertexProgram(vp_oss.str()); - ss->setAttributeAndModes(vp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + osg::ref_ptr vp = new osg::VertexProgram; + vp->setVertexProgram(vp_oss.str()); + ss->setAttributeAndModes(vp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - if (diffuse_tex_.valid()) { - ss->setTextureAttributeAndModes(diffuseunit_, diffuse_tex_.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - } + if (diffuse_tex_.valid()) { + ss->setTextureAttributeAndModes(diffuseunit_, diffuse_tex_.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + } - if (normal_tex_.valid()) { - ss->setTextureAttributeAndModes(normalunit_, normal_tex_.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - } + if (normal_tex_.valid()) { + ss->setTextureAttributeAndModes(normalunit_, normal_tex_.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + } - osg::ref_ptr tec = new osg::TexEnvCombine; - tec->setCombine_RGB(osg::TexEnvCombine::DOT3_RGB); - tec->setSource0_RGB(osg::TexEnvCombine::PRIMARY_COLOR); - tec->setSource1_RGB(osg::TexEnvCombine::TEXTURE); - ss->setTextureAttributeAndModes(normalunit_, tec.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + osg::ref_ptr tec = new osg::TexEnvCombine; + tec->setCombine_RGB(osg::TexEnvCombine::DOT3_RGB); + tec->setSource0_RGB(osg::TexEnvCombine::PRIMARY_COLOR); + tec->setSource1_RGB(osg::TexEnvCombine::TEXTURE); + ss->setTextureAttributeAndModes(normalunit_, tec.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - osg::ref_ptr te = new osg::TexEnv; - te->setMode(osg::TexEnv::MODULATE); - ss->setTextureAttributeAndModes(diffuseunit_, te.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + osg::ref_ptr te = new osg::TexEnv; + te->setMode(osg::TexEnv::MODULATE); + ss->setTextureAttributeAndModes(diffuseunit_, te.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - addPass(ss.get()); - } + addPass(ss.get()); + } - // second pass, self-shadowing - { - std::ostringstream vp_oss; - vp_oss << - "!!ARBvp1.0\n" - "OPTION ARB_position_invariant;" - "PARAM c0 = { 8, 0, 1, 0 };" - "TEMP R0;" - "ATTRIB v18 = vertex.normal;" - "ATTRIB v16 = vertex.position;" - "PARAM s259[4] = { state.matrix.mvp };" - "PARAM s18 = state.light[" << lightnum_ << "].position;" - "PARAM s631[4] = { state.matrix.modelview[0].invtrans };" - " DP4 R0.x, s631[0], v18;" - " DP4 R0.y, s631[1], v18;" - " DP4 R0.z, s631[2], v18;" - " DP3 R0.x, R0.xyzx, s18.xyzx;" - " MAX R0.x, R0.x, c0.y;" - " MUL R0.x, c0.x, R0.x;" - " MIN result.color.front.primary.xyz, R0.x, c0.z;" - " MOV result.color.front.primary.w, c0.z;" - "END\n"; + // second pass, self-shadowing + { + std::ostringstream vp_oss; + vp_oss << + "!!ARBvp1.0\n" + "OPTION ARB_position_invariant;" + "PARAM c0 = { 8, 0, 1, 0 };" + "TEMP R0;" + "ATTRIB v18 = vertex.normal;" + "ATTRIB v16 = vertex.position;" + "PARAM s259[4] = { state.matrix.mvp };" + "PARAM s18 = state.light[" << lightnum_ << "].position;" + "PARAM s631[4] = { state.matrix.modelview[0].invtrans };" + " DP4 R0.x, s631[0], v18;" + " DP4 R0.y, s631[1], v18;" + " DP4 R0.z, s631[2], v18;" + " DP3 R0.x, R0.xyzx, s18.xyzx;" + " MAX R0.x, R0.x, c0.y;" + " MUL R0.x, c0.x, R0.x;" + " MIN result.color.front.primary.xyz, R0.x, c0.z;" + " MOV result.color.front.primary.w, c0.z;" + "END\n"; - osg::ref_ptr ss = new osg::StateSet; + osg::ref_ptr ss = new osg::StateSet; - osg::ref_ptr depth = new osg::Depth; - depth->setFunction(osg::Depth::EQUAL); - ss->setAttributeAndModes(depth.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + osg::ref_ptr depth = new osg::Depth; + depth->setFunction(osg::Depth::EQUAL); + ss->setAttributeAndModes(depth.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - osg::ref_ptr vp = new osg::VertexProgram; - vp->setVertexProgram(vp_oss.str()); - ss->setAttributeAndModes(vp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + osg::ref_ptr vp = new osg::VertexProgram; + vp->setVertexProgram(vp_oss.str()); + ss->setAttributeAndModes(vp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - osg::ref_ptr bf = new osg::BlendFunc; - bf->setFunction(osg::BlendFunc::DST_COLOR, osg::BlendFunc::ZERO); - ss->setAttributeAndModes(bf.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + osg::ref_ptr bf = new osg::BlendFunc; + bf->setFunction(osg::BlendFunc::DST_COLOR, osg::BlendFunc::ZERO); + ss->setAttributeAndModes(bf.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); - ss->setTextureMode(diffuseunit_, GL_TEXTURE_2D, osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); - ss->setTextureMode(normalunit_, GL_TEXTURE_2D, osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); + ss->setTextureMode(diffuseunit_, GL_TEXTURE_2D, osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); + ss->setTextureMode(normalunit_, GL_TEXTURE_2D, osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); - addPass(ss.get()); - } + addPass(ss.get()); + } - } + } protected: int lightnum_; int diffuseunit_; - int normalunit_; - osg::ref_ptr diffuse_tex_; - osg::ref_ptr normal_tex_; - }; + int normalunit_; + osg::ref_ptr diffuse_tex_; + osg::ref_ptr normal_tex_; + }; } BumpMapping::BumpMapping() : Effect(), - lightnum_(0), + lightnum_(0), diffuseunit_(1), - normalunit_(0) + normalunit_(0) { } @@ -562,74 +568,74 @@ BumpMapping::BumpMapping(const BumpMapping ©, const osg::CopyOp ©op) : Effect(copy, copyop), lightnum_(copy.lightnum_), diffuseunit_(copy.diffuseunit_), - normalunit_(copy.normalunit_), - diffuse_tex_(static_cast(copyop(copy.diffuse_tex_.get()))), - normal_tex_(static_cast(copyop(copy.normal_tex_.get()))) + normalunit_(copy.normalunit_), + diffuse_tex_(static_cast(copyop(copy.diffuse_tex_.get()))), + normal_tex_(static_cast(copyop(copy.normal_tex_.get()))) { } bool BumpMapping::define_techniques() { addTechnique(new FullArbTechnique(lightnum_, diffuseunit_, normalunit_, diffuse_tex_.get(), normal_tex_.get())); - addTechnique(new ArbVpTechnique(lightnum_, diffuseunit_, normalunit_, diffuse_tex_.get(), normal_tex_.get())); + addTechnique(new ArbVpTechnique(lightnum_, diffuseunit_, normalunit_, diffuse_tex_.get(), normal_tex_.get())); return true; } void BumpMapping::prepareGeometry(osg::Geometry *geo) { - osg::ref_ptr tsg = new osgUtil::TangentSpaceGenerator; - tsg->generate(geo, normalunit_); - if (!geo->getVertexAttribArray(6)) - geo->setVertexAttribArray(6, false, tsg->getTangentArray(), osg::Geometry::BIND_PER_VERTEX); - if (!geo->getVertexAttribArray(7)) - geo->setVertexAttribArray(7, false, tsg->getBinormalArray(), osg::Geometry::BIND_PER_VERTEX); - if (!geo->getVertexAttribArray(15)) - geo->setVertexAttribArray(15, false, tsg->getNormalArray(), osg::Geometry::BIND_PER_VERTEX); + osg::ref_ptr tsg = new osgUtil::TangentSpaceGenerator; + tsg->generate(geo, normalunit_); + if (!geo->getVertexAttribArray(6)) + geo->setVertexAttribArray(6, false, tsg->getTangentArray(), osg::Geometry::BIND_PER_VERTEX); + if (!geo->getVertexAttribArray(7)) + geo->setVertexAttribArray(7, false, tsg->getBinormalArray(), osg::Geometry::BIND_PER_VERTEX); + if (!geo->getVertexAttribArray(15)) + geo->setVertexAttribArray(15, false, tsg->getNormalArray(), osg::Geometry::BIND_PER_VERTEX); } void BumpMapping::prepareNode(osg::Node *node) { - TsgVisitor tv(this); - node->accept(tv); + TsgVisitor tv(this); + node->accept(tv); } void BumpMapping::prepareChild() { - if (getChild()) - prepareNode(getChild()); + if (getChild()) + prepareNode(getChild()); } void BumpMapping::setUpDemo() { - // generate texture coordinates - TexCoordGenerator tcg(diffuseunit_, normalunit_); - getChild()->accept(tcg); + // generate texture coordinates + TexCoordGenerator tcg(diffuseunit_, normalunit_); + getChild()->accept(tcg); - // set up diffuse texture - if (!diffuse_tex_.valid()) { - diffuse_tex_ = new osg::Texture2D; - diffuse_tex_->setImage(osgDB::readImageFile("whitemetal_diffuse.jpg")); - diffuse_tex_->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); - diffuse_tex_->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); - diffuse_tex_->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); - diffuse_tex_->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); - diffuse_tex_->setMaxAnisotropy(8); - } + // set up diffuse texture + if (!diffuse_tex_.valid()) { + diffuse_tex_ = new osg::Texture2D; + diffuse_tex_->setImage(osgDB::readImageFile("whitemetal_diffuse.jpg")); + diffuse_tex_->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); + diffuse_tex_->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); + diffuse_tex_->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); + diffuse_tex_->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); + diffuse_tex_->setMaxAnisotropy(8); + } - // set up normal map texture - if (!normal_tex_.valid()) { - normal_tex_ = new osg::Texture2D; - normal_tex_->setImage(osgDB::readImageFile("whitemetal_normal.jpg")); - normal_tex_->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); - normal_tex_->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); - normal_tex_->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); - normal_tex_->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); - normal_tex_->setMaxAnisotropy(8); - } + // set up normal map texture + if (!normal_tex_.valid()) { + normal_tex_ = new osg::Texture2D; + normal_tex_->setImage(osgDB::readImageFile("whitemetal_normal.jpg")); + normal_tex_->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR); + normal_tex_->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); + normal_tex_->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); + normal_tex_->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); + normal_tex_->setMaxAnisotropy(8); + } - // generate tangent-space basis vector - prepareChild(); + // generate tangent-space basis vector + prepareChild(); - // recreate techniques on next step - dirtyTechniques(); + // recreate techniques on next step + dirtyTechniques(); } diff --git a/src/osgFX/SpecularHighlights.cpp b/src/osgFX/SpecularHighlights.cpp index f9bc65481..d6f37bd9b 100644 --- a/src/osgFX/SpecularHighlights.cpp +++ b/src/osgFX/SpecularHighlights.cpp @@ -67,7 +67,7 @@ namespace osg::Vec3(lightvec.x(), lightvec.y(), lightvec.z()), eye_light_ref); - glLoadMatrixf((LM * osg::Matrix::inverse(M)).ptr()); + (LM * osg::Matrix::inverse(M)).glLoadMatrix(); } else { glLoadIdentity(); diff --git a/src/osgProducer/OsgCameraGroup.cpp b/src/osgProducer/OsgCameraGroup.cpp index c75d71249..a079dbdbb 100644 --- a/src/osgProducer/OsgCameraGroup.cpp +++ b/src/osgProducer/OsgCameraGroup.cpp @@ -438,7 +438,11 @@ const osg::Node* OsgCameraGroup::getTopMostSceneData() const void OsgCameraGroup::setView(const osg::Matrix& matrix) { - Producer::Matrix pm(matrix.ptr()); + Producer::Matrix pm;//(matrix.ptr()); + for(int i=0;i<4;++i) + for(int j=0;j<4;++j) + pm(i,j)=matrix(i,j); + setViewByMatrix(pm); } diff --git a/src/osgSim/ScalarBar.cpp b/src/osgSim/ScalarBar.cpp index 7b3cc358b..05cf3d92e 100644 --- a/src/osgSim/ScalarBar.cpp +++ b/src/osgSim/ScalarBar.cpp @@ -7,15 +7,15 @@ using namespace osgSim; std::string ScalarBar::ScalarPrinter::printScalar(float scalar) { - std::stringstream ostr; - ostr<getBound().xMax() < d2->getBound().xMax(); - else if(_axis == Y_AXIS) return d1->getBound().yMax() < d2->getBound().yMax(); - else if(_axis == Z_AXIS) return d1->getBound().zMax() < d2->getBound().zMax(); + bool operator()(const osgText::Text* d1, const osgText::Text* d2) + { + if(_axis == X_AXIS ) return d1->getBound().xMax() < d2->getBound().xMax(); + else if(_axis == Y_AXIS) return d1->getBound().yMax() < d2->getBound().yMax(); + else if(_axis == Z_AXIS) return d1->getBound().zMax() < d2->getBound().zMax(); - return false; - } + return false; + } }; struct AlignCentreOnYValue @@ -126,10 +126,10 @@ struct AlignCentreOnYValue float _y; AlignCentreOnYValue(float y): _y(y) {} void operator()(osgText::Text* t) - { + { t->setPosition(osg::Vec3(t->getBound().center().x(), _y, t->getBound().center().z())); - t->setAlignment(osgText::Text::CENTER_CENTER); - } + t->setAlignment(osgText::Text::CENTER_CENTER); + } }; } @@ -139,162 +139,163 @@ void ScalarBar::createDrawables() // Remove any existing Drawables _drawables.erase(_drawables.begin(), _drawables.end()); - // 1. First the bar - // ================= + // 1. First the bar + // ================= osg::ref_ptr bar = new osg::Geometry(); - // Create the bar - created in 'real' coordinate space the moment, - // with xyz values reflecting those of the actual scalar values in play. - // FIXME: Consider positioning at origin! Should be easy enough to do. + // Create the bar - created in 'real' coordinate space the moment, + // with xyz values reflecting those of the actual scalar values in play. + // FIXME: Consider positioning at origin! Should be easy enough to do. - // Vertices - osg::ref_ptr vs(new osg::Vec3Array); - vs->reserve(2*(_numColors+1)); + // Vertices + osg::ref_ptr vs(new osg::Vec3Array); + vs->reserve(2*(_numColors+1)); float incr = (_stc->getMax() - _stc->getMin()) / _numColors; - float arOffset; - if(_orientation==HORIZONTAL) - { - arOffset = _numColors * incr * _aspectRatio; // Bar height for a horizontal bar - } - else - { + float arOffset; + if(_orientation==HORIZONTAL) + { + arOffset = _numColors * incr * _aspectRatio; // Bar height for a horizontal bar + } + else + { arOffset = (_numColors*incr)/_aspectRatio; // Bar width for a vertical bar - } + } - for(int i=1; i<=_numColors; ++i) - { - // Make a quad - if(_orientation==HORIZONTAL) - { - vs->push_back(osg::Vec3(_stc->getMin() + (i-1) * incr, 0.0f, 0.0f)); - vs->push_back(osg::Vec3(_stc->getMin() + (i-1) * incr, arOffset, 0.0f)); - vs->push_back(osg::Vec3(_stc->getMin() + i * incr, arOffset, 0.0f)); - vs->push_back(osg::Vec3(_stc->getMin() + i * incr, 0.0f, 0.0f)); - } - else - { - vs->push_back(osg::Vec3(0.0f, _stc->getMin() + (i-1) * incr, 0.0f)); - vs->push_back(osg::Vec3(arOffset, _stc->getMin() + (i-1) * incr, 0.0f)); - vs->push_back(osg::Vec3(arOffset, _stc->getMin() + i * incr, 0.0f)); - vs->push_back(osg::Vec3(0.0f, _stc->getMin() + i * incr, 0.0f)); - } - } - bar->setVertexArray(vs.get()); + int i; + for(i=1; i<=_numColors; ++i) + { + // Make a quad + if(_orientation==HORIZONTAL) + { + vs->push_back(osg::Vec3(_stc->getMin() + (i-1) * incr, 0.0f, 0.0f)); + vs->push_back(osg::Vec3(_stc->getMin() + (i-1) * incr, arOffset, 0.0f)); + vs->push_back(osg::Vec3(_stc->getMin() + i * incr, arOffset, 0.0f)); + vs->push_back(osg::Vec3(_stc->getMin() + i * incr, 0.0f, 0.0f)); + } + else + { + vs->push_back(osg::Vec3(0.0f, _stc->getMin() + (i-1) * incr, 0.0f)); + vs->push_back(osg::Vec3(arOffset, _stc->getMin() + (i-1) * incr, 0.0f)); + vs->push_back(osg::Vec3(arOffset, _stc->getMin() + i * incr, 0.0f)); + vs->push_back(osg::Vec3(0.0f, _stc->getMin() + i * incr, 0.0f)); + } + } + bar->setVertexArray(vs.get()); // Colours osg::ref_ptr cs(new osg::Vec4Array); cs->reserve(_numColors); - const float halfIncr = incr*0.5; - for(int i=0; i<_numColors; ++i) - { - // We add half an increment to the color look-up to get the color - // square in the middle of the 'block'. - cs->push_back(_stc->getColor(_stc->getMin() + (i*incr) + halfIncr)); - } - bar->setColorArray(cs.get()); - bar->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); + const float halfIncr = incr*0.5; + for(i=0; i<_numColors; ++i) + { + // We add half an increment to the color look-up to get the color + // square in the middle of the 'block'. + cs->push_back(_stc->getColor(_stc->getMin() + (i*incr) + halfIncr)); + } + bar->setColorArray(cs.get()); + bar->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); - // Normal + // Normal osg::ref_ptr ns(new osg::Vec3Array); ns->push_back(osg::Vec3(0.0f,0.0f,1.0f)); - bar->setNormalArray(ns.get()); - bar->setNormalBinding(osg::Geometry::BIND_OVERALL); + bar->setNormalArray(ns.get()); + bar->setNormalBinding(osg::Geometry::BIND_OVERALL); - // The Quad strip that represents the bar - bar->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,vs->size())); + // The Quad strip that represents the bar + bar->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,vs->size())); addDrawable(bar.get()); // 2. Then the text labels - // ======================= + // ======================= // Check the character size, if it's 0, estimate a good character size float characterSize = _textProperties._characterSize; - if(characterSize == 0) characterSize = ((_stc->getMax()-_stc->getMin())*0.3)/_numLabels; + if(characterSize == 0) characterSize = ((_stc->getMax()-_stc->getMin())*0.3)/_numLabels; - osgText::Font* font = osgText::readFontFile(_textProperties._fontFile.c_str()); + osgText::Font* font = osgText::readFontFile(_textProperties._fontFile.c_str()); - std::vector texts(_numLabels); // We'll need to collect pointers to these for later + std::vector texts(_numLabels); // We'll need to collect pointers to these for later float labelIncr = (_stc->getMax()-_stc->getMin())/(_numLabels-1); - for(int i=0; i<_numLabels; ++i) - { + for(i=0; i<_numLabels; ++i) + { osgText::Text* text = new osgText::Text; text->setFont(font); text->setColor(_textProperties._color); text->setFontResolution(_textProperties._fontResolution.first,_textProperties._fontResolution.second); - text->setCharacterSize(characterSize); - text->setText(_sp->printScalar(_stc->getMin()+(i*labelIncr))); + text->setCharacterSize(characterSize); + text->setText(_sp->printScalar(_stc->getMin()+(i*labelIncr))); - if(_orientation == HORIZONTAL) - { + if(_orientation == HORIZONTAL) + { text->setPosition(osg::Vec3(_stc->getMin() + (i*labelIncr), arOffset, 0.0f)); - text->setAlignment(osgText::Text::CENTER_BOTTOM); - } - else - { + text->setAlignment(osgText::Text::CENTER_BOTTOM); + } + else + { text->setPosition(osg::Vec3(arOffset, _stc->getMin() + (i*labelIncr), 0.0f)); - text->setAlignment(osgText::Text::LEFT_CENTER); - } - - addDrawable(text); - - texts[i] = text; - } - - // Make sure the text labels are all properly aligned - different words will have a different - // vertical alignment depending on the letters used in the labels. E.g. a 'y' has a dangling tail. - if(_orientation == HORIZONTAL) - { - std::vector::iterator maxYIt = std::max_element(texts.begin(), texts.end(), MaxCoordLess(MaxCoordLess::Y_AXIS)); - std::for_each(texts.begin(), texts.end(), AlignCentreOnYValue((*maxYIt)->getBound().center().y())); - } - - // 3. And finally the title - // ======================== - - if(_title != "") - { - osgText::Text* text = new osgText::Text; - text->setFont(font); - text->setColor(_textProperties._color); - text->setFontResolution(_textProperties._fontResolution.first,_textProperties._fontResolution.second); - text->setCharacterSize(characterSize); - text->setText(_title); - - if(_orientation==HORIZONTAL) - { - // Horizontal bars have the title above the scalar bar and the labels. - // Need to move the title above any labels, using maximum y value of the - // existing text objects - - std::vector::iterator maxYIt = std::max_element(texts.begin(), texts.end(), MaxCoordLess(MaxCoordLess::Y_AXIS)); - - float titleY; - if(maxYIt != texts.end()) titleY = (*maxYIt)->getBound().yMax() * 1.1f; - else titleY = arOffset; // No labels, so just use arOffset - - // Position the title at the middle of the bar above any labels. - text->setPosition(osg::Vec3(_stc->getMin() + ((_stc->getMax()-_stc->getMin())/2.0f), titleY, 0.0f)); - text->setAlignment(osgText::Text::CENTER_BOTTOM); - } - else if(_orientation==VERTICAL) - { - // Vertical bars have the title to the right of the scalar bar and the labels. - // Need to move the title out beyond any labels, using the maximum x value of the - // existing text objects - - std::vector::iterator maxXIt = std::max_element(texts.begin(), texts.end(), MaxCoordLess(MaxCoordLess::X_AXIS)); - - float titleX; - if(maxXIt != texts.end()) titleX = (*maxXIt)->getBound().xMax() * 1.1f; - else titleX = arOffset; // No labels, so just use arOffset - - // Position the title in the at the middle of the bar, to the right of any labels. - text->setPosition(osg::Vec3(titleX, _stc->getMin() + ((_stc->getMax()-_stc->getMin())/2.0f), 0.0f)); - text->setAlignment(osgText::Text::LEFT_CENTER); + text->setAlignment(osgText::Text::LEFT_CENTER); } - addDrawable(text); - } + addDrawable(text); + + texts[i] = text; + } + + // Make sure the text labels are all properly aligned - different words will have a different + // vertical alignment depending on the letters used in the labels. E.g. a 'y' has a dangling tail. + if(_orientation == HORIZONTAL) + { + std::vector::iterator maxYIt = std::max_element(texts.begin(), texts.end(), MaxCoordLess(MaxCoordLess::Y_AXIS)); + std::for_each(texts.begin(), texts.end(), AlignCentreOnYValue((*maxYIt)->getBound().center().y())); + } + + // 3. And finally the title + // ======================== + + if(_title != "") + { + osgText::Text* text = new osgText::Text; + text->setFont(font); + text->setColor(_textProperties._color); + text->setFontResolution(_textProperties._fontResolution.first,_textProperties._fontResolution.second); + text->setCharacterSize(characterSize); + text->setText(_title); + + if(_orientation==HORIZONTAL) + { + // Horizontal bars have the title above the scalar bar and the labels. + // Need to move the title above any labels, using maximum y value of the + // existing text objects + + std::vector::iterator maxYIt = std::max_element(texts.begin(), texts.end(), MaxCoordLess(MaxCoordLess::Y_AXIS)); + + float titleY; + if(maxYIt != texts.end()) titleY = (*maxYIt)->getBound().yMax() * 1.1f; + else titleY = arOffset; // No labels, so just use arOffset + + // Position the title at the middle of the bar above any labels. + text->setPosition(osg::Vec3(_stc->getMin() + ((_stc->getMax()-_stc->getMin())/2.0f), titleY, 0.0f)); + text->setAlignment(osgText::Text::CENTER_BOTTOM); + } + else if(_orientation==VERTICAL) + { + // Vertical bars have the title to the right of the scalar bar and the labels. + // Need to move the title out beyond any labels, using the maximum x value of the + // existing text objects + + std::vector::iterator maxXIt = std::max_element(texts.begin(), texts.end(), MaxCoordLess(MaxCoordLess::X_AXIS)); + + float titleX; + if(maxXIt != texts.end()) titleX = (*maxXIt)->getBound().xMax() * 1.1f; + else titleX = arOffset; // No labels, so just use arOffset + + // Position the title in the at the middle of the bar, to the right of any labels. + text->setPosition(osg::Vec3(titleX, _stc->getMin() + ((_stc->getMax()-_stc->getMin())/2.0f), 0.0f)); + text->setAlignment(osgText::Text::LEFT_CENTER); + } + + addDrawable(text); + } } diff --git a/src/osgSim/SphereSegment.cpp b/src/osgSim/SphereSegment.cpp index ae09ab59a..02b23c3f9 100644 --- a/src/osgSim/SphereSegment.cpp +++ b/src/osgSim/SphereSegment.cpp @@ -431,7 +431,8 @@ void SphereSegment::EdgeLine_drawImplementation(osg::State& /* state */) const // Top edge glBegin(GL_LINE_STRIP); - for(int i=0; i<=_density; i++) + int i; + for(i=0; i<=_density; i++) { float az = _azMin + (i*azIncr); glVertex3f( @@ -443,7 +444,7 @@ void SphereSegment::EdgeLine_drawImplementation(osg::State& /* state */) const // Bottom edge glBegin(GL_LINE_STRIP); - for(int i=0; i<=_density; i++) + for(i=0; i<=_density; i++) { float az = _azMin + (i*azIncr); glVertex3f( @@ -455,7 +456,8 @@ void SphereSegment::EdgeLine_drawImplementation(osg::State& /* state */) const // Left edge glBegin(GL_LINE_STRIP); - for(int j=0; j<=_density; j++) + int j; + for(j=0; j<=_density; j++) { float elev = _elevMin + (j*elevIncr); glVertex3f( @@ -467,7 +469,7 @@ void SphereSegment::EdgeLine_drawImplementation(osg::State& /* state */) const // Right edge glBegin(GL_LINE_STRIP); - for(int j=0; j<=_density; j++) + for(j=0; j<=_density; j++) { float elev = _elevMin + (j*elevIncr); glVertex3f( @@ -487,7 +489,8 @@ bool SphereSegment::EdgeLine_computeBound(osg::BoundingBox& bbox) const float elevIncr = (_elevMax - _elevMin)/_density; // Top edge - for(int i=0; i<=_density; i++) + int i; + for(i=0; i<=_density; i++) { float az = _azMin + (i*azIncr); bbox.expandBy( @@ -497,7 +500,7 @@ bool SphereSegment::EdgeLine_computeBound(osg::BoundingBox& bbox) const } // Bottom edge - for(int i=0; i<=_density; i++) + for(i=0; i<=_density; i++) { float az = _azMin + (i*azIncr); bbox.expandBy( @@ -507,7 +510,8 @@ bool SphereSegment::EdgeLine_computeBound(osg::BoundingBox& bbox) const } // Left edge - for(int j=0; j<=_density; j++) + int j; + for(j=0; j<=_density; j++) { float elev = _elevMin + (j*elevIncr); bbox.expandBy( @@ -517,7 +521,7 @@ bool SphereSegment::EdgeLine_computeBound(osg::BoundingBox& bbox) const } // Right edge - for(int j=0; j<=_density; j++) + for(j=0; j<=_density; j++) { float elev = _elevMin + (j*elevIncr); bbox.expandBy( diff --git a/src/osgUtil/CullVisitor.cpp b/src/osgUtil/CullVisitor.cpp index 62644e278..cfda04097 100644 --- a/src/osgUtil/CullVisitor.cpp +++ b/src/osgUtil/CullVisitor.cpp @@ -153,9 +153,12 @@ float CullVisitor::getDistanceToEyePoint(const Vec3& pos, bool withLODScale) con else return (pos-getEyeLocal()).length(); } -inline float distance(const osg::Vec3& coord,const osg::Matrix& matrix) +inline CullVisitor::NearFarReal distance(const osg::Vec3& coord,const osg::Matrix& matrix) { - return -(coord[0]*matrix(0,2)+coord[1]*matrix(1,2)+coord[2]*matrix(2,2)+matrix(3,2)); + + //std::cout << "distance("<_computed_zfar) _computed_zfar = d_far; - } - else - { - if ( !EQUAL_F(d_near, d_far) ) - { - osg::notify(osg::WARN)<<"Warning: CullVisitor::updateCalculatedNearFar(.) near>far in range calculation,"<< std::endl; - osg::notify(osg::WARN)<<" correcting by swapping values d_near="<far in range calculation,"<< std::endl; +// osg::notify(osg::WARN)<<" correcting by swapping values d_near="<