FadeLOD and Polylight improved

This commit is contained in:
Shalin Shodhan 2004-06-18 21:31:30 +00:00
parent 6d457d093c
commit 4a938d6085
11 changed files with 769 additions and 129 deletions

View File

@ -41,6 +41,7 @@
depthWriteAttrib.I depthWriteAttrib.h \ depthWriteAttrib.I depthWriteAttrib.h \
directionalLight.I directionalLight.h \ directionalLight.I directionalLight.h \
drawCullHandler.I drawCullHandler.h \ drawCullHandler.I drawCullHandler.h \
fadeLodNode.I fadeLodNode.h \
findApproxLevelEntry.I findApproxLevelEntry.h \ findApproxLevelEntry.I findApproxLevelEntry.h \
findApproxPath.I findApproxPath.h \ findApproxPath.I findApproxPath.h \
fog.I fog.h \ fog.I fog.h \
@ -128,6 +129,7 @@
depthWriteAttrib.cxx \ depthWriteAttrib.cxx \
directionalLight.cxx \ directionalLight.cxx \
drawCullHandler.cxx \ drawCullHandler.cxx \
fadeLodNode.cxx \
findApproxLevelEntry.cxx \ findApproxLevelEntry.cxx \
findApproxPath.cxx \ findApproxPath.cxx \
fog.cxx \ fog.cxx \
@ -214,6 +216,7 @@
depthWriteAttrib.I depthWriteAttrib.h \ depthWriteAttrib.I depthWriteAttrib.h \
directionalLight.I directionalLight.h \ directionalLight.I directionalLight.h \
drawCullHandler.I drawCullHandler.h \ drawCullHandler.I drawCullHandler.h \
fadeLodNode.I fadeLodNode.h \
fog.I fog.h \ fog.I fog.h \
fogAttrib.I fogAttrib.h \ fogAttrib.I fogAttrib.h \
geomNode.I geomNode.h \ geomNode.I geomNode.h \

View File

@ -42,6 +42,7 @@
#include "depthTestAttrib.h" #include "depthTestAttrib.h"
#include "depthWriteAttrib.h" #include "depthWriteAttrib.h"
#include "directionalLight.h" #include "directionalLight.h"
#include "fadeLodNode.h"
#include "fog.h" #include "fog.h"
#include "fogAttrib.h" #include "fogAttrib.h"
#include "geomNode.h" #include "geomNode.h"
@ -192,6 +193,7 @@ init_libpgraph() {
DepthTestAttrib::init_type(); DepthTestAttrib::init_type();
DepthWriteAttrib::init_type(); DepthWriteAttrib::init_type();
DirectionalLight::init_type(); DirectionalLight::init_type();
FadeLODNode::init_type();
Fog::init_type(); Fog::init_type();
FogAttrib::init_type(); FogAttrib::init_type();
GeomNode::init_type(); GeomNode::init_type();

202
panda/src/pgraph/fadeLodNode.I Executable file
View File

@ -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 &copy) :
_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 &copy) :
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 &center) {
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;
}

306
panda/src/pgraph/fadeLodNode.cxx Executable file
View File

@ -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 &params) {
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);
}

119
panda/src/pgraph/fadeLodNode.h Executable file
View File

@ -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 &copy);
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 &center);
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 &params);
void fillin(DatagramIterator &scan, BamReader *manager);
private:
class EXPCL_PANDA CData : public CycleData {
public:
INLINE CData();
INLINE CData(const CData &copy);
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<CData> _cycler;
typedef CycleDataReader<CData> CDReader;
typedef CycleDataWriter<CData> 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

View File

@ -31,6 +31,7 @@
#include "alphaTestAttrib.cxx" #include "alphaTestAttrib.cxx"
#include "directionalLight.cxx" #include "directionalLight.cxx"
#include "drawCullHandler.cxx" #include "drawCullHandler.cxx"
#include "fadeLodNode.cxx"
#include "findApproxPath.cxx" #include "findApproxPath.cxx"
#include "findApproxLevelEntry.cxx" #include "findApproxLevelEntry.cxx"
#include "fog.cxx" #include "fog.cxx"

View File

@ -126,13 +126,12 @@ remove_all() {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: PolylightEffect::set_weight // Function: PolylightEffect::set_weight
// Access: Published // Access: Published
// Description: Set a weight between 0 and 1 for the light effect // Description: weight is a constant you add (generally 1 and above)
// This affects how much original color and how much // to make the colorscale brighten the existing color
// light color are applied to the node
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
INLINE bool PolylightEffect:: INLINE bool PolylightEffect::
set_weight(float w) { set_weight(float w) {
nassertr(w >= 0.0 && w <= 1.0 ,false); // nassertr(w >= 0.0 && w <= 1.0 ,false);
_weight = w; _weight = w;
return true; return true;
} }

View File

@ -47,15 +47,17 @@ make() {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
CPT(RenderAttrib) PolylightEffect:: CPT(RenderAttrib) PolylightEffect::
do_poly_light(const CullTraverserData *data, const TransformState *node_transform) const { 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 r,g,b; // To hold the color calculation
float dist; // To calculate the distance of each light from the node 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 fd; // Variable for quadratic attenuation
float Rcollect = 0.0,Gcollect = 0.0,Bcollect = 0.0; // Color variables float Rcollect = 0.0,Gcollect = 0.0,Bcollect = 0.0; // Color variables
int num_lights = 0; // Keep track of number of lights for division int num_lights = 0; // Keep track of number of lights for division
r = 0.0; r = 1.0;
g = 0.0; g = 1.0;
b = 0.0; b = 1.0;
if(is_enabled()) {
LIGHTGROUP::const_iterator light_iter; LIGHTGROUP::const_iterator light_iter;
// Cycle through all the lights in this effect's lightgroup // Cycle through all the lights in this effect's lightgroup
for (light_iter = _lightgroup.begin(); light_iter != _lightgroup.end(); light_iter++){ for (light_iter = _lightgroup.begin(); light_iter != _lightgroup.end(); light_iter++){
@ -120,16 +122,22 @@ do_poly_light(const CullTraverserData *data, const TransformState *node_transfor
} }
if(num_lights == 0) { if(num_lights == 0) {
no_lights_closeby = true;
num_lights = 1; num_lights = 1;
} }
Rcollect /= num_lights; Rcollect /= num_lights;
Gcollect /= num_lights; Gcollect /= num_lights;
Bcollect /= num_lights; Bcollect /= num_lights;
r = (1.0 - _weight) + Rcollect * _weight; if(!no_lights_closeby) {
g = (1.0 - _weight) + Gcollect * _weight; //r = 1.0 + ((1.0 - _weight) + Rcollect * _weight);
b = (1.0 - _weight) + Bcollect * _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;
}
}
return ColorScaleAttrib::make(LVecBase4f(r, g, b, 1.0)); return ColorScaleAttrib::make(LVecBase4f(r, g, b, 1.0));
} }