diff --git a/panda/src/pgraph/Sources.pp b/panda/src/pgraph/Sources.pp index 44722c5491..ddd5c03acb 100644 --- a/panda/src/pgraph/Sources.pp +++ b/panda/src/pgraph/Sources.pp @@ -41,6 +41,7 @@ depthWriteAttrib.I depthWriteAttrib.h \ directionalLight.I directionalLight.h \ drawCullHandler.I drawCullHandler.h \ + fadeLodNode.I fadeLodNode.h \ findApproxLevelEntry.I findApproxLevelEntry.h \ findApproxPath.I findApproxPath.h \ fog.I fog.h \ @@ -128,6 +129,7 @@ depthWriteAttrib.cxx \ directionalLight.cxx \ drawCullHandler.cxx \ + fadeLodNode.cxx \ findApproxLevelEntry.cxx \ findApproxPath.cxx \ fog.cxx \ @@ -214,6 +216,7 @@ depthWriteAttrib.I depthWriteAttrib.h \ directionalLight.I directionalLight.h \ drawCullHandler.I drawCullHandler.h \ + fadeLodNode.I fadeLodNode.h \ fog.I fog.h \ fogAttrib.I fogAttrib.h \ geomNode.I geomNode.h \ diff --git a/panda/src/pgraph/config_pgraph.cxx b/panda/src/pgraph/config_pgraph.cxx index cddab63124..8684242a2e 100644 --- a/panda/src/pgraph/config_pgraph.cxx +++ b/panda/src/pgraph/config_pgraph.cxx @@ -42,6 +42,7 @@ #include "depthTestAttrib.h" #include "depthWriteAttrib.h" #include "directionalLight.h" +#include "fadeLodNode.h" #include "fog.h" #include "fogAttrib.h" #include "geomNode.h" @@ -192,6 +193,7 @@ init_libpgraph() { DepthTestAttrib::init_type(); DepthWriteAttrib::init_type(); DirectionalLight::init_type(); + FadeLODNode::init_type(); Fog::init_type(); FogAttrib::init_type(); GeomNode::init_type(); diff --git a/panda/src/pgraph/fadeLodNode.I b/panda/src/pgraph/fadeLodNode.I new file mode 100755 index 0000000000..6a64e48c28 --- /dev/null +++ b/panda/src/pgraph/fadeLodNode.I @@ -0,0 +1,202 @@ +// Filename: fadelodNode.I +// Created by: sshodhan (14Jun04) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::CData::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE FadeLODNode::CData:: +CData() { + _fade_time = 1.0; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::CData::Copy Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE FadeLODNode::CData:: +CData(const FadeLODNode::CData ©) : + _lod(copy._lod) { + _fade_time = copy._fade_time; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE FadeLODNode:: +FadeLODNode(const string &name) : + PandaNode(name) { + _fade_mode = false; + _previous_child = 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::Copy Constructor +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +INLINE FadeLODNode:: +FadeLODNode(const FadeLODNode ©) : + PandaNode(copy), + _cycler(copy._cycler) { + _fade_mode = copy._fade_mode; + _previous_child = copy._previous_child; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::add_switch +// Access: Published +// Description: Adds a switch range to the FadeLODNode. This implies +// that the corresponding child node has been parented +// to the node. +// +// The sense of in vs. out distances is as if the object +// were coming towards you from far away: it switches +// "in" at the far distance, and switches "out" at the +// close distance. Thus, "in" should be larger than +// "out". +//////////////////////////////////////////////////////////////////// +INLINE void FadeLODNode:: +add_switch(float in, float out) { + CDWriter cdata(_cycler); + cdata->_lod._switch_vector.push_back(LODSwitch(in, out)); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::set_switch +// Access: Published +// Description: Changes the switching range of a particular child of +// the LODNode. See add_switch(). +//////////////////////////////////////////////////////////////////// +INLINE bool FadeLODNode:: +set_switch(int index, float in, float out) { + CDWriter cdata(_cycler); + nassertr(index >= 0 && index < (int)cdata->_lod._switch_vector.size(), false); + cdata->_lod._switch_vector[index].set_range(in, out); + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::clear_switches +// Access: Published +// Description: Removes the set of switching ranges for the LODNode, +// presumably in conjunction with removing all of its +// children. See add_switch(). +//////////////////////////////////////////////////////////////////// +INLINE void FadeLODNode:: +clear_switches(void) { + CDWriter cdata(_cycler); + cdata->_lod._switch_vector.erase(cdata->_lod._switch_vector.begin(), + cdata->_lod._switch_vector.end()); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::get_num_switches +// Access: Published +// Description: Returns the number of switch ranges added to the +// LODNode. This should correspond to the number of +// children of the node in order for the FadeLODNode to +// function correctly. +//////////////////////////////////////////////////////////////////// +INLINE int FadeLODNode:: +get_num_switches() const { + CDReader cdata(_cycler); + return cdata->_lod._switch_vector.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::get_in +// Access: Published +// Description: Returns the "in" distance of the indicated switch +// range. This should be larger than the "out" distance +// of the same range. +//////////////////////////////////////////////////////////////////// +INLINE float FadeLODNode:: +get_in(int index) const { + CDReader cdata(_cycler); + nassertr(index >= 0 && index < (int)cdata->_lod._switch_vector.size(), 0.0); + return cdata->_lod._switch_vector[index].get_in(); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::get_out +// Access: Published +// Description: Returns the "out" distance of the indicated switch +// range. This should be smaller than the "in" distance +// of the same range. +//////////////////////////////////////////////////////////////////// +INLINE float FadeLODNode:: +get_out(int index) const { + CDReader cdata(_cycler); + nassertr(index >= 0 && index < (int)cdata->_lod._switch_vector.size(), 0.0); + return cdata->_lod._switch_vector[index].get_out(); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::set_center +// Access: Published +// Description: Specifies the center of the LOD. This is the point +// that is compared to the camera (in camera space) to +// determine the particular LOD that should be chosen. +//////////////////////////////////////////////////////////////////// +INLINE void FadeLODNode:: +set_center(const LPoint3f ¢er) { + CDWriter cdata(_cycler); + cdata->_lod._center = center; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::get_center +// Access: Published +// Description: Returns the center of the LOD. This is the point +// that is compared to the camera (in camera space) to +// determine the particular LOD that should be chosen. +//////////////////////////////////////////////////////////////////// +INLINE const LPoint3f &FadeLODNode:: +get_center() const { + CDReader cdata(_cycler); + return cdata->_lod._center; +} + + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::set_fade_time +// Access: Published +// Description: set the time taken to complete an LOD switch +//////////////////////////////////////////////////////////////////// +INLINE FadeLODNode:: +set_fade_time(float t) { + CDWriter cdata(_cycler); + cdata->_fade_time = t; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::get_fade_time +// Access: Published +// Description: get the time taken to complete an LOD switch +//////////////////////////////////////////////////////////////////// +INLINE float FadeLODNode:: +get_fade_time() const { + CDReader cdata(_cycler); + return cdata->_fade_time; +} + diff --git a/panda/src/pgraph/fadeLodNode.cxx b/panda/src/pgraph/fadeLodNode.cxx new file mode 100755 index 0000000000..aa6b2d4c16 --- /dev/null +++ b/panda/src/pgraph/fadeLodNode.cxx @@ -0,0 +1,306 @@ +// Filename: fadeLodNode.cxx +// Created by: sshodhan (14Jun04) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#include "fadeLodNode.h" +#include "cullTraverserData.h" +#include "cullTraverser.h" +#include "clockObject.h" +#include "colorScaleAttrib.h" +#include "depthWriteAttrib.h" +#include "transparencyAttrib.h" + +TypeHandle FadeLODNode::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::CData::make_copy +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +CycleData *FadeLODNode::CData:: +make_copy() const { + return new CData(*this); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::CData::write_datagram +// Access: Public, Virtual +// Description: Writes the contents of this object to the datagram +// for shipping out to a Bam file. +//////////////////////////////////////////////////////////////////// +void FadeLODNode::CData:: +write_datagram(BamWriter *manager, Datagram &dg) const { + _lod.write_datagram(dg); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::CData::fillin +// Access: Public, Virtual +// Description: This internal function is called by make_from_bam to +// read in all of the relevant data from the BamFile for +// the new LODNode. +//////////////////////////////////////////////////////////////////// +void FadeLODNode::CData:: +fillin(DatagramIterator &scan, BamReader *manager) { + _lod.read_datagram(scan); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::make_copy +// Access: Public, Virtual +// Description: Returns a newly-allocated Node that is a shallow copy +// of this one. It will be a different Node pointer, +// but its internal data may or may not be shared with +// that of the original Node. +//////////////////////////////////////////////////////////////////// +PandaNode *FadeLODNode:: +make_copy() const { + return new FadeLODNode(*this); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::safe_to_combine +// Access: Public, Virtual +// Description: Returns true if it is generally safe to combine this +// particular kind of PandaNode with other kinds of +// PandaNodes, adding children or whatever. For +// instance, an LODNode should not be combined with any +// other PandaNode, because its set of children is +// meaningful. +//////////////////////////////////////////////////////////////////// +bool FadeLODNode:: +safe_to_combine() const { + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::xform +// Access: Public, Virtual +// Description: Transforms the contents of this PandaNode by the +// indicated matrix, if it means anything to do so. For +// most kinds of PandaNodes, this does nothing. +//////////////////////////////////////////////////////////////////// +void FadeLODNode:: +xform(const LMatrix4f &mat) { + CDWriter cdata(_cycler); + cdata->_lod.xform(mat); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::has_cull_callback +// Access: Public, Virtual +// Description: Should be overridden by derived classes to return +// true if cull_callback() has been defined. Otherwise, +// returns false to indicate cull_callback() does not +// need to be called for this node during the cull +// traversal. +//////////////////////////////////////////////////////////////////// +bool FadeLODNode:: +has_cull_callback() const { + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::cull_callback +// Access: Public, Virtual +// Description: If has_cull_callback() returns true, this function +// will be called during the cull traversal to perform +// any additional operations that should be performed at +// cull time. This may include additional manipulation +// of render state or additional visible/invisible +// decisions, or any other arbitrary operation. +// +// By the time this function is called, the node has +// already passed the bounding-volume test for the +// viewing frustum, and the node's transform and state +// have already been applied to the indicated +// CullTraverserData object. +// +// The return value is true if this node should be +// visible, or false if it should be culled. +//////////////////////////////////////////////////////////////////// +bool FadeLODNode:: +cull_callback(CullTraverser *trav, CullTraverserData &data) { + PandaNode *node = data.node(); + CDReader cdata(_cycler); + if(_fade_mode) { + float in_alpha; + float out_alpha; + _fade_timer -= ClockObject::get_global_clock()->get_dt(); + if(_fade_timer <= (cdata->_fade_time / 2.0)) { + //SECOND HALF OF FADE: + //Fade out the old LOD with z write off and + //draw the opaque new LOD with z write on + out_alpha = (_fade_timer*2.0) / cdata->_fade_time; + if(out_alpha < 0.0) { + out_alpha = 0.0; + } + + CullTraverserData next_data_in(data, node->get_child(_fade_in)); + CullTraverserData next_data_out(data, node->get_child(_fade_out)); + + // Disable Transparency on new LOD + next_data_in._state = next_data_in._state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_none), 0); + // Enable Transparency on old LOD + next_data_out._state = next_data_out._state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha), 0); + // Start Fading the old LOD out + next_data_out._state = next_data_out._state->add_attrib(ColorScaleAttrib::make(LVecBase4f(1.0,1.0,1.0,out_alpha))); + // The new LOD is now opaque and has depth writing + next_data_in._state = next_data_in._state->add_attrib(DepthWriteAttrib::make(DepthWriteAttrib::M_on), 0); + // The old LOD is fading so it doesnt depth write + next_data_out._state = next_data_out._state->add_attrib(DepthWriteAttrib::make(DepthWriteAttrib::M_off), 0); + + + trav->traverse(next_data_in); + trav->traverse(next_data_out); + } + else { + // FIRST HALF OF FADE + // Fade the new LOD in with z writing off + // Keep drawing the old LOD opaque with z writing on + in_alpha = (1.0 - (_fade_timer / cdata->_fade_time))*2.0; + if(in_alpha > 1.0) { + in_alpha = 1.0; + } + + CullTraverserData next_data_out(data, node->get_child(_fade_out)); + CullTraverserData next_data_in(data, node->get_child(_fade_in)); + + // Disable transparency on old LOD + next_data_out._state = next_data_out._state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_none), 0); + // Enable transparency on new LOD + next_data_in._state = next_data_in._state->add_attrib(TransparencyAttrib::make(TransparencyAttrib::M_alpha), 0); + // Start Fading in the new LOD + next_data_in._state = next_data_in._state->add_attrib(ColorScaleAttrib::make(LVecBase4f(1.0,1.0,1.0,in_alpha))); + // Enable depth write for the old LOD + next_data_out._state = next_data_out._state->add_attrib(DepthWriteAttrib::make(DepthWriteAttrib::M_on), 0); + // Disable depth write for the new LOD + next_data_in._state = next_data_in._state->add_attrib(DepthWriteAttrib::make(DepthWriteAttrib::M_off), 0); + + trav->traverse(next_data_out); + trav->traverse(next_data_in); + } + if(_fade_timer < 0) { // Fading Complete + _fade_mode = false; + } + } + else { + if (data._net_transform->is_singular()) { + // If we're under a singular transform, we can't compute the LOD; + // select none of them instead. + //select_child(get_num_children()); + return false; + } + else { + LPoint3f camera_pos(0, 0, 0); + // Get the LOD center in camera space + CPT(TransformState) rel_transform = + trav->get_camera_transform()->invert_compose(data._net_transform); + LPoint3f center = cdata->_lod._center * rel_transform->get_mat(); + // Determine which child to traverse + int index = cdata->_lod.compute_child(camera_pos, center); + //printf("CHILD: %d PREVIOUS %d \n",index,_previous_child); + if(index != _previous_child) { // Transition occurred + _fade_mode = true; + _fade_timer = cdata->_fade_time; + _fade_out = _previous_child; + _fade_in = index; + _previous_child = index; + CullTraverserData next_data_transition(data, node->get_child(_fade_out)); + trav->traverse(next_data_transition); + } + else { + // No transition... handle things as usual + // Traverse only one valid child + CullTraverserData next_data_normal(data, node->get_child(index)); + trav->traverse(next_data_normal); + } + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::output +// Access: Public, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +void FadeLODNode:: +output(ostream &out) const { + PandaNode::output(out); + CDReader cdata(_cycler); + out << " "; + cdata->_lod.output(out); + out<< "Fade Time : " << cdata->_fade_time << endl; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::register_with_read_factory +// Access: Public, Static +// Description: Tells the BamReader how to create objects of type +// LODNode. +//////////////////////////////////////////////////////////////////// +void FadeLODNode:: +register_with_read_factory() { + BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::write_datagram +// Access: Public, Virtual +// Description: Writes the contents of this object to the datagram +// for shipping out to a Bam file. +//////////////////////////////////////////////////////////////////// +void FadeLODNode:: +write_datagram(BamWriter *manager, Datagram &dg) { + PandaNode::write_datagram(manager, dg); + manager->write_cdata(dg, _cycler); +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::make_from_bam +// Access: Protected, Static +// Description: This function is called by the BamReader's factory +// when a new object of type LODNode is encountered +// in the Bam file. It should create the LODNode +// and extract its information from the file. +//////////////////////////////////////////////////////////////////// +TypedWritable *FadeLODNode:: +make_from_bam(const FactoryParams ¶ms) { + FadeLODNode *node = new FadeLODNode(""); + DatagramIterator scan; + BamReader *manager; + + parse_params(params, scan, manager); + node->fillin(scan, manager); + + return node; +} + +//////////////////////////////////////////////////////////////////// +// Function: FadeLODNode::fillin +// Access: Protected +// Description: This internal function is called by make_from_bam to +// read in all of the relevant data from the BamFile for +// the new FadeLODNode. +//////////////////////////////////////////////////////////////////// +void FadeLODNode:: +fillin(DatagramIterator &scan, BamReader *manager) { + PandaNode::fillin(scan, manager); + manager->read_cdata(scan, _cycler); +} diff --git a/panda/src/pgraph/fadeLodNode.h b/panda/src/pgraph/fadeLodNode.h new file mode 100755 index 0000000000..646b06792c --- /dev/null +++ b/panda/src/pgraph/fadeLodNode.h @@ -0,0 +1,119 @@ +// Filename: fadeLodNode.h +// Created by: sshodhan (14Jun04) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef FADELODNODE_H +#define FADELODNODE_H + +#include "pandabase.h" + +#include "pandaNode.h" + +#include "LOD.h" + +//////////////////////////////////////////////////////////////////// +// Class : FadeLODNode +// Description : A Level-of-Detail node with alpha based switching. +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDA FadeLODNode : public PandaNode { +PUBLISHED: + INLINE FadeLODNode(const string &name); + + +protected: + INLINE FadeLODNode(const FadeLODNode ©); +public: + virtual PandaNode *make_copy() const; + virtual bool safe_to_combine() const; + virtual void xform(const LMatrix4f &mat); + virtual bool has_cull_callback() const; + virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data); + virtual void output(ostream &out) const; + +PUBLISHED: + // The sense of in vs. out distances is as if the object were coming + // towards you from far away: it switches "in" at the far distance, + // and switches "out" at the close distance. Thus, "in" should be + // larger than "out". + + INLINE void add_switch(float in, float out); + INLINE bool set_switch(int index, float in, float out); + INLINE void clear_switches(void); + + INLINE int get_num_switches() const; + INLINE float get_in(int index) const; + INLINE float get_out(int index) const; + + INLINE void set_center(const LPoint3f ¢er); + INLINE const LPoint3f &get_center() const; + + INLINE set_fade_time(float t); + INLINE float get_fade_time() const; + +public: + static void register_with_read_factory(); + virtual void write_datagram(BamWriter *manager, Datagram &dg); + +protected: + static TypedWritable *make_from_bam(const FactoryParams ¶ms); + void fillin(DatagramIterator &scan, BamReader *manager); + +private: + class EXPCL_PANDA CData : public CycleData { + public: + INLINE CData(); + INLINE CData(const CData ©); + virtual CycleData *make_copy() const; + virtual void write_datagram(BamWriter *manager, Datagram &dg) const; + virtual void fillin(DatagramIterator &scan, BamReader *manager); + + LOD _lod; + float _fade_time; + }; + + PipelineCycler _cycler; + typedef CycleDataReader CDReader; + typedef CycleDataWriter CDWriter; + bool _fade_mode; + float _fade_timer; + int _fade_out; + int _fade_in; + int _previous_child; + + + +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + PandaNode::init_type(); + register_type(_type_handle, "FadeLODNode", + PandaNode::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() {init_type(); return get_class_type();} + +private: + static TypeHandle _type_handle; +}; + +#include "fadeLodNode.I" + +#endif diff --git a/panda/src/pgraph/pgraph_composite1.cxx b/panda/src/pgraph/pgraph_composite1.cxx index bdd20fccfd..56b7b2c33b 100644 --- a/panda/src/pgraph/pgraph_composite1.cxx +++ b/panda/src/pgraph/pgraph_composite1.cxx @@ -31,6 +31,7 @@ #include "alphaTestAttrib.cxx" #include "directionalLight.cxx" #include "drawCullHandler.cxx" +#include "fadeLodNode.cxx" #include "findApproxPath.cxx" #include "findApproxLevelEntry.cxx" #include "fog.cxx" diff --git a/panda/src/pgraph/polylightEffect.I b/panda/src/pgraph/polylightEffect.I index 0cac962fed..61af28b344 100755 --- a/panda/src/pgraph/polylightEffect.I +++ b/panda/src/pgraph/polylightEffect.I @@ -126,13 +126,12 @@ remove_all() { //////////////////////////////////////////////////////////////////// // Function: PolylightEffect::set_weight // Access: Published -// Description: Set a weight between 0 and 1 for the light effect -// This affects how much original color and how much -// light color are applied to the node +// Description: weight is a constant you add (generally 1 and above) +// to make the colorscale brighten the existing color //////////////////////////////////////////////////////////////////// INLINE bool PolylightEffect:: set_weight(float w) { - nassertr(w >= 0.0 && w <= 1.0 ,false); + // nassertr(w >= 0.0 && w <= 1.0 ,false); _weight = w; return true; } @@ -156,7 +155,7 @@ get_weight() const { // _contribution_type is a string that controls how // this division occurs. // "proximal" : A light only contributes if the node -// is inside its volume +// is inside its volume // "all" : All lights added to the effect are used in // division irrespective of their light volumes //////////////////////////////////////////////////////////////////// diff --git a/panda/src/pgraph/polylightEffect.cxx b/panda/src/pgraph/polylightEffect.cxx index 99382cdbb8..098c5ab17d 100755 --- a/panda/src/pgraph/polylightEffect.cxx +++ b/panda/src/pgraph/polylightEffect.cxx @@ -42,95 +42,103 @@ make() { //////////////////////////////////////////////////////////////////// // Function: PolylightEffect::do_poly_light // Access: Public -// Description: Gets the node's position and based on distance from +// Description: Gets the node's position and based on distance from // lights in the lightgroup calculates the color to be modulated in //////////////////////////////////////////////////////////////////// CPT(RenderAttrib) PolylightEffect:: do_poly_light(const CullTraverserData *data, const TransformState *node_transform) const { + bool no_lights_closeby = false; float r,g,b; // To hold the color calculation float dist; // To calculate the distance of each light from the node - float light_scale = 1.0; // Variable to calculate attenuation + float light_scale = 1.0; // Variable to calculate attenuation float fd; // Variable for quadratic attenuation float Rcollect = 0.0,Gcollect = 0.0,Bcollect = 0.0; // Color variables int num_lights = 0; // Keep track of number of lights for division - r = 0.0; - g = 0.0; - b = 0.0; - LIGHTGROUP::const_iterator light_iter; - // Cycle through all the lights in this effect's lightgroup - for (light_iter = _lightgroup.begin(); light_iter != _lightgroup.end(); light_iter++){ - const PolylightNode *light = DCAST(PolylightNode,light_iter->second.node()); - // light holds the current PolylightNode - if(light->is_enabled()) { // if enabled get all the properties - float light_radius = light->get_radius(); - PolylightNode::Attenuation_Type light_attenuation = light->get_attenuation(); - float light_a0 = light->get_a0(); - float light_a1 = light->get_a1(); - float light_a2 = light->get_a2(); - if(light_a0 == 0 && light_a1 == 0 && light_a2 == 0) { // To prevent division by zero - light_a0 = 1.0; - } - Colorf light_color; - if(light->is_flickering()) { // If flickering, modify color - light_color = light->flicker(); - } - else { - light_color = light->get_color(); - } + r = 1.0; + g = 1.0; + b = 1.0; + if(is_enabled()) { + LIGHTGROUP::const_iterator light_iter; + // Cycle through all the lights in this effect's lightgroup + for (light_iter = _lightgroup.begin(); light_iter != _lightgroup.end(); light_iter++){ + const PolylightNode *light = DCAST(PolylightNode,light_iter->second.node()); + // light holds the current PolylightNode + if(light->is_enabled()) { // if enabled get all the properties + float light_radius = light->get_radius(); + PolylightNode::Attenuation_Type light_attenuation = light->get_attenuation(); + float light_a0 = light->get_a0(); + float light_a1 = light->get_a1(); + float light_a2 = light->get_a2(); + if(light_a0 == 0 && light_a1 == 0 && light_a2 == 0) { // To prevent division by zero + light_a0 = 1.0; + } + Colorf light_color; + if(light->is_flickering()) { // If flickering, modify color + light_color = light->flicker(); + } + else { + light_color = light->get_color(); + } + + // Calculate the distance of the node from the light + //dist = light_iter->second->get_distance(data->_node_path.get_node_path()); + const NodePath lightnp = light_iter->second; + LPoint3f point = data->_node_path.get_node_path().get_relative_point(lightnp, + light->get_pos()); + dist = (point - _effect_center).length(); - // Calculate the distance of the node from the light - //dist = light_iter->second->get_distance(data->_node_path.get_node_path()); - const NodePath lightnp = light_iter->second; - LPoint3f point = data->_node_path.get_node_path().get_relative_point(lightnp, - light->get_pos()); - dist = (point - _effect_center).length(); - - if(dist < light_radius) { // If node is in range of this light - if(light_attenuation == PolylightNode::ALINEAR) { - light_scale = (light_radius - dist)/light_radius; - } - else if(light_attenuation == PolylightNode::AQUADRATIC) { - fd = 1.0 / (light_a0 + light_a1 * dist + light_a2 * dist * dist); - if(fd < 1.0) { - light_scale = fd; - } + if(dist < light_radius) { // If node is in range of this light + if(light_attenuation == PolylightNode::ALINEAR) { + light_scale = (light_radius - dist)/light_radius; + } + else if(light_attenuation == PolylightNode::AQUADRATIC) { + fd = 1.0 / (light_a0 + light_a1 * dist + light_a2 * dist * dist); + if(fd < 1.0) { + light_scale = fd; + } + else { + light_scale = 1.0; + } + } else { light_scale = 1.0; } - } - else { - light_scale = 1.0; - } - // Keep accumulating each lights contribution... we divide by - // number of lights later. - Rcollect += light_color[0] * light_scale; - Gcollect += light_color[1] * light_scale; - Bcollect += light_color[2] * light_scale; - num_lights++; - } // if dist< radius - } // if light is enabled - } // for all lights + // Keep accumulating each lights contribution... we divide by + // number of lights later. + Rcollect += light_color[0] * light_scale; + Gcollect += light_color[1] * light_scale; + Bcollect += light_color[2] * light_scale; + num_lights++; + } // if dist< radius + } // if light is enabled + } // for all lights + + if( _contribution_type == CALL) { + // Sometimes to prevent snapping of color at light volume boundaries + // just divide total contribution by all the lights in the effect + // whether or not they contribute color + num_lights = _lightgroup.size(); + } - if( _contribution_type == CALL) { - // Sometimes to prevent snapping of color at light volume boundaries - // just divide total contribution by all the lights in the effect - // whether or not they contribute color - num_lights = _lightgroup.size(); + if(num_lights == 0) { + no_lights_closeby = true; + num_lights = 1; + } + Rcollect /= num_lights; + Gcollect /= num_lights; + Bcollect /= num_lights; + + if(!no_lights_closeby) { + //r = 1.0 + ((1.0 - _weight) + Rcollect * _weight); + //g = 1.0 + ((1.0 - _weight) + Gcollect * _weight); + //b = 1.0 + ((1.0 - _weight) + Bcollect * _weight); + r = _weight + Rcollect; + g = _weight + Gcollect; + b = _weight + Bcollect; + } } - - if(num_lights == 0) { - num_lights = 1; - } - Rcollect /= num_lights; - Gcollect /= num_lights; - Bcollect /= num_lights; - - r = (1.0 - _weight) + Rcollect * _weight; - g = (1.0 - _weight) + Gcollect * _weight; - b = (1.0 - _weight) + Bcollect * _weight; - - return ColorScaleAttrib::make(LVecBase4f(r,g,b,1.0)); + return ColorScaleAttrib::make(LVecBase4f(r, g, b, 1.0)); } @@ -155,21 +163,21 @@ compare_to_impl(const RenderEffect *other) const { DCAST_INTO_R(ta, other, 0); if (_enabled != ta->_enabled) { - return _enabled ? 1 : -1; + return _enabled ? 1 : -1; } if (_contribution_type != ta->_contribution_type) { return _contribution_type < ta->_contribution_type ? -1 : 1; } - + if (_weight != ta->_weight) { - return _weight < ta->_weight ? -1 :1; + return _weight < ta->_weight ? -1 :1; } if (_lightgroup != ta->_lightgroup) { return _lightgroup < ta->_lightgroup ? -1 : 1; } - + return 0; } diff --git a/panda/src/pgraph/polylightEffect.h b/panda/src/pgraph/polylightEffect.h index 2d013b7031..4d9efd2a7f 100755 --- a/panda/src/pgraph/polylightEffect.h +++ b/panda/src/pgraph/polylightEffect.h @@ -33,16 +33,16 @@ //////////////////////////////////////////////////////////////////// // Class : PolylightEffect // Description : A PolylightEffect can be used on a node to define a -// LightGroup for that node. A LightGroup contains -// Polylights which are essentially nodes that add -// color to the polygons of a model based on distance. -// PolylightNode is a cheap way to get lighting effects +// LightGroup for that node. A LightGroup contains +// Polylights which are essentially nodes that add +// color to the polygons of a model based on distance. +// PolylightNode is a cheap way to get lighting effects // specially for night scenes //////////////////////////////////////////////////////////////////// class EXPCL_PANDA PolylightEffect : public RenderEffect { private: INLINE PolylightEffect(); - + PUBLISHED: enum Contrib_Type { @@ -58,7 +58,7 @@ PUBLISHED: INLINE bool remove_all(); INLINE bool set_weight(float w); INLINE float get_weight() const; - INLINE bool set_contrib(Contrib_Type type); + INLINE bool set_contrib(Contrib_Type type); INLINE Contrib_Type get_contrib() const; INLINE bool is_enabled()const; INLINE void set_effect_center(LPoint3f effect_center); @@ -77,7 +77,7 @@ private: typedef pmap LIGHTGROUP; LIGHTGROUP _lightgroup; LPoint3f _effect_center; - + public: static TypeHandle get_class_type() { diff --git a/panda/src/pgraph/polylightNode.cxx b/panda/src/pgraph/polylightNode.cxx index 6490bfa08d..5dcaf425ab 100755 --- a/panda/src/pgraph/polylightNode.cxx +++ b/panda/src/pgraph/polylightNode.cxx @@ -75,37 +75,37 @@ Colorf PolylightNode::flicker() const { g = color[1]; b = color[2]; float variation= 0.0; - + if(_flicker_type == FRANDOM) { //srand((int)ClockObject::get_global_clock()->get_frame_time()); variation = (rand()%100);// * ClockObject::get_global_clock()->get_dt(); - variation /= 100.0; - //printf("Random Variation: %f\n",variation); - variation += _offset; - variation *= _scale; + variation /= 100.0; + //printf("Random Variation: %f\n",variation); + variation += _offset; + variation *= _scale; } else if(_flicker_type == FSIN) { - double now = ClockObject::get_global_clock()->get_frame_time(); + double now = ClockObject::get_global_clock()->get_frame_time(); variation = sinf(now*_sin_freq);// * ClockObject::get_global_clock()->get_dt(); - //printf("Variation: %f\n",variation); - variation += _offset; - variation *= _scale; + //printf("Variation: %f\n",variation); + variation += _offset; + variation *= _scale; } else if(_flicker_type == FCUSTOM) { // fixed point list of variation values coming soon... //double index = (ClockObject::get_global_clock()->get_frame_time() % len(fixed_points)) * ClockObject::get_global_clock()->get_dt(); - //index *= _speed; - /*if(!(int)index > len(fixed_points) { - variation = _fixed_points[(int)index]; - variation += _offset; - variation *= _scale; - }*/ + //index *= _speed; + /*if(!(int)index > len(fixed_points) { + variation = _fixed_points[(int)index]; + variation += _offset; + variation *= _scale; + }*/ } //printf("Variation: %f\n",variation); r+=variation; g+=variation; b+=variation; - + /* CLAMPING if(fabs(r - color[0]) > 0.5 || fabs(g - color[1]) > 0.5 || fabs(b - color[2]) > 0.5) { r = color[0]; @@ -126,7 +126,7 @@ Colorf PolylightNode::flicker() const { // // Two PolylightNodes are considered equivalent if they // consist of exactly the same properties -// Otherwise, they are different; different +// Otherwise, they are different; different // PolylightNodes will be ranked in a consistent but // undefined ordering; the ordering is useful only for // placing the PolylightNodes in a sorted container like an @@ -134,64 +134,64 @@ Colorf PolylightNode::flicker() const { //////////////////////////////////////////////////////////////////// int PolylightNode:: compare_to(const PolylightNode &other) const { - + if (_enabled != other._enabled) { - return _enabled ? 1 :-1; + return _enabled ? 1 :-1; } if (_radius != other._radius) { - return _radius < other._radius ? -1 :1; + return _radius < other._radius ? -1 :1; } LVecBase3f position = get_pos(); LVecBase3f other_position = other.get_pos(); if (position != other_position) { - return position < other_position ? -1 :1; + return position < other_position ? -1 :1; } Colorf color = get_color(); Colorf other_color = other.get_color(); if (color != other_color) { - return color < other_color ? -1 :1; + return color < other_color ? -1 :1; } if (_attenuation_type != other._attenuation_type) { - return _attenuation_type < other._attenuation_type ? -1 :1; + return _attenuation_type < other._attenuation_type ? -1 :1; } if (_a0 != other._a0) { - return _a0 < other._a0 ? -1 :1; + return _a0 < other._a0 ? -1 :1; } if (_a1 != other._a1) { - return _a1 < other._a1 ? -1 :1; + return _a1 < other._a1 ? -1 :1; } if (_a2 != other._a2) { - return _a2 < other._a2 ? -1 :1; + return _a2 < other._a2 ? -1 :1; } if (_flickering != other._flickering) { - return _flickering ? 1 :-1; + return _flickering ? 1 :-1; } if (_flicker_type != other._flicker_type) { - return _flicker_type < other._flicker_type ? -1 :1; + return _flicker_type < other._flicker_type ? -1 :1; } if (_offset != other._offset) { - return _offset < other._offset ? -1 :1; + return _offset < other._offset ? -1 :1; } if (_scale != other._scale) { - return _scale < other._scale ? -1 :1; + return _scale < other._scale ? -1 :1; } if (_step_size != other._step_size) { - return _step_size < other._step_size ? -1 :1; + return _step_size < other._step_size ? -1 :1; } if (_sin_freq != other._sin_freq) { - return _sin_freq < other._sin_freq ? -1 :1; + return _sin_freq < other._sin_freq ? -1 :1; } @@ -282,7 +282,7 @@ fillin(DatagramIterator &scan, BamReader *manager) { //////////////////////////////////////////////////////////////////// // Function: PolylightNode::output // Access: Public, Virtual -// Description: +// Description: //////////////////////////////////////////////////////////////////// void PolylightNode:: output(ostream &out) const { diff --git a/panda/src/pgraph/polylightNode.h b/panda/src/pgraph/polylightNode.h index 5efc15b7e8..edbcd7e85a 100755 --- a/panda/src/pgraph/polylightNode.h +++ b/panda/src/pgraph/polylightNode.h @@ -38,14 +38,14 @@ class EXPCL_PANDA PolylightNode : public PandaNode{ PUBLISHED: /* - // This was the old constructor... interrogate would generate a - // separate wrapper for each parameter... so its better to + // This was the old constructor... interrogate would generate a + // separate wrapper for each parameter... so its better to // have a simpler constructor and require the programmer // to use set_* methods. PolylightNode(const string &name, float x = 0.0, float y = 0.0, float z = 0.0, - float r = 1.0, float g = 1.0, float b = 1.0, - float radius=50.0, string attenuation_type= "linear", - bool flickering =false, string flicker_type="random"); + float r = 1.0, float g = 1.0, float b = 1.0, + float radius=50.0, string attenuation_type= "linear", + bool flickering =false, string flicker_type="random"); */ enum Flicker_Type { @@ -103,7 +103,7 @@ PUBLISHED: public: Colorf flicker() const; - + private: bool _enabled; LVecBase3f _position; @@ -121,7 +121,7 @@ private: float _sin_freq; //float _speed; //float fixed_points - + public: static void register_with_read_factory();