PlaneNode.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 83 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-02-27 17:31:45 +0100 (Fr, 27. Feb 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 #include "PlaneNode.hh"
00044 #include "../GL/gl.hh"
00045 #include <iostream>
00046 #include <math.h>
00047 
00048 #include "TranslationManipulatorNode.hh"
00049 
00050 namespace ACG {
00051 namespace SceneGraph {
00052 
00053 
00054 //== IMPLEMENTATION ==========================================================
00055 
00056 
00057 void PlaneNode::boundingBox(Vec3f& _bbMin, Vec3f& _bbMax)
00058 {
00059 
00060   Vec3f pos = position_ - xDirection_*0.5 - yDirection_*0.5;
00061 
00062   //add a little offset in normal direction
00063   Vec3f pos0 = pos + normal_*0.1;
00064   Vec3f pos1 = pos - normal_*0.1;
00065 
00066   _bbMin.minimize( pos0 );
00067   _bbMin.minimize( pos0 + xDirection_);
00068   _bbMin.minimize( pos0 + yDirection_);
00069   _bbMin.minimize( pos0 + xDirection_ + yDirection_);
00070   _bbMax.maximize( pos1 );
00071   _bbMax.maximize( pos1 + xDirection_);
00072   _bbMax.maximize( pos1 + yDirection_);
00073   _bbMax.maximize( pos1 + xDirection_ + yDirection_);
00074 }
00075 
00076 //----------------------------------------------------------------------------
00077 
00078 unsigned int PlaneNode::availableDrawModes() const
00079 {
00080   return ( DrawModes::POINTS |
00081                 DrawModes::POINTS_SHADED |
00082                 DrawModes::POINTS_COLORED );
00083 }
00084 
00085 //----------------------------------------------------------------------------
00086 
00087 void PlaneNode::drawPlane( GLState&  /*_state*/) {
00088 
00089   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00090 
00091   Vec3f origin(0.0, 0.0, 0.0);
00092   Vec3f xy = xDirection_ + yDirection_;
00093 
00094   //first draw the lines
00095   glColor3f( 1.0, 1.0, 1.0 );
00096   glLineWidth(2.0);
00097 
00098   //draw the plane
00099   glBegin(GL_QUADS);
00100     glVertex3fv( &origin[0] );
00101     glVertex3fv( &xDirection_[0] );
00102     glVertex3fv( &xy[0] );
00103     glVertex3fv( &yDirection_[0] );
00104   glEnd();
00105 
00106 
00107   glLineWidth(1.0);
00108 
00109   //then the red front side
00110 
00111   glEnable (GL_BLEND);
00112   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00113 
00114   glPolygonMode(GL_FRONT, GL_FILL);
00115 
00116   glColor4f( 0.6, 0.15, 0.2, 0.5 );
00117 
00118   //draw the plane
00119   glBegin(GL_QUADS);
00120     glVertex3fv( &origin[0] );
00121     glVertex3fv( &xDirection_[0] );
00122     glVertex3fv( &xy[0] );
00123     glVertex3fv( &yDirection_[0] );
00124   glEnd();
00125 
00126   //finally the green back side
00127 
00128   glPolygonMode(GL_BACK, GL_FILL);
00129 
00130   glColor4f( 0.1, 0.8, 0.2, 0.5 );
00131 
00132   //draw the plane
00133   glBegin(GL_QUADS);
00134     glVertex3fv( &origin[0] );
00135     glVertex3fv( &xDirection_[0] );
00136     glVertex3fv( &xy[0] );
00137     glVertex3fv( &yDirection_[0] );
00138   glEnd();
00139 
00140 }
00141 
00142 //----------------------------------------------------------------
00143 
00144 void PlaneNode::drawPlanePick( GLState&  _state) {
00145 
00146   _state.pick_set_maximum(1);
00147   _state.pick_set_name(0);
00148 
00149   Vec3f origin(0.0, 0.0, 0.0);
00150   Vec3f xy = xDirection_ + yDirection_;
00151 
00152   //draw the plane
00153   glBegin(GL_QUADS);
00154     glVertex3fv( &origin[0] );
00155     glVertex3fv( &xDirection_[0] );
00156     glVertex3fv( &xy[0] );
00157     glVertex3fv( &yDirection_[0] );
00158   glEnd();
00159 }
00160 
00161 //----------------------------------------------------------------
00162 
00163 void PlaneNode::draw(GLState&  _state  , unsigned int /*_drawMode*/)
00164 {
00165 
00166   _state.push_modelview_matrix();
00167   glPushAttrib(GL_COLOR_BUFFER_BIT);
00168   glPushAttrib(GL_LIGHTING_BIT);
00169 
00170   glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
00171   glEnable(GL_COLOR_MATERIAL);
00172 
00173   Vec3f pos = position_ - xDirection_*0.5 - yDirection_*0.5;
00174 
00175   _state.translate(pos[0], pos[1], pos[2]);
00176 
00177   drawPlane(_state);
00178 
00179   glPopAttrib();
00180   glPopAttrib();
00181   _state.pop_modelview_matrix();
00182 }
00183 
00184 //----------------------------------------------------------------
00185 
00186 void PlaneNode::setPosition(const Vec3f& _position, const Vec3f& _normal)
00187 {
00188 
00189   //find a non zero component
00190   int comp = -1;
00191   for (int i=0; i < 2; i++)
00192     if ( _normal[i] != 0.0 ){
00193       comp = i;
00194       break;
00195     }
00196 
00197   if (comp == -1){
00198     std::cerr << "PlaneNode: normal is invalid!" << std::endl;
00199     return;
00200   }
00201 
00202   //compute orthogonal vectors in the plane
00203   xDirection_[comp] = (-_normal[ (comp + 1) % 3 ] - _normal[(comp + 2) % 3]) / _normal[comp];
00204   xDirection_[ (comp + 1) % 3 ] = 1;
00205   xDirection_[ (comp + 2) % 3 ] = 1;
00206   xDirection_ = xDirection_.normalize();
00207 
00208   yDirection_ = _normal % xDirection_;
00209   yDirection_ = yDirection_.normalize();
00210 
00211   position_ = _position;
00212   normal_   = _normal;
00213 }
00214 
00215 //----------------------------------------------------------------
00216 
00217 
00218 void PlaneNode::setPosition(const Vec3f& _position, const Vec3f& _xDirection, const Vec3f& _yDirection)
00219 {
00220   position_ = _position;
00221   xDirection_ = _xDirection;
00222   yDirection_ = _yDirection;
00223   normal_ = (_xDirection % _yDirection).normalize();
00224 }
00225 
00226 //----------------------------------------------------------------
00227 
00228 void PlaneNode::transform(const ACG::Matrix4x4d& _mat)
00229 {
00230   position_ = _mat.transform_point(position_);
00231   xDirection_ = _mat.transform_vector(xDirection_);
00232   yDirection_ = _mat.transform_vector(yDirection_);
00233 
00234   normal_ = (xDirection_ % yDirection_).normalize();
00235 }
00236 
00237 //----------------------------------------------------------------
00238 
00239 void PlaneNode::setSize(double _xDirection, double _yDirection)
00240 {
00241   xDirection_ = xDirection_.normalize() * _xDirection;
00242   yDirection_ = yDirection_.normalize() * _yDirection;
00243 }
00244 
00245 //----------------------------------------------------------------
00246 
00247 void
00248 PlaneNode::pick(GLState& _state, PickTarget _target)
00249 {
00250   if (_target == PICK_ANYTHING) {
00251 
00252           _state.push_modelview_matrix();
00253           
00254     Vec3f pos = position_ - xDirection_*0.5 - yDirection_*0.5;
00255 
00256           _state.translate(pos[0], pos[1], pos[2]);
00257           
00258           drawPlanePick(_state);
00259           
00260           _state.pop_modelview_matrix();
00261   }
00262 }
00263 
00264 //----------------------------------------------------------------
00265 
00266 Vec3f PlaneNode::position()
00267 {
00268     return position_;
00269 }
00270 
00271 //----------------------------------------------------------------
00272 
00273 Vec3f PlaneNode::normal()
00274 {
00275     return normal_;
00276 }
00277 
00278 //----------------------------------------------------------------
00279 
00280 Vec3f PlaneNode::xDirection()
00281 {
00282   return xDirection_;
00283 }
00284 
00285 //----------------------------------------------------------------
00286 
00287 Vec3f PlaneNode::yDirection()
00288 {
00289   return yDirection_;
00290 }
00291 
00292 //=============================================================================
00293 } // namespace SceneGraph
00294 } // namespace ACG
00295 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .