OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
scene.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_SCENE_HH
20 #define OST_SCENE_HH
21 
22 /*
23  Author: Ansgar Philippsen
24 */
25 
26 #include <map>
27 #include <stack>
28 #include <vector>
29 
30 #include <boost/shared_array.hpp>
31 
32 #include <ost/gfx/module_config.hh>
33 #include <ost/geom/transform.hh>
34 #include <ost/mol/atom_handle.hh>
35 
36 #include "gl_include.hh"
37 #include "color.hh"
38 #include "gfx_object_fw.hh"
39 #include "gfx_node_fw.hh"
40 #include "gfx_node_visitor.hh"
41 #include "glwin_base.hh"
42 #include "scene_observer.hh"
43 #include "gfx_prim.hh"
44 #include "povray_fw.hh"
45 #include "exporter_fw.hh"
46 #include "gradient.hh"
47 #include "bitmap_io.hh"
48 
49 namespace ost { namespace gfx {
50 
51 class InputEvent;
53 
54 typedef std::vector<SceneObserver*> SceneObserverList;
55 
56 struct Viewport {
57  int x;
58  int y;
59  int width;
60  int height;
61 };
62 
63 namespace impl {class SceneFX;}
64 
82  friend class impl::SceneFX;
83  private:
84 
85  // TODO: this struct may be the seed for a proper
86  // refactoring of the scene it into a management
87  // and a view part
88  struct SceneViewStackEntry {
89  geom::Transform transform;
90  float fov;
91  float znear,zfar;
92  };
93 
94  typedef std::stack<SceneViewStackEntry> SceneViewStack;
95 
96  public:
97  static Scene& Instance();
98 
100  void SetFog(bool f);
102  bool GetFog() const;
104  void SetFogColor(const Color& c);
106  Color GetFogColor() const;
107 
109  void SetShadow(bool f);
111  bool GetShadow() const;
113  void SetShadowQuality(int q);
115  int GetShadowQuality() const;
117  void SetShadowWeight(float w);
119  float GetShadowWeight() const;
120 
122  void SetDepthDarkening(bool f);
124  void SetDepthDarkeningWeight(float f);
125 
127  void SetAmbientOcclusion(bool f);
129  bool GetAmbientOcclusion() const;
131  void SetAmbientOcclusionWeight(float f);
133  float GetAmbientOcclusionWeight() const;
135  void SetAmbientOcclusionMode(uint m);
137  uint GetAmbientOcclusionMode() const;
139  void SetAmbientOcclusionQuality(uint q);
141  uint GetAmbientOcclusionQuality() const;
143  void SetAmbientOcclusionSize(float f);
145  float GetAmbientOcclusionSize() const;
146 
149  void SetShadingMode(const std::string& smode);
150 
151  geom::Mat4 GetProjection() const {return pmat_;}
152  geom::Mat4 GetInvertedProjection() const {return ipmat_;}
153 
155 
156 
157  float GetNear() const;
158 
160  void SetNear(float n);
161 
163  float GetFar() const;
164 
166  void SetFar(float f);
167 
169  void SetNearFar(float n, float f);
170 
172  void SetFOV(float f);
173 
174  // \brief get the field of view
175  float GetFOV() const;
176 
177  float GetAspect() const {return aspect_ratio_;}
178 
180  void SetFogNearOffset(float o);
181 
183  float GetFogNearOffset() const;
184 
186  void SetFogFarOffset(float o);
187 
189  float GetFogFarOffset() const;
190 
192  void SetFogOffsets(float no, float fo);
193 
195  void Autoslab(bool fast);
196 
198  void Autoslab(bool fast, bool);
199 
201  void AutoslabMax();
202 
210  void Autoslab();
211 
219  void SetAutoslabMode(int mode) {
220  autoslab_mode_=std::min(2,std::max(0,mode));
221  }
222 
224  int GetAutoslabMode() const {
225  return autoslab_mode_;
226  }
227 
233  void AutoAutoslab(bool f);
234 
236  bool GetAutoAutoslab() const { return auto_autoslab_; }
237 
239 
242  void SetStereoMode(unsigned int mode);
243  int GetStereoMode() const {return stereo_mode_;}
244 
246  void SetStereoFlip(bool f);
248  bool GetStereoFlip() const {return stereo_inverted_;}
249 
252  void SetStereoView(int);
254  int GetStereoView() const {return stereo_eye_;}
255 
257  void SetStereoIOD(Real);
259  Real GetStereoIOD() const {return stereo_iod_;}
260 
262  void SetStereoDistance(Real);
264  Real GetStereoDistance() const {return stereo_distance_;}
265 
268  void SetStereoAlg(unsigned int);
270  unsigned int GetStereoAlg() const {return stereo_alg_;}
271 
273  void SetLightDir(const geom::Vec3& dir);
275  void SetLightProp(const Color& amb, const Color& diff, const Color& spec);
277  void SetLightProp(float amb, float diff, float spec);
279  geom::Vec3 GetLightDir() const {return light_dir_;}
281  geom::Mat3 GetLightRot() const {return light_rot_;}
282 
284  /*
285  bad style for now: 0=(reserved), 1=atom, 2=residue, 3=chain, 4=bond, 5=torsion
286  */
287  void SetSelectionMode(uint m);
288  uint GetSelectionMode() const;
289 
291 
292 
293 
294 
295  void Export(const String& fname, unsigned int w,
296  unsigned int h, bool transparent=false);
297 
299  void Export(const String& fname, bool transparent=false);
300 
302  void ExportPov(const std::string& fname, const std::string& wdir=".");
303 
305  void Export(Exporter* ex) const;
306 
308 
309  void OnInput(const InputEvent& e);
310 
312  void InitGL(bool full=true);
313 
315  void Resize(int w, int h);
316 
318  void Pick(int mx, int my, int mask);
319 
320  float GetDefaultTextSize();
321 
323  std::pair<GfxObjP, mol::AtomHandle> PickAtom(int mx, int my);
324 
326  void RenderGL();
327 
329  void RequestRedraw();
330 
332  void StatusMessage(const String& s);
333 
335  void SetViewport(int w, int h);
336 
338  void SetBackground(const Color& c);
339 
341  void SetBackground(const Gradient& g);
342 
344  void SetBackground(const Bitmap& bm);
345 
347  Color GetBackground() const;
348 
350  void SetCenter(const geom::Vec3& cen);
351 
353  geom::Vec3 GetCenter() const;
354 
356  void CenterOn(const String& s);
357 
359  void CenterOn(const GfxObjP& s);
360 
362  geom::Vec3 Project(const geom::Vec3& v, bool ignore_vp=false) const;
363 
365  geom::Vec3 UnProject(const geom::Vec3& v, bool ignore_vp=false) const;
366 
368 
373  geom::AlignedCuboid GetBoundingBox(bool use_tf=true) const;
374 
376  geom::AlignedCuboid GetBoundingBox(const geom::Transform& tf) const;
377 
379  geom::Transform GetTransform() const;
380 
382  void SetTransform(const geom::Transform& t);
383 
385  geom::Mat4 GetRTC() const;
386 
388  void SetRTC(const geom::Mat4& rtc);
389 
391  void PushView();
392 
394  void PopView();
395 
397  void ResetProjection();
398 
400  void Register(GLWinBase* win);
402  void Unregister(GLWinBase* win);
403 
405 
406 
407  void Add(const GfxNodeP& go, bool redraw=true);
410  void Remove(const GfxNodeP& go);
412  void Remove(const String& name);
413 
415  void RemoveAll();
416 
419  bool Rename(const String& old_name, const String& new_name);
420 
422  GfxObjP operator[](const String& name);
423 
425  bool HasNode(const String& name) const;
426 
428  void Apply(const InputEvent& ie, bool request_redraw=true);
429 
431  void Apply(GfxNodeVisitor& v) const;
432 
437  size_t GetNodeCount() const;
438 
440  GfxNodeP GetRootNode() const;
442 
444  void AttachObserver(SceneObserver* o);
446  void DetachObserver(SceneObserver* o);
447 
448  bool InOffscreenMode() const;
449 
451  void SetTestMode(bool t);
452 
453  float ElapsedTime() const;
454 
455  Viewport GetViewport() const;
456 
470  bool StartOffscreenMode(unsigned int w, unsigned int h);
472  void StopOffscreenMode();
473 
475  void SetShowCenter(bool f);
476 
477  bool GetShowCenter() const {return cor_flag_;}
478 
480  void SetFixCenter(bool f) {fix_cor_flag_=f;}
481 
483  bool GetFixCenter() const {return fix_cor_flag_;}
484 
486  void SetBlur(uint n);
488  void BlurSnapshot();
489 
491  void RenderText(const TextPrim& t);
492 
494  void SetBeacon(int wx, int wy);
496  void SetBeaconOff();
497 
498  void SetExportAspect(float a);
499  float GetExportAspect() const {return export_aspect_;}
500  void SetShowExportAspect(bool f);
501  bool GetShowExportAspect() const {return show_export_aspect_;}
502 
503 protected:
504  friend class GfxObj;
505  friend class GfxNode;
506 
507  // TODO: this is really a hack and not clean communication
508  friend class Entity;
509 
510  void ObjectChanged(const String& name);
511  void SelectionChanged(const String& name, const mol::EntityView& sel);
512  void NodeTransformed(const GfxObjP& object);
513  void NodeAdded(const GfxNodeP& node);
514  void RenderModeChanged(const String& name);
515 
516 
517 private:
518 
519  template <typename ACTION>
520  void NotifyObservers(const ACTION& action) {
521  std::for_each(observers_.begin(), observers_.end(), action);
522  }
523  Scene();
524  Scene(const Scene&) {}
525  Scene& operator=(const Scene&) {return *this;}
526 
527  GLWinBase* win_; // target gl window
528 
529  mutable GfxNodeP root_node_; // mutable is slightly hackish
530  SceneObserverList observers_;
531 
532  geom::Transform transform_; // overall modelview transformation
533 
534  bool gl_init_;
535 
536  float fov_; // field of view
537  float znear_,zfar_; // near and far clipping plane
538  float fnear_,ffar_; // fog near and far offsets
539 
540  geom::Mat4 pmat_,ipmat_; // projection and inverted projection matrix
541  unsigned int vp_width_,vp_height_; // viewport
542 
543  SceneViewStack scene_view_stack_;
544 
545  float aspect_ratio_; // aspect ratio for viewport
546 
547  Color background_; // background (clear) color
548 
549  geom::Vec3 light_dir_; // infinite light source direction
550  geom::Mat3 light_rot_; // transform needed for the shadow map
551  Color light_amb_;
552  Color light_diff_;
553  Color light_spec_;
554 
555  bool cor_flag_;
556  bool fix_cor_flag_;
557  bool fog_flag_;
558  Color fog_color_;
559  bool auto_autoslab_; // run autoslab on each scene update
560  bool do_autoslab_; // run autoslab on next scene update
561  int autoslab_mode_; // 0: fast, 1:precise, 2: max
562 
563  bool offscreen_flag_; // a simple indicator whether in offscreen mode or not
564  OffscreenBuffer* main_offscreen_buffer_; // not null if a main offscreen buffer is present
565  uint old_vp_[2]; // used by the offline rendering code
566  std::string def_shading_mode_;
567 
568  uint selection_mode_;
569 
570  bool test_flag_;
571  std::vector<unsigned char> tmp_tex_;
572 
573  GLuint glyph_tex_id_;
574  std::vector<geom::Vec2> glyph_map_;
575  float def_text_size_;
576 
577  uint blur_count_;
578  std::vector<boost::shared_array<unsigned char> > blur_buffer_;
579 
580  unsigned int stereo_mode_;
581  unsigned int stereo_alg_;
582  bool stereo_inverted_;
583  unsigned int stereo_eye_;
584  Real stereo_iod_,stereo_distance_;
585  unsigned int scene_left_tex_;
586  unsigned int scene_right_tex_;
587  unsigned int bg_mode_;
588  bool update_bg_;
589  Gradient bg_grad_;
590  Bitmap bg_bm_;
591  unsigned int bg_tex_;
592 
593  float export_aspect_;
594  bool show_export_aspect_;
595 
596  void set_near(float n);
597  void set_far(float f);
598  void update_fog();
599  void flag_all_dirty();
600  void prep_glyphs();
601  void prep_blur();
602  void stereo_projection(int view);
603  void render_bg();
604  void render_export_aspect();
605  void render_scene();
606  void render_glow();
607  void render_stereo();
608  void set_bg();
609  void do_autoslab();
610 
611  bool IsNameAvailable(const String& name) const;
612 
613 };
614 
615 }} // ns
616 
617 #endif