Open Inventor Reference
MeVis/ThirdParty/Sources/Inventor/inventor/lib/database/include/Inventor/elements/SoLazyElement.h
Go to the documentation of this file.
00001 /*
00002  *
00003  *  Copyright (C) 2000 Silicon Graphics, Inc.  All Rights Reserved. 
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  Further, this software is distributed without any warranty that it is
00016  *  free of the rightful claim of any third person regarding infringement
00017  *  or the like.  Any license provided herein, whether implied or
00018  *  otherwise, applies only to this software file.  Patent licenses, if
00019  *  any, provided herein do not apply to combinations of this program with
00020  *  other software, or any other product whatsoever.
00021  * 
00022  *  You should have received a copy of the GNU Lesser General Public
00023  *  License along with this library; if not, write to the Free Software
00024  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025  *
00026  *  Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
00027  *  Mountain View, CA  94043, or:
00028  * 
00029  *  http://www.sgi.com 
00030  * 
00031  *  For further information regarding this notice, see: 
00032  * 
00033  *  http://oss.sgi.com/projects/GenInfo/NoticeExplan/
00034  *
00035  */
00036 
00037 
00038 /*
00039  * Copyright (C) 1990,91   Silicon Graphics, Inc.
00040  *
00041  _______________________________________________________________________
00042  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
00043  |
00044  |   $Revision: 1.1.1.1 $
00045  |
00046  |   Description:
00047  |      This file defines the SoLazyElement and SoColorPacker classes.
00048  |
00049  |   Author(s)          : Alan Norton, Gavin Bell
00050  |
00051  ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
00052  _______________________________________________________________________
00053  */
00054 
00055 #ifndef  _SO_LAZY_ELEMENT
00056 #define  _SO_LAZY_ELEMENT
00057 
00058 #include <Inventor/system/SbSystem.h>
00059 #include <Inventor/SbColor.h>
00060 #include <Inventor/elements/SoElement.h>
00061 #include <Inventor/elements/SoSubElement.h>
00062 #include <math.h>
00063 
00064 class SoMFFloat; class SoMFColor; class SoColorPacker;
00065 
00082 
00084 #define SO_LAZY_SHINY_THRESHOLD         0.005
00085 
00087 #define SO_LAZY_NUM_COMPONENTS          9
00088 
00089 SoEXTENDER class INVENTOR_API SoLazyElement : public SoElement {
00090 
00091     SO_ELEMENT_HEADER(SoLazyElement);
00092 
00093   public:   
00100     enum cases{ 
00101         LIGHT_MODEL_CASE        =   0,
00102         COLOR_MATERIAL_CASE     =   1,
00103         DIFFUSE_CASE            =   2, 
00104         AMBIENT_CASE            =   3,
00105         EMISSIVE_CASE           =   4,
00106         SPECULAR_CASE           =   5,
00107         SHININESS_CASE          =   6,
00108         BLENDING_CASE           =   7, 
00109         TRANSPARENCY_CASE       =   8
00110     }; 
00111     enum masks{
00112         LIGHT_MODEL_MASK        = 1<<LIGHT_MODEL_CASE, 
00113         COLOR_MATERIAL_MASK     = 1<<COLOR_MATERIAL_CASE,  
00114         DIFFUSE_MASK            = 1<<DIFFUSE_CASE, 
00115         AMBIENT_MASK            = 1<<AMBIENT_CASE, 
00116         EMISSIVE_MASK           = 1<<EMISSIVE_CASE,
00117         SPECULAR_MASK           = 1<<SPECULAR_CASE,
00118         SHININESS_MASK          = 1<<SHININESS_CASE,
00119         TRANSPARENCY_MASK       = 1<<TRANSPARENCY_CASE,
00120         BLENDING_MASK           = 1<<BLENDING_CASE, 
00121         ALL_MASK                = (1<<SO_LAZY_NUM_COMPONENTS)-1
00122     };
00123 
00125     enum LightModel {
00126         BASE_COLOR      = 0, 
00127         PHONG           = 1
00128     };
00129     
00131     virtual void        init(SoState *state);
00132 
00134  
00135     static void setDiffuse(SoState *state, SoNode *node, int32_t numColors, 
00136             const SbColor *colors, SoColorPacker *cPacker);                 
00137     static void setTransparency(SoState *state, SoNode *node, int32_t numTransp, 
00138             const float *transp, SoColorPacker *cPacker);                              
00139     static void         setPacked(SoState *state, SoNode *node,
00140             int32_t numColors, const uint32_t *colors);    
00141     static void         setColorIndices(SoState *state, SoNode *node, 
00142             int32_t numIndices, const int32_t *indices);                                                                      
00143     static void         setAmbient(SoState *state, const SbColor* color);                   
00144     static void         setEmissive(SoState *state, const SbColor* color);          
00145     static void         setSpecular(SoState *state, const SbColor* color);                  
00146     static void         setShininess(SoState *state, float value);                  
00147     static void         setColorMaterial(SoState *state, SbBool value);                     
00148     static void         setBlending(SoState *state,  SbBool value);             
00149     static void         setLightModel(SoState *state, const int32_t model);
00150     
00154     static const SbColor &      getDiffuse(SoState* state, int index);  
00155     static float                getTransparency(SoState*, int index);
00156     static const uint32_t       *getPackedColors(SoState*);       
00157     static const int32_t        *getColorIndices(SoState*);
00158     static int32_t              getColorIndex(SoState*, int num);
00159     static const SbColor &      getAmbient(SoState*);   
00160     static const SbColor &      getEmissive(SoState*);  
00161     static const SbColor &      getSpecular(SoState*);  
00162     static float                getShininess(SoState*);
00163     static SbBool               getColorMaterial(SoState*);
00164     static SbBool               getBlending(SoState*);
00165     static int32_t              getLightModel(SoState*);
00166     
00168     int32_t                     getNumDiffuse() const
00169         {return ivState.numDiffuseColors;} 
00170     int32_t                     getNumTransparencies() const
00171         {return ivState.numTransparencies;}
00172     int32_t                     getNumColorIndices() const
00173         {if (ivState.colorIndices) 
00174             return ivState.numDiffuseColors;
00175          else return 0;}
00176     SbBool                      isPacked() const
00177         {return ivState.packed;}                
00178     SbBool                      isTransparent() const
00179         {return(ivState.packedTransparent ||
00180             (ivState.numTransparencies > 1)||
00181             (ivState.transparencies[0]>0.0));}
00182     
00191     static  SoLazyElement * getInstance(SoState *state)
00192     {  return (SoLazyElement *) 
00193         (state->getElementNoPush(classStackIndex));}
00194          
00196     static SbColor      getDefaultDiffuse()
00197             {return SbColor(0.8f, 0.8f, 0.8f);}
00198     static SbColor      getDefaultAmbient()
00199             {return SbColor(0.2f, 0.2f, 0.2f);}
00200     static SbColor      getDefaultSpecular()
00201             {return SbColor(0.0f, 0.0f, 0.0f);}
00202     static SbColor      getDefaultEmissive()
00203             {return SbColor(0.0f, 0.0f, 0.0f);}
00204     static float        getDefaultShininess()
00205             {return 0.2f;}
00206     static uint32_t     getDefaultPacked()
00207             {return (0xccccccff);}          
00208     static float        getDefaultTransparency()
00209             {return 0.0f;}
00210     static int32_t      getDefaultLightModel()
00211             {return PHONG;}
00212     static int32_t      getDefaultColorIndex()
00213             {return 1;}
00214    
00215     SoINTERNAL public:
00216     
00218     static void setMaterials(SoState *state, SoNode *node, uint32_t bitmask,
00219         SoColorPacker *cPacker,   
00220         const SoMFColor& diffuse, const SoMFFloat& transp, 
00221         const SoMFColor& ambient, const SoMFColor& emissive, 
00222         const SoMFColor& specular, const SoMFFloat& shininess);
00223                           
00225     virtual void        push(SoState *state);
00226 
00230     virtual SbBool      matches(const SoElement *) const; 
00231         
00232     virtual SoElement   *copyMatchInfo() const; 
00233                 
00235     static SoLazyElement * getWInstance(SoState *state)
00236         {return (SoLazyElement *)
00237         (state->getElement(classStackIndex));}
00238     
00240     virtual void        print(FILE *fp) const;
00241 
00243     static void         initClass();
00244     
00249     const uint32_t *getPackedPointer() const
00250         {return ivState.packedColors;}
00251         
00252     const SbColor*      getDiffusePointer() const
00253         {return ivState.diffuseColors; }
00254          
00255     const int32_t       *getColorIndexPointer() const
00256         {return ivState.colorIndices;}
00257         
00258     const float         *getTransparencyPointer() const
00259         {return ivState.transparencies;}
00260         
00263     static void setTransparencyType(SoState *state, int32_t type);
00264 
00265     enum internalMasks{
00266         OTHER_COLOR_MASK        = AMBIENT_MASK|EMISSIVE_MASK|SPECULAR_MASK|SHININESS_MASK,   
00267         ALL_COLOR_MASK          = OTHER_COLOR_MASK|DIFFUSE_MASK, 
00268         NO_COLOR_MASK           = ALL_MASK & (~ALL_COLOR_MASK), 
00269         ALL_BUT_DIFFUSE_MASK    =       ALL_MASK &(~ DIFFUSE_MASK), 
00270         DIFFUSE_ONLY_MASK       =  ALL_MASK &(~ OTHER_COLOR_MASK)
00271         };
00272    
00273   protected:
00274 
00277     virtual void        registerRedundantSet(SoState *, uint32_t);
00278     
00281     virtual void        registerGetDependence(SoState *, uint32_t);             
00282      
00284 #ifdef WIN32
00285     struct ivStateStructName{ 
00286 #else
00287     struct {
00288 #endif
00289 
00290 
00291         uint32_t            diffuseNodeId;
00292         
00295         uint32_t            transpNodeId;
00296             
00299         SbColor             ambientColor;
00300         SbColor             emissiveColor;
00301         SbColor             specularColor;
00302         float               shininess;
00303         SbBool              colorMaterial;
00304         SbBool              blending;
00305         int32_t             lightModel;
00306         int32_t             stippleNum;
00307         
00310         SbBool              packed;
00311         SbBool              packedTransparent;
00312         int32_t             numDiffuseColors;
00313         int32_t             numTransparencies;
00314         const SbColor       *diffuseColors;
00315         const float         *transparencies;
00316         const uint32_t      *packedColors;
00317         const int32_t       *colorIndices;
00318         SbBool              transpType;
00319         uint32_t            cacheLevelSetBits;
00320         uint32_t            cacheLevelSendBits;
00321     } ivState;
00322 
00323     virtual ~SoLazyElement();
00324     
00328     uint32_t invalidBits;
00329  
00330   
00333     
00334     static SbColor      *defaultDiffuseColor;
00335     static float        *defaultTransparency;
00336     static int32_t      *defaultColorIndices;
00337     static uint32_t     *defaultPackedColor;
00338     
00341     static int          getNumPatterns()        { return 64; }
00342     
00343     private:
00345     virtual void        setDiffuseElt(SoNode*,  int32_t numColors, 
00346         const SbColor *colors, SoColorPacker *cPacker);   
00347     virtual void        setPackedElt( SoNode *, int32_t numColors, 
00348         const uint32_t *colors);
00349     virtual void        setColorIndexElt( SoNode*, int32_t numIndices, 
00350         const int32_t *indices);        
00351     virtual void        setTranspElt(SoNode *, int32_t numTrans, 
00352         const float *trans, SoColorPacker *cPacker);
00353         
00354     virtual void        setTranspTypeElt(int32_t type);
00355     virtual void        setAmbientElt(const SbColor* color);
00356     virtual void        setEmissiveElt(const SbColor* color);
00357     virtual void        setSpecularElt(const SbColor* color);
00358     virtual void        setShininessElt(float value);
00359     virtual void        setColorMaterialElt(SbBool value);
00360     virtual void        setBlendingElt(SbBool value);
00361     virtual void        setLightModelElt(SoState *state, int32_t model);
00362     virtual void        setMaterialElt(SoNode *, uint32_t bitmask, 
00363         SoColorPacker *cPacker, const SoMFColor&, const SoMFFloat&, 
00364         const SoMFColor&, const SoMFColor&, const SoMFColor&, const SoMFFloat&);
00365 
00366 };
00367 
00376 
00377 class INVENTOR_API SoColorPacker {
00378     public:
00380     SoColorPacker();
00381     
00383     ~SoColorPacker();
00384     
00385     uint32_t* getPackedColors() const
00386     { return packedColors;}
00387     
00388     SbBool diffuseMatch(uint32_t nodeId)
00389     { return (nodeId == diffuseNodeId);}
00390     
00391     SbBool transpMatch(uint32_t nodeId)
00392     { return (nodeId == transpNodeId);}
00393     
00394     void setNodeIds(uint32_t diffNodeId, uint32_t tNodeId)
00395     {diffuseNodeId = diffNodeId; transpNodeId = tNodeId;}
00396 
00397     int32_t getSize()
00398     { return packedArraySize;}
00399     
00400     void reallocate(int32_t size);
00401     
00402     private:
00404     uint32_t    transpNodeId;
00405     uint32_t    diffuseNodeId;
00407     uint32_t*   packedColors;
00409     int32_t     packedArraySize;
00410 };
00411     
00412 
00413 #endif /* _SO_LAZY_ELEMENT */
00414