OpenStructure
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-2020 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);
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;
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 
261  Real GetStereoIOD() const {return stereo_iod_;}
262 
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  */
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 
322 
324  std::pair<GfxObjP, mol::AtomHandle> PickAtom(int mx, int my);
325 
327  void RenderGL();
328 
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 
349 
353  bool GetBackgroundStereoMode() const {return bg_stereo_mode_;}
354 
357  float GetBackgroundStereoOffset() const {return bg_stereo_offset_;}
358 
360  void SetCenter(const geom::Vec3& cen);
361 
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 
387 
390 
393 
396 
398  void SetRTC(const geom::Mat4& rtc);
399 
401  void PushView();
402 
404  void PopView();
405 
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 
452 
457 
459  void SetTestMode(bool t);
460 
461  float ElapsedTime() const;
462 
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 
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 flag_all_dirty();
605  void prep_glyphs();
606  void prep_blur();
607  void stereo_projection(int view);
608  void render_bg();
609  void render_export_aspect();
610  void render_scene();
611  void render_glow();
612  void render_stereo();
613  void set_bg();
614  void do_autoslab();
615 
616  bool IsNameAvailable(const String& name) const;
617 
618 };
619 
620 }} // ns
621 
622 #endif
axis-aligned cuboid
basic and essential transformation class, including translation, rotation and center of rotation
Definition: transform.hh:39
Three dimensional vector class, using Real precision.
Definition: vec3.hh:48
graphical rendering of mol::EntityHandle entites
Definition: entity.hh:60
main class for all graphic objects
Definition: gfx_object.hh:52
color gradient
Definition: gradient.hh:59
main class for organization and root for the graphical display
Definition: scene.hh:80
bool HasMultisample() const
Definition: scene.hh:495
void SetShadowWeight(float w)
multiplier for shadow strength
std::pair< GfxObjP, mol::AtomHandle > PickAtom(int mx, int my)
pick atom at given mouse coord
void SetAmbientOcclusion(bool f)
experimental feature
int GetShadowQuality() const
get shadow quality
unsigned int GetStereoAlg() const
return current stereo algorithm
Definition: scene.hh:272
geom::Vec3 GetLightDir() const
get main light direction
Definition: scene.hh:281
void Autoslab()
adjust near and far clipping plane to fit visible objects
void SetBackgroundStereoOffset(float)
background tile left/right offset
void SetLightProp(const Color &amb, const Color &diff, const Color &spec)
set ambient, diffuse and specular light color
uint GetSelectionMode() const
void Apply(GfxNodeVisitor &v) const
apply node visitor to root node
void SetExportAspect(float a)
bool HasNode(const String &name) const
whether the scene contains a node of the given name
bool GetStereoFlip() const
return invert flag for stereo
Definition: scene.hh:250
bool GetFog() const
check fog status
void BlurSnapshot()
experimental feature
void SetDepthDarkening(bool f)
experimental feature
void Pick(int mx, int my, int mask)
pick at given mouse coords
GfxNodeP GetRootNode() const
get root node of scene graph
void AttachObserver(SceneObserver *o)
observer interface (internal use)
void InitGL(bool full=true)
initialize OpenGL after context has been setup (internal use)
Real GetStereoDistance() const
return current stereo distance offset from COR
Definition: scene.hh:266
bool GetShadow() const
get shadow mapping status
geom::Vec4 GetHemiParams() const
Definition: scene.hh:147
void SetBackground(const Bitmap &bm)
set background image
void SetStereoFlip(bool f)
invert stereo eyes for stereo mode=0
void SetLightProp(float amb, float diff, float spec)
set ambient, diffuse and specular light intensity
void Remove(const GfxNodeP &go)
remove graphical object from scene remove graphical object from the scene
void ExportPov(const std::string &fname, const std::string &wdir=".")
export scene into povray files named fname.pov and fname.inc
geom::Mat4 GetProjection() const
Definition: scene.hh:153
bool GetShowExportAspect() const
Definition: scene.hh:493
size_t GetNodeCount() const
get total number of nodes in scene
Viewport GetViewport() const
void SetBackgroundStereoMode(bool)
use bg bitmap for stereo mode this tiles the background bitmap at the far plane
bool GetAmbientOcclusion() const
experimental feature
void SetStereoMode(unsigned int mode)
set stereo mode one of 0 (off), 1 (quad-buffered) 2 (interlaced (for special monitors))
void SetStereoDistance(Real)
set stereo distance offset from COR
void Unregister(GLWinBase *win)
gui glue interface (internal use)
void SetBlur(uint n)
experimental feature
void Export(const String &fname, unsigned int w, unsigned int h, bool transparent=false)
int GetAutoslabMode() const
return current autoslab mode
Definition: scene.hh:226
void SetBackground(const Gradient &g)
set background gradient
void SetBackground(const Color &c)
set background color
geom::Vec3 Project(const geom::Vec3 &v, bool ignore_vp=false) const
calculate projection of a point into the scene
void PopView()
retrieve a previously pushed orientation
void SetTestMode(bool t)
switch into test mode (internal use)
void SetFogFarOffset(float o)
offset between far clipping plane and end of fog
void CenterOn(const GfxObjP &s)
center given object
void DetachObserver(SceneObserver *o)
observer interface (internal use)
uint GetAmbientOcclusionMode() const
experimental feature
void Export(const String &fname, bool transparent=false)
export snapshot of current scene
geom::AlignedCuboid GetBoundingBox(bool use_tf=true) const
return bounding box of scene
float GetExportAspect() const
Definition: scene.hh:491
void SetFogNearOffset(float o)
offset between near clipping plane and start of fog
geom::Transform GetTransform() const
get full underlying transformation
void SetTransform(const geom::Transform &t)
set transform
void Autoslab(bool fast, bool)
DEPRECATED, use Autoslab() and SetAutoslabMode(int)
void Export(Exporter *ex) const
export scene via exporter
float GetFogFarOffset() const
void SetStereoView(int)
stereo view mode one of 0 (center), -1 (left), 1 (right)
Color GetFogColor() const
get the fog color
geom::AlignedCuboid GetBoundingBox(const geom::Transform &tf) const
return bounding box of with a given transform
void SetRTC(const geom::Mat4 &rtc)
sets a previously retrieved orientation
Color GetBackground() const
get background color
void SetShadow(bool f)
turn shadow mapping on and off
void SetFixCenter(bool f)
if true fix center of rotation upon input induced shift
Definition: scene.hh:472
bool Rename(const String &old_name, const String &new_name)
rename an existing graphical object defunct for now
void SetNearFar(float n, float f)
set near and far clipping plane at once
float GetAspect() const
Definition: scene.hh:179
void NodeTransformed(const GfxObjP &object)
void Remove(const String &name)
remove graphical object from the scene
void SetFogOffsets(float no, float fo)
convenciene function to set fog near and far offset
void SelectionChanged(const String &name, const mol::EntityView &sel)
void ResetProjection()
brief re-generates the projection matrix (internal use)
void SetShowCenter(bool f)
show center of rotation of true
void SetStereoAlg(unsigned int)
set stereo algorithm one of 0 (default) or 1
bool GetFixCenter() const
return flag
Definition: scene.hh:475
uint GetAmbientOcclusionQuality() const
experimental feature
void SetSelectionMode(uint m)
set the selection mode
void SetNear(float n)
set near clipping plane
void SetAmbientOcclusionSize(float f)
experimental feature
int GetStereoMode() const
Definition: scene.hh:245
void Register(GLWinBase *win)
gui glue interface (internal use)
void RenderGL()
render all gl objects (internal use)
void SetShowExportAspect(bool f)
geom::Vec3 GetCenter() const
retrieve center
float GetDefaultTextSize()
void SetStereoIOD(Real)
set stereo eye distance
void Resize(int w, int h)
handle new viewport size (internal use)
void SetAmbientOcclusionWeight(float f)
experimental feature
bool GetBackgroundStereoMode() const
Definition: scene.hh:353
void SetLightDir(const geom::Vec3 &dir)
set main light direction
void StatusMessage(const String &s)
send status message to gui
void RemoveAll()
remove all objects from the scene
float GetAmbientOcclusionWeight() const
experimental feature
void RenderText(const TextPrim &t)
internal use
void SetFOV(float f)
set field of view angle
void SetFar(float f)
set far clipping plane
void SetAmbientOcclusionMode(uint m)
experimental feature
void NodeAdded(const GfxNodeP &node)
geom::Mat3 GetLightRot() const
get main light orientation (internal debugging use)
Definition: scene.hh:283
float GetFogNearOffset() const
void AutoAutoslab(bool f)
turn automatic autoslab'bing on or off for each scene update
void RequestRedraw()
request redraw of gl scene
void CenterOn(const String &s)
center on object of given name
void SetShadingMode(const std::string &smode)
select shading mode one of fallback, basic, default, hf, toon1, toon2
float GetBackgroundStereoOffset() const
Definition: scene.hh:357
GfxObjP operator[](const String &name)
retrieve gfx object by name
void ContextSwitch()
void Apply(const InputEvent &ie, bool request_redraw=true)
actual event handling for scene (internal use)
geom::Vec3 UnProject(const geom::Vec3 &v, bool ignore_vp=false) const
calculate unprojected point out of the scene
float GetShadowWeight() const
get shadow strength
void AutoslabMax()
DEPRECATED, use SetAutoslabMode(2)
bool GetAutoAutoslab() const
get current state of automatic autoslab'bing
Definition: scene.hh:238
void SetBeaconOff()
experimental feature
void SetCenter(const geom::Vec3 &cen)
center rotation on the given point
void SetAlphaBias(Real bias)
geom::Mat4 GetRTC() const
returns a compact, internal representation of the scene orientation
void Export(const String &fname, unsigned int w, unsigned int h, int max_samples, bool transparent=false)
export into bitmap, using multisample anti-aliasing
void ObjectChanged(const String &name)
float ElapsedTime() const
bool GetShowCenter() const
Definition: scene.hh:469
float GetFOV() const
void Autoslab(bool fast)
DEPRECATED, use Autoslab() and SetAutoslabMode(int)
void SetBeacon(int wx, int wy)
experimental feature
static Scene & Instance()
void SetFogColor(const Color &c)
set the fog color
void SetHemiParams(const geom::Vec4 &)
void SetDepthDarkeningWeight(float f)
experimental feature
void SetShadowQuality(int q)
shadow quality from 0 (low) to 3 (high), default=1
void PushView()
push the current orientation onto a stack
void SetAmbientOcclusionQuality(uint q)
experimental feature
void SetViewport(int w, int h)
set the viewport; the mapping to the visible window (internal use)
void SetFog(bool f)
turn fog on or off
int GetStereoView() const
return current stereo view mode
Definition: scene.hh:256
void SetAutoslabMode(int mode)
set autoslab mode
Definition: scene.hh:221
geom::Mat4 GetInvertedProjection() const
Definition: scene.hh:154
float GetAmbientOcclusionSize() const
experimental feature
Real GetStereoIOD() const
return current stereo eye distance
Definition: scene.hh:261
void RenderModeChanged(const String &name)
float GetFar() const
get far clipping plane
Interface for observing graphical objects.
definition of EntityView
Definition: entity_view.hh:86
#define DLLEXPORT_OST_GFX
unsigned int uint
Definition: base.hh:29
float Real
Definition: base.hh:44
std::string String
Definition: base.hh:54
boost::shared_ptr< GfxNode > GfxNodeP
Definition: gfx_node_fw.hh:31
boost::shared_ptr< GfxObj > GfxObjP
std::vector< SceneObserver * > SceneObserverList
Definition: scene.hh:51
Definition: base.dox:1