1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
13//osgFX - Copyright (C) 2003 Marco Jez
15#ifndef OSGFX_BUMPMAPPING_
16#define OSGFX_BUMPMAPPING_
18#include <osgFX/Export>
19#include <osgFX/Effect>
22#include <osg/Texture2D>
28 This effect makes surfaces appear bumpy. Children nodes must use two textures,
29 one for diffuse color and one for the normal map (which can be created
30 from a height map with tools like nVIDIA's normal map generator). Furthermore,
31 tangent-space basis vectors must be created and assigned to each Geometry; this
32 can be done quickly by calling BumpMapping::prepareChildren(). Note that both
33 diffuse and normal map textures must have corresponding UV maps defined in
35 This effect defines a preferred technique which uses ARB vertex & fragment
36 programs, and a fallback technique which doesn't use fragment programs. The
37 latter is more limited though since it can't handle ambient and specular
40 class OSGFX_EXPORT BumpMapping: public Effect {
43 BumpMapping(const BumpMapping& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
45 META_Effect(osgFX, BumpMapping,
49 "This effect makes surfaces appear bumpy. Children nodes must use two textures, "
50 "one for diffuse color and one for the normal map (which can be created "
51 "from a height map with tools like nVIDIA's normal map generator). Furthermore, "
52 "tangent-space basis vectors must be created and assigned to each Geometry; this "
53 "can be done quickly by calling BumpMapping::prepareChildren(). Note that both "
54 "diffuse and normal map textures must have corresponding UV maps defined in "
56 "This effect defines a preferred technique which uses ARB vertex & fragment "
57 "programs, and a fallback technique which doesn't use fragment programs. The "
58 "latter is more limited though since it can't handle ambient and specular "
64 /** get the OpenGL light number */
65 inline int getLightNumber() const;
67 /** set the OpenGL light number that will be used in lighting computations */
68 inline void setLightNumber(int n);
70 /** get the texture unit that contains diffuse color texture. Default is 1 */
71 inline int getDiffuseTextureUnit() const;
73 /** set the texture unit that contains diffuse color texture. Default is 1 */
74 inline void setDiffuseTextureUnit(int n);
76 /** get the texture unit that contains normal map texture. Default is 0 */
77 inline int getNormalMapTextureUnit() const;
79 /** set the texture unit that contains normal map texture. Default is 0 */
80 inline void setNormalMapTextureUnit(int n);
82 /** get the diffuse color texture that overrides children's texture */
83 inline osg::Texture2D* getOverrideDiffuseTexture();
85 /** get the const diffuse color texture that overrides children's texture */
86 inline const osg::Texture2D* getOverrideDiffuseTexture() const;
88 /** set the diffuse color texture that overrides children's texture */
89 inline void setOverrideDiffuseTexture(osg::Texture2D* texture);
91 /** get the normal map texture that overrides children's texture */
92 inline osg::Texture2D* getOverrideNormalMapTexture();
94 /** get the const normal map texture that overrides children's texture */
95 inline const osg::Texture2D* getOverrideNormalMapTexture() const;
97 /** set the normal map texture that overrides children's texture */
98 inline void setOverrideNormalMapTexture(osg::Texture2D* texture);
101 prepare a Geometry for bump lighting. Tangent-space basis vectors are
102 generated and attached to the geometry as vertex attribute arrays.
104 void prepareGeometry(osg::Geometry* geo);
106 /** prepare a Node for bump lighting, calling prepareGeometry() for each Geometry */
107 void prepareNode(osg::Node* node);
109 /** prepare children for bump lighting. Actually calls prepareNode() for each child */
110 void prepareChildren();
112 /** set up a demo environment with predefined diffuse and normal maps, as well as texture coordinates */
116 virtual ~BumpMapping() {}
117 BumpMapping &operator=(const BumpMapping &) { return *this; }
119 bool define_techniques();
125 osg::ref_ptr<osg::Texture2D> _diffuse_tex;
126 osg::ref_ptr<osg::Texture2D> _normal_tex;
131 inline int BumpMapping::getLightNumber() const
136 inline void BumpMapping::setLightNumber(int n)
142 inline int BumpMapping::getDiffuseTextureUnit() const
144 return _diffuse_unit;
147 inline void BumpMapping::setDiffuseTextureUnit(int n)
153 inline int BumpMapping::getNormalMapTextureUnit() const
158 inline void BumpMapping::setNormalMapTextureUnit(int n)
164 inline osg::Texture2D* BumpMapping::getOverrideDiffuseTexture()
166 return _diffuse_tex.get();
169 inline const osg::Texture2D* BumpMapping::getOverrideDiffuseTexture() const
171 return _diffuse_tex.get();
174 inline void BumpMapping::setOverrideDiffuseTexture(osg::Texture2D* texture)
176 _diffuse_tex = texture;
180 inline osg::Texture2D* BumpMapping::getOverrideNormalMapTexture()
182 return _normal_tex.get();
185 inline const osg::Texture2D* BumpMapping::getOverrideNormalMapTexture() const
187 return _normal_tex.get();
190 inline void BumpMapping::setOverrideNormalMapTexture(osg::Texture2D* texture)
192 _normal_tex = texture;