1 #ifndef COIN_SOLAZYELEMENT_H
2 #define COIN_SOLAZYELEMENT_H
3 
4 /**************************************************************************\
5  * Copyright (c) Kongsberg Oil & Gas Technologies AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from
21  * this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 \**************************************************************************/
35 
36 #include <Inventor/elements/SoElement.h>
37 #include <Inventor/elements/SoSubElement.h>
38 #include <Inventor/SbColor.h>
39 
40 class SoMFFloat;
41 class SoMFColor;
42 class SoColorPacker;
43 class SoLazyElementP;
44 
45 #define SO_LAZY_SHINY_THRESHOLD 0.005f
46 
47 
48 class COIN_DLL_API SoLazyElement : public SoElement {
49   typedef SoElement inherited;
50 
51   SO_ELEMENT_HEADER(SoLazyElement);
52 
53 public:
54   static void initClass();
55 protected:
56   ~SoLazyElement();
57 public:
58   enum cases {
59     LIGHT_MODEL_CASE = 0,
60     COLOR_MATERIAL_CASE,
61     DIFFUSE_CASE,
62     AMBIENT_CASE,
63     EMISSIVE_CASE,
64     SPECULAR_CASE,
65     SHININESS_CASE,
66     BLENDING_CASE,
67     TRANSPARENCY_CASE,
68     VERTEXORDERING_CASE,
69     TWOSIDE_CASE,
70     CULLING_CASE,
71     SHADE_MODEL_CASE,
72     ALPHATEST_CASE,
73     GLIMAGE_CASE, // OBSOLETED
74     LAZYCASES_LAST // must be last
75   };
76   enum masks{
77     LIGHT_MODEL_MASK = 1 << LIGHT_MODEL_CASE,           // 0x0001
78     COLOR_MATERIAL_MASK = 1 << COLOR_MATERIAL_CASE,     // 0x0002
79     DIFFUSE_MASK = 1 << DIFFUSE_CASE,                   // 0x0004
80     AMBIENT_MASK = 1 << AMBIENT_CASE,                   // 0x0008
81     EMISSIVE_MASK = 1<<EMISSIVE_CASE,                   // 0x0010
82     SPECULAR_MASK = 1 << SPECULAR_CASE,                 // 0x0020
83     SHININESS_MASK = 1 << SHININESS_CASE,               // 0x0040
84     TRANSPARENCY_MASK = 1 << TRANSPARENCY_CASE,         // 0x0080
85     BLENDING_MASK = 1 << BLENDING_CASE,                 // 0x0100
86     VERTEXORDERING_MASK = 1 << VERTEXORDERING_CASE,     // 0x0200
87     TWOSIDE_MASK = 1 << TWOSIDE_CASE,                   // 0x0400
88     CULLING_MASK = 1 << CULLING_CASE,                   // 0x0800
89     SHADE_MODEL_MASK = 1 << SHADE_MODEL_CASE,           // 0x1000
90     ALPHATEST_MASK = 1 << ALPHATEST_CASE,               // 0x2000
91     GLIMAGE_MASK = 1 << GLIMAGE_CASE,                   // obsoleted
92     ALL_MASK = (1 << LAZYCASES_LAST)-1
93   };
94 
95   enum internalMasks{
96     OTHER_COLOR_MASK = AMBIENT_MASK|EMISSIVE_MASK|SPECULAR_MASK|SHININESS_MASK,
97     ALL_COLOR_MASK = OTHER_COLOR_MASK|DIFFUSE_MASK,
98     NO_COLOR_MASK = ALL_MASK & (~ALL_COLOR_MASK),
99     ALL_BUT_DIFFUSE_MASK = ALL_MASK &(~ DIFFUSE_MASK),
100     DIFFUSE_ONLY_MASK = ALL_MASK &(~ OTHER_COLOR_MASK)
101   };
102 
103   enum LightModel {
104     BASE_COLOR,
105     PHONG
106   };
107 
108   enum VertexOrdering {
109     CW,
110     CCW
111   };
112 
113   virtual void init(SoState *state);
114   virtual void push(SoState *state);
115   virtual SbBool matches(const SoElement *) const;
116   virtual SoElement *copyMatchInfo(void) const;
117 
118   static void setToDefault(SoState * state);
119   static void setDiffuse(SoState * state, SoNode * node, int32_t numcolors,
120                          const SbColor * colors, SoColorPacker * packer);
121   static void setTransparency(SoState *state, SoNode *node, int32_t numvalues,
122                               const float * transparency, SoColorPacker * packer);
123   static void setPacked(SoState * state, SoNode * node,
124                         int32_t numcolors, const uint32_t * colors,
125                         const SbBool packedtransparency = FALSE);
126   static void setColorIndices(SoState *state, SoNode *node,
127                               int32_t numindices, const int32_t *indices);
128   static void setAmbient(SoState *state, const SbColor * color);
129   static void setEmissive(SoState *state, const SbColor * color);
130   static void setSpecular(SoState *state, const SbColor * color);
131   static void setShininess(SoState *state, float value);
132   static void setColorMaterial(SoState *state, SbBool value);
133   static void enableBlending(SoState *state,
134                              int sfactor,
135                              int dfactor);
136   static void enableSeparateBlending(SoState *state,
137                                      int sfactor,
138                                      int dfactor,
139                                      int alpha_sfactor,
140                                      int alpha_dfactor);
141 
142   static void disableBlending(SoState * state);
143   static void setLightModel(SoState *state, const int32_t model);
144   static void setVertexOrdering(SoState * state, VertexOrdering ordering);
145   static void setBackfaceCulling(SoState * state, SbBool onoff);
146   static void setTwosideLighting(SoState * state, SbBool onoff);
147   static void setShadeModel(SoState * state, SbBool flatshading);
148   static void setAlphaTest(SoState * state, int func, float value);
149 
150   static const SbColor & getDiffuse(SoState* state, int index);
151   static float getTransparency(SoState*, int index);
152   static const uint32_t * getPackedColors(SoState*);
153   static const int32_t  * getColorIndices(SoState*);
154   static int32_t getColorIndex(SoState*, int num);
155   static const SbColor & getAmbient(SoState *);
156   static const SbColor & getEmissive(SoState *);
157   static const SbColor & getSpecular(SoState *);
158   static float getShininess(SoState*);
159   static SbBool getColorMaterial(SoState*);
160   static SbBool getBlending(SoState *,
161                             int & sfactor, int & dfactor);
162   static SbBool getAlphaBlending(SoState *,
163                                  int & sfactor, int & dfactor);
164 
165   static int32_t getLightModel(SoState*);
166   static int getAlphaTest(SoState * state, float & value);
167   static SbBool getTwoSidedLighting(SoState * state);
168 
169   int32_t getNumDiffuse(void) const;
170   int32_t getNumTransparencies(void) const;
171   int32_t getNumColorIndices(void) const;
172   SbBool isPacked(void) const;
173   SbBool isTransparent(void) const;
174   static SoLazyElement * getInstance(SoState *state);
175   static float getDefaultAmbientIntensity(void);
176 
177   static SbColor getDefaultDiffuse(void);
178   static SbColor getDefaultAmbient(void);
179   static SbColor getDefaultSpecular(void);
180   static SbColor getDefaultEmissive(void);
181   static float getDefaultShininess(void);
182   static uint32_t getDefaultPacked(void);
183   static float getDefaultTransparency(void);
184   static int32_t getDefaultLightModel(void);
185   static int32_t getDefaultColorIndex(void);
186 
187   static void setMaterials(SoState * state, SoNode *node, uint32_t bitmask,
188                            SoColorPacker * cPacker,
189                            const SbColor * diffuse,
190                            const int numdiffuse,
191                            const float * transp,
192                            const int numtransp,
193                            const SbColor & ambient,
194                            const SbColor & emissive,
195                            const SbColor & specular,
196                            const float shininess,
197                            const SbBool istransparent);
198 
199   static SoLazyElement * getWInstance(SoState *state);
200 
201   const uint32_t * getPackedPointer(void) const;
202   const SbColor * getDiffusePointer(void) const;
203   const int32_t * getColorIndexPointer(void) const;
204 
205   const float * getTransparencyPointer(void) const;
206   static void setTransparencyType(SoState * state, int32_t type);
207 
208 protected:
209 
210   struct COIN_DLL_API CoinState {
211     SbColor ambient;
212     SbColor specular;
213     SbColor emissive;
214     float shininess;
215     SbBool blending;
216     int blend_sfactor;
217     int blend_dfactor;
218     int alpha_blend_sfactor;
219     int alpha_blend_dfactor;
220     int32_t lightmodel;
221     SbBool packeddiffuse;
222     int32_t numdiffuse;
223     int32_t numtransp;
224     const SbColor * diffusearray;
225     const uint32_t * packedarray;
226     const float * transparray;
227     const int32_t * colorindexarray;
228     int32_t transptype;
229     SbBool istransparent;
230     SbUniqueId diffusenodeid;
231     SbUniqueId transpnodeid;
232     int32_t stipplenum;
233     VertexOrdering vertexordering;
234     SbBool twoside;
235     SbBool culling;
236     SbBool flatshading;
237     int alphatestfunc;
238     float alphatestvalue;
239   } coinstate;
240 
241 protected:
242   virtual void lazyDidSet(uint32_t mask);
243   virtual void lazyDidntSet(uint32_t mask);
244 
245   virtual void setDiffuseElt(SoNode*,  int32_t numcolors,
246                              const SbColor * colors, SoColorPacker * packer);
247   virtual void setPackedElt(SoNode * node, int32_t numcolors,
248                             const uint32_t * colors, const SbBool packedtransparency);
249   virtual void setColorIndexElt(SoNode * node, int32_t numindices,
250                                 const int32_t * indices);
251   virtual void setTranspElt(SoNode * node, int32_t numtransp,
252                             const float * transp, SoColorPacker * packer);
253 
254   virtual void setTranspTypeElt(int32_t type);
255   virtual void setAmbientElt(const SbColor* color);
256   virtual void setEmissiveElt(const SbColor* color);
257   virtual void setSpecularElt(const SbColor* color);
258   virtual void setShininessElt(float value);
259   virtual void setColorMaterialElt(SbBool value);
260   virtual void enableBlendingElt(int sfactor, int dfactor, int alpha_sfactor, int alpha_dfactor);
261   virtual void disableBlendingElt(void);
262   virtual void setLightModelElt(SoState *state, int32_t model);
263   virtual void setMaterialElt(SoNode * node, uint32_t bitmask,
264                               SoColorPacker * packer,
265                               const SbColor * diffuse, const int numdiffuse,
266                               const float * transp, const int numtransp,
267                               const SbColor & ambient,
268                               const SbColor & emissive,
269                               const SbColor & specular,
270                               const float shininess,
271                               const SbBool istransparent);
272   virtual void setVertexOrderingElt(VertexOrdering ordering);
273   virtual void setBackfaceCullingElt(SbBool onoff);
274   virtual void setTwosideLightingElt(SbBool onoff);
275   virtual void setShadeModelElt(SbBool flatshading);
276   virtual void setAlphaTestElt(int func, float value);
277 
278 private:
279   SoLazyElementP * pimpl; // for future use
280 
281 };
282 
283 class COIN_DLL_API SoColorPacker {
284 public:
285   SoColorPacker(void);
286   ~SoColorPacker();
287 
getPackedColors(void)288   uint32_t * getPackedColors(void) const {
289     return this->array;
290   }
diffuseMatch(const SbUniqueId nodeid)291   SbBool diffuseMatch(const SbUniqueId nodeid) const {
292     return nodeid == this->diffuseid;
293   }
transpMatch(const SbUniqueId nodeid)294   SbBool transpMatch(const SbUniqueId nodeid) const {
295     return nodeid == this->transpid;
296   }
setNodeIds(const SbUniqueId diffuse,const SbUniqueId transp)297   void setNodeIds(const SbUniqueId diffuse, const SbUniqueId transp) {
298     this->diffuseid = diffuse;
299     this->transpid = transp;
300   }
getSize(void)301   int32_t getSize(void) const {
302     return this->arraysize;
303   }
304   void reallocate(const int32_t size);
305 
getDiffuseId(void)306   SbUniqueId getDiffuseId(void) const {
307     return this->diffuseid;
308   }
getTranspId(void)309   SbUniqueId getTranspId(void) const {
310     return this->transpid;
311   }
312 private:
313   SbUniqueId transpid;
314   SbUniqueId diffuseid;
315   uint32_t * array;
316   int32_t arraysize;
317 };
318 
319 #endif // !COIN_SOLAZYELEMENT_H
320