changed: gcc8 base update
[opensg.git] / Source / System / Window / Base / OSGCamera.cpp
blobe4f73407b3f9e7eaa2f8fcb26a6e153ab423c427
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
47 #include "OSGLog.h"
49 #include "OSGGL.h"
51 #include "OSGNode.h"
52 #include "OSGViewport.h"
53 #include "OSGWindow.h"
54 #include "OSGLine.h"
56 #include "OSGCamera.h"
58 OSG_USING_NAMESPACE
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 /***************************************************************************\
66 * Class methods *
67 \***************************************************************************/
69 void Camera::initMethod(InitPhase ePhase)
71 Inherited::initMethod(ePhase);
74 /***************************************************************************\
75 * Instance methods *
76 \***************************************************************************/
78 Camera::Camera(void) :
79 Inherited()
83 Camera::Camera(const Camera &source) :
84 Inherited(source)
88 Camera::~Camera(void)
92 void Camera::changed(ConstFieldMaskArg whichField,
93 UInt32 origin,
94 BitVector details)
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
111 default is identity.
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;
132 return;
135 getBeacon()->getToWorld(result);
136 result.invert();
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)
145 Matrix mv,prt,pr;
147 getProjection (pr , width, height);
148 getProjectionTranslation(prt, width, height);
149 getViewing (mv , width, height);
151 pr.mult(prt);
152 pr.mult(mv );
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,
168 UInt32 height)
170 Matrix temp_mat;
171 this->getProjection(temp_mat, width,height);
172 return temp_mat;
175 Matrix Camera::getProjectionTranslationVal(UInt32 width,
176 UInt32 height)
178 Matrix temp_mat;
179 this->getProjectionTranslation(temp_mat, width, height);
180 return temp_mat;
183 Matrix Camera::getViewingVal(UInt32 width,
184 UInt32 height)
186 Matrix temp_mat;
187 this->getViewing(temp_mat, width, height);
188 return temp_mat;
191 Matrix Camera::getDecorationVal(UInt32 width, UInt32 height)
193 Matrix temp_mat;
194 this->getDecoration(temp_mat, width, height);
195 return temp_mat;
198 /*! Calculate the frustum of this camera's visible area.
201 void Camera::calcFrustum(FrustumVolume &result, const Viewport &p)
203 Matrix mv,prt,pr;
205 getProjection (pr ,
206 p.calcPixelWidth (),
207 p.calcPixelHeight());
208 getProjectionTranslation(prt,
209 p.calcPixelWidth (),
210 p.calcPixelHeight());
211 getViewing (mv ,
212 p.calcPixelWidth (),
213 p.calcPixelHeight());
215 pr.mult(prt);
216 pr.mult(mv );
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)
227 Matrix mv,prt,pr;
229 getProjection (result,
230 p.calcPixelWidth (),
231 p.calcPixelHeight());
232 getProjectionTranslation(prt ,
233 p.calcPixelWidth (),
234 p.calcPixelHeight());
235 getViewing (mv ,
236 p.calcPixelWidth (),
237 p.calcPixelHeight());
239 result.mult(prt);
240 result.mult(mv );
243 FrustumVolume Camera::calcFrustumVal(const Viewport &port)
245 FrustumVolume vol;
246 this->calcFrustum(vol, port);
247 return vol;
250 Matrix Camera::calcWorldToScreenVal(const Viewport &port)
252 Matrix temp_mat;
253 this->calcWorldToScreen(temp_mat, port);
254 return temp_mat;
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,
273 Int32 x,
274 Int32 y,
275 const Viewport &port,
276 Real32 *t )
278 if(port.calcPixelWidth() <= 0 || port.calcPixelHeight() <= 0)
280 return false;
283 Matrix proj, projtrans, view;
285 getProjection(proj,
286 port.calcPixelWidth(),
287 port.calcPixelHeight());
289 getProjectionTranslation(projtrans,
290 port.calcPixelWidth(),
291 port.calcPixelHeight());
293 getViewing(view,
294 port.calcPixelWidth(),
295 port.calcPixelHeight());
297 Matrix wctocc = proj;
299 wctocc.mult(projtrans);
300 wctocc.mult(view);
302 Matrix cctowc;
304 cctowc.invertFrom(wctocc);
306 Real32 rx(0.f), ry(0.f);
307 port.calcNormalizedCoordinates(rx, ry, x, y);
309 Pnt3f from, at;
311 cctowc.multFull(Pnt3f(rx, ry, -1), from);
312 cctowc.multFull(Pnt3f(rx, ry, 1), at );
314 Vec3f dir = at - from;
316 if(t != NULL)
318 *t = dir.length();
321 line.setValue(from, dir);
323 return true;
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;