1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 /*!
34   \class SoGLColorIndexElement Inventor/elements/SoGLColorIndexElement.h
35   \brief The SoGLColorIndexElement class sets the current OpenGL color.
36 
37   \ingroup elements
38 
39   This element is only used when the OpenGL canvas is in colorindex
40   mode, ie where colors for individual pixels are fetched from a color
41   lookup table ("CLUT"). The usual thing to do is to set up a canvas
42   in RGBA truecolor mode.
43 
44   One common use for colorindex mode OpenGL canvases is to use one in
45   the overlay planes (which are usually limited to only 2 or 4
46   available colors), if supported by the OpenGL hardware and / or
47   driver.
48 
49   \sa SoColorIndex
50 */
51 
52 #include <Inventor/elements/SoGLColorIndexElement.h>
53 #include <Inventor/elements/SoGLLazyElement.h>
54 #include <Inventor/misc/SoState.h>
55 #include <cassert>
56 
57 #ifdef HAVE_CONFIG_H
58 #include <config.h>
59 #endif // HAVE_CONFIG_H
60 
61 #include <Inventor/system/gl.h>
62 
63 SO_ELEMENT_SOURCE(SoGLColorIndexElement);
64 
65 // doc in parent
66 void
initClass(void)67 SoGLColorIndexElement::initClass(void)
68 {
69   SO_ELEMENT_INIT_CLASS(SoGLColorIndexElement, inherited);
70 }
71 
72 
73 // doc in parent
74 void
init(SoState * stateptr)75 SoGLColorIndexElement::init(SoState * stateptr)
76 {
77   inherited::init(state);
78   this->state = stateptr;
79 }
80 
81 /*!
82   The destructor.
83 */
~SoGLColorIndexElement()84 SoGLColorIndexElement::~SoGLColorIndexElement()
85 {
86 }
87 
88 /*!
89   Returns \c TRUE if the current GL context is in color index mode.
90 */
91 SbBool
isColorIndexMode(SoState * state)92 SoGLColorIndexElement::isColorIndexMode(SoState * state)
93 {
94   return SoGLLazyElement::isColorIndex(state);
95 }
96 
97 /*!
98   Sets current color indices.
99 */
100 void
set(SoState * const state,SoNode * const node,const int32_t numindices,const int32_t * const indices)101 SoGLColorIndexElement::set(SoState * const state, SoNode * const node,
102                            const int32_t numindices,
103                            const int32_t * const indices)
104 {
105   SoLazyElement::setColorIndices(state, node, numindices, indices);
106 }
107 
108 /*!
109   Returns number of color indices in element.
110 */
111 int32_t
getNum(void) const112 SoGLColorIndexElement::getNum(void) const
113 {
114   return SoLazyElement::getInstance(this->state)->getNumColorIndices();
115 }
116 
117 /*!
118   Returns the current element.
119 */
120 const SoGLColorIndexElement *
getInstance(SoState * state)121 SoGLColorIndexElement::getInstance(SoState *state)
122 {
123   return (const SoGLColorIndexElement *)
124     state->getElementNoPush(classStackIndex);
125 }
126 
127 int32_t
get(const int index) const128 SoGLColorIndexElement::get(const int index) const
129 {
130   assert(index >= 0 && index < this->getNum());
131   return SoLazyElement::getColorIndices(this->state)[index];
132 }
133 
134 int32_t
getDefault(void)135 SoGLColorIndexElement::getDefault(void)
136 {
137   return SoLazyElement::getDefaultColorIndex();
138 }
139