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 const SbColor* getDiffusePointer() const;
00294
00295 static const int32_t* getColorIndexPointer( SoState* state );
00296 static const float* getTransparencyPointer( SoState* state );
00297 const float* getTransparencyPointer() const;
00298
00299 static void setHasTransparency( SoState* state, SoNode* node, int hasTransparency );
00300
00301 static const SoMaterialElement* getInstance( SoState* state );
00302
00303 const SbColor& getAmbient() const;
00304 const SbColor& getEmissive() const;
00305 const SbColor& getSpecular() const;
00306 float getShininess() const;
00307 int32_t getNumTransparencies() const;
00308 bool isTransparent() const;
00309 SoCpuBufferObject* getPackedColors() const;
00310 bool isPacked() const
00311 {
00312 return m_materialProperties.packedColors.ptr() != nullptr;
00313 }
00314
00315 private:
00316
00317 struct Material
00318 {
00319 public:
00320 Material()
00321 {
00322
00323
00324 memset(this, 0, sizeof(Material));
00325 dirtyPackedColor = true;
00326 packedColorExplicitelySet = false;
00327 }
00328 const SbColor *diffuseColors;
00329 SoRef<SoCpuBufferObject> packedColors;
00330 const float *transparencies;
00331 const int32_t *colorIndices;
00332
00333 SbColor ambientColor;
00334 SbColor emissiveColor;
00335 SbColor specularColor;
00336 float shininess;
00337 int32_t colorMaterial;
00338
00339 int32_t numDiffuseColors;
00340 int32_t numTransparencies;
00341
00342
00343 bool dirtyPackedColor : 1;
00344
00345 bool packedColorExplicitelySet : 1;
00346 };
00347 Material m_materialProperties;
00348
00349 virtual ~SoMaterialElement();
00350
00351
00352
00353 static SbColor defaultDiffuseColor;
00354 static float defaultTransparency;
00355 static int32_t defaultColorIndices;
00356 static SoCpuBufferObject* s_defaultPackedColorBuffer;
00357
00358
00359 int m_hasTransparency;
00360
00361 private:
00366 static SoMaterialElement* getWInstance( SoState* state, SoNode* node = nullptr );
00367
00369 SoCpuBufferObject* packDiffuseColors();
00370
00371 void setDiffuseElt(int32_t numColors, const SbColor *colors);
00372 void setColorIndexElt(int32_t numIndices, const int32_t *indices);
00373 void setTranspElt(int32_t numTrans, const float *trans);
00374 void setAmbientElt(const SbColor *color);
00375 void setEmissiveElt(const SbColor *color);
00376 void setSpecularElt(const SbColor *color);
00377 void setShininessElt(float value);
00378 void setColorMaterialElt(SbBool value);
00379 void setMaterialElt(uint32_t bitmask, const SoMFColor &, const SoMFFloat &,
00380 const SoMFColor &, const SoMFColor &, const SoMFColor &,
00381 const SoMFFloat &);
00382
00383 static const float S_SHINY_THRESHOLD;
00384 };
00385
00386 #endif
00387
00388