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 SoGLDrawStyleElement Inventor/elements/SoGLDrawStyleElement.h
35   \brief The SoGLDrawStyleElement updates the current draw style in OpenGL.
36 
37   \ingroup elements
38 */
39 
40 #include <Inventor/elements/SoGLDrawStyleElement.h>
41 #include "coindefs.h"
42 
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif // HAVE_CONFIG_H
46 
47 #include <Inventor/system/gl.h>
48 
49 #include <cassert>
50 
51 SO_ELEMENT_SOURCE(SoGLDrawStyleElement);
52 
53 // doc in superclass
54 void
initClass(void)55 SoGLDrawStyleElement::initClass(void)
56 {
57   SO_ELEMENT_INIT_CLASS(SoGLDrawStyleElement, inherited);
58 }
59 
~SoGLDrawStyleElement(void)60 SoGLDrawStyleElement::~SoGLDrawStyleElement(void)
61 {
62 }
63 
64 // doc in superclass
65 void
init(SoState * state)66 SoGLDrawStyleElement::init(SoState * state)
67 {
68   inherited::init(state);
69 }
70 
71 // doc in superclass
72 void
push(SoState * state)73 SoGLDrawStyleElement::push(SoState * state)
74 {
75   SoGLDrawStyleElement * prev = (SoGLDrawStyleElement*)
76     this->getNextInStack();
77   // copy data to avoid unessesary GL calls
78   this->data = prev->data;
79   // capture previous element since we might or might not change the
80   // GL state in set/pop
81   prev->capture(state);
82 }
83 
84 // doc in superclass
85 void
pop(SoState * COIN_UNUSED_ARG (state),const SoElement * prevTopElement)86 SoGLDrawStyleElement::pop(SoState * COIN_UNUSED_ARG(state),
87                           const SoElement * prevTopElement)
88 {
89   SoGLDrawStyleElement * prev = (SoGLDrawStyleElement*) prevTopElement;
90   if (this->data != prev->data) this->updategl();
91 }
92 
93 // doc in superclass
94 void
setElt(int32_t style)95 SoGLDrawStyleElement::setElt(int32_t style)
96 {
97   if (style != this->data) {
98     this->data = (int32_t)style;
99     this->updategl();
100   }
101 }
102 
103 void
updategl(void)104 SoGLDrawStyleElement::updategl(void)
105 {
106   switch ((Style)this->data) {
107   case SoDrawStyleElement::FILLED:
108     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
109     break;
110   case SoDrawStyleElement::LINES:
111     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
112     break;
113   case SoDrawStyleElement::POINTS:
114     glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
115     break;
116   case SoDrawStyleElement::INVISIBLE:
117     // handled in SoShape::shouldGLRender()
118     break;
119   default:
120     assert(0 && "unsupported switch case");
121     break;
122   }
123 }
124