From ef0189651ecf2434186e1f4a88beaf9e9d37eaa7 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Sun, 16 Dec 2007 08:04:08 +0000 Subject: [PATCH] Added per-pixel lighting support --- panda/src/framework/pandaFramework.I | 10 ++++++++ panda/src/framework/pandaFramework.cxx | 34 +++++++++++++++++++++++++ panda/src/framework/pandaFramework.h | 4 +++ panda/src/framework/windowFramework.I | 10 ++++++++ panda/src/framework/windowFramework.cxx | 25 ++++++++++++++++++ panda/src/framework/windowFramework.h | 3 +++ 6 files changed, 86 insertions(+) diff --git a/panda/src/framework/pandaFramework.I b/panda/src/framework/pandaFramework.I index 53c4fc2447..c86eb69156 100644 --- a/panda/src/framework/pandaFramework.I +++ b/panda/src/framework/pandaFramework.I @@ -143,6 +143,16 @@ get_lighting() const { return _lighting_enabled; } +//////////////////////////////////////////////////////////////////// +// Function: PandaFramework::get_perpixel +// Access: Public +// Description: Returns the current state of the perpixel flag. +//////////////////////////////////////////////////////////////////// +INLINE bool PandaFramework:: +get_perpixel() const { + return _perpixel_enabled; +} + //////////////////////////////////////////////////////////////////// // Function: PandaFramework::get_background_type // Access: Public diff --git a/panda/src/framework/pandaFramework.cxx b/panda/src/framework/pandaFramework.cxx index c0a6251012..4dc8ce57f5 100644 --- a/panda/src/framework/pandaFramework.cxx +++ b/panda/src/framework/pandaFramework.cxx @@ -377,6 +377,7 @@ open_window(const WindowProperties &props, GraphicsPipe *pipe, wf->set_texture(get_texture()); wf->set_two_sided(get_two_sided()); wf->set_lighting(get_lighting()); + wf->set_perpixel(get_perpixel()); wf->set_background_type(get_background_type()); GraphicsWindow *win = wf->open_window(props, get_graphics_engine(), @@ -617,6 +618,22 @@ set_lighting(bool enable) { _lighting_enabled = enable; } +//////////////////////////////////////////////////////////////////// +// Function: PandaFramework::set_perpixel +// Access: Public +// Description: Sets the perpixel state on all windows. +//////////////////////////////////////////////////////////////////// +void PandaFramework:: +set_perpixel(bool enable) { + Windows::iterator wi; + for (wi = _windows.begin(); wi != _windows.end(); ++wi) { + WindowFramework *wf = (*wi); + wf->set_perpixel(enable); + } + + _perpixel_enabled = enable; +} + //////////////////////////////////////////////////////////////////// // Function: BackgroundFramework::set_background_type // Access: Public @@ -843,6 +860,7 @@ do_enable_default_keys() { define_key("b", "toggle backface (double-sided) rendering", event_b, this); define_key("i", "invert (reverse) single-sided faces", event_i, this); define_key("l", "toggle lighting", event_l, this); + define_key("p", "toggle per-pixel lighting", event_p, this); define_key("c", "recenter view on object", event_c, this); define_key("a", "toggle animation controls", event_a, this); define_key("shift-c", "toggle collision surfaces", event_C, this); @@ -1017,6 +1035,22 @@ event_l(const Event *event, void *) { } } +//////////////////////////////////////////////////////////////////// +// Function: PandaFramework::event_p +// Access: Public, Static +// Description: Default handler for p key: toggle per-pixel lighting. +//////////////////////////////////////////////////////////////////// +void PandaFramework:: +event_p(const Event *event, void *) { + if (event->get_num_parameters() == 1) { + EventParameter param = event->get_parameter(0); + WindowFramework *wf; + DCAST_INTO_V(wf, param.get_ptr()); + + wf->set_perpixel(!wf->get_perpixel()); + } +} + //////////////////////////////////////////////////////////////////// // Function: PandaFramework::event_c // Access: Public, Static diff --git a/panda/src/framework/pandaFramework.h b/panda/src/framework/pandaFramework.h index 4a5363d384..1688d2addb 100644 --- a/panda/src/framework/pandaFramework.h +++ b/panda/src/framework/pandaFramework.h @@ -87,12 +87,14 @@ public: void set_texture(bool enable); void set_two_sided(bool enable); void set_lighting(bool enable); + void set_perpixel(bool enable); void set_background_type(WindowFramework::BackgroundType type); INLINE bool get_wireframe() const; INLINE bool get_texture() const; INLINE bool get_two_sided() const; INLINE bool get_lighting() const; + INLINE bool get_perpixel() const; INLINE WindowFramework::BackgroundType get_background_type() const; static int hide_collision_solids(NodePath node); @@ -131,6 +133,7 @@ public: static void event_b(const Event *, void *data); static void event_i(const Event *, void *data); static void event_l(const Event *, void *data); + static void event_p(const Event *, void *data); static void event_c(const Event *, void *data); static void event_a(const Event *, void *data); static void event_C(const Event *, void *data); @@ -177,6 +180,7 @@ private: bool _texture_enabled; bool _two_sided_enabled; bool _lighting_enabled; + bool _perpixel_enabled; WindowFramework::BackgroundType _background_type; NodePath _highlight; diff --git a/panda/src/framework/windowFramework.I b/panda/src/framework/windowFramework.I index 4e929c0e5a..76197943e9 100644 --- a/panda/src/framework/windowFramework.I +++ b/panda/src/framework/windowFramework.I @@ -123,6 +123,16 @@ get_lighting() const { return _lighting_enabled; } +//////////////////////////////////////////////////////////////////// +// Function: WindowFramework::get_perpixel +// Access: Public +// Description: Returns the current state of the perpixel flag. +//////////////////////////////////////////////////////////////////// +INLINE bool WindowFramework:: +get_perpixel() const { + return _perpixel_enabled; +} + //////////////////////////////////////////////////////////////////// // Function: WindowFramework::get_background_type // Access: Public diff --git a/panda/src/framework/windowFramework.cxx b/panda/src/framework/windowFramework.cxx index 48c1cf35c9..894c3ccac1 100644 --- a/panda/src/framework/windowFramework.cxx +++ b/panda/src/framework/windowFramework.cxx @@ -97,6 +97,7 @@ WindowFramework(PandaFramework *panda_framework) : _two_sided_enabled = false; _one_sided_reverse_enabled = false; _lighting_enabled = false; + _perpixel_enabled = false; _background_type = BT_default; } @@ -123,6 +124,7 @@ WindowFramework(const WindowFramework ©, DisplayRegion *display_region) : _two_sided_enabled = false; _one_sided_reverse_enabled = false; _lighting_enabled = false; + _perpixel_enabled = false; _background_type = BT_default; set_background_type(copy._background_type); @@ -223,6 +225,7 @@ close_window() { _two_sided_enabled = false; _one_sided_reverse_enabled = false; _lighting_enabled = false; + _perpixel_enabled = false; if (_frame_rate_meter != (FrameRateMeter *)NULL) { _frame_rate_meter->clear_window(); @@ -966,6 +969,28 @@ set_lighting(bool enable) { _lighting_enabled = enable; } +//////////////////////////////////////////////////////////////////// +// Function: WindowFramework::set_perpixel +// Access: Public +// Description: Turns per-pixel lighting on (true) or off (false). +//////////////////////////////////////////////////////////////////// +void WindowFramework:: +set_perpixel(bool enable) { + if (enable == _perpixel_enabled) { + return; + } + + NodePath render = get_render(); + + if (enable) { + render.set_shader_auto(); + } else { + render.set_shader_off(); + } + + _perpixel_enabled = enable; +} + //////////////////////////////////////////////////////////////////// // Function: WindowFramework::set_background_type // Access: Public diff --git a/panda/src/framework/windowFramework.h b/panda/src/framework/windowFramework.h index 2baa09e4d0..b7a6864a27 100644 --- a/panda/src/framework/windowFramework.h +++ b/panda/src/framework/windowFramework.h @@ -120,6 +120,7 @@ public: void set_two_sided(bool enable); void set_one_sided_reverse(bool enable); void set_lighting(bool enable); + void set_perpixel(bool enable); void set_background_type(BackgroundType type); INLINE bool get_wireframe() const; @@ -127,6 +128,7 @@ public: INLINE bool get_two_sided() const; INLINE bool get_one_sided_reverse() const; INLINE bool get_lighting() const; + INLINE bool get_perpixel() const; INLINE BackgroundType get_background_type() const; static TextFont *get_shuttle_controls_font(); @@ -192,6 +194,7 @@ private: bool _two_sided_enabled; bool _one_sided_reverse_enabled; bool _lighting_enabled; + bool _perpixel_enabled; PT(FrameRateMeter) _frame_rate_meter;