| 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 | | SoPerspectiveCamera |
| 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 <Inventor/nodes/SoPerspectiveCamera.h> |
| 55 | #include <cmath> |
| 56 | |
| 57 | #define MINIMUM_NEAR_PLANE 0.01 /* minimum near clipping distance */ |
| 58 | /* (from center) */ |
| 59 | |
| 60 | SO_NODE_SOURCE(SoPerspectiveCamera); |
| 61 | |
| 62 | //////////////////////////////////////////////////////////////////////// |
| 63 | // |
| 64 | // Description: |
| 65 | // Constructor |
| 66 | // |
| 67 | // Use: public |
| 68 | |
| 69 | SoPerspectiveCamera::SoPerspectiveCamera() |
| 70 | // |
| 71 | //////////////////////////////////////////////////////////////////////// |
| 72 | { |
| 73 | SO_NODE_CONSTRUCTOR(SoPerspectiveCamera); |
| 74 | SO_NODE_ADD_FIELD(heightAngle, (M_PI_4)); // 45 degrees |
| 75 | isBuiltIn = TRUE; |
| 76 | } |
| 77 | |
| 78 | //////////////////////////////////////////////////////////////////////// |
| 79 | // |
| 80 | // Description: |
| 81 | // Destructor (necessary since inline destructor is too complex) |
| 82 | // |
| 83 | // Use: private |
| 84 | |
| 85 | SoPerspectiveCamera::~SoPerspectiveCamera() |
| 86 | // |
| 87 | //////////////////////////////////////////////////////////////////////// |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | //////////////////////////////////////////////////////////////////////// |
| 92 | // |
| 93 | // Description: |
| 94 | // Scales the height of the camera, in this case the 'heightAngle' field |
| 95 | // |
| 96 | // Use: public |
| 97 | |
| 98 | void |
| 99 | SoPerspectiveCamera::scaleHeight(float scaleFactor) |
| 100 | // |
| 101 | //////////////////////////////////////////////////////////////////////// |
| 102 | { |
| 103 | if (scaleFactor == 0.0) |
| 104 | return; |
| 105 | |
| 106 | heightAngle.setValue(scaleFactor * heightAngle.getValue()); |
| 107 | } |
| 108 | |
| 109 | //////////////////////////////////////////////////////////////////////// |
| 110 | // |
| 111 | // Description: |
| 112 | // Fills in a view volume structure, based on the camera. If the |
| 113 | // useAspectRatio field is not 0.0 (the default), the camera uses |
| 114 | // that ratio instead of the one it has. |
| 115 | // |
| 116 | // Use: public |
| 117 | |
| 118 | SbViewVolume |
| 119 | SoPerspectiveCamera::getViewVolume( float useAspectRatio) const |
| 120 | // |
| 121 | //////////////////////////////////////////////////////////////////////// |
| 122 | { |
| 123 | SbViewVolume view; |
| 124 | |
| 125 | float camAspect = (useAspectRatio != 0.0 ? useAspectRatio : |
| 126 | aspectRatio.getValue()); |
| 127 | |
| 128 | // Set up the perspective camera. |
| 129 | view.perspective(heightAngle.getValue(), camAspect, |
| 130 | nearDistance.getValue(), farDistance.getValue()); |
| 131 | |
| 132 | // Note that these move the camera rather than moving objects |
| 133 | // relative to the camera. |
| 134 | view.rotateCamera(orientation.getValue()); |
| 135 | view.translateCamera(position.getValue()); |
| 136 | return view; |
| 137 | } |
| 138 | |
| 139 | //////////////////////////////////////////////////////////////////////// |
| 140 | // |
| 141 | // Description: |
| 142 | // Finds a bounding sphere around the passed bounding box, then |
| 143 | // positions the camera without changing its height angle so that |
| 144 | // the entire sphere is visible. The aspect ratio to use for the |
| 145 | // camera is passed in. |
| 146 | // |
| 147 | // Use: protected |
| 148 | |
| 149 | void |
| 150 | SoPerspectiveCamera::viewBoundingBox(const SbBox3f &box, |
| 151 | float aspect, float slack) |
| 152 | // |
| 153 | //////////////////////////////////////////////////////////////////////// |
| 154 | { |
| 155 | SbSphere bSphere; |
| 156 | float distToCenter; |
| 157 | SbMatrix rotation; |
| 158 | SbVec3f pos; |
| 159 | |
| 160 | // if the bounding box is not empty, create the bounding sphere |
| 161 | if (! box.isEmpty()) |
| 162 | bSphere.circumscribe(box); |
| 163 | else { |
| 164 | pos.setValue(0., 0., 0.); |
| 165 | bSphere.setValue(pos, 1.0); |
| 166 | } |
| 167 | |
| 168 | // Find the angle necessary to fit the object completely in the |
| 169 | // window. We don't need any slack, because the bounding sphere |
| 170 | // is already bigger than the bounding box. |
| 171 | // h = height (radius of sphere) |
| 172 | // d = distance to eye |
| 173 | // tan(alpha) gives us h/d |
| 174 | float hOverD = tan(heightAngle.getValue() / 2.0); |
| 175 | if (aspect < 1.0) |
| 176 | hOverD *= aspect; |
| 177 | |
| 178 | distToCenter = bSphere.getRadius() / hOverD; // h / (h/d) gives us d |
| 179 | |
| 180 | rotation = orientation.getValue(); |
| 181 | rotation.multVecMatrix(SbVec3f(0.0, 0.0, distToCenter), pos); |
| 182 | |
| 183 | position = pos + bSphere.getCenter(); |
| 184 | nearDistance = distToCenter - slack * bSphere.getRadius(); |
| 185 | if (nearDistance.getValue() < 0.0 || |
| 186 | nearDistance.getValue() < (MINIMUM_NEAR_PLANE * distToCenter)) |
| 187 | nearDistance = MINIMUM_NEAR_PLANE * distToCenter; |
| 188 | farDistance = distToCenter + slack * bSphere.getRadius(); |
| 189 | focalDistance = distToCenter; |
| 190 | } |