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 SoGLLinePatternElement Inventor/elements/SoGLLinePatternElement.h
35   \brief The SoGLLinePatternElement class changes the line stipple pattern
36          of the OpenGL render state.
37 
38   \ingroup elements
39 
40   Requests from the scenegraph to change the stipple pattern when rendering
41   OpenGL line primitives will be made through this element, which forwards
42   it to the appropriate native OpenGL call.
43 */
44 
45 #include <Inventor/elements/SoGLLinePatternElement.h>
46 #include "coindefs.h"
47 
48 
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif // HAVE_CONFIG_H
52 
53 #include <Inventor/system/gl.h>
54 
55 #include <cassert>
56 
57 SO_ELEMENT_SOURCE(SoGLLinePatternElement);
58 
59 /*!
60   This static method initializes static data for the
61   SoGLLinePatternElement class.
62 */
63 
64 void
initClass(void)65 SoGLLinePatternElement::initClass(void)
66 {
67   SO_ELEMENT_INIT_CLASS(SoGLLinePatternElement, inherited);
68 }
69 
70 /*!
71   The destructor.
72 */
73 
~SoGLLinePatternElement(void)74 SoGLLinePatternElement::~SoGLLinePatternElement(void)
75 {
76 }
77 
78 /*!
79   Initializes element in state to default value.
80 */
81 
82 void
init(SoState * state)83 SoGLLinePatternElement::init(SoState * state)
84 {
85   inherited::init(state);
86 }
87 
88 /*!
89   Creates new element in stack.
90 */
91 
92 void
push(SoState * state)93 SoGLLinePatternElement::push(SoState * state)
94 {
95   SoGLLinePatternElement * prev = (SoGLLinePatternElement*)
96     this->getNextInStack();
97 
98   this->data = prev->data;
99   // capture element since we might or might not change the GL state
100   prev->capture(state);
101 }
102 
103 /*!
104   Removes element from stack.
105 */
106 
107 void
pop(SoState * COIN_UNUSED_ARG (state),const SoElement * prevTopElement)108 SoGLLinePatternElement::pop(SoState * COIN_UNUSED_ARG(state),
109                             const SoElement * prevTopElement)
110 {
111   SoGLLinePatternElement * prev = (SoGLLinePatternElement*) prevTopElement;
112   if (this->data != prev->data) this->updategl();
113 }
114 
115 //! Called whenever element value is set. Triggers GL update.
116 
117 void
setElt(int32_t pattern)118 SoGLLinePatternElement::setElt(int32_t pattern)
119 {
120   if (pattern != this->data) {
121     this->data = pattern;
122     this->updategl();
123   }
124 }
125 
126 /*!
127   Applies line stipple patterns to OpenGL state.
128 */
129 
130 void
updategl()131 SoGLLinePatternElement::updategl()
132 {
133   //
134   // FIXME: store flag to keep enable/disable state, pederb 990624
135   //
136   if ((this->data & 0xffff) == (int32_t) CONTINUOUS) {
137     glDisable(GL_LINE_STIPPLE);
138   }
139   else {
140     // Enable line stipple before setting the pattern. This is
141     // needed to work around a bug in the nVidia 2.1.1 drivers.
142     glEnable(GL_LINE_STIPPLE);
143     glLineStipple((GLint) (this->data >> 16), (GLushort) (this->data & 0xffff));
144   }
145 }
146