OSGClipPlaneChunk.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042
00043 #include <cstdlib>
00044 #include <cstdio>
00045
00046 #include "OSGConfig.h"
00047
00048 #include "OSGGL.h"
00049
00050 #include "OSGCamera.h"
00051 #include "OSGDrawEnv.h"
00052
00053 #include "OSGClipPlaneChunk.h"
00054
00055 OSG_USING_NAMESPACE
00056
00057 // Documentation for this class is emited in the
00058 // OSGClipPlaneChunkBase.cpp file.
00059 // To modify it, please change the .fcd file (OSGClipPlaneChunk.fcd) and
00060 // regenerate the base file.
00061
00062 /***************************************************************************\
00063  *                           Class variables                               *
00064 \***************************************************************************/
00065
00066 StateChunkClass ClipPlaneChunk::_class("ClipPlane", 6);
00067
00068 void ClipPlaneChunk::initMethod(InitPhase ePhase)
00069 {
00070     Inherited::initMethod(ePhase);
00071 }
00072
00073 /***************************************************************************\
00074  *                           Instance methods                              *
00075 \***************************************************************************/
00076
00077 /*-------------------------------------------------------------------------*\
00078  -  private                                                                 -
00079 \*-------------------------------------------------------------------------*/
00080
00081 /*----------------------- constructors & destructors ----------------------*/
00082
00083 ClipPlaneChunk::ClipPlaneChunk(void) :
00084     Inherited()
00085 {
00086 }
00087
00088 ClipPlaneChunk::ClipPlaneChunk(const ClipPlaneChunk &source) :
00089     Inherited(source)
00090 {
00091 }
00092
00093 ClipPlaneChunk::~ClipPlaneChunk(void)
00094 {
00095 }
00096
00097 /*----------------------- Chunk Class Access -----------------------------*/
00098
00099 const StateChunkClass *ClipPlaneChunk::getClass(void) const
00100 {
00101      return &_class;
00102 }
00103
00104 /*------------------------------- Sync -----------------------------------*/
00105
00106 void ClipPlaneChunk::changed(ConstFieldMaskArg whichField,
00107                              UInt32            origin,
00108                              BitVector         details)
00109 {
00110     Inherited::changed(whichField, origin, details);
00111 }
00112
00113 /*------------------------------ Output ----------------------------------*/
00114
00115 void ClipPlaneChunk::dump(      UInt32    ,
00116                           const BitVector) const
00117 {
00118     SLOG << "Dump ClipPlaneChunk NI" << std::endl;
00119 }
00120
00121
00122 /*------------------------------ State ------------------------------------*/
00123
00124 void ClipPlaneChunk::activate(DrawEnv *pEnv, UInt32 idx)
00125 {
00126     Matrix beaconMat;
00127
00128     Matrix cameraMat = pEnv->getCameraViewing();
00129
00130     if(getBeacon() != NULL)
00131     {
00132         getBeacon()->getToWorld(beaconMat);
00133     }
00134     else
00135     {
00136         beaconMat.setIdentity();
00137
00138         SWARNING << "NO beacon" << std::endl;
00139     }
00140
00141     cameraMat.mult(beaconMat);
00142
00143     if(getEnable() == true)
00144     {
00145               GLdouble  glEq[4];
00146         const Vec4f    &eq = getEquation();
00147
00148         glEq[0] = eq[0];
00149         glEq[1] = eq[1];
00150         glEq[2] = eq[2];
00151         glEq[3] = eq[3];
00152
00153         glPushMatrix();
00154         glLoadMatrixf(cameraMat.getValues());
00155
00156         glClipPlane( GL_CLIP_PLANE0 + idx, glEq);
00157         glEnable( GL_CLIP_PLANE0 + idx);
00158
00159         glPopMatrix();
00160     }
00161 }
00162
00163
00164
00165
00166 void ClipPlaneChunk::changeFrom(DrawEnv    *pEnv,
00167                                 StateChunk *old_chunk,
00168                                 UInt32      idx)
00169 {
00170     ClipPlaneChunk const *old = dynamic_cast<ClipPlaneChunk const*>(old_chunk);
00171
00172     // change from me to me?
00173     // this assumes I haven't changed in the meantime. 
00174     // is that a valid assumption?
00175
00176     if(old == this)
00177         return;
00178
00179     Matrix beaconMat;
00180
00181     Matrix cameraMat = pEnv->getCameraViewing();
00182
00183     if(getBeacon() != NULL)
00184     {
00185         getBeacon()->getToWorld(beaconMat);
00186     }
00187     else
00188     {
00189         beaconMat.setIdentity();
00190
00191         SWARNING << "ClipPlaneChunk::changeFrom: NO beacon" << std::endl;
00192     }
00193
00194     cameraMat.mult(beaconMat);
00195
00196     if(getEnable() != old->getEnable() ||
00197        getBeacon() != old->getBeacon()  )
00198     {
00199         if(getEnable() == true)
00200         {
00201                   GLdouble  glEq[4];
00202
00203             const Vec4f    &eq = getEquation();
00204
00205             glEq[0] = eq[0];
00206             glEq[1] = eq[1];
00207             glEq[2] = eq[2];
00208             glEq[3] = eq[3];
00209
00210             glPushMatrix();
00211             glLoadMatrixf(cameraMat.getValues());
00212
00213             glClipPlane(GL_CLIP_PLANE0 + idx, glEq);
00214             glEnable   (GL_CLIP_PLANE0 + idx      );
00215
00216             glPopMatrix();
00217         }
00218         else
00219         {
00220             glDisable(GL_CLIP_PLANE0 + idx);
00221         }
00222     }
00223 }
00224
00225
00226 void ClipPlaneChunk::deactivate(DrawEnv *, UInt32 idx)
00227 {
00228     if(getEnable() == true)
00229     {
00230         glDisable( GL_CLIP_PLANE0 + idx);
00231     }
00232 }
00233
00234
00235 /*-------------------------- Comparison -----------------------------------*/
00236
00237 Real32 ClipPlaneChunk::switchCost(StateChunk *OSG_CHECK_ARG(chunk))
00238 {
00239     return 0;
00240 }
00241
00242 bool ClipPlaneChunk::operator < (const StateChunk &other) const
00243 {
00244     return this < &other;
00245 }
00246
00247 bool ClipPlaneChunk::operator == (const StateChunk &other) const
00248 {
00249     ClipPlaneChunk const *tother = dynamic_cast<ClipPlaneChunk const*>(&other);
00250
00251     if(!tother)
00252         return false;
00253
00254     if(getEnable() != tother->getEnable())
00255         return false;
00256
00257     if(getEquation() != tother->getEquation())
00258         return false;
00259
00260     if(getBeacon() != tother->getBeacon())
00261         return false;
00262
00263     return true;
00264 }
00265
00266 bool ClipPlaneChunk::operator != (const StateChunk &other) const
00267 {
00268     return ! (*this == other);
00269 }