vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
World.h
Go to the documentation of this file.
1 /***************************************************************************
2  World.h - description
3  -------------------
4  begin : Jun 2004
5  copyright : (C) 2004 by Jan Flasar
6  email : flasar@fi.muni.cz
7  ***************************************************************************/
8 
9 #ifndef WORLD_H
10 #define WORLD_H
11 
12 
13 
39 #include <vrecko/BaseClass.h>
40 #include <vrecko/vThread.h>
41 #include <vrecko/Scheduler.h>
42 #include <vrecko/Ability.h>
43 #include <vrecko/Viewer.h>
44 #include <vrecko/EventDispatcher.h>
45 
46 #include <osg/Group>
47 #include <osgText/Text>
48 
49 
50 //---------------------------------------------------------------
51 //Shadows
52 //---------------------------------------------------------------
53 #include <osgShadow/ShadowedScene>
54 #include <osgShadow/ShadowVolume>
55 #include <osgShadow/ShadowTexture>
56 #include <osgShadow/ShadowMap>
57 #include <osgShadow/SoftShadowMap>
58 //---------------------------------------------------------------
59 //Shadows
60 //---------------------------------------------------------------
61 
62 #include <vrecko/Logger.h> // at jde kdekoliv pouzivat globalni promenna [logger] (resp. vrecko::logger)
63 #include <vrecko/PersistentCache.h>
64 
65 
66 namespace vrecko {
67 
68 class VRECKO_EXPORT Scene;
69 class VRECKO_EXPORT DeviceManager;
70 class VRECKO_EXPORT EventDispatcher;
71 
72 class VRECKO_EXPORT World: public BaseClass, public osg::Group {
73  public:
74  World();
75  ~World();
76  World(const World &w);
77 
78  void setViewerPtr(ViewerBase *pV);
79  ViewerBase *getViewerPtr() { return pViewer; };
80  osg::Camera *getCamera(void) {return (pViewer ? pViewer->getCamera(0) : NULL);}
81 
82  //---------------------------------------------------------------
83  //Shadows
84  //---------------------------------------------------------------
85  osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene;
86  osgShadow::ShadowedScene* getShadowedScenePtr() { return shadowedScene.get(); }
87  osg::LightSource* getShadowedSceneLight() { return mainLightSource.get(); }
88  //---------------------------------------------------------------
89  //Shadows
90  //---------------------------------------------------------------
91 
93  void setDescription(const std::string &desc) {description = desc;}
95  inline const std::string &getDescription(void) const {return description;}
96 
98  void setScenePtr(Scene *nScene);
100  Scene *getScenePtr(void) {return pScene;}
101 
103  typedef struct AvatarView {
105  std::string ID;
109  osg::Vec3 eye_position;
111  osg::Vec3 look_at_center;
113  osg::Vec3 up_vector;
114  };
115 
117  AvatarView *getActualAvatarView(void) {return avatar_view_vector[0];}
119  void setActualAvatarView(AvatarView *av) {avatar_view_vector.insert(avatar_view_vector.begin(), av);}
121  bool removeAvatarView(AvatarView *av);
122 
124  struct AvatarEye
125  {
127  osg::Vec3 eye_offset;
129  osg::Matrix projection;
130  };
131 
132  class
133  {
134  private:
135  std::vector<AvatarEye> _eyes;
136 
137  public:
138  void CreateEyes(int count) { _eyes.resize(count); }
139  void DestroyEyes() { _eyes.clear(); }
140 
141  int GetCount() { return _eyes.size(); }
142 
143  AvatarEye *Get(int eye_index)
144  { return ((eye_index >= 0) && (eye_index < GetCount())) ? (&(_eyes[eye_index])) : 0; }
145  } AvatarEyes;
146 
147 
148 
149 
150 
152  typedef enum {
160  SET_TEXT
161  } SG_OPERATION_TYPE;
162 
164  typedef struct {
165  osg::Node *pNode;
166  osg::Group *pGroup;
167  osg::StateSet *pStateSet;
168  osgText::Text *pText;
169  std::string text_string;
171  } SG_CHANGE;
172 
173 
174  // You can set [owner] to NULL, which means "world".
175  vrecko::Ability* createAbility(const std::string& plugin_name, const std::string& ability_name, BaseClass* _owner, const char* _overridingID = NULL);
176 
177 
179  void setDeviceManagerPtr(DeviceManager *ptr) {pDeviceManager = ptr;}
181  DeviceManager* getDeviceManagerPtr() {return pDeviceManager;}
182 
184  void setEventDispatcherPtr(EventDispatcher *ptr) {pEventDispatcher = ptr;}
186  EventDispatcher* getEventDispatcherPtr() {return pEventDispatcher;}
187 
189  void setSchedulerPtr(Scheduler *sched) {pScheduler = sched;}
191  inline Scheduler* getSchedulerPtr() {return pScheduler;}
192 
193  inline PersistentCache* getPersistentCache() { return persistentCache; }
194 
196  bool getTerminationFlag() {return termination_flag;}
197 
199  bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *parametersNode);
200 
201  // ----- LogFile functions
202 // void textLog(const char *text = "", ...);
203  void noticeLog(const char *notice = "", ...);
204 // void noticeLog(int level, const char *notice = "", ...);
205  void warningLog(const char *warning = "", ...);
206 // void warningLog(int level, const char *warning = "", ...);
207  void errorLog(const char *error = "", ...);
208 // void errorLog(int level, const char *error = "", ...);
209  void debugLog(const char *debugText = "", ...);
210  // The debugFileLog() function only writes info to the file
211  void debugFileLog(const char *debugText = "", ...);
212 
213  // functions for handling abilities
215  bool addAbility(Ability *pAbility) {ability_dispatcher->addAbility(pAbility); return true;}
217  bool removeAbility(Ability *pAbility) {ability_dispatcher->removeAbility(pAbility); return true;}
218  bool removeAbility(const std::string &ability_id);
220  Ability *getAbility(const std::string &ab_id) {return ability_dispatcher->getAbility(ab_id);}
222  //Ability *getAppropriateAbility(const std::string &message_ID) {return ability_dispatcher->getAppropriateAbility(message_ID);}
224  //Ability *getAbilityWithInput(const std::string &input_name) {return ability_dispatcher->getAbilityWithInput(input_name);}
226  //Ability *getAbilityWithOutput(const std::string &output_name) {return ability_dispatcher->getAbilityWithOutput(output_name);}
228  std::map<std::string, Ability*> &getAbilityMap(void) {return ability_dispatcher->getAbilityMap();}
229 
230 
231 
232 // Heavily used methods from EventDispatcher are inlined here, so we do not need to write "world->getEventDispatcher()->reportEvent",
233 // but just "world->reportEvent"
234  inline bool reportEvent(const std::string &senderAndOutputString, VreckoMessage *pMessage, EventType eType = EXPRESS_EVENT) {
235  return getEventDispatcherPtr()->reportEvent(senderAndOutputString, pMessage, eType);
236  }
237  inline bool reportEvent(BaseClass *receiver_ptr, const std::string &input_name, VreckoMessage *pMessage, EventType eType = EXPRESS_EVENT) {
238  return getEventDispatcherPtr()->reportEvent(receiver_ptr, input_name, pMessage, eType);
239  }
240  inline osg::ref_ptr<VreckoMessage> request(const std::string &req_sender, VreckoMessage *pMessage) {
241  return getEventDispatcherPtr()->request(req_sender, pMessage);
242  }
243  inline osg::ref_ptr<VreckoMessage> request(BaseClass *receiver_ptr, const std::string &request_name, VreckoMessage *pMessage) {
244  return getEventDispatcherPtr()->request(receiver_ptr, request_name, pMessage);
245  }
246 
247 
248 
249 
250  inline unsigned long getFrameID() { return frameID; }
251  inline void incrementFrameID() { ++frameID; }
252 
253  inline bool getShowTimeWarnings() { return bShowTimeWarnings; };
254  inline void setShowTimeWarnings(bool bNewValue) { bShowTimeWarnings = bNewValue; };
255 
256  inline unsigned long getTimeWarningsTime() { return ulTimeWarningsTime; };
257  inline void setTimeWarningsTime(unsigned long ulNewValue) { ulTimeWarningsTime = ulNewValue; };
258 
260 
261  /*
262  * Method saves arguments and its count from main class to be accessible for objects
263  */
264  void setArgumens(int &count, char **arguments);
265 
266  /*
267  * Method returns number of arguments with which the program started
268  */
269  int getNumberOfArguments();
270 
271  /*
272  * Method returns the field of arguments with which the program started
273  */
274  char ** getArguments();
275 
276  protected:
278  std::string description;
282  std::vector<AvatarView *> avatar_view_vector;
283 
284  // ???
286 
287  //NEW shadows and light;
288  osg::ref_ptr<osg::LightSource> mainLightSource;
289 
292 
293  // Data cache that is persistent throughout multiple app runs.
295 
296  private:
297  //saved arguments from command
298  int arguments_count;
299  char **arguments;
300 
301  // Pointer to the viewer
302  ViewerBase *pViewer;
303 
305  DeviceManager *pDeviceManager;
306 
308  EventDispatcher *pEventDispatcher;
309 
311  Scheduler *pScheduler;
312 
314  bool termination_flag;
315 
316  // Current frame ID (will be different for each frame)
317  unsigned long frameID;
318 
319  // Will display warnings like "Processing a MESSAGE took a long time (... ms)"
320  bool bShowTimeWarnings;
321  unsigned long ulTimeWarningsTime; // in ms. An event will need to take at least this time for the warning to be displayed.
322 };
323 
324 VRECKO_EXPORT bool initXML();
325 VRECKO_EXPORT bool doneXML();
326 
327 
331 VRECKO_EXPORT const std::string& getSafeSceneFile(const std::string &sceneFile);
332 
334 VRECKO_EXPORT unsigned long getNumSceneFiles();
335 
337 VRECKO_EXPORT const std::string& getSafeSceneFile(unsigned long index);
338 
341 VRECKO_EXPORT const std::string& getSafeDefaultSceneFile();
342 
345 VRECKO_EXPORT void setDefaultSceneFile(const std::string &newDefaultSceneFile);
346 
347 
348 }
349 
350 #endif
351 
352 
353 namespace vrecko {
354  extern VRECKO_EXPORT World *world;
355 };
356