00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef OST_SCENE_HH
00020 #define OST_SCENE_HH
00021
00022
00023
00024
00025
00026 #include <map>
00027 #include <stack>
00028 #include <vector>
00029
00030 #include <boost/shared_array.hpp>
00031
00032 #include <ost/gfx/module_config.hh>
00033 #include <ost/geom/transform.hh>
00034 #include <ost/mol/atom_handle.hh>
00035
00036 #include "gl_include.hh"
00037 #include "color.hh"
00038 #include "gfx_object_fw.hh"
00039 #include "gfx_node_fw.hh"
00040 #include "gfx_node_visitor.hh"
00041 #include "glwin_base.hh"
00042 #include "scene_observer.hh"
00043 #include "gfx_prim.hh"
00044 #include "povray_fw.hh"
00045 #include "exporter_fw.hh"
00046 #include "gradient.hh"
00047 #include "bitmap_io.hh"
00048
00049 namespace ost { namespace gfx {
00050
00051 class InputEvent;
00052 class OffscreenBuffer;
00053
00054 typedef std::vector<SceneObserver*> SceneObserverList;
00055
00056 struct Viewport {
00057 int x;
00058 int y;
00059 int width;
00060 int height;
00061 };
00062
00063 namespace impl {class SceneFX;}
00064
00081 class DLLEXPORT_OST_GFX Scene {
00082 friend class impl::SceneFX;
00083 private:
00084
00085
00086
00087
00088 struct SceneViewStackEntry {
00089 geom::Transform transform;
00090 float fov;
00091 float znear,zfar;
00092 };
00093
00094 typedef std::stack<SceneViewStackEntry> SceneViewStack;
00095
00096 public:
00097 static Scene& Instance();
00098
00100 void SetFog(bool f);
00102 bool GetFog() const;
00104 void SetFogColor(const Color& c);
00106 Color GetFogColor() const;
00107
00109 void SetShadow(bool f);
00111 bool GetShadow() const;
00113 void SetShadowQuality(int q);
00115 int GetShadowQuality() const;
00117 void SetShadowWeight(float w);
00119 float GetShadowWeight() const;
00120
00122 void SetDepthDarkening(bool f);
00124 void SetDepthDarkeningWeight(float f);
00125
00127 void SetAmbientOcclusion(bool f);
00129 bool GetAmbientOcclusion() const;
00131 void SetAmbientOcclusionWeight(float f);
00133 float GetAmbientOcclusionWeight() const;
00135 void SetAmbientOcclusionMode(uint m);
00137 uint GetAmbientOcclusionMode() const;
00139 void SetAmbientOcclusionQuality(uint q);
00141 uint GetAmbientOcclusionQuality() const;
00143 void SetAmbientOcclusionSize(float f);
00145 float GetAmbientOcclusionSize() const;
00146
00147 void SetHemiParams(const geom::Vec4&);
00148 geom::Vec4 GetHemiParams() const {return hemi_param_;}
00149
00152 void SetShadingMode(const std::string& smode);
00153
00154 geom::Mat4 GetProjection() const {return pmat_;}
00155 geom::Mat4 GetInvertedProjection() const {return ipmat_;}
00156
00158
00159
00160 float GetNear() const;
00161
00163 void SetNear(float n);
00164
00166 float GetFar() const;
00167
00169 void SetFar(float f);
00170
00172 void SetNearFar(float n, float f);
00173
00175 void SetFOV(float f);
00176
00177
00178 float GetFOV() const;
00179
00180 float GetAspect() const {return aspect_ratio_;}
00181
00183 void SetFogNearOffset(float o);
00184
00186 float GetFogNearOffset() const;
00187
00189 void SetFogFarOffset(float o);
00190
00192 float GetFogFarOffset() const;
00193
00195 void SetFogOffsets(float no, float fo);
00196
00198 void Autoslab(bool fast);
00199
00201 void Autoslab(bool fast, bool);
00202
00204 void AutoslabMax();
00205
00213 void Autoslab();
00214
00222 void SetAutoslabMode(int mode) {
00223 autoslab_mode_=std::min(2,std::max(0,mode));
00224 }
00225
00227 int GetAutoslabMode() const {
00228 return autoslab_mode_;
00229 }
00230
00236 void AutoAutoslab(bool f);
00237
00239 bool GetAutoAutoslab() const { return auto_autoslab_; }
00240
00242
00245 void SetStereoMode(unsigned int mode);
00246 int GetStereoMode() const {return stereo_mode_;}
00247
00249 void SetStereoFlip(bool f);
00251 bool GetStereoFlip() const {return stereo_inverted_;}
00252
00255 void SetStereoView(int);
00257 int GetStereoView() const {return stereo_eye_;}
00258
00260 void SetStereoIOD(Real);
00262 Real GetStereoIOD() const {return stereo_iod_;}
00263
00265 void SetStereoDistance(Real);
00267 Real GetStereoDistance() const {return stereo_distance_;}
00268
00271 void SetStereoAlg(unsigned int);
00273 unsigned int GetStereoAlg() const {return stereo_alg_;}
00274
00276 void SetLightDir(const geom::Vec3& dir);
00278 void SetLightProp(const Color& amb, const Color& diff, const Color& spec);
00280 void SetLightProp(float amb, float diff, float spec);
00282 geom::Vec3 GetLightDir() const {return light_dir_;}
00284 geom::Mat3 GetLightRot() const {return light_rot_;}
00285
00287
00288
00289
00290 void SetSelectionMode(uint m);
00291 uint GetSelectionMode() const;
00292
00294
00295
00296
00297
00298 void Export(const String& fname, unsigned int w,
00299 unsigned int h, bool transparent=false);
00301 void Export(const String& fname, unsigned int w,
00302 unsigned int h, int max_samples, bool transparent=false);
00303
00305 void Export(const String& fname, bool transparent=false);
00306
00308 void ExportPov(const std::string& fname, const std::string& wdir=".");
00309
00311 void Export(Exporter* ex) const;
00312
00314
00315 void OnInput(const InputEvent& e);
00316
00318 void InitGL(bool full=true);
00319
00321 void Resize(int w, int h);
00322
00324 void Pick(int mx, int my, int mask);
00325
00326 float GetDefaultTextSize();
00327
00329 std::pair<GfxObjP, mol::AtomHandle> PickAtom(int mx, int my);
00330
00332 void RenderGL();
00333
00335 void RequestRedraw();
00336
00338 void StatusMessage(const String& s);
00339
00341 void SetViewport(int w, int h);
00342
00344 void SetBackground(const Color& c);
00345
00347 void SetBackground(const Gradient& g);
00348
00350 void SetBackground(const Bitmap& bm);
00351
00353 Color GetBackground() const;
00354
00357 void SetBackgroundStereoMode(bool);
00358 bool GetBackgroundStereoMode() const {return bg_stereo_mode_;}
00359
00361 void SetBackgroundStereoOffset(float);
00362 float GetBackgroundStereoOffset() const {return bg_stereo_offset_;}
00363
00365 void SetCenter(const geom::Vec3& cen);
00366
00368 geom::Vec3 GetCenter() const;
00369
00371 void CenterOn(const String& s);
00372
00374 void CenterOn(const GfxObjP& s);
00375
00377 geom::Vec3 Project(const geom::Vec3& v, bool ignore_vp=false) const;
00378
00380 geom::Vec3 UnProject(const geom::Vec3& v, bool ignore_vp=false) const;
00381
00383
00388 geom::AlignedCuboid GetBoundingBox(bool use_tf=true) const;
00389
00391 geom::AlignedCuboid GetBoundingBox(const geom::Transform& tf) const;
00392
00394 geom::Transform GetTransform() const;
00395
00397 void SetTransform(const geom::Transform& t);
00398
00400 geom::Mat4 GetRTC() const;
00401
00403 void SetRTC(const geom::Mat4& rtc);
00404
00406 void PushView();
00407
00409 void PopView();
00410
00412 void ResetProjection();
00413
00415 void Register(GLWinBase* win);
00417 void Unregister(GLWinBase* win);
00418
00420
00421
00422 void Add(const GfxNodeP& go, bool redraw=true);
00425 void Remove(const GfxNodeP& go);
00427 void Remove(const String& name);
00428
00430 void RemoveAll();
00431
00434 bool Rename(const String& old_name, const String& new_name);
00435
00437 GfxObjP operator[](const String& name);
00438
00440 bool HasNode(const String& name) const;
00441
00443 void Apply(const InputEvent& ie, bool request_redraw=true);
00444
00446 void Apply(GfxNodeVisitor& v) const;
00447
00452 size_t GetNodeCount() const;
00453
00455 GfxNodeP GetRootNode() const;
00457
00459 void AttachObserver(SceneObserver* o);
00461 void DetachObserver(SceneObserver* o);
00462
00463 bool InOffscreenMode() const;
00464
00466 void SetTestMode(bool t);
00467
00468 float ElapsedTime() const;
00469
00470 Viewport GetViewport() const;
00471
00492 bool StartOffscreenMode(unsigned int w, unsigned int h, int max_samples);
00493 bool StartOffscreenMode(unsigned int w, unsigned int h);
00494
00496 void StopOffscreenMode();
00497
00499 void SetShowCenter(bool f);
00500
00501 bool GetShowCenter() const {return cor_flag_;}
00502
00504 void SetFixCenter(bool f) {fix_cor_flag_=f;}
00505
00507 bool GetFixCenter() const {return fix_cor_flag_;}
00508
00510 void SetBlur(uint n);
00512 void BlurSnapshot();
00513
00515 void RenderText(const TextPrim& t);
00516
00518 void SetBeacon(int wx, int wy);
00520 void SetBeaconOff();
00521
00522 void SetExportAspect(float a);
00523 float GetExportAspect() const {return export_aspect_;}
00524 void SetShowExportAspect(bool f);
00525 bool GetShowExportAspect() const {return show_export_aspect_;}
00526
00527 bool HasMultisample() const {return ms_flag_;}
00528 protected:
00529 friend class GfxObj;
00530 friend class GfxNode;
00531
00532
00533 friend class Entity;
00534
00535 void ObjectChanged(const String& name);
00536 void SelectionChanged(const String& name, const mol::EntityView& sel);
00537 void NodeTransformed(const GfxObjP& object);
00538 void NodeAdded(const GfxNodeP& node);
00539 void RenderModeChanged(const String& name);
00540
00541
00542 private:
00543
00544 template <typename ACTION>
00545 void NotifyObservers(const ACTION& action) {
00546 std::for_each(observers_.begin(), observers_.end(), action);
00547 }
00548 Scene();
00549 Scene(const Scene&) {}
00550 Scene& operator=(const Scene&) {return *this;}
00551
00552 GLWinBase* win_;
00553
00554 mutable GfxNodeP root_node_;
00555 SceneObserverList observers_;
00556
00557 geom::Transform transform_;
00558
00559 bool gl_init_;
00560
00561 float fov_;
00562 float znear_,zfar_;
00563 float fnear_,ffar_;
00564
00565 geom::Mat4 pmat_,ipmat_;
00566 unsigned int vp_width_,vp_height_;
00567
00568 SceneViewStack scene_view_stack_;
00569
00570 float aspect_ratio_;
00571
00572 Color background_;
00573
00574 geom::Vec3 light_dir_;
00575 geom::Mat3 light_rot_;
00576 Color light_amb_;
00577 Color light_diff_;
00578 Color light_spec_;
00579 geom::Vec4 hemi_param_;
00580
00581 bool cor_flag_;
00582 bool fix_cor_flag_;
00583 bool fog_flag_;
00584 Color fog_color_;
00585 bool auto_autoslab_;
00586 bool do_autoslab_;
00587 int autoslab_mode_;
00588
00589 bool offscreen_flag_;
00590 OffscreenBuffer* main_offscreen_buffer_;
00591 uint old_vp_[2];
00592 std::string def_shading_mode_;
00593
00594 uint selection_mode_;
00595
00596 bool test_flag_;
00597 std::vector<unsigned char> tmp_tex_;
00598
00599 GLuint glyph_tex_id_;
00600 std::vector<geom::Vec2> glyph_map_;
00601 float def_text_size_;
00602
00603 uint blur_count_;
00604 std::vector<boost::shared_array<unsigned char> > blur_buffer_;
00605
00606 unsigned int stereo_mode_;
00607 unsigned int stereo_alg_;
00608 bool stereo_inverted_;
00609 int stereo_eye_;
00610 Real stereo_iod_,stereo_distance_;
00611 unsigned int scene_left_tex_;
00612 unsigned int scene_right_tex_;
00613 unsigned int bg_mode_;
00614 bool update_bg_;
00615 bool bg_stereo_mode_;
00616 float bg_stereo_offset_;
00617 Gradient bg_grad_;
00618 Bitmap bg_bm_;
00619 unsigned int bg_tex_;
00620
00621 bool ms_flag_;
00622
00623 float export_aspect_;
00624 bool show_export_aspect_;
00625
00626 void set_near(float n);
00627 void set_far(float f);
00628 void update_fog();
00629 void flag_all_dirty();
00630 void prep_glyphs();
00631 void prep_blur();
00632 void stereo_projection(int view);
00633 void render_bg();
00634 void render_export_aspect();
00635 void render_scene();
00636 void render_glow();
00637 void render_stereo();
00638 void set_bg();
00639 void do_autoslab();
00640
00641 bool IsNameAvailable(const String& name) const;
00642
00643 };
00644
00645 }}
00646
00647 #endif