OSGScreenGroup.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 <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include <OSGConfig.h>
00047
00048 #include "OSGScreenGroup.h"
00049 #include "OSGIntersectAction.h"
00050 #include "OSGRenderAction.h"
00051 #include "OSGViewport.h"
00052 #include "OSGCamera.h"
00053
00054 OSG_BEGIN_NAMESPACE
00055
00056 /***************************************************************************\
00057  *                            Description                                  *
00058 \***************************************************************************/
00059
00063 /***************************************************************************\
00064  *                           Class variables                               *
00065 \***************************************************************************/
00066
00067 /***************************************************************************\
00068  *                           Class methods                                 *
00069 \***************************************************************************/
00070
00071 void ScreenGroup::initMethod(InitPhase ePhase)
00072 {
00073     Inherited::initMethod(ePhase);
00074
00075     if(ePhase == TypeObject::SystemPost)
00076     {
00077         IntersectAction::registerEnterDefault(
00078             ScreenGroup::getClassType(),
00079             reinterpret_cast<Action::Callback>(&ScreenGroup::intersectEnter));
00080
00081         IntersectAction::registerLeaveDefault(
00082             ScreenGroup::getClassType(),
00083             reinterpret_cast<Action::Callback>(&ScreenGroup::intersectLeave));
00084
00085         RenderAction::registerEnterDefault(
00086             ScreenGroup::getClassType(),
00087             reinterpret_cast<Action::Callback>(&ScreenGroup::renderEnter));
00088
00089         RenderAction::registerLeaveDefault(
00090             ScreenGroup::getClassType(),
00091             reinterpret_cast<Action::Callback>(&ScreenGroup::renderLeave));
00092     }
00093 }
00094
00095
00096 /***************************************************************************\
00097  *                           Instance methods                              *
00098 \***************************************************************************/
00099
00100 /*-------------------------------------------------------------------------*\
00101  -  private                                                                 -
00102 \*-------------------------------------------------------------------------*/
00103
00104 /*----------------------- constructors & destructors ----------------------*/
00105
00106 ScreenGroup::ScreenGroup(void) :
00107     Inherited()
00108 {
00109 }
00110
00111 ScreenGroup::ScreenGroup(const ScreenGroup &source) :
00112     Inherited(source)
00113 {
00114 }
00115
00116 ScreenGroup::~ScreenGroup(void)
00117 {
00118 }
00119
00120 /*----------------------------- class specific ----------------------------*/
00121
00122 void ScreenGroup::changed(BitVector whichField,
00123                           UInt32    origin,
00124                           BitVector details)
00125 {
00126     Inherited::changed(whichField, origin, details);
00127 }
00128
00129 void ScreenGroup::dump(      UInt32    ,
00130                        const BitVector ) const
00131 {
00132     SLOG << "Dump ScreenGroup NI" << std::endl;
00133 }
00134
00135
00136 /*------------------------- volume update -------------------------------*/
00137
00138 void ScreenGroup::adjustVolume(Volume &volume)
00139 {
00140     volume.transform(_camTransform);
00141 }
00142
00143 void ScreenGroup::accumulateMatrix(Matrix &result)
00144 {
00145     result.mult(_camTransform);
00146 }
00147
00148 void ScreenGroup::calcMatrix(      RenderAction *pAction,
00149                              const Matrix        &mToWorld,
00150                                    Matrix        &mResult )
00151 {
00152     const DrawEnv &oEnv = pAction->getActivePartition()->getDrawEnv();
00153
00154     Matrix mToScreen = oEnv.getVPWorldToScreen();
00155
00156 #if 0
00157     Viewport *viewport = pAction->getViewport();
00158     pAction->getCamera()->getWorldToScreen(mToScreen, *viewport);
00159 #endif
00160 
00161     mToScreen.mult(mToWorld);
00162
00163     Pnt3f origin(0.f, 0.f, 0.f);
00164
00165     mToScreen.multFull(origin, origin);
00166
00167     Pnt3f xAxis(1.f, 0.f, 0.f);
00168
00169     mToScreen.multFull(xAxis, xAxis);
00170
00171     Real32 scaleX =
00172         2.f / oEnv.getPixelWidth() / (xAxis - origin).length();
00173
00174     Pnt3f yAxis(0.f, 1.f, 0.f);
00175
00176     mToScreen.multFull(yAxis, yAxis);
00177
00178     Real32 scaleY =
00179         2.f / oEnv.getPixelHeight() / (yAxis - origin).length();
00180
00181     mResult.setScale    (scaleX, scaleY, 1.f);
00182     mResult.setTranslate(0.375 * scaleX, 0.375 * scaleY, 0.f);
00183
00184     bool equal = mResult.equals(_camTransform, 0.00001f);
00185
00186     _camTransform = mResult;
00187
00188     if(!equal)
00189         this->invalidateVolume();
00190 }
00191
00192 /*-------------------------------------------------------------------------*/
00193 /*                            Intersect                                    */
00194
00195 Action::ResultE ScreenGroup::intersectEnter(Action *action)
00196 {
00197 #if 0
00198     IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
00199     Matrix           m(_camTransform);
00200
00201     m.invert();
00202
00203     Pnt3f pos;
00204     Vec3f dir;
00205
00206 #ifndef OSG_2_PREP
00207     m.multFullMatrixPnt(ia->getLine().getPosition (), pos);
00208     m.multMatrixVec    (ia->getLine().getDirection(), dir);
00209 #else
00210     m.multFull(ia->getLine().getPosition (), pos);
00211     m.mult    (ia->getLine().getDirection(), dir);
00212 #endif
00213 
00214     ia->setLine(Line(pos, dir), ia->getMaxDist());
00215     ia->scale(dir.length());
00216 #endif
00217 
00218     return Action::Continue;
00219 }
00220
00221 Action::ResultE ScreenGroup::intersectLeave(Action *action)
00222 {
00223 #if 0
00224     IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
00225     Matrix           m(_camTransform);
00226
00227     Pnt3f pos;
00228     Vec3f dir;
00229
00230 #ifndef OSG_2_PREP
00231     m.multFullMatrixPnt(ia->getLine().getPosition (), pos);
00232     m.multMatrixVec    (ia->getLine().getDirection(), dir);
00233 #else
00234     m.multFull(ia->getLine().getPosition (), pos);
00235     m.mult    (ia->getLine().getDirection(), dir);
00236 #endif
00237 
00238     ia->setLine(Line(pos, dir), ia->getMaxDist());
00239     ia->scale(dir.length());
00240 #endif
00241 
00242     return Action::Continue;
00243 }
00244
00245 /*-------------------------------------------------------------------------*/
00246 /*                                Render                                   */
00247
00248 Action::ResultE ScreenGroup::renderEnter(Action *action)
00249 {
00250     RenderAction *pAction = dynamic_cast<RenderAction *>(action);
00251
00252     Matrix mMat;
00253
00254 #if 0
00255     if(!pAction->getEffectsPass())
00256         calcMatrix(pAction, pAction->top_matrix(), mMat);
00257     else
00258         mMat = _camTransform;
00259 #else
00260     calcMatrix(pAction, pAction->topMatrix(), mMat);
00261 #endif
00262 
00263     pAction->pushMatrix(mMat);
00264
00265 // !!! can't use visibles, as ToWorld gives garbage leading to wrong culling
00266 //    pAction->selectVisibles();
00267
00268     return Action::Continue;
00269 }
00270
00271 Action::ResultE ScreenGroup::renderLeave(Action *action)
00272 {
00273     RenderAction *pAction = dynamic_cast<RenderAction *>(action);
00274
00275     pAction->popMatrix();
00276
00277     return Action::Continue;
00278 }
00279
00280 OSG_END_NAMESPACE
00281