| 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 | * Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 | * |
| 40 | _______________________________________________________________________ |
| 41 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 | | |
| 43 | | $Revision: 1.1.1.1 $ |
| 44 | | |
| 45 | | Classes: |
| 46 | | SoGLClipPlaneElement |
| 47 | | |
| 48 | | Author(s) : Paul S. Strauss |
| 49 | | |
| 50 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 | _______________________________________________________________________ |
| 52 | */ |
| 53 | |
| 54 | #include <GL/gl.h> |
| 55 | #include <Inventor/elements/SoGLClipPlaneElement.h> |
| 56 | #include <Inventor/misc/SoState.h> |
| 57 | |
| 58 | // This holds the maximum number of concurrent GL clip planes |
| 59 | int SoGLClipPlaneElement::maxGLPlanes = -1; |
| 60 | |
| 61 | SO_ELEMENT_SOURCE(SoGLClipPlaneElement); |
| 62 | |
| 63 | //////////////////////////////////////////////////////////////////////// |
| 64 | // |
| 65 | // Description: |
| 66 | // Destructor. |
| 67 | // |
| 68 | // Use: private |
| 69 | |
| 70 | SoGLClipPlaneElement::~SoGLClipPlaneElement() |
| 71 | // |
| 72 | //////////////////////////////////////////////////////////////////////// |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | //////////////////////////////////////////////////////////////////////// |
| 77 | // |
| 78 | // Description: |
| 79 | // Pops element, causing side effects in GL. |
| 80 | // |
| 81 | // Use: public |
| 82 | |
| 83 | void |
| 84 | SoGLClipPlaneElement::pop(SoState *state, const SoElement *prevTopElement) |
| 85 | // |
| 86 | //////////////////////////////////////////////////////////////////////// |
| 87 | { |
| 88 | // Since popping this element has GL side effects, make sure any |
| 89 | // open caches capture it |
| 90 | capture(state); |
| 91 | |
| 92 | // Disable clip planes created in previous elements. Note that the |
| 93 | // index into the planes array is identical to the GL clip plane |
| 94 | // id, for convenience. |
| 95 | |
| 96 | const SoGLClipPlaneElement *prevElt = |
| 97 | (const SoGLClipPlaneElement *) prevTopElement; |
| 98 | |
| 99 | int maxId = prevElt->getNum(); |
| 100 | if (maxId > getMaxGLPlanes()) |
| 101 | maxId = getMaxGLPlanes(); |
| 102 | |
| 103 | for (int i = prevElt->startIndex; i < maxId; i++) |
| 104 | if (i < getMaxGLPlanes()) |
| 105 | glDisable((GLenum)(GL_CLIP_PLANE0 + i)); |
| 106 | |
| 107 | // Do parent's pop stuff |
| 108 | SoClipPlaneElement::pop(state, prevTopElement); |
| 109 | } |
| 110 | |
| 111 | //////////////////////////////////////////////////////////////////////// |
| 112 | // |
| 113 | // Description: |
| 114 | // Returns the maximum number of concurrent clipping planes supported |
| 115 | // by GL implementation. |
| 116 | // |
| 117 | // Use: protected, static |
| 118 | |
| 119 | int |
| 120 | SoGLClipPlaneElement::getMaxGLPlanes() |
| 121 | // |
| 122 | //////////////////////////////////////////////////////////////////////// |
| 123 | { |
| 124 | // Inquire GL if not already done |
| 125 | if (maxGLPlanes < 0) { |
| 126 | GLint max; |
| 127 | glGetIntegerv(GL_MAX_CLIP_PLANES, &max); |
| 128 | maxGLPlanes = (int) max; |
| 129 | } |
| 130 | |
| 131 | return maxGLPlanes; |
| 132 | } |
| 133 | |
| 134 | //////////////////////////////////////////////////////////////////////// |
| 135 | // |
| 136 | // Description: |
| 137 | // Adds clip plane to element and sends it to GL. |
| 138 | // |
| 139 | // Use: protected, virtual |
| 140 | |
| 141 | void |
| 142 | SoGLClipPlaneElement::addToElt(const SbPlane &plane, |
| 143 | const SbMatrix &modelMatrix) |
| 144 | // |
| 145 | //////////////////////////////////////////////////////////////////////// |
| 146 | { |
| 147 | // Do normal stuff |
| 148 | SoClipPlaneElement::addToElt(plane, modelMatrix); |
| 149 | |
| 150 | // If we haven't run out of clipping planes, send this one to GL. |
| 151 | // Note that we send the plane in object space, since GL already |
| 152 | // will transform it by the current model matrix. |
| 153 | int planeId = getNum() - 1; |
| 154 | if (planeId < getMaxGLPlanes()) { |
| 155 | |
| 156 | const SbPlane &objPlane = get(planeId, FALSE); |
| 157 | const SbVec3f &norm = objPlane.getNormal(); |
| 158 | GLdouble planeEquation[4]; |
| 159 | |
| 160 | planeEquation[0] = norm[0]; |
| 161 | planeEquation[1] = norm[1]; |
| 162 | planeEquation[2] = norm[2]; |
| 163 | planeEquation[3] = -objPlane.getDistanceFromOrigin(); |
| 164 | |
| 165 | glClipPlane((GLenum)(GL_CLIP_PLANE0 + planeId), planeEquation); |
| 166 | |
| 167 | glEnable((GLenum)(GL_CLIP_PLANE0 + planeId)); |
| 168 | } |
| 169 | } |