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;
52 
53 typedef std::vector<SceneObserver*> SceneObserverList;
54 
55 struct Viewport {
56  int x;
57  int y;
58  int width;
59  int height;
60 };
61 
62 namespace impl {class SceneFX;}
63 
81  friend class impl::SceneFX;
82  private:
83 
84  // TODO: this struct may be the seed for a proper
85  // refactoring of the scene it into a management
86  // and a view part
87  struct SceneViewStackEntry {
88  geom::Transform transform;
89  float fov;
90  float znear,zfar;
91  };
92 
93  typedef std::stack<SceneViewStackEntry> SceneViewStack;
94 
95  public:
96  static Scene& Instance();
97 
99  void SetFog(bool f);
101  bool GetFog() const;
103  void SetFogColor(const Color& c);
105  Color GetFogColor() const;
106 
108  void SetShadow(bool f);
110  bool GetShadow() const;
112  void SetShadowQuality(int q);
114  int GetShadowQuality() const;
116  void SetShadowWeight(float w);
118  float GetShadowWeight() const;
119 
121  void SetDepthDarkening(bool f);
123  void SetDepthDarkeningWeight(float f);
124 
126  void SetAmbientOcclusion(bool f);
128  bool GetAmbientOcclusion() const;
130  void SetAmbientOcclusionWeight(float f);
132  float GetAmbientOcclusionWeight() const;
134  void SetAmbientOcclusionMode(uint m);
136  uint GetAmbientOcclusionMode() const;
138  void SetAmbientOcclusionQuality(uint q);
140  uint GetAmbientOcclusionQuality() const;
142  void SetAmbientOcclusionSize(float f);
144  float GetAmbientOcclusionSize() const;
145 
146  void SetHemiParams(const geom::Vec4&);
147  geom::Vec4 GetHemiParams() const {return hemi_param_;}
148 
151  void SetShadingMode(const std::string& smode);
152 
153  geom::Mat4 GetProjection() const {return pmat_;}
154  geom::Mat4 GetInvertedProjection() const {return ipmat_;}
155 
157 
158  float GetNear() const;
160 
162  void SetNear(float n);
163 
165  float GetFar() const;
166 
168  void SetFar(float f);
169 
171  void SetNearFar(float n, float f);
172 
174  void SetFOV(float f);
175 
176  // \brief get the field of view
177  float GetFOV() const;
178 
179  float GetAspect() const {return aspect_ratio_;}
180 
182  void SetFogNearOffset(float o);
183 
185  float GetFogNearOffset() const;
186 
188  void SetFogFarOffset(float o);
189 
191  float GetFogFarOffset() const;
192 
194  void SetFogOffsets(float no, float fo);
195 
197  void Autoslab(bool fast);
198 
200  void Autoslab(bool fast, bool);
201 
203  void AutoslabMax();
204 
212  void Autoslab();
213 
221  void SetAutoslabMode(int mode) {
222  autoslab_mode_=std::min(2,std::max(0,mode));
223  }
224 
226  int GetAutoslabMode() const {
227  return autoslab_mode_;
228  }
229 
235  void AutoAutoslab(bool f);
236 
238  bool GetAutoAutoslab() const { return auto_autoslab_; }
239 
241 
244  void SetStereoMode(unsigned int mode);
245  int GetStereoMode() const {return stereo_mode_;}
246 
248  void SetStereoFlip(bool f);
250  bool GetStereoFlip() const {return stereo_inverted_;}
251 
254  void SetStereoView(int);
256  int GetStereoView() const {return stereo_eye_;}
257 
259  void SetStereoIOD(Real);
261  Real GetStereoIOD() const {return stereo_iod_;}
262 
264  void SetStereoDistance(Real);
266  Real GetStereoDistance() const {return stereo_distance_;}
267 
270  void SetStereoAlg(unsigned int);
272  unsigned int GetStereoAlg() const {return stereo_alg_;}
273 
275  void SetLightDir(const geom::Vec3& dir);
277  void SetLightProp(const Color& amb, const Color& diff, const Color& spec);
279  void SetLightProp(float amb, float diff, float spec);
281  geom::Vec3 GetLightDir() const {return light_dir_;}
283  geom::Mat3 GetLightRot() const {return light_rot_;}
284 
286  /*
287  bad style for now: 0=(reserved), 1=atom, 2=residue, 3=chain, 4=bond, 5=torsion
288  */
289  void SetSelectionMode(uint m);
290  uint GetSelectionMode() const;
291 
292 
293  void Export(const String& fname, unsigned int w,
294  unsigned int h, bool transparent=false);
296  void Export(const String& fname, unsigned int w,
297  unsigned int h, int max_samples, bool transparent=false);
298 
300  void Export(const String& fname, bool transparent=false);
301 
303  void ExportPov(const std::string& fname, const std::string& wdir=".");
304 
306  void Export(Exporter* ex) const;
307 
309  void OnInput(const InputEvent& e);
311 
313  void InitGL(bool full=true);
314 
316  void Resize(int w, int h);
317 
319  void Pick(int mx, int my, int mask);
320 
321  float GetDefaultTextSize();
322 
324  std::pair<GfxObjP, mol::AtomHandle> PickAtom(int mx, int my);
325 
327  void RenderGL();
328 
330  void RequestRedraw();
331 
333  void StatusMessage(const String& s);
334 
336  void SetViewport(int w, int h);
337 
339  void SetBackground(const Color& c);
340 
342  void SetBackground(const Gradient& g);
343 
345  void SetBackground(const Bitmap& bm);
346 
348  Color GetBackground() const;
349 
352  void SetBackgroundStereoMode(bool);
353  bool GetBackgroundStereoMode() const {return bg_stereo_mode_;}
354 
356  void SetBackgroundStereoOffset(float);
357  float GetBackgroundStereoOffset() const {return bg_stereo_offset_;}
358 
360  void SetCenter(const geom::Vec3& cen);
361 
363  geom::Vec3 GetCenter() const;
364 
366  void CenterOn(const String& s);
367 
369  void CenterOn(const GfxObjP& s);
370 
372  geom::Vec3 Project(const geom::Vec3& v, bool ignore_vp=false) const;
373 
375  geom::Vec3 UnProject(const geom::Vec3& v, bool ignore_vp=false) const;
376 
378 
383  geom::AlignedCuboid GetBoundingBox(bool use_tf=true) const;
384 
386  geom::AlignedCuboid GetBoundingBox(const geom::Transform& tf) const;
387 
389  geom::Transform GetTransform() const;
390 
392  void SetTransform(const geom::Transform& t);
393 
395  geom::Mat4 GetRTC() const;
396 
398  void SetRTC(const geom::Mat4& rtc);
399 
401  void PushView();
402 
404  void PopView();
405 
407  void ResetProjection();
408 
410  void Register(GLWinBase* win);
412  void Unregister(GLWinBase* win);
413 
415 
416  void Add(const GfxNodeP& go, bool redraw=true);
420  void Remove(const GfxNodeP& go);
422  void Remove(const String& name);
423 
425  void RemoveAll();
426 
429  bool Rename(const String& old_name, const String& new_name);
430 
432  GfxObjP operator[](const String& name);
433 
435  bool HasNode(const String& name) const;
436 
438  void Apply(const InputEvent& ie, bool request_redraw=true);
439 
441  void Apply(GfxNodeVisitor& v) const;
442 
447  size_t GetNodeCount() const;
448 
450  GfxNodeP GetRootNode() const;
452 
454  void AttachObserver(SceneObserver* o);
456  void DetachObserver(SceneObserver* o);
457 
459  void SetTestMode(bool t);
460 
461  float ElapsedTime() const;
462 
463  Viewport GetViewport() const;
464 
465 
467  void SetShowCenter(bool f);
468 
469  bool GetShowCenter() const {return cor_flag_;}
470 
472  void SetFixCenter(bool f) {fix_cor_flag_=f;}
473 
475  bool GetFixCenter() const {return fix_cor_flag_;}
476 
478  void SetBlur(uint n);
480  void BlurSnapshot();
481 
483  void RenderText(const TextPrim& t);
484 
486  void SetBeacon(int wx, int wy);
488  void SetBeaconOff();
489 
490  void SetExportAspect(float a);
491  float GetExportAspect() const {return export_aspect_;}
492  void SetShowExportAspect(bool f);
493  bool GetShowExportAspect() const {return show_export_aspect_;}
494 
495  bool HasMultisample() const {return ms_flag_;}
496 
497  void SetAlphaBias(Real bias);
498 
499  void ContextSwitch();
500 
501 protected:
502  friend class GfxObj;
503  friend class GfxNode;
504 
505  // TODO: this is really a hack and not clean communication
506  friend class Entity;
507 
508  void ObjectChanged(const String& name);
509  void SelectionChanged(const String& name, const mol::EntityView& sel);
510  void NodeTransformed(const GfxObjP& object);
511  void NodeAdded(const GfxNodeP& node);
512  void RenderModeChanged(const String& name);
513 
514 private:
515 
516  template <typename ACTION>
517  void NotifyObservers(const ACTION& action) {
518  std::for_each(observers_.begin(), observers_.end(), action);
519  }
520  Scene();
521  Scene(const Scene&) {}
522  Scene& operator=(const Scene&) {return *this;}
523  ~Scene();
524 
525  // The GL Context gets typically handled externally, e.g. using QT when
526  // calling InitGL(), RenderGL() etc. However, there is no guarantee that the
527  // desired GLContext is active when calling gl related functions in other
528  // occasions. This can be enforced by ActivateGLContext()
529  void ActivateGLContext() const;
530 
531  GLWinBase* win_; // target gl window
532 
533  mutable GfxNodeP root_node_; // mutable is slightly hackish
534  SceneObserverList observers_;
535 
536  geom::Transform transform_; // overall modelview transformation
537 
538  bool gl_init_;
539 
540  float fov_; // field of view
541  float znear_,zfar_; // near and far clipping plane
542  float fnear_,ffar_; // fog near and far offsets
543 
544  geom::Mat4 pmat_,ipmat_; // projection and inverted projection matrix
545  unsigned int vp_width_,vp_height_; // viewport
546 
547  SceneViewStack scene_view_stack_;
548 
549  float aspect_ratio_; // aspect ratio for viewport
550 
551  Color background_; // background (clear) color
552 
553  geom::Vec3 light_dir_; // infinite light source direction
554  geom::Mat3 light_rot_; // transform needed for the shadow map
555  Color light_amb_;
556  Color light_diff_;
557  Color light_spec_;
558  geom::Vec4 hemi_param_;
559 
560  bool cor_flag_;
561  bool fix_cor_flag_;
562  bool fog_flag_;
563  Color fog_color_;
564  bool auto_autoslab_; // run autoslab on each scene update
565  bool do_autoslab_; // run autoslab on next scene update
566  int autoslab_mode_; // 0: fast, 1:precise, 2: max
567 
568  std::string def_shading_mode_;
569 
570  uint selection_mode_;
571 
572  bool test_flag_;
573  std::vector<unsigned char> tmp_tex_;
574 
575  GLuint glyph_tex_id_;
576  std::vector<geom::Vec2> glyph_map_;
577  float def_text_size_;
578 
579  uint blur_count_;
580  std::vector<boost::shared_array<unsigned char> > blur_buffer_;
581 
582  unsigned int stereo_mode_;
583  unsigned int stereo_alg_;
584  bool stereo_inverted_;
585  int stereo_eye_;
586  Real stereo_iod_,stereo_distance_;
587  unsigned int scene_left_tex_;
588  unsigned int scene_right_tex_;
589  unsigned int bg_mode_;
590  bool update_bg_;
591  bool bg_stereo_mode_;
592  float bg_stereo_offset_;
593  Gradient bg_grad_;
594  Bitmap bg_bm_;
595  unsigned int bg_tex_;
596 
597  bool ms_flag_; // multisample flag
598 
599  float export_aspect_;
600  bool show_export_aspect_;
601 
602  void set_near(float n);
603  void set_far(float f);
604  void update_fog();
605  void flag_all_dirty();
606  void prep_glyphs();
607  void prep_blur();
608  void stereo_projection(int view);
609  void render_bg();
610  void render_export_aspect();
611  void render_scene();
612  void render_glow();
613  void render_stereo();
614  void set_bg();
615  void do_autoslab();
616 
617  bool IsNameAvailable(const String& name) const;
618 
619 };
620 
621 }} // ns
622 
623 #endif
geom::Mat3 GetLightRot() const
get main light orientation (internal debugging use)
Definition: scene.hh:283
bool HasMultisample() const
Definition: scene.hh:495
boost::shared_ptr< GfxObj > GfxObjP
bool GetShowExportAspect() const
Definition: scene.hh:493
std::string String
Definition: base.hh:54
float Real
Definition: base.hh:44
geom::Vec4 GetHemiParams() const
Definition: scene.hh:147
void SetAutoslabMode(int mode)
set autoslab mode
Definition: scene.hh:221
axis-aligned cuboid
bool GetShowCenter() const
Definition: scene.hh:469
main class for organization and root for the graphical display
Definition: scene.hh:80
geom::Vec3 GetLightDir() const
get main light direction
Definition: scene.hh:281
main class for all graphic objects
Definition: gfx_object.hh:51
Interface for observing graphical objects.
int GetStereoMode() const
Definition: scene.hh:245
geom::Mat4 GetProjection() const
Definition: scene.hh:153
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:238
int GetStereoView() const
return current stereo view mode
Definition: scene.hh:256
bool GetBackgroundStereoMode() const
Definition: scene.hh:353
Real GetStereoIOD() const
return current stereo eye distance
Definition: scene.hh:261
int GetAutoslabMode() const
return current autoslab mode
Definition: scene.hh:226
bool GetStereoFlip() const
return invert flag for stereo
Definition: scene.hh:250
std::vector< SceneObserver * > SceneObserverList
Definition: scene.hh:51
void SetFixCenter(bool f)
if true fix center of rotation upon input induced shift
Definition: scene.hh:472
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:266
Three dimensional vector class, using Real precision.
Definition: vec3.hh:42
geom::Mat4 GetInvertedProjection() const
Definition: scene.hh:154
float GetExportAspect() const
Definition: scene.hh:491
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:357
bool GetFixCenter() const
return flag
Definition: scene.hh:475
color gradient
Definition: gradient.hh:59
float GetAspect() const
Definition: scene.hh:179
unsigned int GetStereoAlg() const
return current stereo algorithm
Definition: scene.hh:272
unsigned int uint
Definition: base.hh:29