1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
52 #include "OSGViewport.h"
53 #include "OSGWindow.h"
56 #include "OSGCamera.h"
60 // Documentation for this class is emited in the
61 // OSGCameraBase.cpp file.
62 // To modify it, please change the .fcd file (OSGCamera.fcd) and
63 // regenerate the base file.
65 /***************************************************************************\
67 \***************************************************************************/
69 void Camera::initMethod(InitPhase ePhase
)
71 Inherited::initMethod(ePhase
);
74 /***************************************************************************\
76 \***************************************************************************/
78 Camera::Camera(void) :
83 Camera::Camera(const Camera
&source
) :
92 void Camera::changed(ConstFieldMaskArg whichField
,
96 Inherited::changed(whichField
, origin
, details
);
100 /*! Get/calculate the projection matrix for this camera.
103 void Camera::getProjection(Matrix
&OSG_CHECK_ARG(result
),
104 UInt32
OSG_CHECK_ARG(width
),
105 UInt32
OSG_CHECK_ARG(height
))
107 SFATAL
<< "Camera::getProjection: NIY" << std::endl
;
110 /*! Get/calculate the projection translation matrix for this camera. The
114 void Camera::getProjectionTranslation(Matrix
&result
,
115 UInt32
OSG_CHECK_ARG(width
),
116 UInt32
OSG_CHECK_ARG(height
))
118 result
.setIdentity();
121 /*! Get/calculate the viewing matrix for this camera. This is the inverse
122 of the beacon's toWorld transformation.
125 void Camera::getViewing(Matrix
&result
,
126 UInt32
OSG_CHECK_ARG(width
),
127 UInt32
OSG_CHECK_ARG(height
))
129 if (getBeacon() == NULL
)
131 SWARNING
<< "Camera::setup: no beacon!" << std::endl
;
135 getBeacon()->getToWorld(result
);
140 /*! Calculate the frustum of this camera's visible area (w,h instead port).
142 void Camera::getFrustum(FrustumVolume
& result
,
143 UInt32 width
, UInt32 height
)
147 getProjection (pr
, width
, height
);
148 getProjectionTranslation(prt
, width
, height
);
149 getViewing (mv
, width
, height
);
154 result
.setPlanes(pr
);
157 /*! Get/calculate the decoration matrix for this camera.
158 The default is identity.
161 void Camera::getDecoration(Matrix
&result
, UInt32 width
, UInt32 height
)
163 result
.setIdentity();
167 Matrix
Camera::getProjectionVal(UInt32 width
,
171 this->getProjection(temp_mat
, width
,height
);
175 Matrix
Camera::getProjectionTranslationVal(UInt32 width
,
179 this->getProjectionTranslation(temp_mat
, width
, height
);
183 Matrix
Camera::getViewingVal(UInt32 width
,
187 this->getViewing(temp_mat
, width
, height
);
191 Matrix
Camera::getDecorationVal(UInt32 width
, UInt32 height
)
194 this->getDecoration(temp_mat
, width
, height
);
198 /*! Calculate the frustum of this camera's visible area.
201 void Camera::calcFrustum(FrustumVolume
&result
, const Viewport
&p
)
207 p
.calcPixelHeight());
208 getProjectionTranslation(prt
,
210 p
.calcPixelHeight());
213 p
.calcPixelHeight());
218 result
.setPlanes(pr
);
221 /*! Calculate the matrix that transforms world coordinates into the screen
222 coordinate system for this camera.
225 void Camera::calcWorldToScreen(Matrix
&result
, const Viewport
&p
)
229 getProjection (result
,
231 p
.calcPixelHeight());
232 getProjectionTranslation(prt
,
234 p
.calcPixelHeight());
237 p
.calcPixelHeight());
243 FrustumVolume
Camera::calcFrustumVal(const Viewport
&port
)
246 this->calcFrustum(vol
, port
);
250 Matrix
Camera::calcWorldToScreenVal(const Viewport
&port
)
253 this->calcWorldToScreen(temp_mat
, port
);
257 Vec2u
Camera::tileGetFullSize(void) const
259 return Vec2u(0u, 0u);
262 Vec4f
Camera::tileGetRegion(void) const
264 return Vec4f(0.f
, 1.f
, 0.f
, 1.f
);
267 /*! Calculate a ray that starts at the camera position and goes through the
268 pixel \a x, \a y in the viewport \a port. \a x and \a y are relative to the
269 viewport's upper left corner. \a t is the length of the viewing ray.
272 bool Camera::calcViewRay( Line
&line
,
275 const Viewport
&port
,
278 if(port
.calcPixelWidth() <= 0 || port
.calcPixelHeight() <= 0)
283 Matrix proj
, projtrans
, view
;
286 port
.calcPixelWidth(),
287 port
.calcPixelHeight());
289 getProjectionTranslation(projtrans
,
290 port
.calcPixelWidth(),
291 port
.calcPixelHeight());
294 port
.calcPixelWidth(),
295 port
.calcPixelHeight());
297 Matrix wctocc
= proj
;
299 wctocc
.mult(projtrans
);
304 cctowc
.invertFrom(wctocc
);
306 Real32
rx(0.f
), ry(0.f
);
307 port
.calcNormalizedCoordinates(rx
, ry
, x
, y
);
311 cctowc
.multFull(Pnt3f(rx
, ry
, -1), from
);
312 cctowc
.multFull(Pnt3f(rx
, ry
, 1), at
);
314 Vec3f dir
= at
- from
;
321 line
.setValue(from
, dir
);
326 /*------------------------------- dump ----------------------------------*/
328 void Camera::dump( UInt32
OSG_CHECK_ARG(uiIndent
),
329 const BitVector
OSG_CHECK_ARG(bvFlags
)) const
331 SLOG
<< "Dump Camera NI" << std::endl
;