Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleaned up the cameramanager, commented all functions, corrected spelling mistakes :-P

File size: 10.5 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    State::setCamera( camera,  dynamic_cast<CameraTarget*>(object));
116    return;
117  }
118
119 printf("ERROR CAMERAMANAGER: Couldn't change target for camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),objectName.c_str() );
120
121}
122
123/**
124 *  Attaches a camera to a new pnode. (the camera follows the pnode)
125 *
126 * @param cameraName the name of the camera
127 * @param className the class of the new parent node
128 * @param objectName the name of the new parent node
129 */
130void CameraMan::attachCamera(const std::string& cameraName, const std::string& className, const std::string& targetEntity)
131{
132  BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity);
133  Camera* camera = Camera::objectList().getObject(cameraName);
134
135  if( object != NULL && camera != NULL && object->isA(PNode::staticClassID()) )
136  {
137    camera->target->atach(dynamic_cast<PNode*>(object));
138    camera->setViewMode(Camera::ViewNormal);
139    State::setCamera( camera,  dynamic_cast<CameraTarget*>(object));
140    return;
141  }
142
143  printf("ERROR CAMERAMANAGER: Couldn't attach camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),targetEntity.c_str() );
144
145}
146
147
148/**
149 *  Sets a camera to a new position
150 *
151 * @param cameraName the name of the camera
152 * @param x the new x coordinate
153 * @param y the new y coordinate
154 * @param z the new z coordinate
155 */
156void CameraMan::jumpCam(const std::string& cameraName, float x, float y, float z)
157{
158  Camera* camera = Camera::objectList().getObject(cameraName);
159  if( camera != NULL )
160  {
161    camera->target->jump( x, y, z );
162  }
163}
164
165/**
166 *  Moves a camera slowly to a new position
167 *
168 * @param cameraName the name of the camera
169 * @param x the new x coordinate
170 * @param y the new y coordinate
171 * @param z the new z coordinate
172 */
173void CameraMan::moveCam(const std::string& cameraName, float x, float y, float z)
174{
175  Camera* camera = Camera::objectList().getObject(cameraName);
176  if( camera != NULL )
177  {
178    camera->target->trans( x, y, z );
179  }
180}
181
182/**
183 *  Sets the coordinate of a camera relative to its target.
184 *
185 * @param cameraName the name of the camera
186 * @param x the new x coordinate
187 * @param y the new y coordinate
188 * @param z the new z coordinate
189 */
190void CameraMan::setRelCameraCoor(const std::string& cameraName, float x, float y, float z)
191{
192  Camera* camera = Camera::objectList().getObject(cameraName);
193  camera->setRelCoor(x,y,z);
194  camera->target->setRelCoor(0,0,0);
195}
196
197/**
198 *  Sets the coordinate of a camera relative to its target. (softly)
199 *
200 * @param cameraName the name of the camera
201 * @param x the new x coordinate
202 * @param y the new y coordinate
203 * @param z the new z coordinate
204 */
205void CameraMan::setRelCameraCoorSoft(const std::string& cameraName, float x, float y, float z, float bias)
206{
207  Camera* newCam = Camera::objectList().getObject(cameraName);
208  newCam->setRelCoor(x,y,z);
209  newCam->target->setRelCoorSoft(0,0,0);
210}
211
212/**
213 *  Sets a new camera as the current one.
214 *
215 * @param cameraName the name of the new camera
216 */
217void CameraMan::setCam(const std::string& cameraName)
218{
219  Camera* camera = Camera::objectList().getObject(cameraName);
220
221  if(camera != NULL)
222  {
223    this->setCam(camera);
224    return;
225  }
226  printf("ERROR CAMERAMANAGER: Couldn't set camera : %s \n", cameraName.c_str());
227}
228
229/**
230 *  Sets a new camera as the current one.
231 *
232 * @param camera a pointer to the new camera
233 */
234void CameraMan::setCam(Camera* newCamera)
235{
236  if( newCamera == NULL)
237  {
238    PRINTF(0)("trying to add a zero camera! uiuiui!\n");
239    return;
240  }
241 
242  State::setCamera(newCamera, newCamera->getTarget());
243  OrxSound::SoundEngine::getInstance()->setListener(newCamera);
244
245  this->fadeToBlack->setRelCoor(0., 0., 0.);
246  this->fadeToBlack->setParent(newCamera);
247
248}
249
250/**
251 *  Sets the clipregion of all the cameras
252 *
253 * @param nearClip the new near clip
254 * @param nearClip the new far clip
255 */
256void CameraMan::setClipRegion(float nearClip, float farClip)
257{
258  this->nearClip=nearClip;
259  this->farClip=farClip;
260
261  for (ObjectList<Camera>::const_iterator it = Camera::objectList().begin();
262       it != Camera::objectList().end();
263       ++it)
264  {
265     (*it)->setClipRegion(this->nearClip, this->farClip);
266  }
267}
268
269/**
270 *  Changes the target of the current camera. (where the camera looks at)
271 *
272 * @param className the class of the new target
273 * @param objectName the name of the new target
274 */
275void CameraMan::changeCurrTarget(const std::string& className, const std::string& objectName)
276{
277  this->changeTarget( (State::getCamera())->getName() , className, objectName);
278}
279
280/**
281 *  Attaches the current camera to a new pnode. (the camera follows the pnode)
282 *
283 * @param className the class of the new parent node
284 * @param objectName the name of the new parent node
285 */
286void CameraMan::attachCurrCamera(const std::string& className, const std::string& targetEntity)
287{
288  this->attachCamera( (State::getCamera())->getName(), className,  targetEntity);
289}
290
291/**
292 *  Sets the current camera to a new position
293 *
294 * @param x the new x coordinate
295 * @param y the new y coordinate
296 * @param z the new z coordinate
297 */
298void CameraMan::jumpCurrCam(float x, float y, float z)
299{
300  this->jumpCam((State::getCamera())->getName(), x, y, z);
301}
302
303/**
304 *  Moves the current camera slowly to a new position
305 *
306 * @param x the new x coordinate
307 * @param y the new y coordinate
308 * @param z the new z coordinate
309 */
310void CameraMan::moveCurrCam(float x, float y, float z)
311{
312  this->moveCam( (State::getCamera())->getName(), x, y, z);
313}
314
315/**
316 *  Toggles the fadestate (from in to out and via versa)
317 */
318void CameraMan::togglFade()
319{
320 if( this->fadeToBlack)
321    fadeToBlack->toggleFade();
322}
323
324/**
325 *  Sets the fadestate immediately to black (fade out)
326 */
327void CameraMan::initFadeBlack()
328{
329  if( this->fadeToBlack)
330    fadeToBlack->initFadeBlack();
331}
Note: See TracBrowser for help on using the repository browser.