OSGCamera.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 #include "OSGLog.h"
00048
00049 #include "OSGGL.h"
00050
00051 #include "OSGNode.h"
00052 #include "OSGViewport.h"
00053 #include "OSGWindow.h"
00054 #include "OSGLine.h"
00055
00056 #include "OSGCamera.h"
00057
00058 OSG_USING_NAMESPACE
00059
00060 // Documentation for this class is emited in the
00061 // OSGCameraBase.cpp file.
00062 // To modify it, please change the .fcd file (OSGCamera.fcd) and
00063 // regenerate the base file.
00064
00065 /***************************************************************************\
00066  *                           Class methods                                 *
00067 \***************************************************************************/
00068
00069 void Camera::initMethod(InitPhase ePhase)
00070 {
00071     Inherited::initMethod(ePhase);
00072 }
00073
00074 /***************************************************************************\
00075  *                           Instance methods                              *
00076 \***************************************************************************/
00077
00078 Camera::Camera(void) :
00079     Inherited()
00080 {
00081 }
00082
00083 Camera::Camera(const Camera &source) :
00084     Inherited(source)
00085 {
00086 }
00087
00088 Camera::~Camera(void)
00089 {
00090 }
00091
00092 void Camera::changed(ConstFieldMaskArg whichField,
00093                      UInt32            origin,
00094                      BitVector         details)
00095 {
00096     Inherited::changed(whichField, origin, details);
00097 }
00098
00099
00100 #ifdef OSG_OLD_RENDER_ACTION
00101 /*-------------------------- your_category---------------------------------*/
00102
00107 void Camera::setup(      DrawActionBase *OSG_CHECK_ARG(action),
00108                    const Viewport       &port                 )
00109 {
00110     Matrixr m, t;
00111
00112     // set the projection
00113
00114     getProjection           (m, port.getPixelWidth(), port.getPixelHeight());
00115     getProjectionTranslation(t, port.getPixelWidth(), port.getPixelHeight());
00116
00117     m.mult(t);
00118
00119     //SDEBUG << "Projection matrix: " << m << std::endl;
00120
00121     glMatrixMode (GL_PROJECTION);
00122     GLP::glLoadMatrixf(m.getValues());
00123
00124     // set the viewing
00125
00126     getViewing(m, port.getPixelWidth(), port.getPixelHeight());
00127
00128     //SDEBUG << "Viewing matrix: " << m << std::endl;
00129
00130     glMatrixMode (GL_MODELVIEW );
00131     GLP::glLoadMatrixf(m.getValues());
00132 }
00133
00137 void Camera::setupProjection(      DrawActionBase *OSG_CHECK_ARG(action),
00138                              const Viewport       &port                 )
00139 {
00140     Matrixr m, t;
00141
00142     // set the projection
00143
00144     getProjection           (m, port.getPixelWidth(), port.getPixelHeight());
00145     getProjectionTranslation(t, port.getPixelWidth(), port.getPixelHeight());
00146
00147     m.mult(t);
00148
00149     //SDEBUG << "Projection matrix: " << m << std::endl;
00150
00151     glMatrixMode (GL_PROJECTION);
00152     GLP::glLoadMatrixf(m.getValues());
00153 }
00154
00158 #endif
00159 
00163 void Camera::getProjection(Matrixr &OSG_CHECK_ARG(result),
00164                            UInt32   OSG_CHECK_ARG(width ),
00165                            UInt32   OSG_CHECK_ARG(height))
00166 {
00167     SFATAL << "Camera::getProjection: NIY" << std::endl;
00168 }
00169
00174 void Camera::getProjectionTranslation(Matrixr &result,
00175                                       UInt32   OSG_CHECK_ARG(width ),
00176                                       UInt32   OSG_CHECK_ARG(height))
00177 {
00178     result.setIdentity();
00179 }
00180
00185 void Camera::getViewing(Matrixr &result,
00186                         UInt32   OSG_CHECK_ARG(width ),
00187                         UInt32   OSG_CHECK_ARG(height))
00188 {
00189     if (getBeacon() == NULL)
00190     {
00191         SWARNING << "Camera::setup: no beacon!" << std::endl;
00192         return;
00193     }
00194
00195     getBeacon()->getToWorld(result);
00196     result.invert();
00197 }
00198
00202 void Camera::getFrustum(FrustumVolume& result, const Viewport& p)
00203 {
00204     Matrixr mv,prt,pr;
00205
00206     getProjection           (pr , p.getPixelWidth(), p.getPixelHeight());
00207     getProjectionTranslation(prt, p.getPixelWidth(), p.getPixelHeight());
00208     getViewing              (mv , p.getPixelWidth(), p.getPixelHeight());
00209
00210     pr.mult(prt);
00211     pr.mult(mv );
00212
00213     result.setPlanes(pr);
00214 }
00215
00218 void Camera::getFrustum(FrustumVolume& result,
00219                         UInt32  width, UInt32  height)
00220 {
00221     Matrix mv,prt,pr;
00222
00223     getProjection           (pr , width, height);
00224     getProjectionTranslation(prt, width, height);
00225     getViewing              (mv , width, height);
00226
00227     pr.mult(prt);
00228     pr.mult(mv );
00229
00230     result.setPlanes(pr);
00231 }
00232
00237 void Camera::getWorldToScreen(Matrixr &result, const Viewport& p)
00238 {
00239     Matrixr mv,prt,pr;
00240
00241     getProjection           (result, p.getPixelWidth(), p.getPixelHeight());
00242     getProjectionTranslation(prt   , p.getPixelWidth(), p.getPixelHeight());
00243     getViewing              (mv    , p.getPixelWidth(), p.getPixelHeight());
00244
00245     result.mult(prt);
00246     result.mult(mv );
00247 }
00248
00253 void Camera::getDecoration(Matrixr &result, UInt32 width, UInt32 height)
00254 {
00255     result.setIdentity();
00256 }
00257
00258
00259 Matrixr Camera::getProjectionVal           (   UInt32         width,
00260                                           UInt32         height)
00261 {
00262    Matrixr temp_mat;
00263    this->getProjection(temp_mat, width,height);
00264    return temp_mat;
00265 }
00266
00267 Matrixr Camera::getProjectionTranslationVal(   UInt32         width,
00268                                           UInt32         height)
00269 {
00270    Matrixr temp_mat;
00271    this->getProjectionTranslation(temp_mat, width, height);
00272    return temp_mat;
00273 }
00274
00275 Matrixr Camera::getViewingVal              ( UInt32         width,
00276                                           UInt32         height)
00277 {
00278    Matrixr temp_mat;
00279    this->getViewing(temp_mat, width, height);
00280    return temp_mat;
00281 }
00282
00283 FrustumVolume Camera::getFrustumVal        (  const Viewport      &port  )
00284 {
00285    FrustumVolume vol;
00286    this->getFrustum(vol, port);
00287    return vol;
00288 }
00289
00290 Matrixr Camera::getWorldToScreenVal        ( const Viewport      &port  )
00291 {
00292    Matrixr temp_mat;
00293    this->getWorldToScreen(temp_mat, port);
00294    return temp_mat;
00295 }
00296
00297 Matrixr Camera::getDecorationVal(UInt32 width, UInt32 height)
00298 {
00299    Matrixr temp_mat;
00300    this->getDecoration(temp_mat, width, height);
00301    return temp_mat;
00302 }
00303
00304 Vec2u Camera::tileGetFullSize(void) const
00305 {
00306     return Vec2u(0u, 0u);
00307 }
00308
00309 Vec4f Camera::tileGetRegion(void) const
00310 {
00311     return Vec4f(0.f, 1.f, 0.f, 1.f);
00312 }
00313
00314 #ifndef OSG_EMBEDDED
00315 
00320 bool Camera::calcViewRay(      Line    &line,
00321                                Int32     x,
00322                                Int32     y,
00323                          const Viewport &port,
00324                                Real32   *t   )
00325 {
00326     if(port.getPixelWidth() <= 0 || port.getPixelHeight() <= 0)
00327     {
00328         return false;
00329     }
00330
00331     Matrix proj, projtrans, view;
00332
00333     getProjection(proj,
00334                   port.getPixelWidth(),
00335                   port.getPixelHeight());
00336
00337     getProjectionTranslation(projtrans,
00338                              port.getPixelWidth(),
00339                              port.getPixelHeight());
00340
00341     getViewing(view,
00342                port.getPixelWidth(),
00343                port.getPixelHeight());
00344
00345     Matrix wctocc = proj;
00346
00347     wctocc.mult(projtrans);
00348     wctocc.mult(view);
00349
00350     Matrix cctowc;
00351
00352     cctowc.invertFrom(wctocc);
00353
00354     Real32 rx(0.f), ry(0.f);
00355     port.getNormalizedCoordinates(rx, ry, x, y);
00356
00357     Pnt3f from, at;
00358
00359     cctowc.multFull(Pnt3f(rx, ry, -1), from);
00360     cctowc.multFull(Pnt3f(rx, ry,  1), at  );
00361
00362     Vec3f dir = at - from;
00363
00364     if(t != NULL)
00365     {
00366         *t = dir.length();
00367     }
00368
00369     line.setValue(from, dir);
00370
00371     return true;
00372 }
00373 #endif
00374 
00375 /*------------------------------- dump ----------------------------------*/
00376
00377 void Camera::dump(      UInt32    OSG_CHECK_ARG(uiIndent),
00378                   const BitVector OSG_CHECK_ARG(bvFlags )) const
00379 {
00380     SLOG << "Dump Camera NI" << std::endl;
00381 }