Open Inventor Reference
SoLazyElement.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * Further, this software is distributed without any warranty that it is
16  * free of the rightful claim of any third person regarding infringement
17  * or the like. Any license provided herein, whether implied or
18  * otherwise, applies only to this software file. Patent licenses, if
19  * any, provided herein do not apply to combinations of this program with
20  * other software, or any other product whatsoever.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27  * Mountain View, CA 94043, or:
28  *
29  * http://www.sgi.com
30  *
31  * For further information regarding this notice, see:
32  *
33  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34  *
35  */
36 
37 
38 /*
39  * Copyright (C) 1990,91 Silicon Graphics, Inc.
40  *
41  _______________________________________________________________________
42  ______________ S I L I C O N G R A P H I C S I N C . ____________
43  |
44  | $Revision: 1.1.1.1 $
45  |
46  | Description:
47  | This file defines the SoLazyElement and SoColorPacker classes.
48  |
49  | Author(s) : Alan Norton, Gavin Bell
50  |
51  ______________ S I L I C O N G R A P H I C S I N C . ____________
52  _______________________________________________________________________
53  */
54 
55 #ifndef _SO_LAZY_ELEMENT
56 #define _SO_LAZY_ELEMENT
57 
59 #include <Inventor/SbColor.h>
63 #include <math.h>
64 
65 class SoMFFloat; class SoMFColor; class SoColorPacker;
66 
83 
85 #define SO_LAZY_SHINY_THRESHOLD 0.005
86 
88 #define SO_LAZY_NUM_COMPONENTS 9
89 
90 typedef void SoDrawElementsCallback(void* userData, SoState* state, GLenum mode,
91  GLsizei count,
92  GLenum type,
93  const GLvoid * indices);
94 
95 typedef void SoDrawArraysCallback(void* userData, SoState* state, GLenum mode,
96  GLint first,
97  GLsizei count);
98 
100 
102 
103  public:
110  enum cases{
111  LIGHT_MODEL_CASE = 0,
112  COLOR_MATERIAL_CASE = 1,
113  DIFFUSE_CASE = 2,
114  AMBIENT_CASE = 3,
115  EMISSIVE_CASE = 4,
116  SPECULAR_CASE = 5,
117  SHININESS_CASE = 6,
118  BLENDING_CASE = 7,
119  TRANSPARENCY_CASE = 8
120  };
121  enum masks{
122  LIGHT_MODEL_MASK = 1<<LIGHT_MODEL_CASE,
123  COLOR_MATERIAL_MASK = 1<<COLOR_MATERIAL_CASE,
124  DIFFUSE_MASK = 1<<DIFFUSE_CASE,
125  AMBIENT_MASK = 1<<AMBIENT_CASE,
126  EMISSIVE_MASK = 1<<EMISSIVE_CASE,
127  SPECULAR_MASK = 1<<SPECULAR_CASE,
128  SHININESS_MASK = 1<<SHININESS_CASE,
129  TRANSPARENCY_MASK = 1<<TRANSPARENCY_CASE,
130  BLENDING_MASK = 1<<BLENDING_CASE,
131  ALL_MASK = (1<<SO_LAZY_NUM_COMPONENTS)-1
132  };
133 
135  enum LightModel {
136  BASE_COLOR = 0,
137  PHONG = 1
138  };
139 
141  virtual void init(SoState *state);
142 
144 
145  static void setDiffuse(SoState *state, SoNode *node, int32_t numColors,
146  const SbColor *colors, SoColorPacker *cPacker);
147  static void setTransparency(SoState *state, SoNode *node, int32_t numTransp,
148  const float *transp, SoColorPacker *cPacker);
149  static void setPacked(SoState *state, SoNode *node,
150  int32_t numColors, const uint32_t *colors);
151  static void setAmbient(SoState *state, const SbColor* color);
152  static void setEmissive(SoState *state, const SbColor* color);
153  static void setSpecular(SoState *state, const SbColor* color);
154  static void setShininess(SoState *state, float value);
155  static void setColorMaterial(SoState *state, bool value);
156  static void setBlending(SoState *state, bool value);
160  static void setOverrideBlending(SoState *state, bool flag);
161  static void setLightModel(SoState *state, const int32_t model);
162 
166  static const SbColor & getDiffuse(SoState* state, int index);
167  static float getTransparency(SoState*, int index);
168  static const uint32_t *getPackedColors(SoState*);
169  static const SbColor & getAmbient(SoState*);
170  static const SbColor & getEmissive(SoState*);
171  static const SbColor & getSpecular(SoState*);
172  static float getShininess(SoState*);
173  static bool getColorMaterial(SoState*);
174  static bool getBlending(SoState*);
176 
179  {return ivState.numDiffuseColors;}
181  {return ivState.numTransparencies;}
182  bool isPacked() const
183  {return ivState.packed;}
184  bool isTransparent() const
185  {return(ivState.packedTransparent ||
186  (ivState.numTransparencies > 1)||
187  (ivState.transparencies[0]>0.0));}
188 
198  { return (SoLazyElement *)
199  (state->getElementNoPush(classStackIndex));}
200 
203  {return SbColor(0.8f, 0.8f, 0.8f);}
205  {return SbColor(0.2f, 0.2f, 0.2f);}
207  {return SbColor(0.0f, 0.0f, 0.0f);}
209  {return SbColor(0.0f, 0.0f, 0.0f);}
210  static float getDefaultShininess()
211  {return 0.2f;}
213  {return (0xccccccff);}
214  static float getDefaultTransparency()
215  {return 0.0f;}
217  {return PHONG;}
218 
219  SoINTERNAL public:
220 
222  static void setMaterials(SoState *state, SoNode *node, uint32_t bitmask,
223  SoColorPacker *cPacker,
224  const SoMFColor& diffuse, const SoMFFloat& transp,
225  const SoMFColor& ambient, const SoMFColor& emissive,
226  const SoMFColor& specular, const SoMFFloat& shininess);
227 
229  virtual void push(SoState *state);
230 
234  virtual bool matches(const SoElement *) const;
235 
236  virtual SoElement *copyMatchInfo() const;
237 
240  {return (SoLazyElement *)
241  (state->getElement(classStackIndex));}
242 
244  virtual void print(FILE *fp) const;
245 
247  static void initClass();
248 
253  const uint32_t *getPackedPointer() const
254  {return ivState.packedColors;}
255 
256  const SbColor* getDiffusePointer() const
257  {return ivState.diffuseColors; }
258 
259  const float *getTransparencyPointer() const
260  {return ivState.transparencies;}
261 
264  static void setTransparencyType(SoState *state, int32_t type);
265 
267  static void setDrawElementsCallback(SoState* state, SoDrawElementsCallback* cb, void* userData);
268 
269  // Fixed positions for attributes, shaders need to use these fixed attribute locations:
271  ATTRIB_VERTEX = 0,
272  ATTRIB_NORMAL = 1,
273  ATTRIB_COLOR = 2,
274  ATTRIB_TEXCOORD = 3
275  };
276 
278  static void setUseVertexAttributes( SoState* state, bool flag );
280  static bool shouldUseVertexAttributes( SoState* state );
281 
283  static void setDrawArraysCallback(SoState* state, SoDrawArraysCallback* cb, void* userData);
284 
286  static void drawArrays(SoState* state, GLenum mode, GLint first, GLsizei count);
287 
289  static void drawElements(SoState* state, GLenum mode, GLsizei count,
290  GLenum type, const GLvoid * indices);
291 
293  OTHER_COLOR_MASK = AMBIENT_MASK|EMISSIVE_MASK|SPECULAR_MASK|SHININESS_MASK,
294  ALL_COLOR_MASK = OTHER_COLOR_MASK|DIFFUSE_MASK,
295  NO_COLOR_MASK = ALL_MASK & (~ALL_COLOR_MASK),
296  ALL_BUT_DIFFUSE_MASK = ALL_MASK &(~ DIFFUSE_MASK),
297  DIFFUSE_ONLY_MASK = ALL_MASK &(~ OTHER_COLOR_MASK)
298  };
299 
300  protected:
301 
305 
309 
311  struct ivStateStruct {
315 
319 
325  float shininess;
327  bool blending;
330 
333  bool packed;
338  const float *transparencies;
344 
346 
351  };
352 
354 
355  virtual ~SoLazyElement();
356 
361 
362 
365 
367  static float *defaultTransparency;
369 
372  static int getNumPatterns() { return 64; }
373 
374  private:
376  virtual void setDiffuseElt(SoNode*, int32_t numColors,
377  const SbColor *colors, SoColorPacker *cPacker);
378  virtual void setPackedElt( SoNode *, int32_t numColors,
379  const uint32_t *colors);
380  virtual void setTranspElt(SoNode *, int32_t numTrans,
381  const float *trans, SoColorPacker *cPacker);
382 
383  virtual void setTranspTypeElt(int32_t type);
384  virtual void setAmbientElt(const SbColor* color);
385  virtual void setEmissiveElt(const SbColor* color);
386  virtual void setSpecularElt(const SbColor* color);
387  virtual void setShininessElt(float value);
388  virtual void setColorMaterialElt(bool value);
389  virtual void setBlendingElt(bool value);
390  virtual void setLightModelElt(SoState *state, int32_t model);
391  virtual void setMaterialElt(SoNode *, uint32_t bitmask,
392  SoColorPacker *cPacker, const SoMFColor&, const SoMFFloat&,
393  const SoMFColor&, const SoMFColor&, const SoMFColor&, const SoMFFloat&);
394 
395 };
396 
405 
407  public:
410 
413 
415  { return packedColors;}
416 
418  { return (nodeId == diffuseNodeId);}
419 
421  { return (nodeId == transpNodeId);}
422 
423  void setNodeIds(SbNodeIdType diffNodeId, SbNodeIdType tNodeId)
424  {diffuseNodeId = diffNodeId; transpNodeId = tNodeId;}
425 
427  { return packedArraySize;}
428 
429  void reallocate(int32_t size);
430 
431  private:
433  SbNodeIdType transpNodeId;
434  SbNodeIdType diffuseNodeId;
436  uint32_t* packedColors;
438  int32_t packedArraySize;
439 };
440 
441 
442 #endif /* _SO_LAZY_ELEMENT */
443 
#define SoEXTENDER
Provide inline template functions for abs, swap, min, max, and clamp.
Definition: SbBasic.h:154
#define SoINTERNAL
Definition: SbBasic.h:155
size_t SbNodeIdType
Type of unique ids of SoNode, used in the notification/change detection handling.
Definition: SbBasic.h:90
#define INVENTOR_API
Disable some annoying warnings on MSVC 6.
Definition: SbSystem.h:81
unsigned int uint32_t
Definition: SbTypeDefs.h:44
signed int int32_t
Definition: SbTypeDefs.h:43
#define SO_LAZY_NUM_COMPONENTS
number of components (subelements) in this element:
Definition: SoLazyElement.h:88
void SoDrawArraysCallback(void *userData, SoState *state, GLenum mode, GLint first, GLsizei count)
Definition: SoLazyElement.h:95
void SoDrawElementsCallback(void *userData, SoState *state, GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
Definition: SoLazyElement.h:90
#define SO_ELEMENT_HEADER(className)
This one is for non-abstract element classes.
Definition: SoSubElement.h:112
Color vector class.
Definition: SbColor.h:84
This class is meant to be used by all property nodes that set either a diffuse color or transparency ...
bool transpMatch(SbNodeIdType nodeId)
bool diffuseMatch(SbNodeIdType nodeId)
uint32_t * getPackedColors() const
void reallocate(int32_t size)
SoColorPacker()
Constructor, makes a colorPacker with NULL packedColor pointer:
~SoColorPacker()
destructor, deletes packed color array
int32_t getSize()
void setNodeIds(SbNodeIdType diffNodeId, SbNodeIdType tNodeId)
This is the abstract base class for all state elements.
Definition: SoElement.h:96
Element that manages several properties such as colors, that need to be lazily tracked by GL.
Definition: SoLazyElement.h:99
virtual void registerRedundantSet(SoState *, uint32_t)
method to tell the cache that a redundant set was issued.
ivStateStruct ivState
static float getTransparency(SoState *, int index)
cases
Following masks and cases define the components of the lazy element.
int32_t getNumDiffuse() const
Methods to inquire about current colors:
static void setOverrideBlending(SoState *state, bool flag)
This method allows to enable overriding of the blending mode.
static void setPacked(SoState *state, SoNode *node, int32_t numColors, const uint32_t *colors)
static void setTransparencyType(SoState *state, int32_t type)
set method for transparency type is SoINTERNAL, because it should only be invoked by SoGLRenderAction
static SbColor getDefaultEmissive()
static SbColor getDefaultSpecular()
static SbColor getDefaultAmbient()
static SbColor * defaultDiffuseColor
store pointers to the default color, transp so that we can set point to them if no other color or tra...
bool isPacked() const
static SoLazyElement * getInstance(SoState *state)
Returns the top (current) instance of the element in the state Note that the cache dependencies assoc...
static void setBlending(SoState *state, bool value)
static uint32_t getDefaultPacked()
static void setEmissive(SoState *state, const SbColor *color)
virtual void init(SoState *state)
Initializes element.
static void setLightModel(SoState *state, const int32_t model)
static SoLazyElement * getWInstance(SoState *state)
Get a Writable instance, so will force a push if needed:
static void setShininess(SoState *state, float value)
static void setDrawArraysCallback(SoState *state, SoDrawArraysCallback *cb, void *userData)
Set a callback for drawElements().
static void setSpecular(SoState *state, const SbColor *color)
static void setDrawElementsCallback(SoState *state, SoDrawElementsCallback *cb, void *userData)
Set a callback for drawElements().
static void setTransparency(SoState *state, SoNode *node, int32_t numTransp, const float *transp, SoColorPacker *cPacker)
static float getDefaultShininess()
static const SbColor & getSpecular(SoState *)
const float * getTransparencyPointer() const
LightModel
Enum values to be used in setting/getting light model:
static const SbColor & getDiffuse(SoState *state, int index)
get() methods get value from Inventor state.
static bool shouldUseVertexAttributes(SoState *state)
Returns if vertex attributes should be used instead of old style glVertexPointer/glNormalPointer etc.
bool isTransparent() const
virtual void print(FILE *fp) const
Prints element (for debugging)
static int32_t getDefaultLightModel()
const uint32_t * getPackedPointer() const
Following SoINTERNAL get() methods do NOT cause cache dependency, should only be invoked by nodes tha...
virtual SoElement * copyMatchInfo() const
Create a copy that we can put in a cache used list and call matches() on later.
const SbColor * getDiffusePointer() const
virtual void registerGetDependence(SoState *, uint32_t)
method to register dependence due to get().
static void setColorMaterial(SoState *state, bool value)
virtual void push(SoState *state)
push (for non-GL elements)
static const uint32_t * getPackedColors(SoState *)
static const SbColor & getEmissive(SoState *)
static void setUseVertexAttributes(SoState *state, bool flag)
Sets if vertex attributes should be used instead of old style glVertexPointer/glNormalPointer etc.
static void setAmbient(SoState *state, const SbColor *color)
static void drawElements(SoState *state, GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
Draws the given data using glDrawElements() or via a callback if one has been set.
static bool getBlending(SoState *)
static void drawArrays(SoState *state, GLenum mode, GLint first, GLsizei count)
Draws the given data using glDrawArrays() or via a callback if one has been set.
static void setDiffuse(SoState *state, SoNode *node, int32_t numColors, const SbColor *colors, SoColorPacker *cPacker)
static set methods:
static void setMaterials(SoState *state, SoNode *node, uint32_t bitmask, SoColorPacker *cPacker, const SoMFColor &diffuse, const SoMFFloat &transp, const SoMFColor &ambient, const SoMFColor &emissive, const SoMFColor &specular, const SoMFFloat &shininess)
set method for use in SoMaterial nodes:
static bool getColorMaterial(SoState *)
static const SbColor & getAmbient(SoState *)
static float * defaultTransparency
static uint32_t * defaultPackedColor
static float getDefaultTransparency()
uint32_t invalidBits
This is more convenient here, but might logically be kept with SoGLLazyElement.
int32_t getNumTransparencies() const
static int32_t getLightModel(SoState *)
static int getNumPatterns()
Returns number of transparency levels supported with stipple patterns.
static void initClass()
Initializes the SoLazyElement class.
virtual bool matches(const SoElement *) const
note: matches, copyMatchinfo not used by this element.
virtual ~SoLazyElement()
static SbColor getDefaultDiffuse()
Specify inventor defaults for colors, etc.
static float getShininess(SoState *)
Multiple-value field containing any number of RGB colors stored as three floats.
Definition: SoMFColor.h:88
Multiple-value field containing any number of floating point values.
Definition: SoMFFloat.h:82
Abstract base class for all database nodes.
Definition: SoNode.h:103
An SoState collects and holds state while traversing a scene graph.
Definition: SoState.h:82
SoElement * getElementNoPush(int stackIndex) const
Internal-only, dangerous method that returns a writeable element without checking for state depth and...
Definition: SoState.h:135
SoElement * getElement(int stackIndex)
Returns a writable instance of the element on the top of the stack with the given index.
Struct to hold the inventor state:
SoDrawElementsCallback * drawElementsCallback
bool packed
following are not used for matching GL & IV, but must be copied on push:
SoDrawArraysCallback * drawArraysCallback
SbNodeIdType transpNodeId
for transparency, keep either nodeid, or 0 if opaque.
SbColor ambientColor
store a value of each color component; ambient, emissive, specular, shininess, or appropriate info to...
SbNodeIdType diffuseNodeId
Keep nodeID to compare diffuse GL and diffuse inventor state: 0 is initial value, 1 is invalid
const uint32_t * packedColors