00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _SO_MATERIAL_ELEMENT
00026 #define _SO_MATERIAL_ELEMENT
00027
00028 #include <Inventor/SbBasic.h>
00029 #include <Inventor/SbColor.h>
00030 #include <Inventor/elements/SoReplacedElement.h>
00031 #include <Inventor/elements/SoSubElement.h>
00032 #include <Inventor/devices/SoCpuBufferObject.h>
00033 #include <math.h>
00034
00035 class SoMFFloat;
00036 class SoMFColor;
00037 class SbThreadMutex;
00038
00063 SoEXTENDER_Documented class SoMaterialElement : public SoReplacedElement {
00064 SO_ELEMENT_HEADER(SoMaterialElement);
00065
00066 public:
00067
00069 enum masks {
00071 COLOR_MATERIAL_MASK = 1 << 1,
00073 DIFFUSE_MASK = 1 << 2,
00075 AMBIENT_MASK = 1 << 3,
00077 EMISSIVE_MASK = 1 << 4,
00079 SPECULAR_MASK = 1 << 5,
00081 SHININESS_MASK = 1 << 6,
00083 TRANSPARENCY_MASK = 1 << 8,
00085 ALL_MASK = 0xFFFF,
00086 };
00087
00094 static void setDiffuse(SoState *state,
00095 SoNode *node,
00096 int32_t numColors,
00097 const SbColor *colors);
00104 static void setTransparency(SoState *state,
00105 SoNode *node,
00106 int32_t numTransp,
00107 const float *transp);
00108
00112 static void setPacked(SoState *state,
00113 SoNode *node,
00114 int32_t numColors,
00115 const uint32_t *colors);
00116
00123 static void setColorIndices(SoState *state,
00124 SoNode *node,
00125 int32_t numIndices,
00126 const int32_t *indices);
00127
00128 static void setAmbient(SoState *state, SoNode* node, const SbColor &color);
00129 static void setEmissive(SoState *state, SoNode* node, const SbColor &color);
00130 static void setSpecular(SoState *state, SoNode* node, const SbColor &color);
00131
00132 static void setShininess(SoState *state, SoNode* node, float value);
00133 static void setColorMaterial(SoState *state, SoNode* node, SbBool value);
00138 static SbColor getDiffuse(SoState *state, int index);
00144 static float getTransparency(SoState *, int index);
00145
00147 static SoCpuBufferObject* getPackedColors(SoState *state);
00148
00153 static const int32_t *getColorIndices(SoState *state);
00154 static int32_t getColorIndex(SoState *, int num);
00155 static const SbColor &getAmbient(SoState *);
00156 static const SbColor &getEmissive(SoState *);
00157 static const SbColor &getSpecular(SoState *);
00158 static float getShininess(SoState *);
00159 static SbBool getColorMaterial(SoState *);
00160
00164 static size_t getNumDiffuse(SoState* state);
00165
00169 static size_t getNumColorIndices(SoState* state);
00170
00174 int32_t getNumDiffuse() const {
00175 return m_materialProperties.numDiffuseColors;
00176 }
00180 static int32_t getNumTransparencies( SoState* state );
00181
00185 int32_t getNumColorIndices() const {
00186 return (m_materialProperties.colorIndices ? m_materialProperties.numDiffuseColors : 0);
00187 }
00188
00192 static bool isTransparent( SoState* state );
00193
00197 static float getDefaultAmbientIntensity() {
00198 return 0.2F;
00199 }
00200
00204 static SbColor getDefaultDiffuse() {
00205 return SbColor(0.8F, 0.8F, 0.8F);
00206 }
00210 static SbColor getDefaultAmbient() {
00211 return SbColor(0.2F, 0.2F, 0.2F);
00212 }
00216 static SbColor getDefaultSpecular() {
00217 return SbColor(0.0F, 0.0F, 0.0F);
00218 }
00222 static SbColor getDefaultEmissive() {
00223 return SbColor(0.0F, 0.0F, 0.0F);
00224 }
00228 static float getDefaultShininess() {
00229 return 0.2F;
00230 }
00234 static float getDefaultTransparency() {
00235 return 0.0F;
00236 }
00240 static int32_t getDefaultColorIndex() {
00241 return 1;
00242 }
00243
00245 virtual void push(SoState *state);
00247 virtual void pop( SoState* state, const SoElement* prevTopElement );
00248
00249 private:
00250
00252 virtual void commonInit();
00253
00255 virtual void init(SoState *state);
00256
00263 virtual SbBool matches(const SoElement *) const;
00264
00270 virtual SoElement* copyMatchInfo() const;
00271
00272 private:
00273
00274
00275 static void setMaterials(SoState *state,
00276 SoNode *node,
00277 uint32_t bitmask,
00278 const SoMFColor &diffuse,
00279 const SoMFFloat &transp,
00280 const SoMFColor &ambient,
00281 const SoMFColor &emissive,
00282 const SoMFColor &specular,
00283 const SoMFFloat &shininess);
00284
00285
00286 virtual void print(FILE *fp) const;
00287
00288
00289 static void initClass();
00290 static void exitClass();
00291
00292 static const SbColor* getDiffusePointer( SoState* state );
00293 static const int32_t* getColorIndexPointer( SoState* state );
00294 static const float* getTransparencyPointer( SoState* state );
00295
00296 static void setHasTransparency( SoState* state, SoNode* node, int hasTransparency );
00297
00298
00299
00300 static void setTransparencyType(SoState *state, int32_t type);
00301
00302 private:
00303
00304 struct Material
00305 {
00306 public:
00307 Material()
00308 {
00309
00310
00311 memset(this, 0, sizeof(Material));
00312 dirtyPackedColor = true;
00313 }
00314 const SbColor *diffuseColors;
00315 SoRef<SoCpuBufferObject> packedColors;
00316 const float *transparencies;
00317 const int32_t *colorIndices;
00318
00319 SbColor ambientColor;
00320 SbColor emissiveColor;
00321 SbColor specularColor;
00322 float shininess;
00323 int32_t colorMaterial;
00324
00325 int32_t numDiffuseColors;
00326 int32_t numTransparencies;
00327
00328
00329 bool dirtyPackedColor;
00330 };
00331 Material m_materialProperties;
00332
00333 virtual ~SoMaterialElement();
00334
00335
00336
00337 static SbColor defaultDiffuseColor;
00338 static float defaultTransparency;
00339 static int32_t defaultColorIndices;
00340 static SoCpuBufferObject* s_defaultPackedColorBuffer;
00341
00342
00343 int m_hasTransparency;
00344
00345 private:
00347 static SoMaterialElement *getWInstance(SoState *state, SoNode* node)
00348 {
00349 return getElement<SoMaterialElement>(state, node);
00350 }
00351
00353 SoCpuBufferObject* packDiffuseColors();
00354
00355 void setDiffuseElt(int32_t numColors, const SbColor *colors);
00356 void setColorIndexElt(int32_t numIndices, const int32_t *indices);
00357 void setTranspElt(int32_t numTrans, const float *trans);
00358 void setAmbientElt(const SbColor *color);
00359 void setEmissiveElt(const SbColor *color);
00360 void setSpecularElt(const SbColor *color);
00361 void setShininessElt(float value);
00362 void setColorMaterialElt(SbBool value);
00363 void setMaterialElt(uint32_t bitmask, const SoMFColor &, const SoMFFloat &,
00364 const SoMFColor &, const SoMFColor &, const SoMFColor &,
00365 const SoMFFloat &);
00366
00367 static const float S_SHINY_THRESHOLD;
00368 };
00369
00370 #endif
00371
00372