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"
48 #include "OSGMatrixUtility.h"
49 #include "OSGQuaternion.h"
52 #include "OSGProjectionCameraDecorator.h"
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) :
68 ProjectionCameraDecorator::ProjectionCameraDecorator(
69 const ProjectionCameraDecorator
&source
) :
75 ProjectionCameraDecorator::~ProjectionCameraDecorator(void)
79 /*----------------------------- class specific ----------------------------*/
81 void ProjectionCameraDecorator::initMethod(InitPhase ePhase
)
83 Inherited::initMethod(ePhase
);
86 void ProjectionCameraDecorator::changed(ConstFieldMaskArg whichField
,
90 Inherited::changed(whichField
, origin
, details
);
92 if(whichField
& SurfaceFieldMask
)
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 "
113 Pnt3f
p0(getSurface(0));
116 d1
= getSurface(1) - p0
;
117 d2
= getSurface(3) - p0
;
123 FWARNING(("ProjectionCameraDecorator: normal is zero, surface "
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();
144 FWARNING(("ProjectionCameraDecorator::getViewing: no user!\n"));
146 Camera
*pCamera
= getDecoratee();
150 result
.setIdentity();
155 pCamera
->getBeacon()->getToWorld(result
);
161 pUser
->getToWorld(result
);
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 ();
176 FWARNING(("ProjectionCameraDecorator::getProjection: no "
179 result
.setIdentity();
187 camera
->getBeacon()->getToWorld(cam
);
191 FWARNING(("ProjectionCameraDecorator::getProjection: no user!\n"));
197 pUser
->getToWorld(user
);
204 Pnt3f
viewer(cam
[3]);
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(
232 UInt32
OSG_CHECK_ARG(width
),
233 UInt32
OSG_CHECK_ARG(height
))
235 Camera
*camera
= getDecoratee();
236 Node
*pUser
= getUser ();
240 FFATAL(("ProjectionCameraDecorator::getProjectionTranslation: "
243 result
.setIdentity();
251 camera
->getBeacon()->getToWorld(cam
);
255 FWARNING(("ProjectionCameraDecorator::getProjectionTranslation: "
262 pUser
->getToWorld(user
);
268 Vec3f
dir(getNormal().getNormal()),
269 up (getBottom().getNormal()),
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
);
295 static bool hack
= true;
296 Quaternion
q( getNormal().getNormal(), Vec3f(0,0,1));
300 q
.getValueAsAxisDeg(v
,a
);
310 if(v
[1] < 0) ang1
= -ang1
;
315 q1
.setValueAsAxisDeg(0,1,0,ang1
);
316 q2
.setValueAsAxisDeg(1,0,0,ang2
);