changed: gcc8 base update
[opensg.git] / Source / System / Window / Camera / OSGProjectionCameraDecorator.cpp
blob05e3a3475eb2b3892fafc094e50bc00b0c56dfbb
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"
48 #include "OSGMatrixUtility.h"
49 #include "OSGQuaternion.h"
50 #include "OSGNode.h"
52 #include "OSGProjectionCameraDecorator.h"
54 OSG_USING_NAMESPACE
56 // Documentation for this class is emited in the
57 // OSGProjectionCameraDecoratorBase.cpp file.
58 // To modify it, please change the .fcd file (OSGProjectionCameraDecorator.fcd) and
59 // regenerate the base file.
61 /*----------------------- constructors & destructors ----------------------*/
63 ProjectionCameraDecorator::ProjectionCameraDecorator(void) :
64 Inherited()
68 ProjectionCameraDecorator::ProjectionCameraDecorator(
69 const ProjectionCameraDecorator &source) :
71 Inherited(source)
75 ProjectionCameraDecorator::~ProjectionCameraDecorator(void)
79 /*----------------------------- class specific ----------------------------*/
81 void ProjectionCameraDecorator::initMethod(InitPhase ePhase)
83 Inherited::initMethod(ePhase);
86 void ProjectionCameraDecorator::changed(ConstFieldMaskArg whichField,
87 UInt32 origin,
88 BitVector details)
90 Inherited::changed(whichField, origin, details);
92 if(whichField & SurfaceFieldMask)
93 updateData();
96 void ProjectionCameraDecorator::dump( UInt32 ,
97 const BitVector ) const
99 SLOG << "Dump ProjectionCameraDecorator NI" << std::endl;
102 /*! Update dependent data, i.e. the internal Field values.
104 void ProjectionCameraDecorator::updateData(void)
106 if(getMFSurface()->size() != 4)
108 FWARNING(("ProjectionCameraDecorator: only defined for 4 point "
109 "surfaces!\n"));
110 return;
113 Pnt3f p0(getSurface(0));
114 Vec3f d1,d2,n;
116 d1 = getSurface(1) - p0;
117 d2 = getSurface(3) - p0;
119 n = d1.cross(d2);
121 if(n.isZero())
123 FWARNING(("ProjectionCameraDecorator: normal is zero, surface "
124 "ill-defined!\n"));
125 return;
128 this->setLeft (Plane(d1,p0));
129 this->setBottom(Plane(d2,p0));
130 this->setNormal(Plane(n ,p0));
131 this->setWidth (d1.length());
132 this->setHeight(d2.length());
136 void ProjectionCameraDecorator::getViewing(Matrix &result,
137 UInt32 OSG_CHECK_ARG(width ),
138 UInt32 OSG_CHECK_ARG(height))
140 Node *pUser = getUser();
142 if(pUser == NULL)
144 FWARNING(("ProjectionCameraDecorator::getViewing: no user!\n"));
146 Camera *pCamera = getDecoratee();
148 if(pCamera == NULL)
150 result.setIdentity();
152 return;
155 pCamera->getBeacon()->getToWorld(result);
157 result.invert();
159 else
161 pUser->getToWorld(result);
163 result.invert();
167 void ProjectionCameraDecorator::getProjection(Matrix &result,
168 UInt32 OSG_CHECK_ARG(width ),
169 UInt32 OSG_CHECK_ARG(height))
171 Camera *camera = getDecoratee();
172 Node *pUser = getUser ();
174 if(camera == NULL)
176 FWARNING(("ProjectionCameraDecorator::getProjection: no "
177 "decoratee!\n"));
179 result.setIdentity();
181 return;
185 Matrix cam,user;
187 camera->getBeacon()->getToWorld(cam);
189 if(pUser == NULL)
191 FWARNING(("ProjectionCameraDecorator::getProjection: no user!\n"));
193 user = cam;
195 else
197 pUser->getToWorld(user);
200 cam.invert();
202 cam.mult(user);
204 Pnt3f viewer(cam[3]);
206 Real32 eyeFac;
208 if(getLeftEye())
210 eyeFac=-.5;
212 else
214 eyeFac=+.5;
217 viewer += Vec3f(cam[0]) * eyeFac * getEyeSeparation();
219 Real32 dist = getNormal().distance(viewer),
220 dl = getLeft ().distance(viewer),
221 db = getBottom().distance(viewer),
222 f = camera->getNear() / dist;
224 MatrixFrustum(result, -dl * f, (getWidth ()-dl) * f,
225 -db * f, (getHeight()-db) * f,
226 camera->getNear(), camera->getFar() );
230 void ProjectionCameraDecorator::getProjectionTranslation(
231 Matrix &result,
232 UInt32 OSG_CHECK_ARG(width ),
233 UInt32 OSG_CHECK_ARG(height))
235 Camera *camera = getDecoratee();
236 Node *pUser = getUser ();
238 if(camera == NULL)
240 FFATAL(("ProjectionCameraDecorator::getProjectionTranslation: "
241 "no decoratee!\n"));
243 result.setIdentity();
245 return;
249 Matrix cam,user;
251 camera->getBeacon()->getToWorld(cam);
253 if(pUser == NULL)
255 FWARNING(("ProjectionCameraDecorator::getProjectionTranslation: "
256 "no user!\n"));
258 user = cam;
260 else
262 pUser->getToWorld(user);
265 cam.invert();
266 cam.mult(user);
268 Vec3f dir(getNormal().getNormal()),
269 up (getBottom().getNormal()),
270 right;
271 Vec3f pos(cam[3]);
273 Real32 eyeFac;
275 if(getLeftEye())
277 eyeFac=-.5;
279 else
281 eyeFac=+.5;
284 pos += Vec3f(cam[0]) * eyeFac * getEyeSeparation();
286 right = up.cross(dir);
287 up = dir.cross(right);
289 result.setIdentity();
290 result.setValue(right, up, dir, pos);
291 result.invert();
293 result.mult(cam);
294 #if 0
295 static bool hack = true;
296 Quaternion q( getNormal().getNormal(), Vec3f(0,0,1));
298 Vec3f v;
299 Real32 a;
300 q.getValueAsAxisDeg(v,a);
302 // HACKKK
303 if(hack && a > 50)
305 q.getValue(result);
307 Real32 ang1 = 54.44;
308 Real32 ang2 = 6.27;
310 if(v[1] < 0) ang1 = -ang1;
312 Quaternion q1,q2;
313 Matrix m1,m2;
315 q1.setValueAsAxisDeg(0,1,0,ang1);
316 q2.setValueAsAxisDeg(1,0,0,ang2);
318 q1.getValue(m1);
319 q2.getValue(m2);
321 result = m2;
322 result.mult(m1);
324 #endif