1// @configure_input@ 2 3/**************************************************************************\ 4 * Copyright (c) Kongsberg Oil & Gas Technologies AS 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name of the copyright holder nor the names of its 19 * contributors may be used to endorse or promote products derived from 20 * this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33\**************************************************************************/ 34 35#include <assert.h> 36 37#include <Inventor/SbViewportRegion.h> 38#include <Inventor/SbLinear.h> 39#include <Inventor/errors/SoDebugError.h> 40#include <Inventor/actions/SoGLRenderAction.h> 41#include <Inventor/actions/SoPickAction.h> 42#include <Inventor/actions/SoRayPickAction.h> 43#include <Inventor/actions/SoGetMatrixAction.h> 44#include <Inventor/elements/SoModelMatrixElement.h> 45 46#include <Inventor/@Gui@/nodes/SoGuiViewportFix.h> 47 48// ************************************************************************* 49 50SO_NODE_SOURCE(SoGuiViewportFix); 51 52void 53SoGuiViewportFix::initClass(void) 54{ 55 SO_NODE_INIT_CLASS(SoGuiViewportFix, SoTransformation, "Transformation"); 56} 57 58SoGuiViewportFix::SoGuiViewportFix(void) 59{ 60 this->internals = NULL; 61 62 SO_NODE_CONSTRUCTOR(SoGuiViewportFix); 63 64 SO_NODE_ADD_FIELD(corner, (SoGuiViewportFix::LEFT_BOTTOM)); 65 SO_NODE_ADD_FIELD(viewportSize, (SbVec3f(0.0f, 0.0f, 0.0f))); 66 67 SO_NODE_DEFINE_ENUM_VALUE(Corner, LEFT_TOP); 68 SO_NODE_DEFINE_ENUM_VALUE(Corner, RIGHT_TOP); 69 SO_NODE_DEFINE_ENUM_VALUE(Corner, LEFT_BOTTOM); 70 SO_NODE_DEFINE_ENUM_VALUE(Corner, RIGHT_BOTTOM); 71 72 SO_NODE_SET_SF_ENUM_TYPE(corner, Corner); 73} 74 75SoGuiViewportFix::~SoGuiViewportFix(void) 76{ 77} 78 79void 80SoGuiViewportFix::doAction(SoAction * action) 81{ 82 SoState * state = action->getState(); 83 if ( ! state->isElementEnabled(SoModelMatrixElement::getClassStackIndex()) ) return; 84 85 SbVec3f vpsize = this->viewportSize.getValue(); 86 if ( vpsize[0] <= 0.0f || vpsize[1] <= 0.0f ) return; 87 88 SoModelMatrixElement::makeIdentity(state, this); 89 90 switch ( this->corner.getValue() ) { 91 case SoGuiViewportFix::LEFT_BOTTOM: 92 do { 93 SbVec3f translation(-1.0f, -1.0f, 0.0f); 94 SbVec3f scale(2.0f, 2.0f, 1.0f); 95 if ( vpsize[0] > vpsize[1] ) { 96 translation[0] = translation[0] * (vpsize[0] / vpsize[1]); 97 scale[0] = scale[0] * (vpsize[0] / vpsize[1]); 98 } else { 99 translation[1] = translation[1] * (vpsize[1] / vpsize[0]); 100 scale[1] = scale[1] * (vpsize[1] / vpsize[0]); 101 } 102 SoModelMatrixElement::translateBy(state, this, translation); 103 SoModelMatrixElement::scaleBy(state, this, scale); 104 // FIXME: scale 105 } while ( FALSE ); 106 break; 107 default: 108 do { 109 SoDebugError::postInfo("SoGuiViewportFix::GLRender", "unimplemented corner"); 110 } while ( FALSE ); 111 break; 112 } 113} 114 115void 116SoGuiViewportFix::GLRender(SoGLRenderAction * action) 117{ 118 const SbViewportRegion & vp = action->getViewportRegion(); 119 SbVec2s vpsize = vp.getViewportSizePixels(); 120 121 SbVec3f viewport(vpsize[0], vpsize[1], 0.0f); 122 if ( viewport != this->viewportSize.getValue() ) 123 this->viewportSize.setValue(vpsize[0], vpsize[1], 0.0f); 124 125 this->doAction(action); 126} 127 128void 129SoGuiViewportFix::pick(SoPickAction * action) 130{ 131 this->doAction(action); 132} 133 134void 135SoGuiViewportFix::rayPick(SoRayPickAction * action) 136{ 137 this->doAction(action); 138} 139 140void 141SoGuiViewportFix::getMatrix(SoGetMatrixAction * action) 142{ 143 SoDebugError::postInfo("SoGuiViewportFix::getMatrix", "not implemented yet"); 144 145 SbVec3f vpsize = this->viewportSize.getValue(); 146 if ( vpsize[0] <= 0.0f || vpsize[1] <= 0.0f ) return; 147 148 action->getMatrix() = SbMatrix::identity(); 149 150 switch ( this->corner.getValue() ) { 151 case SoGuiViewportFix::LEFT_BOTTOM: 152 do { 153 SbVec3f translation(-1.0f, -1.0f, 0.0f); 154 SbVec3f scale(2.0f, 2.0f, 1.0f); 155 if ( vpsize[0] > vpsize[1] ) { 156 translation[0] = translation[0] * (vpsize[0] / vpsize[1]); 157 scale[0] = scale[0] * (vpsize[0] / vpsize[1]); 158 } else { 159 translation[1] = translation[1] * (vpsize[1] / vpsize[0]); 160 scale[1] = scale[1] * (vpsize[1] / vpsize[0]); 161 } 162 SbMatrix matrix = SbMatrix::identity(); 163 matrix.setTranslate(translation); 164 action->getMatrix().multLeft(matrix); 165 166 matrix = SbMatrix::identity(); 167 matrix.setScale(scale); 168 action->getMatrix().multLeft(matrix); 169 } while ( FALSE ); 170 break; 171 default: 172 do { 173 SoDebugError::postInfo("SoGuiViewportFix::GLRender", "unimplemented corner"); 174 } while ( FALSE ); 175 break; 176 } 177} 178 179