2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
2003-08-27 22:13:12 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGUTIL_TANGENTSPACEGENERATOR_
|
|
|
|
#define OSGUTIL_TANGENTSPACEGENERATOR_
|
|
|
|
|
|
|
|
#include <osgUtil/Export>
|
|
|
|
|
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Referenced>
|
|
|
|
#include <osg/Array>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
|
|
|
|
namespace osgUtil
|
|
|
|
{
|
|
|
|
|
2004-10-24 22:42:40 +08:00
|
|
|
/**
|
|
|
|
The TangentSpaceGenerator class generates three arrays containing tangent-space basis vectors.
|
|
|
|
It takes a texture-mapped Geometry object as input, traverses its primitive sets and computes
|
|
|
|
Tangent, Normal and Binormal vectors for each vertex, storing them into arrays.
|
|
|
|
The resulting arrays can be used as vertex program varying (per-vertex) parameters,
|
|
|
|
enabling advanced effects like bump-mapping.
|
|
|
|
To use this class, simply call the generate() method specifying the Geometry object
|
|
|
|
you want to process and the texture unit that contains UV mapping for the normal map;
|
|
|
|
then you can retrieve the TBN arrays by calling getTangentArray(), getNormalArray()
|
|
|
|
and getBinormalArray() methods.
|
|
|
|
*/
|
|
|
|
class OSGUTIL_EXPORT TangentSpaceGenerator: public osg::Referenced {
|
|
|
|
public:
|
|
|
|
TangentSpaceGenerator();
|
|
|
|
TangentSpaceGenerator(const TangentSpaceGenerator ©, const osg::CopyOp ©op = osg::CopyOp::SHALLOW_COPY);
|
2003-08-27 22:13:12 +08:00
|
|
|
|
2005-09-28 21:53:54 +08:00
|
|
|
void generate(osg::Geometry *geo, int normal_map_tex_unit = 0, bool use_indices = false);
|
2003-08-27 22:13:12 +08:00
|
|
|
|
2004-10-24 22:42:40 +08:00
|
|
|
inline osg::Vec4Array *getTangentArray() { return T_.get(); }
|
|
|
|
inline const osg::Vec4Array *getTangentArray() const { return T_.get(); }
|
|
|
|
inline void setTangentArray(osg::Vec4Array *array) { T_ = array; }
|
2003-08-27 22:13:12 +08:00
|
|
|
|
2004-10-24 22:42:40 +08:00
|
|
|
inline osg::Vec4Array *getNormalArray() { return N_.get(); }
|
|
|
|
inline const osg::Vec4Array *getNormalArray() const { return N_.get(); }
|
|
|
|
inline void setNormalArray(osg::Vec4Array *array) { N_ = array; }
|
2003-08-27 22:13:12 +08:00
|
|
|
|
2004-10-24 22:42:40 +08:00
|
|
|
inline osg::Vec4Array *getBinormalArray() { return B_.get(); }
|
|
|
|
inline const osg::Vec4Array *getBinormalArray() const { return B_.get(); }
|
|
|
|
inline void setBinormalArray(osg::Vec4Array *array) { B_ = array; }
|
2003-08-27 22:13:12 +08:00
|
|
|
|
2005-03-08 01:25:30 +08:00
|
|
|
inline osg::IndexArray *getIndices() { return indices_.get(); }
|
2005-09-28 21:53:54 +08:00
|
|
|
|
2004-10-24 22:42:40 +08:00
|
|
|
protected:
|
2005-09-28 21:53:54 +08:00
|
|
|
|
2004-10-24 22:42:40 +08:00
|
|
|
virtual ~TangentSpaceGenerator() {}
|
|
|
|
TangentSpaceGenerator &operator=(const TangentSpaceGenerator &) { return *this; }
|
2003-08-27 22:13:12 +08:00
|
|
|
|
2005-09-28 21:53:54 +08:00
|
|
|
// Base class for computing basis vectors
|
|
|
|
class BasisVectorsComputer : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit BasisVectorsComputer(TangentSpaceGenerator* base) : base_( base ) {}
|
|
|
|
|
|
|
|
virtual void compute(osg::PrimitiveSet *pset,
|
|
|
|
const osg::Array *vx,
|
|
|
|
const osg::Array *nx,
|
|
|
|
const osg::Array *tx,
|
|
|
|
const osg::IndexArray *vix,
|
|
|
|
const osg::IndexArray *nix,
|
|
|
|
const osg::IndexArray *tix,
|
|
|
|
int iA, int iB, int iC) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~BasisVectorsComputer() {}
|
|
|
|
BasisVectorsComputer(const TangentSpaceGenerator ©, const osg::CopyOp ©op);
|
|
|
|
|
|
|
|
TangentSpaceGenerator* base_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Class for computing basis vectors without using indices
|
|
|
|
class VectorsComputerNoIndices : public BasisVectorsComputer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit VectorsComputerNoIndices(TangentSpaceGenerator* base) : BasisVectorsComputer( base ) {}
|
|
|
|
|
|
|
|
void compute(osg::PrimitiveSet *pset,
|
|
|
|
const osg::Array *vx,
|
|
|
|
const osg::Array *nx,
|
|
|
|
const osg::Array *tx,
|
|
|
|
const osg::IndexArray *vix,
|
|
|
|
const osg::IndexArray *nix,
|
|
|
|
const osg::IndexArray *tix,
|
|
|
|
int iA, int iB, int iC);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~VectorsComputerNoIndices() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Class for computing basis vectors using indices
|
|
|
|
class VectorsComputerUsingIndices : public BasisVectorsComputer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit VectorsComputerUsingIndices(TangentSpaceGenerator* base) : BasisVectorsComputer( base ) {}
|
|
|
|
|
|
|
|
void compute(osg::PrimitiveSet *pset,
|
|
|
|
const osg::Array *vx,
|
|
|
|
const osg::Array *nx,
|
|
|
|
const osg::Array *tx,
|
|
|
|
const osg::IndexArray *vix,
|
|
|
|
const osg::IndexArray *nix,
|
|
|
|
const osg::IndexArray *tix,
|
|
|
|
int iA, int iB, int iC);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~VectorsComputerUsingIndices() {}
|
|
|
|
};
|
2003-08-27 22:13:12 +08:00
|
|
|
|
2005-10-27 04:22:59 +08:00
|
|
|
// declare as friends so that may operator on TangentSpaceGenerator protected member variables.
|
|
|
|
friend class BasisVectorsComputer;
|
|
|
|
friend class VectorsComputerNoIndices;
|
|
|
|
friend class VectorsComputerUsingIndices;
|
|
|
|
|
2004-10-24 22:42:40 +08:00
|
|
|
osg::ref_ptr<osg::Vec4Array> T_;
|
|
|
|
osg::ref_ptr<osg::Vec4Array> B_;
|
|
|
|
osg::ref_ptr<osg::Vec4Array> N_;
|
2005-03-08 01:25:30 +08:00
|
|
|
osg::ref_ptr<osg::UIntArray> indices_;
|
2004-10-24 22:42:40 +08:00
|
|
|
};
|
2003-08-27 22:13:12 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|