Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/cameraman.cc @ 10497

Last change on this file since 10497 was 10497, checked in by bknecht, 18 years ago

added some more scriptable functions to cameraManager

File size: 11.2 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 "shell_command.h"
17#include "cameraman.h"
18#include "game_world_data.h"
19#include "state.h"
20#include "sound_engine.h"
21#include <string>
22#include "script_class.h"
23#include "loading/load_param_xml.h"
24#include "blackscreen.h"
25#include "p_node.h"
26#include "camera.h"
27
28ObjectListDefinition(CameraMan);
29
30SHELL_COMMAND(camerainfo, CameraMan, cameraInfo);
31
32
33CREATE_SCRIPTABLE_CLASS(CameraMan,
34                        addMethod("changeCurrTarget", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::changeCurrTarget))
35                        ->addMethod("atachCurrCameraToWorldEntity", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::atachCurrCameraToWorldEntity))
36                        ->addMethod("changeTarget", Executor3<CameraMan, lua_State*, const std::string&, const std::string&,const std::string&>(&CameraMan::changeTarget))
37                        ->addMethod("atachCameraToWorldEntity", Executor3<CameraMan, lua_State*,const std::string&,const std::string&,const std::string&>(&CameraMan::atachCameraToWorldEntity))
38                        ->addMethod("detachCurrCamera", Executor0<CameraMan, lua_State*>(&CameraMan::detachCurrCamera))
39                        ->addMethod("setCam", Executor1<CameraMan, lua_State*, const std::string&>(&CameraMan::setCam))
40                        ->addMethod("toggleFade", Executor0<CameraMan, lua_State*>(&CameraMan::togglFade))
41                        ->addMethod("initFadeBlack", Executor0<CameraMan, lua_State*>(&CameraMan::initFadeBlack))
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                        ->addMethod("jumpCurrCam", Executor3<CameraMan, lua_State*,float,float,float>(&CameraMan::jumpCurrCam))
46                        ->addMethod("jumpCam", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::jumpCam))
47                        //->addMethod("setViewMode", Executor2<CameraMan, lua_State*,const std::string&,const std::string&>(&CameraMan::setViewMode))
48                        ->addMethod("setRelCoor", Executor4<CameraMan, lua_State*,const std::string&,float,float,float>(&CameraMan::setRelCameraCoor))
49                        ->addMethod("setRelCoorSoft", Executor5<CameraMan, lua_State*,const std::string&,float,float,float,float>(&CameraMan::setRelCameraCoorSoft))
50                       );
51
52
53CameraMan::CameraMan(const TiXmlElement* root)
54{
55  this->registerObject(this, CameraMan::_objectList);
56
57  this->nearClip = 1.0;
58  this->farClip = 1000.0;
59
60  this->fadeToBlack=new BlackScreen();
61
62  this->setCam( State::getCamera());
63
64  if (root != NULL)
65    this->loadParams(root);
66}
67
68
69void CameraMan::loadParams(const TiXmlElement* root)
70{
71  BaseObject::loadParams(root);
72  LoadParamXML(root, "Cameras", this, CameraMan, createCameras);
73}
74
75
76void CameraMan::createCameras(const TiXmlElement* camerasTag)
77{
78
79    LOAD_PARAM_START_CYCLE(camerasTag, object);
80    {
81      this->createCam(object);
82    }
83    LOAD_PARAM_END_CYCLE(object);
84
85}
86
87
88void CameraMan::createCam(const TiXmlElement* root)
89{
90  this->cameras.push_back(new Camera(root));
91  cameras[cameras.size()-1]->setClipRegion(nearClip, farClip);
92}
93
94/*void CameraMan::setViewMode(const std::string& cameraName, const std::string& viewMode)
95{
96     BaseObject* newCam = ObjectListBase::getBaseObject("Camera", cameraName);
97     
98     if (!strcmp(viewMode, "ViewNormal"))
99     {
100         dynamic_cast<Camera*>(newCam)->setViewMode(Camera::ViewNormal);
101     }
102     else if (!strcmp(viewMode, "ViewTop"))
103     {
104         dynamic_cast<Camera*>(newCam)->setViewMode(Camera::ViewTop);
105     }
106     else if (!strcmp(viewMode, "ViewBehind"))
107     {
108         dynamic_cast<Camera*>(newCam)->setViewMode(Camera::ViewBehind);
109     }
110     else if (!strcmp(viewMode, "ViewFront"))
111     {
112         dynamic_cast<Camera*>(newCam)->setViewMode(Camera::ViewFront);
113     }
114     else if (!strcmp(viewMode, "ViewLeft"))
115     {
116         dynamic_cast<Camera*>(newCam)->setViewMode(Camera::ViewLeft);
117     }
118     else if (!strcmp(viewMode, "ViewRight"))
119     {
120         dynamic_cast<Camera*>(newCam)->setViewMode(Camera::ViewRight);
121     }
122}*/
123
124void CameraMan::setRelCameraCoor(const std::string& camName, float x, float y, float z)
125{
126     BaseObject* newCam = ObjectListBase::getBaseObject("Camera", camName);
127     dynamic_cast<Camera*>(newCam)->setRelCoor(x,y,z);
128     dynamic_cast<Camera*>(newCam)->target->setRelCoor(0,0,0);
129}
130
131void CameraMan::setRelCameraCoorSoft(const std::string& camName, float x, float y, float z, float bias)
132{
133     BaseObject* newCam = ObjectListBase::getBaseObject("Camera", camName);
134     dynamic_cast<Camera*>(newCam)->setRelCoorSoft(x,y,z,bias);
135     dynamic_cast<Camera*>(newCam)->target->setRelCoorSoft(0,0,0,bias);
136}
137
138void CameraMan::setCam(unsigned int cameraNo)
139{
140  if (cameraNo<cameras.size())
141  {
142    this->setCam( cameras[cameraNo]);
143  }
144}
145
146void CameraMan::setCam(const std::string& camName)
147{
148  BaseObject* object = ObjectListBase::getBaseObject("Camera", camName);
149
150  if(object != NULL)
151  {
152    this->setCam(dynamic_cast<Camera*>(object));
153    return;
154  }
155  printf("ERROR CAMERAMANAGER: Couldn't set camera : %s \n", camName.c_str());
156}
157
158
159void CameraMan::setCam(Camera* camera)
160{
161  if( camera == NULL)
162  {
163    PRINTF(0)("trying to add a zero camera! uiuiui!\n");
164    return;
165  }
166 
167  this->currentCam = camera;
168
169  State::setCamera(currentCam, currentCam->getTarget());
170  OrxSound::SoundEngine::getInstance()->setListener(currentCam);
171
172  // check if it is already added
173  if( ! this->cameraIsInVector(currentCam) )
174    this->cameras.push_back(currentCam);
175
176  this->fadeToBlack->setRelCoor(0., 0., 0.);
177  this->fadeToBlack->setParent(this->currentCam);
178}
179
180
181void CameraMan::moveCurrCam(int x, int y, int z)
182{
183  currentCam->target->trans(x,y,z);
184}
185
186
187void CameraMan::changeCurrTarget(const std::string& className, const std::string& objectName)
188{
189  BaseObject* object = ObjectListBase::getBaseObject(className, objectName);
190  if( object != NULL && object->isA(PNode::staticClassID()))
191  {
192    currentCam->lookAt(dynamic_cast<PNode*>(object));
193    State::setCamera(this->currentCam,  dynamic_cast<CameraTarget*>(object));
194  }
195}
196
197
198void CameraMan::atachCurrCameraToWorldEntity(const std::string& className, const std::string& targetEntity)
199{
200  BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity);
201
202  if(object != NULL && object->isA(PNode::staticClassID()))
203  {
204   this->atachTarget(this->currentCam, dynamic_cast<PNode*>(object));
205   return;
206  }
207
208printf("ERROR CAMERAMANAGER: Couldn't set camera to: %s %s \n", className.c_str(),targetEntity.c_str() );
209}
210
211
212
213void CameraMan::detachCurrCamera()
214{
215  currentCam->target->detach();
216}
217
218
219
220void CameraMan::jumpCurrCam(float x, float y, float z)
221{
222  currentCam->target->jump(x, y, z);
223}
224
225void CameraMan::togglFade()
226{
227  if( this->fadeToBlack)
228    fadeToBlack->toggleFade();
229}
230
231
232void CameraMan::initFadeBlack()
233{
234  if( this->fadeToBlack)
235    fadeToBlack->initFadeBlack();
236}
237
238
239void CameraMan::moveCam(int x, int y, int z, int camNo)
240{
241  cameras[camNo]->target->trans(x,y,z);
242}
243
244
245void CameraMan::changeTarget(int camNo,const std::string& className, const std::string& objectName)
246{
247  BaseObject* object = ObjectListBase::getBaseObject(className, objectName);
248  if( object != NULL && object->isA(PNode::staticClassID()))
249  {
250    cameras[camNo]->lookAt(dynamic_cast<PNode*>(object));
251  }
252}
253
254
255void CameraMan::changeTarget(const std::string& camName,const std::string& className, const std::string& objectName)
256{
257  BaseObject* object = ObjectListBase::getBaseObject(className, objectName);
258  BaseObject* newCam = ObjectListBase::getBaseObject("Camera", camName);
259  if( object != NULL && newCam != NULL && object->isA(PNode::staticClassID()))
260  {
261    dynamic_cast<Camera*>(newCam)->lookAt(dynamic_cast<PNode*>(object));
262    State::setCamera( dynamic_cast<Camera*>(newCam),  dynamic_cast<CameraTarget*>(object));
263  }
264}
265
266
267void CameraMan::atachCameraToWorldEntity(const std::string& cameraName, const std::string& className, const std::string& targetEntity)
268{
269  BaseObject* object = ObjectListBase::getBaseObject(className, targetEntity);
270  BaseObject* targetCam = ObjectListBase::getBaseObject("Camera", cameraName);
271
272  if( object != NULL && targetCam != NULL && object->isA(PNode::staticClassID()) )
273  {
274    this->atachTarget(dynamic_cast<Camera*>(targetCam), dynamic_cast<PNode*>( object ));
275    return;
276  }
277
278  printf("ERROR CAMERAMANAGER: Couldn't set camera %s to: %s %s \n", cameraName.c_str(), className.c_str(),targetEntity.c_str() );
279}
280
281
282
283void CameraMan::jumpCam(int x, int y, int z, int camNo)
284{
285  cameras[camNo]->target->jump(x, y, z);
286}
287
288
289void CameraMan::jumpCam(const std::string& cameraName, float x, float y, float z)
290{
291  BaseObject* targetCam = ObjectListBase::getBaseObject("Camera", cameraName);
292  if( targetCam != NULL )
293  {
294    dynamic_cast<Camera*>(targetCam)->target->jump( x, y, z );
295  }
296
297}
298
299
300void CameraMan::setClipRegion(float nearCli, float farCli)
301{
302  this->nearClip=nearCli;
303  this->farClip=farCli;
304
305  for(unsigned int i = 0; i < this->cameras.size(); i++)
306    cameras[i]->setClipRegion(nearCli, farCli);
307}
308
309
310
311bool CameraMan::cameraIsInVector(Camera* camera)
312{
313
314  for(std::vector<Camera*>::const_iterator it = cameras.begin(); it != cameras.end(); it++ )
315  {
316    if( (*it) == camera)
317    {
318      return true;
319    }
320  }
321  return false;
322
323
324}
325
326
327void CameraMan::cameraInfo()
328{
329  bool sameCam = (this->currentCam == State::getCamera());
330
331
332  PRINT(0)("==== CameraMan::cameraInfo ===\n");
333  PRINT(0)("=  Camera Name: %s\n", this->currentCam->getName().c_str());
334  PRINT(0)("=  Tests:\n");
335  PRINT(0)("==  State::Cam == this::Cam  %i\n", sameCam);
336  PRINT(0)("==  Parenting Informations:\n");
337  this->currentCam->debugNode(10);
338  PRINT(0)("==============================\n");
339}
340
341
342float CameraMan::getCurrCameraCoorX()
343{ return this->currentCam->getAbsCoorX(); }
344
345float CameraMan::getCurrCameraCoorY()
346{ return this->currentCam->getAbsCoorY(); }
347
348float CameraMan::getCurrCameraCoorZ()
349{ return this->currentCam->getAbsCoorZ(); }
350
351
352
353void CameraMan::atachTarget(Camera* cam ,PNode* target)
354{
355  cam->target->atach(target);
356  cam->setViewMode(Camera::ViewNormal);
357  State::setCamera( cam,  dynamic_cast<CameraTarget*>(target));
358
359}
360
361//how to get a class fkt pointer
362
363//BaseObject* object = ObjectListBase::getBaseObject(className, objectName);
364
365
366
367
368
369
370
371
372
373
374
375
376
377
Note: See TracBrowser for help on using the repository browser.