Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cleanup/src/world_entities/tools/cameraman.cc @ 10595

Last change on this file since 10595 was 10595, checked in by snellen, 17 years ago

now the real cameraTarget is set in the state as target… not just the pnode of it!

File size: 10.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Filip Gospodinov
13   co-programmer: Silvan Nellen
14*/
15
16#include "cameraman.h"
17#include "blackscreen.h"
18#include "loading/load_param_xml.h"
19#include "sound_engine.h"
20#include "script_class.h"
21
22ObjectListDefinition(CameraMan);
23
24CREATE_SCRIPTABLE_CLASS(CameraMan,
25                        /// Camera management
26                        addMethod("changeTarget", Executor3<CameraMan, lua_State*, const std::string&, const std::string&,const std::string&>(&CameraMan::changeTarget))
27                        ->addMethod("attachCamera", Executor3<CameraMan, lua_State*,const std::string&,const std::string&,const std::string&>(&CameraMan::attachCamera))
28                        ->addMethod("jumpCam", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::jumpCam))
29                        ->addMethod("moveCam", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::moveCam))
30                        ->addMethod("setRelCoor", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::setRelCameraCoor))
31                        ->addMethod("setRelCoorSoft", Executor5<CameraMan, lua_State*,const std::string&,float,float,float,float>(&CameraMan::setRelCameraCoorSoft))
32                        ->addMethod("setCam", Executor1<CameraMan, lua_State*, const std::string&>(&CameraMan::setCam))
33                        /// Current Camera
34                        ->addMethod("changeCurrTarget", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::changeCurrTarget))
35                        ->addMethod("attachCurrCamera", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::attachCurrCamera))
36                        ->addMethod("jumpCurrCam", Executor3<CameraMan, lua_State*,float,float,float>(&CameraMan::jumpCurrCam))
37                        ->addMethod("moveCurrCam", Executor3<CameraMan, lua_State*,float,float,float>(&CameraMan::moveCurrCam))
38                        /// Fading
39                        ->addMethod("toggleFade", Executor0<CameraMan, lua_State*>(&CameraMan::togglFade))
40                        ->addMethod("initFadeBlack", Executor0<CameraMan, lua_State*>(&CameraMan::initFadeBlack))
41                        /// Polling
42                        ->addMethod("getCurrCameraCoorX", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorX))
43                        ->addMethod("getCurrCameraCoorY", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorY))
44                        ->addMethod("getCurrCameraCoorZ", Executor0ret<CameraMan, lua_State*,float>(&CameraMan::getCurrCameraCoorZ))
45                       );
46
47
48
49/**
50 * constructor of the CameraManager
51 */
52CameraMan::CameraMan(const TiXmlElement* root)
53{
54  this->registerObject(this, CameraMan::_objectList);
55
56  this->nearClip = 1.0;
57  this->farClip = 1000.0;
58
59  this->fadeToBlack=new BlackScreen();
60
61  this->setCam( State::getCamera() );
62
63  if (root != NULL)
64    this->loadParams(root);
65}
66
67/**
68 *  Loads the parameters of the CameraManager from the xml file
69 * @param root reference to the xml root element
70 */
71void CameraMan::loadParams(const TiXmlElement* root)
72{
73  BaseObject::loadParams(root);
74  LoadParamXML(root, "Cameras", this, CameraMan, createCameras);
75}
76
77/**
78 *  Creates all the Cameras
79 * @param root reference to the xml root element
80 */
81void CameraMan::createCameras(const TiXmlElement* camerasTag)
82{
83    LOAD_PARAM_START_CYCLE(camerasTag, object);
84    {
85      this->createCam(object);
86    }
87    LOAD_PARAM_END_CYCLE(object);
88}
89
90/**
91 *  Creates a Camera from a xml node
92 * @param root reference to the xml node
93 */
94void CameraMan::createCam(const TiXmlElement* root)
95{
96  Camera* newCam = new Camera(root);
97  newCam->setClipRegion(nearClip, farClip);
98}
99
100/**
101 *  Changes the target of a camera. (where the camera looks at)
102 *
103 * @param cameraName the name of the camera whose target is changed
104 * @param className the class of the new target
105 * @param objectName the name of the new target
106 */
107void CameraMan::changeTarget(const std::string& cameraName,const std::string& className, const std::string& objectName)
108{
109  BaseObject* object = ObjectListBase::getBaseObject(className, objectName);
110  Camera* camera = Camera::objectList().getObject(cameraName);
111
112  if( object != NULL && camera != NULL && object->isA(PNode::staticClassID()))
113  {
114    camera->lookAt(dynamic_cast<PNode*>(object));
115    if( camera == State::getCamera() ) // If it is the current cam, we have to update the target
116    State::setCamera( camera, camera->getTarget());
117    return;
118  }
119
120 printf("ERROR CAMERAMANAGER: Couldn't change target for camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),objectName.c_str() );
121
122}
123
124/**
125 *  Attaches a camera to a new pnode. (the camera follows the pnode)
126 *
127 * @param cameraName the name of the camera
128 * @param className the class of the new parent node
129 * @param objectName the name of the new parent node
130 */
131void CameraMan::attachCamera(const std::string& cameraName, const std::string& className, const std::string& targetEntity)
132{
133  BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity);
134  Camera* camera = Camera::objectList().getObject(cameraName);
135
136  if( object != NULL && camera != NULL && object->isA(PNode::staticClassID()) )
137  {
138    camera->target->atach(dynamic_cast<PNode*>(object));
139    camera->setViewMode(Camera::ViewNormal);
140    if( camera == State::getCamera() ) // If it is the current cam, we have to update the target
141    State::setCamera( camera, camera->getTarget());
142    return;
143  }
144
145  printf("ERROR CAMERAMANAGER: Couldn't attach camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),targetEntity.c_str() );
146
147}
148
149
150/**
151 *  Sets a camera to a new position
152 *
153 * @param cameraName the name of the camera
154 * @param x the new x coordinate
155 * @param y the new y coordinate
156 * @param z the new z coordinate
157 */
158void CameraMan::jumpCam(const std::string& cameraName, float x, float y, float z)
159{
160  Camera* camera = Camera::objectList().getObject(cameraName);
161  if( camera != NULL )
162  {
163    camera->target->jump( x, y, z );
164  }
165}
166
167/**
168 *  Moves a camera slowly to a new position
169 *
170 * @param cameraName the name of the camera
171 * @param x the new x coordinate
172 * @param y the new y coordinate
173 * @param z the new z coordinate
174 */
175void CameraMan::moveCam(const std::string& cameraName, float x, float y, float z)
176{
177  Camera* camera = Camera::objectList().getObject(cameraName);
178  if( camera != NULL )
179  {
180    camera->target->trans( x, y, z );
181  }
182}
183
184/**
185 *  Sets the coordinate of a camera relative to its target.
186 *
187 * @param cameraName the name of the camera
188 * @param x the new x coordinate
189 * @param y the new y coordinate
190 * @param z the new z coordinate
191 */
192void CameraMan::setRelCameraCoor(const std::string& cameraName, float x, float y, float z)
193{
194  Camera* camera = Camera::objectList().getObject(cameraName);
195  camera->setRelCoor(x,y,z);
196  camera->target->setRelCoor(0,0,0);
197}
198
199/**
200 *  Sets the coordinate of a camera relative to its target. (softly)
201 *
202 * @param cameraName the name of the camera
203 * @param x the new x coordinate
204 * @param y the new y coordinate
205 * @param z the new z coordinate
206 */
207void CameraMan::setRelCameraCoorSoft(const std::string& cameraName, float x, float y, float z, float bias)
208{
209  Camera* newCam = Camera::objectList().getObject(cameraName);
210  newCam->setRelCoor(x,y,z);
211  newCam->target->setRelCoorSoft(0,0,0);
212}
213
214/**
215 *  Sets a new camera as the current one.
216 *
217 * @param cameraName the name of the new camera
218 */
219void CameraMan::setCam(const std::string& cameraName)
220{
221  Camera* camera = Camera::objectList().getObject(cameraName);
222
223  if(camera != NULL)
224  {
225    this->setCam(camera);
226    return;
227  }
228  printf("ERROR CAMERAMANAGER: Couldn't set camera : %s \n", cameraName.c_str());
229}
230
231/**
232 *  Sets a new camera as the current one.
233 *
234 * @param camera a pointer to the new camera
235 */
236void CameraMan::setCam(Camera* newCamera)
237{
238  if( newCamera == NULL)
239  {
240    PRINTF(0)("trying to add a zero camera! uiuiui!\n");
241    return;
242  }
243 
244  State::setCamera(newCamera, newCamera->getTarget());
245  OrxSound::SoundEngine::getInstance()->setListener(newCamera);
246
247  this->fadeToBlack->setRelCoor(0., 0., 0.);
248  this->fadeToBlack->setParent(newCamera);
249
250}
251
252/**
253 *  Sets the clipregion of all the cameras
254 *
255 * @param nearClip the new near clip
256 * @param nearClip the new far clip
257 */
258void CameraMan::setClipRegion(float nearClip, float farClip)
259{
260  this->nearClip=nearClip;
261  this->farClip=farClip;
262
263  for (ObjectList<Camera>::const_iterator it = Camera::objectList().begin();
264       it != Camera::objectList().end();
265       ++it)
266  {
267     (*it)->setClipRegion(this->nearClip, this->farClip);
268  }
269}
270
271/**
272 *  Changes the target of the current camera. (where the camera looks at)
273 *
274 * @param className the class of the new target
275 * @param objectName the name of the new target
276 */
277void CameraMan::changeCurrTarget(const std::string& className, const std::string& objectName)
278{
279  this->changeTarget( (State::getCamera())->getName() , className, objectName);
280}
281
282/**
283 *  Attaches the current camera to a new pnode. (the camera follows the pnode)
284 *
285 * @param className the class of the new parent node
286 * @param objectName the name of the new parent node
287 */
288void CameraMan::attachCurrCamera(const std::string& className, const std::string& targetEntity)
289{
290  this->attachCamera( (State::getCamera())->getName(), className,  targetEntity);
291}
292
293/**
294 *  Sets the current camera to a new position
295 *
296 * @param x the new x coordinate
297 * @param y the new y coordinate
298 * @param z the new z coordinate
299 */
300void CameraMan::jumpCurrCam(float x, float y, float z)
301{
302  this->jumpCam((State::getCamera())->getName(), x, y, z);
303}
304
305/**
306 *  Moves the current camera slowly to a new position
307 *
308 * @param x the new x coordinate
309 * @param y the new y coordinate
310 * @param z the new z coordinate
311 */
312void CameraMan::moveCurrCam(float x, float y, float z)
313{
314  this->moveCam( (State::getCamera())->getName(), x, y, z);
315}
316
317/**
318 *  Toggles the fadestate (from in to out and via versa)
319 */
320void CameraMan::togglFade()
321{
322 if( this->fadeToBlack)
323    fadeToBlack->toggleFade();
324}
325
326/**
327 *  Sets the fadestate immediately to black (fade out)
328 */
329void CameraMan::initFadeBlack()
330{
331  if( this->fadeToBlack)
332    fadeToBlack->initFadeBlack();
333}
Note: See TracBrowser for help on using the repository browser.