2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
2003-09-02 05:53:53 +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.
|
|
|
|
*/
|
|
|
|
//osgFX - Copyright (C) 2003 Marco Jez
|
|
|
|
|
|
|
|
#ifndef OSGFX_BUMPMAPPING_
|
|
|
|
#define OSGFX_BUMPMAPPING_
|
|
|
|
|
|
|
|
#include <osgFX/Export>
|
|
|
|
#include <osgFX/Effect>
|
|
|
|
|
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
|
|
|
|
namespace osgFX
|
|
|
|
{
|
|
|
|
|
2003-11-28 06:06:11 +08:00
|
|
|
/**
|
2003-09-09 19:54:05 +08:00
|
|
|
This effect makes surfaces appear bumpy. Children nodes must use two textures,
|
2003-09-02 05:53:53 +08:00
|
|
|
one for diffuse color and one for the normal map (which can be created
|
|
|
|
from a height map with tools like nVIDIA's normal map generator). Furthermore,
|
|
|
|
tangent-space basis vectors must be created and assigned to each Geometry; this
|
2003-09-09 19:54:05 +08:00
|
|
|
can be done quickly by calling BumpMapping::prepareChildren(). Note that both
|
2003-09-02 05:53:53 +08:00
|
|
|
diffuse and normal map textures must have corresponding UV maps defined in
|
|
|
|
Geometry objects.
|
|
|
|
This effect defines a preferred technique which uses ARB vertex & fragment
|
|
|
|
programs, and a fallback technique which doesn't use fragment programs. The
|
|
|
|
latter is more limited though since it can't handle ambient and specular
|
|
|
|
components.
|
|
|
|
*/
|
|
|
|
class OSGFX_EXPORT BumpMapping: public Effect {
|
|
|
|
public:
|
|
|
|
BumpMapping();
|
2005-04-29 14:32:13 +08:00
|
|
|
BumpMapping(const BumpMapping& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
|
2003-09-02 05:53:53 +08:00
|
|
|
|
|
|
|
META_Effect(osgFX, BumpMapping,
|
|
|
|
|
|
|
|
"Bump Mapping",
|
|
|
|
|
2003-09-09 19:54:05 +08:00
|
|
|
"This effect makes surfaces appear bumpy. Children nodes must use two textures, "
|
2003-09-02 05:53:53 +08:00
|
|
|
"one for diffuse color and one for the normal map (which can be created "
|
|
|
|
"from a height map with tools like nVIDIA's normal map generator). Furthermore, "
|
|
|
|
"tangent-space basis vectors must be created and assigned to each Geometry; this "
|
2003-09-09 19:54:05 +08:00
|
|
|
"can be done quickly by calling BumpMapping::prepareChildren(). Note that both "
|
2003-09-02 05:53:53 +08:00
|
|
|
"diffuse and normal map textures must have corresponding UV maps defined in "
|
|
|
|
"Geometry objects.\n"
|
|
|
|
"This effect defines a preferred technique which uses ARB vertex & fragment "
|
|
|
|
"programs, and a fallback technique which doesn't use fragment programs. The "
|
|
|
|
"latter is more limited though since it can't handle ambient and specular "
|
|
|
|
"components.",
|
|
|
|
|
|
|
|
"Marco Jez");
|
|
|
|
|
|
|
|
|
|
|
|
/** get the OpenGL light number */
|
|
|
|
inline int getLightNumber() const;
|
|
|
|
|
|
|
|
/** set the OpenGL light number that will be used in lighting computations */
|
|
|
|
inline void setLightNumber(int n);
|
|
|
|
|
|
|
|
/** get the texture unit that contains diffuse color texture. Default is 1 */
|
|
|
|
inline int getDiffuseTextureUnit() const;
|
|
|
|
|
|
|
|
/** set the texture unit that contains diffuse color texture. Default is 1 */
|
|
|
|
inline void setDiffuseTextureUnit(int n);
|
|
|
|
|
|
|
|
/** get the texture unit that contains normal map texture. Default is 0 */
|
|
|
|
inline int getNormalMapTextureUnit() const;
|
|
|
|
|
|
|
|
/** set the texture unit that contains normal map texture. Default is 0 */
|
|
|
|
inline void setNormalMapTextureUnit(int n);
|
|
|
|
|
2003-09-09 19:54:05 +08:00
|
|
|
/** get the diffuse color texture that overrides children's texture */
|
2005-04-29 14:32:13 +08:00
|
|
|
inline osg::Texture2D* getOverrideDiffuseTexture();
|
2003-09-02 05:53:53 +08:00
|
|
|
|
2003-09-09 19:54:05 +08:00
|
|
|
/** get the const diffuse color texture that overrides children's texture */
|
2005-04-29 14:32:13 +08:00
|
|
|
inline const osg::Texture2D* getOverrideDiffuseTexture() const;
|
2003-09-02 05:53:53 +08:00
|
|
|
|
2003-09-09 19:54:05 +08:00
|
|
|
/** set the diffuse color texture that overrides children's texture */
|
2005-04-29 14:32:13 +08:00
|
|
|
inline void setOverrideDiffuseTexture(osg::Texture2D* texture);
|
2003-09-02 05:53:53 +08:00
|
|
|
|
2003-09-09 19:54:05 +08:00
|
|
|
/** get the normal map texture that overrides children's texture */
|
2005-04-29 14:32:13 +08:00
|
|
|
inline osg::Texture2D* getOverrideNormalMapTexture();
|
2003-09-02 05:53:53 +08:00
|
|
|
|
2003-09-09 19:54:05 +08:00
|
|
|
/** get the const normal map texture that overrides children's texture */
|
2005-04-29 14:32:13 +08:00
|
|
|
inline const osg::Texture2D* getOverrideNormalMapTexture() const;
|
2003-09-02 05:53:53 +08:00
|
|
|
|
2003-09-09 19:54:05 +08:00
|
|
|
/** set the normal map texture that overrides children's texture */
|
2005-04-29 14:32:13 +08:00
|
|
|
inline void setOverrideNormalMapTexture(osg::Texture2D* texture);
|
2003-09-02 05:53:53 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
prepare a Geometry for bump lighting. Tangent-space basis vectors are
|
|
|
|
generated and attached to the geometry as vertex attribute arrays.
|
|
|
|
*/
|
2005-04-29 14:32:13 +08:00
|
|
|
void prepareGeometry(osg::Geometry* geo);
|
2003-09-02 05:53:53 +08:00
|
|
|
|
|
|
|
/** prepare a Node for bump lighting, calling prepareGeometry() for each Geometry */
|
2005-04-29 14:32:13 +08:00
|
|
|
void prepareNode(osg::Node* node);
|
2003-09-02 05:53:53 +08:00
|
|
|
|
2003-09-09 19:54:05 +08:00
|
|
|
/** prepare children for bump lighting. Actually calls prepareNode() for each child */
|
|
|
|
void prepareChildren();
|
2003-09-02 05:53:53 +08:00
|
|
|
|
|
|
|
/** set up a demo environment with predefined diffuse and normal maps, as well as texture coordinates */
|
|
|
|
void setUpDemo();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~BumpMapping() {}
|
|
|
|
BumpMapping &operator=(const BumpMapping &) { return *this; }
|
|
|
|
|
|
|
|
bool define_techniques();
|
|
|
|
|
|
|
|
private:
|
2005-04-29 14:32:13 +08:00
|
|
|
int _lightnum;
|
|
|
|
int _diffuse_unit;
|
|
|
|
int _normal_unit;
|
|
|
|
osg::ref_ptr<osg::Texture2D> _diffuse_tex;
|
|
|
|
osg::ref_ptr<osg::Texture2D> _normal_tex;
|
2003-09-02 05:53:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// INLINE METHODS
|
|
|
|
|
|
|
|
inline int BumpMapping::getLightNumber() const
|
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
return _lightnum;
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void BumpMapping::setLightNumber(int n)
|
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
_lightnum = n;
|
2003-09-02 05:53:53 +08:00
|
|
|
dirtyTechniques();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int BumpMapping::getDiffuseTextureUnit() const
|
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
return _diffuse_unit;
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void BumpMapping::setDiffuseTextureUnit(int n)
|
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
_diffuse_unit = n;
|
2003-11-28 06:06:11 +08:00
|
|
|
dirtyTechniques();
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int BumpMapping::getNormalMapTextureUnit() const
|
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
return _normal_unit;
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void BumpMapping::setNormalMapTextureUnit(int n)
|
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
_normal_unit = n;
|
2003-11-28 06:06:11 +08:00
|
|
|
dirtyTechniques();
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 14:32:13 +08:00
|
|
|
inline osg::Texture2D* BumpMapping::getOverrideDiffuseTexture()
|
2003-09-02 05:53:53 +08:00
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
return _diffuse_tex.get();
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 14:32:13 +08:00
|
|
|
inline const osg::Texture2D* BumpMapping::getOverrideDiffuseTexture() const
|
2003-09-02 05:53:53 +08:00
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
return _diffuse_tex.get();
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 14:32:13 +08:00
|
|
|
inline void BumpMapping::setOverrideDiffuseTexture(osg::Texture2D* texture)
|
2003-09-02 05:53:53 +08:00
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
_diffuse_tex = texture;
|
2003-11-28 06:06:11 +08:00
|
|
|
dirtyTechniques();
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 14:32:13 +08:00
|
|
|
inline osg::Texture2D* BumpMapping::getOverrideNormalMapTexture()
|
2003-09-02 05:53:53 +08:00
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
return _normal_tex.get();
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 14:32:13 +08:00
|
|
|
inline const osg::Texture2D* BumpMapping::getOverrideNormalMapTexture() const
|
2003-09-02 05:53:53 +08:00
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
return _normal_tex.get();
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 14:32:13 +08:00
|
|
|
inline void BumpMapping::setOverrideNormalMapTexture(osg::Texture2D* texture)
|
2003-09-02 05:53:53 +08:00
|
|
|
{
|
2005-04-29 14:32:13 +08:00
|
|
|
_normal_tex = texture;
|
2003-11-28 06:06:11 +08:00
|
|
|
dirtyTechniques();
|
2003-09-02 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|