Selaa lähdekoodia

add SwitchNode

David Rose 23 vuotta sitten
vanhempi
sitoutus
cf031f570a

+ 3 - 0
panda/src/pgraph/Sources.pp

@@ -80,6 +80,7 @@
     sequenceNode.I sequenceNode.h \
     showBoundsEffect.I showBoundsEffect.h \
     spotlight.I spotlight.h \
+    switchNode.I switchNode.h \
     texMatrixAttrib.I texMatrixAttrib.h \
     textureApplyAttrib.I textureApplyAttrib.h \
     textureAttrib.I textureAttrib.h \
@@ -162,6 +163,7 @@
     sequenceNode.cxx \
     showBoundsEffect.cxx \
     spotlight.cxx \
+    switchNode.cxx \
     texMatrixAttrib.cxx \
     textureApplyAttrib.cxx \
     textureAttrib.cxx \
@@ -240,6 +242,7 @@
     sequenceNode.I sequenceNode.h \
     showBoundsEffect.I showBoundsEffect.h \
     spotlight.I spotlight.h \
+    switchNode.I switchNode.h \
     texMatrixAttrib.I texMatrixAttrib.h \
     textureApplyAttrib.I textureApplyAttrib.h \
     textureAttrib.I textureAttrib.h \

+ 3 - 0
panda/src/pgraph/config_pgraph.cxx

@@ -71,6 +71,7 @@
 #include "sequenceNode.h"
 #include "showBoundsEffect.h"
 #include "spotlight.h"
+#include "switchNode.h"
 #include "texMatrixAttrib.h"
 #include "textureApplyAttrib.h"
 #include "textureAttrib.h"
@@ -211,6 +212,7 @@ init_libpgraph() {
   SequenceNode::init_type();
   ShowBoundsEffect::init_type();
   Spotlight::init_type();
+  SwitchNode::init_type();
   TexMatrixAttrib::init_type();
   TextureApplyAttrib::init_type();
   TextureAttrib::init_type();
@@ -262,6 +264,7 @@ init_libpgraph() {
   SequenceNode::register_with_read_factory();
   ShowBoundsEffect::register_with_read_factory();
   Spotlight::register_with_read_factory();
+  SwitchNode::register_with_read_factory();
   TexMatrixAttrib::register_with_read_factory();
   TextureApplyAttrib::register_with_read_factory();
   TextureAttrib::register_with_read_factory();

+ 1 - 0
panda/src/pgraph/pgraph_composite2.cxx

@@ -29,6 +29,7 @@
 #include "sequenceNode.cxx"
 #include "showBoundsEffect.cxx"
 #include "spotlight.cxx"
+#include "switchNode.cxx"
 #include "test_pgraph.cxx"
 #include "texMatrixAttrib.cxx"
 #include "textureApplyAttrib.cxx"

+ 73 - 0
panda/src/pgraph/switchNode.I

@@ -0,0 +1,73 @@
+// Filename: switchNode.I
+// Created by:  drose (31Jul02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, 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://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::CData::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE SwitchNode::CData::
+CData() {
+  _visible_child = 0;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::CData::Copy Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE SwitchNode::CData::
+CData(const SwitchNode::CData &copy) :
+  _visible_child(copy._visible_child)
+{
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::Constructor
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE SwitchNode::
+SwitchNode(const string &name) :
+  SelectiveChildNode(name)
+{
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::set_visible_child
+//       Access: Published
+//  Description: Specifies the particular child of this node, by
+//               index, that will be visible.
+////////////////////////////////////////////////////////////////////
+INLINE void SwitchNode::
+set_visible_child(int index) {
+  CDWriter cdata(_cycler);
+  cdata->_visible_child = index;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::get_visible_child
+//       Access: Published
+//  Description: Returns the index of the child that should be visible.
+////////////////////////////////////////////////////////////////////
+INLINE int SwitchNode::
+get_visible_child() const {
+  CDReader cdata(_cycler);
+  return cdata->_visible_child;
+}

+ 191 - 0
panda/src/pgraph/switchNode.cxx

@@ -0,0 +1,191 @@
+// Filename: switchNode.cxx
+// Created by:  drose (31Jul02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, 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://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "pandabase.h"
+#include "switchNode.h"
+#include "cullTraverser.h"
+
+TypeHandle SwitchNode::_type_handle;
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::CData::make_copy
+//       Access: Public, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+CycleData *SwitchNode::CData::
+make_copy() const {
+  return new CData(*this);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::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 SwitchNode::
+safe_to_combine() const {
+  return false;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::CData::write_datagram
+//       Access: Public, Virtual
+//  Description: Writes the contents of this object to the datagram
+//               for shipping out to a Bam file.
+////////////////////////////////////////////////////////////////////
+void SwitchNode::CData::
+write_datagram(BamWriter *manager, Datagram &dg) const {
+  dg.add_int32(_visible_child);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::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 SwitchNode.
+////////////////////////////////////////////////////////////////////
+void SwitchNode::CData::
+fillin(DatagramIterator &scan, BamReader *manager) {
+  _visible_child = scan.get_int32();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::Copy Constructor
+//       Access: Protected
+//  Description:
+////////////////////////////////////////////////////////////////////
+SwitchNode::
+SwitchNode(const SwitchNode &copy) :
+  SelectiveChildNode(copy),
+  _cycler(copy._cycler)
+{
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::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 *SwitchNode::
+make_copy() const {
+  return new SwitchNode(*this);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::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 SwitchNode::
+has_cull_callback() const {
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::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 SwitchNode::
+cull_callback(CullTraverser *, CullTraverserData &) {
+  select_child(get_visible_child());
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::register_with_read_factory
+//       Access: Public, Static
+//  Description: Tells the BamReader how to create objects of type
+//               SwitchNode.
+////////////////////////////////////////////////////////////////////
+void SwitchNode::
+register_with_read_factory() {
+  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::write_datagram
+//       Access: Public, Virtual
+//  Description: Writes the contents of this object to the datagram
+//               for shipping out to a Bam file.
+////////////////////////////////////////////////////////////////////
+void SwitchNode::
+write_datagram(BamWriter *manager, Datagram &dg) {
+  SelectiveChildNode::write_datagram(manager, dg);
+  manager->write_cdata(dg, _cycler);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::make_from_bam
+//       Access: Protected, Static
+//  Description: This function is called by the BamReader's factory
+//               when a new object of type SwitchNode is encountered
+//               in the Bam file.  It should create the SwitchNode
+//               and extract its information from the file.
+////////////////////////////////////////////////////////////////////
+TypedWritable *SwitchNode::
+make_from_bam(const FactoryParams &params) {
+  SwitchNode *node = new SwitchNode("");
+  DatagramIterator scan;
+  BamReader *manager;
+
+  parse_params(params, scan, manager);
+  node->fillin(scan, manager);
+
+  return node;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: SwitchNode::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 SwitchNode.
+////////////////////////////////////////////////////////////////////
+void SwitchNode::
+fillin(DatagramIterator &scan, BamReader *manager) {
+  SelectiveChildNode::fillin(scan, manager);
+  manager->read_cdata(scan, _cycler);
+}

+ 92 - 0
panda/src/pgraph/switchNode.h

@@ -0,0 +1,92 @@
+// Filename: switchNode.h
+// Created by:  drose (31Jul02)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, 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://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef SWITCHNODE_H
+#define SWITCHNODE_H
+
+#include "pandabase.h"
+
+#include "selectiveChildNode.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : SwitchNode
+// Description : A node that renders only one of its children,
+//               according to the user's indication.
+////////////////////////////////////////////////////////////////////
+class EXPCL_PANDA SwitchNode : public SelectiveChildNode {
+PUBLISHED:
+  INLINE SwitchNode(const string &name);
+
+public:
+  SwitchNode(const SwitchNode &copy);
+
+  virtual PandaNode *make_copy() const;
+  virtual bool safe_to_combine() const;
+
+  virtual bool has_cull_callback() const;
+  virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
+
+PUBLISHED:
+  INLINE void set_visible_child(int index);
+  INLINE int get_visible_child() const;
+
+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);
+
+    int _visible_child;
+  };
+
+  PipelineCycler<CData> _cycler;
+  typedef CycleDataReader<CData> CDReader;
+  typedef CycleDataWriter<CData> CDWriter;
+
+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);
+
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    SelectiveChildNode::init_type();
+    register_type(_type_handle, "SwitchNode",
+                  SelectiveChildNode::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 "switchNode.I"
+
+#endif