interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/libs/vos/metaobjects/a3dl/material.hh

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001, 2002 Peter Amstutz
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020 
00021     Peter Amstutz <http://www.interreality.org>
00022 */
00023 #ifndef _MATERIAL_HH_
00024 #define _MATERIAL_HH_
00025 
00026 
00027 #include "a3dldefs.hh"
00028 #include <vos/vos/vos.hh>
00029 
00030 #ifndef A3DL_EXPORTS
00031 IMPORT_METAOBJECT_FACTORIES(A3DL_Material)
00032 #endif
00033 
00034 namespace A3DL
00035 {
00036     class TextureIterator;
00037     class Texture;
00038 
00039 
00040     /** @class Material material.hh vos/metaobjects/a3dl/material.hh
00041      *  @ingroup libmetaobject_a3dl
00042      *  Contains material (appearence) information for a 3D object.
00043      *  Materials hold one or more texture layers, solid colors,
00044      *  transparency value and various lighting parameters.
00045      *
00046      *  @todo Need to add functions for setting shading (CS mixmode |=
00047      *  CS_FX_GORAUD), keycolor (CS mixmode |= CS_FX_KEYCOLOR), tiling (CS
00048      *  mixmode |= CS_FX_TILING)
00049      */
00050     class A3DL_API Material : public VOS::MetaObject
00051     {
00052     public:
00053 
00054         /** Special blend modes for setBlendMode() and getBlendMode() */
00055         enum BlendMode {BLEND_ADD, BLEND_MULTIPLY, BLEND_DOUBLE_MULTIPLY, BLEND_NORMAL};
00056 
00057 
00058     public:
00059         Material(VOS::VobjectBase* superobject);
00060         virtual ~Material();
00061 
00062         static VOS::MetaObject* new_Material(VOS::VobjectBase* superobject, const std::string& type);
00063         virtual const std::string getVOSType();
00064 
00065         /** Get pointers to texture layers. */
00066         virtual TextureIterator getTextureLayers();
00067 
00068         /** Use a VUtil::vRef or call release on the returned object.
00069             @return 0 if idx is greater than the number of texture layers.
00070         */
00071         virtual VUtil::vRef<Texture> getTextureLayer(int idx);
00072 
00073         /** Insert a texture layer at the given index.
00074          *  Textures are applied to an object from lowest to highest index.
00075          */
00076         virtual void insertTextureLayer(int idx, Texture& t);
00077 
00078         /** Insert a new texture layer into this material at the given index,
00079          *  using the given file as the image backend source (using FileProperty)
00080          *  @see FileProperty
00081          *  @throw FileAccessError if there is an error opening the file
00082          */
00083         virtual VUtil::vRef<Texture> insertTextureLayerFromFile(int idx, const std::string& filename,
00084                                                               const std::string& datatype);
00085 
00086         /** Create a new Texture object using given image data and datatype
00087          * identifiel, and insert it into this material at the
00088          * given index.
00089          */
00090         virtual VUtil::vRef<Texture> insertTextureLayer(int idx, const std::string& data, const std::string& datatype, double alpha = 1.0, BlendMode blendMode = BLEND_NORMAL);
00091 
00092         virtual void replaceTextureLayer(int idx, Texture& t);
00093         virtual void replaceTextureLayer(int idx, Texture* t) {
00094             assert(t);
00095             replaceTextureLayer(idx, *t);
00096         }
00097         virtual void removeTextureLayer(int idx);
00098 
00099         /** Set the "base" color of the material: a color to use if there are no
00100          * texture layers.
00101          */
00102         virtual void setColor(float r, float g, float b);
00103 
00104         /** Get the "base" color of the material: a color to use if there are no
00105          * texture layers.
00106          */
00107         virtual void getColor(float& r, float& g, float& b);
00108 
00109         /** Convert an HTML-style hex triplet ("#RRGGBB") to three normalized
00110          * (0.0-1.0) floating-point values.
00111          * @todo Move this out into just the A3DL namespace?
00112          * */
00113         static void hexStringToFloats(const std::string& str, float& r, float& g, float& b);
00114 
00115         /** Convert three normalized floats (0.0-1.0) to an HTML-style hex
00116          *  tripplet ("#RRGGBB). If any input parameter is less than 0.0,
00117          *  it is considered to be 0. If any parameter is greater than 1.0,
00118          *  it is considered to be 1.
00119          * @todo Move this out into just the A3DL namespace?
00120          */
00121         static void floatsToHexString(float r, float g, float b, std::string& str);
00122 
00123         /** Set alpha transparency of the object.
00124             @param t    A value between 0 (fully transparent) and 1 (no
00125                         transparency). Some renderers may round to certain
00126                         values (e.g. 1.0, 0.75, 0.5, 0.25, 0.0)
00127                         If t is > 1, 1 will be used. If t < 0, 0 will be
00128                         used.
00129         */
00130         virtual void setAlpha(double t);
00131 
00132         /** Get alpha. Will be between 0 (fully transparent) and
00133             1 (no transparency)
00134             @throws Vobject:NoSuchObjectError if there is no "a3dl:alpha" object.
00135          */
00136         virtual double getAlpha();
00137 
00138         /** Get transparency property. */
00139         virtual VUtil::vRef<VOS::Property> getAlphaObj();
00140 
00141         /** Set transparency property object */
00142         virtual void setAlphaObj(VOS::Property* obj);
00143 
00144         /** Set special blend mode.
00145          *  These blend modes determine how the background
00146          *  affects the object's material.
00147          */
00148         virtual void setBlendMode(BlendMode mode);
00149 
00150         /** Get special blend mode.
00151          *  These blend modes determine how the background affects the
00152          *  object's material.
00153          *  @throws Vobject::NoSuchObjectError if there is no "a3dl:blend-mode"
00154          *  property.
00155          */
00156         virtual BlendMode getBlendMode();
00157 
00158         /** Get blend mode property object. */
00159         virtual VUtil::vRef<VOS::Property> getBlendModeObj();
00160 
00161         /** Set blend mode property object. */
00162         virtual void setBlendModeObj(VOS::Property* obj);
00163 
00164 
00165         /** Set blend color: this color modifies the blending of this object
00166          * against its background.
00167          */
00168         virtual void setBlendColor(float r, float g, float b);
00169 
00170         /** Get blend color
00171          * @throws Vobject::NoSuchObjectError if there is no "a3dl:blend-mode"
00172          * property.
00173          */
00174         virtual void getBlendColor(float& r, float& g, float& b);
00175 
00176 
00177         /** Set a3dl:blend-color property object */
00178         virtual void setBlendColorObj(VOS::Property* obj);
00179 
00180         /** Get a3dl:blende-color property object */
00181         virtual VUtil::vRef<VOS::Property> getBlendColorObj();
00182     };
00183 }
00184 
00185 namespace VUtil {
00186     template<> inline void iteratorReleaseItem(A3DL::Material** v)
00187     {
00188         (*v)->release();
00189     }
00190 }
00191 
00192 namespace A3DL {
00193     class MaterialIterator : public VUtil::Iterator<Material*>
00194     {
00195     public:
00196         MaterialIterator() : VUtil::Iterator<Material*>() { }
00197         MaterialIterator(const MaterialIterator& i) : VUtil::Iterator<Material*>(i) { }
00198         MaterialIterator(std::vector<Material*>& vec) {
00199             items->resize(vec.size());
00200             int c = 0;
00201             for(std::vector<Material*>::const_iterator i = vec.begin();
00202                 i != vec.end();
00203                 i++)
00204             {
00205                 (*i)->acquire();
00206                 (*items)[c] = (*i);
00207                 c++;
00208             }
00209         }
00210         VUtil::vRef<Material> operator*() {
00211             if(pos < items->size()) return VUtil::vRef<Material>((*items)[pos], true);
00212             else return VUtil::vRef<Material>();
00213         }
00214     };
00215 }
00216 
00217 #endif