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 
147  void SetHemiParams(const geom::Vec4&);
148  geom::Vec4 GetHemiParams() const {return hemi_param_;}
149 
152  void SetShadingMode(const std::string& smode);
153 
154  geom::Mat4 GetProjection() const {return pmat_;}
155  geom::Mat4 GetInvertedProjection() const {return ipmat_;}
156 
158 
159  float GetNear() const;
161 
163  void SetNear(float n);
164 
166  float GetFar() const;
167 
169  void SetFar(float f);
170 
172  void SetNearFar(float n, float f);
173 
175  void SetFOV(float f);
176 
177  // \brief get the field of view
178  float GetFOV() const;
179 
180  float GetAspect() const {return aspect_ratio_;}
181 
183  void SetFogNearOffset(float o);
184 
186  float GetFogNearOffset() const;
187 
189  void SetFogFarOffset(float o);
190 
192  float GetFogFarOffset() const;
193 
195  void SetFogOffsets(float no, float fo);
196 
198  void Autoslab(bool fast);
199 
201  void Autoslab(bool fast, bool);
202 
204  void AutoslabMax();
205 
213  void Autoslab();
214 
222  void SetAutoslabMode(int mode) {
223  autoslab_mode_=std::min(2,std::max(0,mode));
224  }
225 
227  int GetAutoslabMode() const {
228  return autoslab_mode_;
229  }
230 
236  void AutoAutoslab(bool f);
237 
239  bool GetAutoAutoslab() const { return auto_autoslab_; }
240 
242 
245  void SetStereoMode(unsigned int mode);
246  int GetStereoMode() const {return stereo_mode_;}
247 
249  void SetStereoFlip(bool f);
251  bool GetStereoFlip() const {return stereo_inverted_;}
252 
255  void SetStereoView(int);
257  int GetStereoView() const {return stereo_eye_;}
258 
260  void SetStereoIOD(Real);
262  Real GetStereoIOD() const {return stereo_iod_;}
263 
265  void SetStereoDistance(Real);
267  Real GetStereoDistance() const {return stereo_distance_;}
268 
271  void SetStereoAlg(unsigned int);
273  unsigned int GetStereoAlg() const {return stereo_alg_;}
274 
276  void SetLightDir(const geom::Vec3& dir);
278  void SetLightProp(const Color& amb, const Color& diff, const Color& spec);
280  void SetLightProp(float amb, float diff, float spec);
282  geom::Vec3 GetLightDir() const {return light_dir_;}
284  geom::Mat3 GetLightRot() const {return light_rot_;}
285 
287  /*
288  bad style for now: 0=(reserved), 1=atom, 2=residue, 3=chain, 4=bond, 5=torsion
289  */
290  void SetSelectionMode(uint m);
291  uint GetSelectionMode() const;
292 
294 
295  void Export(const String& fname, unsigned int w,
299  unsigned int h, bool transparent=false);
301  void Export(const String& fname, unsigned int w,
302  unsigned int h, int max_samples, bool transparent=false);
303 
305  void Export(const String& fname, bool transparent=false);
306 
308  void ExportPov(const std::string& fname, const std::string& wdir=".");
309 
311  void Export(Exporter* ex) const;
312 
314  void OnInput(const InputEvent& e);
316 
318  void InitGL(bool full=true);
319 
321  void Resize(int w, int h);
322 
324  void Pick(int mx, int my, int mask);
325 
326  float GetDefaultTextSize();
327 
329  std::pair<GfxObjP, mol::AtomHandle> PickAtom(int mx, int my);
330 
332  void RenderGL();
333 
335  void RequestRedraw();
336 
338  void StatusMessage(const String& s);
339 
341  void SetViewport(int w, int h);
342 
344  void SetBackground(const Color& c);
345 
347  void SetBackground(const Gradient& g);
348 
350  void SetBackground(const Bitmap& bm);
351 
353  Color GetBackground() const;
354 
357  void SetBackgroundStereoMode(bool);
358  bool GetBackgroundStereoMode() const {return bg_stereo_mode_;}
359 
361  void SetBackgroundStereoOffset(float);
362  float GetBackgroundStereoOffset() const {return bg_stereo_offset_;}
363 
365  void SetCenter(const geom::Vec3& cen);
366 
368  geom::Vec3 GetCenter() const;
369 
371  void CenterOn(const String& s);
372 
374  void CenterOn(const GfxObjP& s);
375 
377  geom::Vec3 Project(const geom::Vec3& v, bool ignore_vp=false) const;
378 
380  geom::Vec3 UnProject(const geom::Vec3& v, bool ignore_vp=false) const;
381 
383 
388  geom::AlignedCuboid GetBoundingBox(bool use_tf=true) const;
389 
391  geom::AlignedCuboid GetBoundingBox(const geom::Transform& tf) const;
392 
394  geom::Transform GetTransform() const;
395 
397  void SetTransform(const geom::Transform& t);
398 
400  geom::Mat4 GetRTC() const;
401 
403  void SetRTC(const geom::Mat4& rtc);
404 
406  void PushView();
407 
409  void PopView();
410 
412  void ResetProjection();
413 
415  void Register(GLWinBase* win);
417  void Unregister(GLWinBase* win);
418 
420 
421  void Add(const GfxNodeP& go, bool redraw=true);
425  void Remove(const GfxNodeP& go);
427  void Remove(const String& name);
428 
430  void RemoveAll();
431 
434  bool Rename(const String& old_name, const String& new_name);
435 
437  GfxObjP operator[](const String& name);
438 
440  bool HasNode(const String& name) const;
441 
443  void Apply(const InputEvent& ie, bool request_redraw=true);
444 
446  void Apply(GfxNodeVisitor& v) const;
447 
452  size_t GetNodeCount() const;
453 
455  GfxNodeP GetRootNode() const;
457 
459  void AttachObserver(SceneObserver* o);
461  void DetachObserver(SceneObserver* o);
462 
463  bool InOffscreenMode() const;
464 
466  void SetTestMode(bool t);
467 
468  float ElapsedTime() const;
469 
470  Viewport GetViewport() const;
471 
492  bool StartOffscreenMode(unsigned int w, unsigned int h, int max_samples);
493  bool StartOffscreenMode(unsigned int w, unsigned int h);
494 
496  void StopOffscreenMode();
497 
499  void SetShowCenter(bool f);
500 
501  bool GetShowCenter() const {return cor_flag_;}
502 
504  void SetFixCenter(bool f) {fix_cor_flag_=f;}
505 
507  bool GetFixCenter() const {return fix_cor_flag_;}
508 
510  void SetBlur(uint n);
512  void BlurSnapshot();
513 
515  void RenderText(const TextPrim& t);
516 
518  void SetBeacon(int wx, int wy);
520  void SetBeaconOff();
521 
522  void SetExportAspect(float a);
523  float GetExportAspect() const {return export_aspect_;}
524  void SetShowExportAspect(bool f);
525  bool GetShowExportAspect() const {return show_export_aspect_;}
526 
527  bool HasMultisample() const {return ms_flag_;}
528 protected:
529  friend class GfxObj;
530  friend class GfxNode;
531 
532  // TODO: this is really a hack and not clean communication
533  friend class Entity;
534 
535  void ObjectChanged(const String& name);
536  void SelectionChanged(const String& name, const mol::EntityView& sel);
537  void NodeTransformed(const GfxObjP& object);
538  void NodeAdded(const GfxNodeP& node);
539  void RenderModeChanged(const String& name);
540 
541 
542 private:
543 
544  template <typename ACTION>
545  void NotifyObservers(const ACTION& action) {
546  std::for_each(observers_.begin(), observers_.end(), action);
547  }
548  Scene();
549  Scene(const Scene&) {}
550  Scene& operator=(const Scene&) {return *this;}
551 
552  GLWinBase* win_; // target gl window
553 
554  mutable GfxNodeP root_node_; // mutable is slightly hackish
555  SceneObserverList observers_;
556 
557  geom::Transform transform_; // overall modelview transformation
558 
559  bool gl_init_;
560 
561  float fov_; // field of view
562  float znear_,zfar_; // near and far clipping plane
563  float fnear_,ffar_; // fog near and far offsets
564 
565  geom::Mat4 pmat_,ipmat_; // projection and inverted projection matrix
566  unsigned int vp_width_,vp_height_; // viewport
567 
568  SceneViewStack scene_view_stack_;
569 
570  float aspect_ratio_; // aspect ratio for viewport
571 
572  Color background_; // background (clear) color
573 
574  geom::Vec3 light_dir_; // infinite light source direction
575  geom::Mat3 light_rot_; // transform needed for the shadow map
576  Color light_amb_;
577  Color light_diff_;
578  Color light_spec_;
579  geom::Vec4 hemi_param_;
580 
581  bool cor_flag_;
582  bool fix_cor_flag_;
583  bool fog_flag_;
584  Color fog_color_;
585  bool auto_autoslab_; // run autoslab on each scene update
586  bool do_autoslab_; // run autoslab on next scene update
587  int autoslab_mode_; // 0: fast, 1:precise, 2: max
588 
589  bool offscreen_flag_; // a simple indicator whether in offscreen mode or not
590  OffscreenBuffer* main_offscreen_buffer_; // not null if a main offscreen buffer is present
591  uint old_vp_[2]; // used by the offline rendering code
592  std::string def_shading_mode_;
593 
594  uint selection_mode_;
595 
596  bool test_flag_;
597  std::vector<unsigned char> tmp_tex_;
598 
599  GLuint glyph_tex_id_;
600  std::vector<geom::Vec2> glyph_map_;
601  float def_text_size_;
602 
603  uint blur_count_;
604  std::vector<boost::shared_array<unsigned char> > blur_buffer_;
605 
606  unsigned int stereo_mode_;
607  unsigned int stereo_alg_;
608  bool stereo_inverted_;
609  int stereo_eye_;
610  Real stereo_iod_,stereo_distance_;
611  unsigned int scene_left_tex_;
612  unsigned int scene_right_tex_;
613  unsigned int bg_mode_;
614  bool update_bg_;
615  bool bg_stereo_mode_;
616  float bg_stereo_offset_;
617  Gradient bg_grad_;
618  Bitmap bg_bm_;
619  unsigned int bg_tex_;
620 
621  bool ms_flag_; // multisample flag
622 
623  float export_aspect_;
624  bool show_export_aspect_;
625 
626  void set_near(float n);
627  void set_far(float f);
628  void update_fog();
629  void flag_all_dirty();
630  void prep_glyphs();
631  void prep_blur();
632  void stereo_projection(int view);
633  void render_bg();
634  void render_export_aspect();
635  void render_scene();
636  void render_glow();
637  void render_stereo();
638  void set_bg();
639  void do_autoslab();
640 
641  bool IsNameAvailable(const String& name) const;
642 
643 };
644 
645 }} // ns
646 
647 #endif
geom::Mat3 GetLightRot() const
get main light orientation (internal debugging use)
Definition: scene.hh:284
bool HasMultisample() const
Definition: scene.hh:527
boost::shared_ptr< GfxObj > GfxObjP
bool GetShowExportAspect() const
Definition: scene.hh:525
std::string String
Definition: base.hh:54
float Real
Definition: base.hh:44
geom::Vec4 GetHemiParams() const
Definition: scene.hh:148
void SetAutoslabMode(int mode)
set autoslab mode
Definition: scene.hh:222
axis-aligned cuboid
bool GetShowCenter() const
Definition: scene.hh:501
main class for organization and root for the graphical display
Definition: scene.hh:81
geom::Vec3 GetLightDir() const
get main light direction
Definition: scene.hh:282
main class for all graphic objects
Definition: gfx_object.hh:51
Interface for observing graphical objects.
int GetStereoMode() const
Definition: scene.hh:246
geom::Mat4 GetProjection() const
Definition: scene.hh:154
graphical rendering of mol::EntityHandle entites
Definition: entity.hh:63
#define DLLEXPORT_OST_GFX
bool GetAutoAutoslab() const
get current state of automatic autoslab&#39;bing
Definition: scene.hh:239
int GetStereoView() const
return current stereo view mode
Definition: scene.hh:257
bool GetBackgroundStereoMode() const
Definition: scene.hh:358
Real GetStereoIOD() const
return current stereo eye distance
Definition: scene.hh:262
int GetAutoslabMode() const
return current autoslab mode
Definition: scene.hh:227
bool GetStereoFlip() const
return invert flag for stereo
Definition: scene.hh:251
std::vector< SceneObserver * > SceneObserverList
Definition: scene.hh:52
void SetFixCenter(bool f)
if true fix center of rotation upon input induced shift
Definition: scene.hh:504
basic and essential transformation class, including translation, rotation and center of rotation ...
Definition: transform.hh:39
Real GetStereoDistance() const
return current stereo distance offset from COR
Definition: scene.hh:267
Three dimensional vector class, using Real precision.
Definition: vec3.hh:42
geom::Mat4 GetInvertedProjection() const
Definition: scene.hh:155
float GetExportAspect() const
Definition: scene.hh:523
definition of EntityView
Definition: entity_view.hh:86
boost::shared_ptr< GfxNode > GfxNodeP
Definition: gfx_node_fw.hh:31
float GetBackgroundStereoOffset() const
Definition: scene.hh:362
bool GetFixCenter() const
return flag
Definition: scene.hh:507
color gradient
Definition: gradient.hh:59
float GetAspect() const
Definition: scene.hh:180
unsigned int GetStereoAlg() const
return current stereo algorithm
Definition: scene.hh:273
unsigned int uint
Definition: base.hh:29