BumpMapping 6.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * 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
{

    /**
     This effect makes surfaces appear bumpy. Children nodes must use two textures, 
     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 
     can be done quickly by calling BumpMapping::prepareChildren(). Note that both 
     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();
        BumpMapping(const BumpMapping& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);

        META_Effect(osgFX, BumpMapping, 
        
            "Bump Mapping", 
            
            "This effect makes surfaces appear bumpy. Children nodes must use two textures, "
            "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 "
            "can be done quickly by calling BumpMapping::prepareChildren(). Note that both "
            "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);
        
        /** get the diffuse color texture that overrides children's texture */
        inline osg::Texture2D* getOverrideDiffuseTexture();
        
        /** get the const diffuse color texture that overrides children's texture */
        inline const osg::Texture2D* getOverrideDiffuseTexture() const;
        
        /** set the diffuse color texture that overrides children's texture */
        inline void setOverrideDiffuseTexture(osg::Texture2D* texture);
        
        /** get the normal map texture that overrides children's texture */
        inline osg::Texture2D* getOverrideNormalMapTexture();
        
        /** get the const normal map texture that overrides children's texture */
        inline const osg::Texture2D* getOverrideNormalMapTexture() const;

        /** set the normal map texture that overrides children's texture */
        inline void setOverrideNormalMapTexture(osg::Texture2D* texture);
        
        /**
         prepare a Geometry for bump lighting. Tangent-space basis vectors are
         generated and attached to the geometry as vertex attribute arrays.
         */
        void prepareGeometry(osg::Geometry* geo);
        
        /** prepare a Node for bump lighting, calling prepareGeometry() for each Geometry */
        void prepareNode(osg::Node* node);
        
        /** prepare children for bump lighting. Actually calls prepareNode() for each child */
        void prepareChildren();
        
        /** 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:
        int _lightnum;
        int _diffuse_unit;
        int _normal_unit;
        osg::ref_ptr<osg::Texture2D> _diffuse_tex;
        osg::ref_ptr<osg::Texture2D> _normal_tex;
    };

    // INLINE METHODS    
   
    inline int BumpMapping::getLightNumber() const
    {
        return _lightnum;
    }
    
    inline void BumpMapping::setLightNumber(int n)
    {
        _lightnum = n;
        dirtyTechniques();
    }
    
    inline int BumpMapping::getDiffuseTextureUnit() const
    {
        return _diffuse_unit;
    }
    
    inline void BumpMapping::setDiffuseTextureUnit(int n)
    {
        _diffuse_unit = n;
        dirtyTechniques();
    }
    
    inline int BumpMapping::getNormalMapTextureUnit() const
    {
        return _normal_unit;
    }
    
    inline void BumpMapping::setNormalMapTextureUnit(int n)
    {
        _normal_unit = n;
        dirtyTechniques();
    }
    
    inline osg::Texture2D* BumpMapping::getOverrideDiffuseTexture()
    {
        return _diffuse_tex.get();
    }

    inline const osg::Texture2D* BumpMapping::getOverrideDiffuseTexture() const
    {
        return _diffuse_tex.get();
    }
    
    inline void BumpMapping::setOverrideDiffuseTexture(osg::Texture2D* texture)
    {
        _diffuse_tex = texture;
        dirtyTechniques();
    }

    inline osg::Texture2D* BumpMapping::getOverrideNormalMapTexture()
    {
        return _normal_tex.get();
    }

    inline const osg::Texture2D* BumpMapping::getOverrideNormalMapTexture() const
    {
        return _normal_tex.get();
    }
    
    inline void BumpMapping::setOverrideNormalMapTexture(osg::Texture2D* texture)
    {
        _normal_tex = texture;
        dirtyTechniques();
    }

}

#endif