From 2b7ef9c7873843cdaafaa574de1e6192dddc62e0 Mon Sep 17 00:00:00 2001 From: rdb Date: Sat, 16 Apr 2016 12:30:07 +0200 Subject: [PATCH] Add SphereLight class --- panda/src/pgraphnodes/config_pgraphnodes.cxx | 3 + .../pgraphnodes/p3pgraphnodes_composite1.cxx | 2 + .../pgraphnodes/p3pgraphnodes_composite2.cxx | 3 +- panda/src/pgraphnodes/sphereLight.I | 48 ++++++ panda/src/pgraphnodes/sphereLight.cxx | 146 ++++++++++++++++++ panda/src/pgraphnodes/sphereLight.h | 90 +++++++++++ 6 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 panda/src/pgraphnodes/sphereLight.I create mode 100644 panda/src/pgraphnodes/sphereLight.cxx create mode 100644 panda/src/pgraphnodes/sphereLight.h diff --git a/panda/src/pgraphnodes/config_pgraphnodes.cxx b/panda/src/pgraphnodes/config_pgraphnodes.cxx index 4623b44d78..47b0ed3baf 100644 --- a/panda/src/pgraphnodes/config_pgraphnodes.cxx +++ b/panda/src/pgraphnodes/config_pgraphnodes.cxx @@ -29,6 +29,7 @@ #include "selectiveChildNode.h" #include "sequenceNode.h" #include "shaderGenerator.h" +#include "sphereLight.h" #include "spotlight.h" #include "switchNode.h" #include "uvScrollNode.h" @@ -123,6 +124,7 @@ init_libpgraphnodes() { SelectiveChildNode::init_type(); SequenceNode::init_type(); ShaderGenerator::init_type(); + SphereLight::init_type(); Spotlight::init_type(); SwitchNode::init_type(); UvScrollNode::init_type(); @@ -137,6 +139,7 @@ init_libpgraphnodes() { PointLight::register_with_read_factory(); SelectiveChildNode::register_with_read_factory(); SequenceNode::register_with_read_factory(); + SphereLight::register_with_read_factory(); Spotlight::register_with_read_factory(); SwitchNode::register_with_read_factory(); UvScrollNode::register_with_read_factory(); diff --git a/panda/src/pgraphnodes/p3pgraphnodes_composite1.cxx b/panda/src/pgraphnodes/p3pgraphnodes_composite1.cxx index 67f6fff97c..eb6199d7f0 100644 --- a/panda/src/pgraphnodes/p3pgraphnodes_composite1.cxx +++ b/panda/src/pgraphnodes/p3pgraphnodes_composite1.cxx @@ -7,3 +7,5 @@ #include "fadeLodNodeData.cxx" #include "lightLensNode.cxx" #include "lightNode.cxx" +#include "lodNode.cxx" +#include "lodNodeType.cxx" diff --git a/panda/src/pgraphnodes/p3pgraphnodes_composite2.cxx b/panda/src/pgraphnodes/p3pgraphnodes_composite2.cxx index 1d67ee3d44..952a2448c9 100644 --- a/panda/src/pgraphnodes/p3pgraphnodes_composite2.cxx +++ b/panda/src/pgraphnodes/p3pgraphnodes_composite2.cxx @@ -1,11 +1,10 @@ -#include "lodNode.cxx" -#include "lodNodeType.cxx" #include "nodeCullCallbackData.cxx" #include "pointLight.cxx" #include "sceneGraphAnalyzer.cxx" #include "selectiveChildNode.cxx" #include "sequenceNode.cxx" #include "shaderGenerator.cxx" +#include "sphereLight.cxx" #include "spotlight.cxx" #include "switchNode.cxx" #include "uvScrollNode.cxx" diff --git a/panda/src/pgraphnodes/sphereLight.I b/panda/src/pgraphnodes/sphereLight.I new file mode 100644 index 0000000000..7f1c1955b3 --- /dev/null +++ b/panda/src/pgraphnodes/sphereLight.I @@ -0,0 +1,48 @@ +/** + * PANDA 3D SOFTWARE + * Copyright (c) Carnegie Mellon University. All rights reserved. + * + * All use of this software is subject to the terms of the revised BSD + * license. You should have received a copy of this license along + * with this source code in a file named "LICENSE." + * + * @file sphereLight.I + * @author rdb + * @date 2016-04-15 + */ + +/** + * + */ +INLINE SphereLight::CData:: +CData() : + _radius(0.01f) +{ +} + +/** + * + */ +INLINE SphereLight::CData:: +CData(const SphereLight::CData ©) : + _radius(copy._radius) +{ +} + +/** + * Returns the radius of the sphere. + */ +INLINE PN_stdfloat SphereLight:: +get_radius() const { + CDReader cdata(_cycler); + return cdata->_radius; +} + +/** + * Sets the radius of the sphere. + */ +INLINE void SphereLight:: +set_radius(PN_stdfloat radius) { + CDWriter cdata(_cycler); + cdata->_radius = radius; +} diff --git a/panda/src/pgraphnodes/sphereLight.cxx b/panda/src/pgraphnodes/sphereLight.cxx new file mode 100644 index 0000000000..c4cf01be5a --- /dev/null +++ b/panda/src/pgraphnodes/sphereLight.cxx @@ -0,0 +1,146 @@ +/** + * PANDA 3D SOFTWARE + * Copyright (c) Carnegie Mellon University. All rights reserved. + * + * All use of this software is subject to the terms of the revised BSD + * license. You should have received a copy of this license along + * with this source code in a file named "LICENSE." + * + * @file sphereLight.cxx + * @author rdb + * @date 2016-04-15 + */ + +#include "sphereLight.h" +#include "graphicsStateGuardianBase.h" +#include "bamWriter.h" +#include "bamReader.h" +#include "datagram.h" +#include "datagramIterator.h" + +TypeHandle SphereLight::_type_handle; + +/** + * + */ +CycleData *SphereLight::CData:: +make_copy() const { + return new CData(*this); +} + +/** + * Writes the contents of this object to the datagram for shipping out to a + * Bam file. + */ +void SphereLight::CData:: +write_datagram(BamWriter *manager, Datagram &dg) const { + dg.add_stdfloat(_radius); +} + +/** + * This internal function is called by make_from_bam to read in all of the + * relevant data from the BamFile for the new Light. + */ +void SphereLight::CData:: +fillin(DatagramIterator &scan, BamReader *manager) { + _radius = scan.get_stdfloat(); +} + +/** + * + */ +SphereLight:: +SphereLight(const string &name) : + PointLight(name) +{ +} + +/** + * Do not call the copy constructor directly; instead, use make_copy() or + * copy_subgraph() to make a copy of a node. + */ +SphereLight:: +SphereLight(const SphereLight ©) : + PointLight(copy), + _cycler(copy._cycler) +{ +} + +/** + * Returns a newly-allocated PandaNode that is a shallow copy of this one. It + * will be a different pointer, but its internal data may or may not be shared + * with that of the original PandaNode. No children will be copied. + */ +PandaNode *SphereLight:: +make_copy() const { + return new SphereLight(*this); +} + +/** + * 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 SphereLight:: +xform(const LMatrix4 &mat) { + PointLight::xform(mat); + CDWriter cdata(_cycler); + cdata->_radius = mat.xform_vec(LVector3(0, 0, cdata->_radius)).length(); + mark_viz_stale(); +} + +/** + * + */ +void SphereLight:: +write(ostream &out, int indent_level) const { + PointLight::write(out, indent_level); + indent(out, indent_level) << *this << ":\n"; + indent(out, indent_level + 2) + << "radius " << get_radius() << "\n"; +} + +/** + * Tells the BamReader how to create objects of type SphereLight. + */ +void SphereLight:: +register_with_read_factory() { + BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); +} + +/** + * Writes the contents of this object to the datagram for shipping out to a + * Bam file. + */ +void SphereLight:: +write_datagram(BamWriter *manager, Datagram &dg) { + PointLight::write_datagram(manager, dg); + manager->write_cdata(dg, _cycler); +} + +/** + * This function is called by the BamReader's factory when a new object of + * type SphereLight is encountered in the Bam file. It should create the + * SphereLight and extract its information from the file. + */ +TypedWritable *SphereLight:: +make_from_bam(const FactoryParams ¶ms) { + SphereLight *node = new SphereLight(""); + DatagramIterator scan; + BamReader *manager; + + parse_params(params, scan, manager); + node->fillin(scan, manager); + + return node; +} + +/** + * This internal function is called by make_from_bam to read in all of the + * relevant data from the BamFile for the new SphereLight. + */ +void SphereLight:: +fillin(DatagramIterator &scan, BamReader *manager) { + PointLight::fillin(scan, manager); + + manager->read_cdata(scan, _cycler); +} diff --git a/panda/src/pgraphnodes/sphereLight.h b/panda/src/pgraphnodes/sphereLight.h new file mode 100644 index 0000000000..da36d71fc2 --- /dev/null +++ b/panda/src/pgraphnodes/sphereLight.h @@ -0,0 +1,90 @@ +/** + * PANDA 3D SOFTWARE + * Copyright (c) Carnegie Mellon University. All rights reserved. + * + * All use of this software is subject to the terms of the revised BSD + * license. You should have received a copy of this license along + * with this source code in a file named "LICENSE." + * + * @file sphereLight.h + * @author rdb + * @date 2016-04-15 + */ + +#ifndef SPHERELIGHT_H +#define SPHERELIGHT_H + +#include "pandabase.h" + +#include "lightLensNode.h" + +/** + * A sphere light is like a point light, except that it represents a sphere + * with a radius, rather than being an infinitely thin point in space. + */ +class EXPCL_PANDA_PGRAPHNODES SphereLight : public PointLight { +PUBLISHED: + SphereLight(const string &name); + +protected: + SphereLight(const SphereLight ©); + +public: + virtual PandaNode *make_copy() const; + virtual void xform(const LMatrix4 &mat); + virtual void write(ostream &out, int indent_level) const; + +PUBLISHED: + INLINE PN_stdfloat get_radius() const; + INLINE void set_radius(PN_stdfloat radius); + MAKE_PROPERTY(radius, get_radius, set_radius); + +private: + // This is the data that must be cycled between pipeline stages. + class EXPCL_PANDA_PGRAPHNODES 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); + virtual TypeHandle get_parent_type() const { + return SphereLight::get_class_type(); + } + + PN_stdfloat _radius; + }; + + PipelineCycler _cycler; + typedef CycleDataReader CDReader; + typedef CycleDataWriter CDWriter; + +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); + +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + PointLight::init_type(); + register_type(_type_handle, "SphereLight", + PointLight::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 "sphereLight.I" + +#endif