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 SoGLClipPlaneElement Inventor/elements/SoGLClipPlaneElement.h
35   \brief The SoGLClipPlaneElement class is yet to be documented.
36 
37   \ingroup elements
38 
39   FIXME: write doc.
40 */
41 
42 #include <Inventor/elements/SoGLClipPlaneElement.h>
43 
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif // HAVE_CONFIG_H
47 
48 #include <Inventor/system/gl.h>
49 #include <Inventor/errors/SoDebugError.h>
50 
51 #include "rendering/SoGL.h"
52 
53 // *************************************************************************
54 
55 SO_ELEMENT_SOURCE(SoGLClipPlaneElement);
56 
57 // *************************************************************************
58 
59 /*!
60   This static method initializes static data for the SoGLClipPlaneElement
61   class.
62 */
63 
64 void
initClass(void)65 SoGLClipPlaneElement::initClass(void)
66 {
67   SO_ELEMENT_INIT_CLASS(SoGLClipPlaneElement, inherited);
68 }
69 
70 /*!
71   The destructor.
72 */
73 
~SoGLClipPlaneElement(void)74 SoGLClipPlaneElement::~SoGLClipPlaneElement(void)
75 {
76 }
77 
78 //! FIXME: write doc.
79 
80 void
init(SoState * state)81 SoGLClipPlaneElement::init(SoState * state)
82 {
83   inherited::init(state);
84 }
85 
86 //! FIXME: write doc.
87 
88 void
pop(SoState * state,const SoElement * prevTopElement)89 SoGLClipPlaneElement::pop(SoState * state,
90                           const SoElement * prevTopElement)
91 {
92   this->capture(state);
93   const SoGLClipPlaneElement * prev = (const SoGLClipPlaneElement*)
94     prevTopElement;
95 
96   // disable used planes
97   for (int i = prev->startIndex; i < prev->getNum(); i++)
98     glDisable((GLenum)((int)GL_CLIP_PLANE0 + i));
99 
100   inherited::pop(state, prevTopElement);
101 }
102 
103 //! FIXME: write doc.
104 
105 int
getMaxGLPlanes(void)106 SoGLClipPlaneElement::getMaxGLPlanes(void)
107 {
108   // FIXME: should also make a likewise method available as part of
109   // the So*GLWidget classes. 20020802 mortene.
110 
111   SoDebugError::postWarning("SoGLClipPlaneElement::getMaxGLPlanes",
112                             "This function is obsoleted. It should not "
113                             "be used because its interface is fubar: "
114                             "the number of clip planes available from "
115                             "the OpenGL driver depends on the context, and "
116                             "this function does not know which context this "
117                             "information is requested for.");
118 
119   GLint val;
120   glGetIntegerv(GL_MAX_CLIP_PLANES, &val);
121 
122   GLenum err = sogl_glerror_debugging() ? glGetError() : GL_NO_ERROR;
123   assert(err == GL_NO_ERROR &&
124          "GL error when calling glGetInteger() -- no current GL context?");
125 
126   return (int)val;
127 }
128 
129 //! FIXME: write doc.
130 
131 void
addToElt(const SbPlane & plane,const SbMatrix & modelMatrix)132 SoGLClipPlaneElement::addToElt(const SbPlane & plane,
133                                const SbMatrix & modelMatrix)
134 {
135   int idxadd = getNum(); // num planes before this one
136   inherited::addToElt(plane, modelMatrix); // store plane
137   SbVec3f norm = plane.getNormal();
138   double equation[4];
139   equation[0] = norm[0];
140   equation[1] = norm[1];
141   equation[2] = norm[2];
142   equation[3] = - plane.getDistanceFromOrigin();
143   glClipPlane((GLenum)((int)GL_CLIP_PLANE0 + idxadd), equation);
144   glEnable((GLenum)((int)GL_CLIP_PLANE0 + idxadd));
145 }
146