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/mol/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 
47 namespace ost { namespace gfx {
48 
49 class InputEvent;
51 
52 typedef std::vector<SceneObserver*> SceneObserverList;
53 
54 struct Viewport {
55  int x;
56  int y;
57  int width;
58  int height;
59 };
60 
61 namespace impl {class SceneFX;}
62 
80  friend class impl::SceneFX;
81  private:
82 
83  // TODO: this struct may be the seed for a proper
84  // refactoring of the scene it into a management
85  // and a view part
86  struct SceneViewStackEntry {
87  mol::Transform transform;
88  float fov;
89  float znear,zfar;
90  };
91 
92  typedef std::stack<SceneViewStackEntry> SceneViewStack;
93 
94  public:
95  static Scene& Instance();
96 
98  void SetFog(bool f);
100  bool GetFog() const;
102  void SetFogColor(const Color& c);
104  Color GetFogColor() const;
105 
107  void SetShadow(bool f);
109  bool GetShadow() const;
111  void SetShadowQuality(int q);
113  int GetShadowQuality() const;
115  void SetShadowWeight(float w);
117  float GetShadowWeight() const;
118 
120  void SetDepthDarkening(bool f);
122  void SetDepthDarkeningWeight(float f);
123 
125  void SetAmbientOcclusion(bool f);
127  bool GetAmbientOcclusion() const;
129  void SetAmbientOcclusionWeight(float f);
131  float GetAmbientOcclusionWeight() const;
133  void SetAmbientOcclusionMode(uint m);
135  uint GetAmbientOcclusionMode() const;
137  void SetAmbientOcclusionQuality(uint q);
139  uint GetAmbientOcclusionQuality() const;
140 
143  void SetShadingMode(const std::string& smode);
144 
146 
147 
148  float GetNear() const;
149 
151  void SetNear(float n);
152 
154  float GetFar() const;
155 
157  void SetFar(float f);
158 
160  void SetNearFar(float n, float f);
161 
163  void SetFOV(float f);
164 
165  // \brief get the field of view
166  float GetFOV() const;
167 
169  void SetFogNearOffset(float o);
170 
172  float GetFogNearOffset() const;
173 
175  void SetFogFarOffset(float o);
176 
178  float GetFogFarOffset() const;
179 
181  void SetFogOffsets(float no, float fo);
182 
184  // TODO: use mode aka fast, precise, max
185  void Autoslab(bool fast=false, bool redraw=true);
186 
189  // TODO: merge with Autoslab
190  void AutoslabMax();
191 
193  // TODO: more sophisticated mode, aka fast, precise, max
194  void AutoAutoslab(bool f);
195 
197  bool GetAutoAutoslab() const { return auto_autoslab_; }
198 
200 
203  void SetStereoMode(unsigned int mode);
204  int GetStereoMode() const {return stereo_mode_;}
205 
207  void SetStereoFlip(bool f);
209  bool GetStereoFlip() const {return stereo_inverted_;}
210 
213  void SetStereoView(int);
215  int GetStereoView() const {return stereo_eye_;}
216 
218  void SetStereoIOD(Real);
220  Real GetStereoIOD() const {return stereo_iod_;}
221 
223  void SetStereoDistance(Real);
225  Real GetStereoDistance() const {return stereo_distance_;}
226 
229  void SetStereoAlg(unsigned int);
231  unsigned int GetStereoAlg() const {return stereo_alg_;}
232 
234  void SetLightDir(const geom::Vec3& dir);
236  void SetLightProp(const Color& amb, const Color& diff, const Color& spec);
238  void SetLightProp(float amb, float diff, float spec);
240  geom::Vec3 GetLightDir() const {return light_dir_;}
242  geom::Mat3 GetLightRot() const {return light_rot_;}
243 
245  /*
246  bad style for now: 0=(reserved), 1=atom, 2=residue, 3=chain, 4=bond, 5=torsion
247  */
248  void SetSelectionMode(uint m);
249  uint GetSelectionMode() const;
250 
252 
253 
254 
255 
256  void Export(const String& fname, unsigned int w,
257  unsigned int h, bool transparent=false);
258 
260  void Export(const String& fname, bool transparent=false);
261 
263  void ExportPov(const std::string& fname, const std::string& wdir=".");
264 
266  void Export(Exporter* ex) const;
267 
269 
270  void OnInput(const InputEvent& e);
271 
273  void InitGL(bool full=true);
274 
276  void Resize(int w, int h);
277 
279  void Pick(int mx, int my, int mask);
280 
281  float GetDefaultTextSize();
282 
284  std::pair<GfxObjP, mol::AtomHandle> PickAtom(int mx, int my);
285 
287  void RenderGL();
288 
290  void RequestRedraw();
291 
293  void StatusMessage(const String& s);
294 
296  void SetViewport(int w, int h);
297 
299  void SetBackground(const Color& c);
300 
302  Color GetBackground() const;
303 
305  void SetCenter(const geom::Vec3& cen);
306 
308  geom::Vec3 GetCenter() const;
309 
311  void CenterOn(const String& s);
312 
314  void CenterOn(const GfxObjP& s);
315 
317  geom::Vec3 Project(const geom::Vec3& v, bool ignore_vp=false) const;
318 
320  geom::Vec3 UnProject(const geom::Vec3& v, bool ignore_vp=false) const;
321 
323  geom::AlignedCuboid GetBoundingBox(const mol::Transform& tf) const;
324 
326  mol::Transform GetTransform() const;
327 
329  void SetTransform(const mol::Transform& t);
330 
332  geom::Mat4 GetRTC() const;
333 
335  void SetRTC(const geom::Mat4& rtc);
336 
338  void PushView();
339 
341  void PopView();
342 
344  void ResetProjection();
345 
347  void Register(GLWinBase* win);
349  void Unregister(GLWinBase* win);
350 
352 
353 
354  void Add(const GfxNodeP& go, bool redraw=true);
357  void Remove(const GfxNodeP& go);
359  void Remove(const String& name);
360 
362  void RemoveAll();
363 
366  bool Rename(const String& old_name, const String& new_name);
367 
369  GfxObjP operator[](const String& name);
370 
372  bool HasNode(const String& name) const;
373 
375  void Apply(const InputEvent& ie, bool request_redraw=true);
376 
378  void Apply(GfxNodeVisitor& v) const;
379 
384  size_t GetNodeCount() const;
385 
387  GfxNodeP GetRootNode() const;
389 
391  void AttachObserver(SceneObserver* o);
393  void DetachObserver(SceneObserver* o);
394 
395  bool InOffscreenMode() const;
396 
398  void SetTestMode(bool t);
399 
400  float ElapsedTime() const;
401 
402  Viewport GetViewport() const;
403 
417  bool StartOffscreenMode(unsigned int w, unsigned int h);
419  void StopOffscreenMode();
420 
422  void SetShowCenter(bool f);
423 
424  bool GetShowCenter() const {return cor_flag_;}
425 
427  void SetFixCenter(bool f) {fix_cor_flag_=f;}
428 
430  bool GetFixCenter() const {return fix_cor_flag_;}
431 
433  void SetBlur(uint n);
435  void BlurSnapshot();
436 
438  void RenderText(const TextPrim& t);
439 
441  void SetBeacon(int wx, int wy);
443  void SetBeaconOff();
444 
445 protected:
446  friend class GfxObj;
447  friend class GfxNode;
448 
449  // TODO: this is really a hack and not clean communication
450  friend class Entity;
451 
452  void ObjectChanged(const String& name);
453  void SelectionChanged(const String& name, const mol::EntityView& sel);
454  void NodeTransformed(const GfxObjP& object);
455  void NodeAdded(const GfxNodeP& node);
456  void RenderModeChanged(const String& name);
457 
458 private:
459 
460  template <typename ACTION>
461  void NotifyObservers(const ACTION& action) {
462  std::for_each(observers_.begin(), observers_.end(), action);
463  }
464  Scene();
465  Scene(const Scene&) {}
466  Scene& operator=(const Scene&) {return *this;}
467 
468  GLWinBase* win_; // target gl window
469 
470  mutable GfxNodeP root_node_; // mutable is slightly hackish
471  SceneObserverList observers_;
472 
473  mol::Transform transform_; // overall modelview transformation
474 
475  bool gl_init_;
476 
477  float fov_; // field of view
478  float znear_,zfar_; // near and far clipping plane
479  float fnear_,ffar_; // fog near and far offsets
480 
481  unsigned int vp_width_,vp_height_; // viewport
482 
483  SceneViewStack scene_view_stack_;
484 
485  float aspect_ratio_; // aspect ratio for viewport
486 
487  Color background_; // background (clear) color
488 
489  geom::Vec3 light_dir_; // infinite light source direction
490  geom::Mat3 light_rot_; // transform needed for the shadow map
491  Color light_amb_;
492  Color light_diff_;
493  Color light_spec_;
494 
495  bool cor_flag_;
496  bool fix_cor_flag_;
497  bool fog_flag_;
498  Color fog_color_;
499  bool auto_autoslab_;
500  bool do_autoslab_,do_autoslab_fast_;
501 
502  bool offscreen_flag_; // a simple indicator whether in offscreen mode or not
503  OffscreenBuffer* main_offscreen_buffer_; // not null if a main offscreen buffer is present
504  uint old_vp_[2]; // used by the offline rendering code
505  std::string def_shading_mode_;
506 
507  uint selection_mode_;
508 
509  bool test_flag_;
510  std::vector<unsigned char> tmp_tex_;
511 
512  GLuint glyph_tex_id_;
513  std::vector<geom::Vec2> glyph_map_;
514  float def_text_size_;
515 
516  uint blur_count_;
517  std::vector<boost::shared_array<unsigned char> > blur_buffer_;
518 
519  unsigned int stereo_mode_;
520  unsigned int stereo_alg_;
521  bool stereo_inverted_;
522  unsigned int stereo_eye_;
523  Real stereo_iod_,stereo_distance_;
524  unsigned int scene_left_tex_;
525  unsigned int scene_right_tex_;
526 
527  void set_near(float n);
528  void set_far(float f);
529  void update_fog();
530  void flag_all_dirty();
531  void prep_glyphs();
532  void prep_blur();
533  void stereo_projection(int view);
534  void render_scene();
535  void render_glow();
536  void render_stereo();
537 
538  void do_autoslab();
539 
540  bool IsNameAvailable(const String& name) const;
541 
542 };
543 
544 }} // ns
545 
546 #endif