Panda3D
 All Classes Functions Variables Enumerations
geomNode.h
00001 // Filename: geomNode.h
00002 // Created by:  drose (22eb02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef GEOMNODE_H
00016 #define GEOMNODE_H
00017 
00018 #include "pandabase.h"
00019 #include "config_pgraph.h"
00020 #include "pandaNode.h"
00021 #include "pointerToArray.h"
00022 #include "geom.h"
00023 #include "pipelineCycler.h"
00024 #include "cycleData.h"
00025 #include "pvector.h"
00026 #include "copyOnWritePointer.h"
00027 
00028 class GraphicsStateGuardianBase;
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //       Class : GeomNode
00032 // Description : A node that holds Geom objects, renderable pieces of
00033 //               geometry.  This is the primary kind of leaf node in
00034 //               the scene graph; almost all visible objects will be
00035 //               contained in a GeomNode somewhere.
00036 ////////////////////////////////////////////////////////////////////
00037 class EXPCL_PANDA_PGRAPH GeomNode : public PandaNode {
00038 PUBLISHED:
00039   GeomNode(const string &name);
00040 
00041 protected:
00042   GeomNode(const GeomNode &copy);
00043 public:
00044   virtual ~GeomNode();
00045   virtual PandaNode *make_copy() const;
00046   virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs,
00047                                          int attrib_types,
00048                                          GeomTransformer &transformer);
00049   virtual void xform(const LMatrix4 &mat);
00050   virtual PandaNode *combine_with(PandaNode *other); 
00051   virtual CPT(TransformState)
00052     calc_tight_bounds(LPoint3 &min_point, LPoint3 &max_point,
00053                       bool &found_any,
00054                       const TransformState *transform,
00055                       Thread *current_thread) const;
00056   virtual bool is_renderable() const;
00057   virtual void add_for_draw(CullTraverser *trav, CullTraverserData &data);
00058   virtual CollideMask get_legal_collide_mask() const;
00059 
00060   virtual bool safe_to_flatten() const;
00061   virtual bool safe_to_combine() const;
00062 
00063   virtual void r_prepare_scene(GraphicsStateGuardianBase *gsg,
00064                                const RenderState *node_state,
00065                                GeomTransformer &transformer,
00066                                Thread *current_thread);
00067 
00068 PUBLISHED:
00069   INLINE void set_preserved(bool value);
00070   INLINE bool get_preserved() const;
00071 
00072   INLINE int get_num_geoms() const;
00073   INLINE CPT(Geom) get_geom(int n) const;
00074   MAKE_SEQ(get_geoms, get_num_geoms, get_geom);
00075   INLINE PT(Geom) modify_geom(int n);
00076   INLINE const RenderState *get_geom_state(int n) const;
00077   MAKE_SEQ(get_geom_states, get_num_geoms, get_geom_state);
00078   INLINE void set_geom_state(int n, const RenderState *state);
00079 
00080   void add_geom(Geom *geom, const RenderState *state = RenderState::make_empty());
00081   void add_geoms_from(const GeomNode *other);
00082   void set_geom(int n, Geom *geom);
00083   INLINE void remove_geom(int n);
00084   INLINE void remove_all_geoms();
00085   bool check_valid() const;
00086 
00087   void decompose();
00088   void unify(int max_indices, bool preserve_order);
00089 
00090   void write_geoms(ostream &out, int indent_level) const;
00091   void write_verbose(ostream &out, int indent_level) const;
00092 
00093   INLINE static CollideMask get_default_collide_mask();
00094 
00095 public:
00096   virtual void output(ostream &out) const;
00097 
00098   virtual bool is_geom_node() const;
00099 
00100   void do_premunge(GraphicsStateGuardianBase *gsg,
00101                    const RenderState *node_state,
00102                    GeomTransformer &transformer);
00103 
00104 protected:
00105   virtual void r_mark_geom_bounds_stale(Thread *current_thread);
00106   virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
00107                                        int &internal_vertices,
00108                                        int pipeline_stage,
00109                                        Thread *current_thread) const;
00110 
00111 public:
00112   // This must be declared public so that VC6 will allow the nested
00113   // CData class to access it.
00114   class GeomEntry {
00115   public:
00116     INLINE GeomEntry(Geom *geom, const RenderState *state);
00117     COWPT(Geom) _geom;
00118     CPT(RenderState) _state;
00119   };
00120 
00121   typedef CopyOnWriteObj< pvector<GeomEntry> > GeomList;
00122 
00123 private:
00124 
00125   bool _preserved;
00126   typedef pmap<const InternalName *, int> NameCount;
00127 
00128   INLINE void count_name(NameCount &name_count, const InternalName *name);
00129   INLINE int get_name_count(const NameCount &name_count, const InternalName *name);
00130 
00131   // This is the data that must be cycled between pipeline stages.
00132   class EXPCL_PANDA_PGRAPH CData : public CycleData {
00133   public:
00134     INLINE CData();
00135     CData(const CData &copy);
00136     virtual CycleData *make_copy() const;
00137     virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
00138     virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
00139     virtual void fillin(DatagramIterator &scan, BamReader *manager);
00140     virtual TypeHandle get_parent_type() const {
00141       return GeomNode::get_class_type();
00142     }
00143 
00144     INLINE CPT(GeomList) get_geoms() const;
00145     INLINE PT(GeomList) modify_geoms();
00146     INLINE void set_geoms(GeomList *geoms);
00147 
00148   private:
00149     COWPT(GeomList) _geoms;
00150   };
00151 
00152   PipelineCycler<CData> _cycler;
00153   typedef CycleDataReader<CData> CDReader;
00154   typedef CycleDataWriter<CData> CDWriter;
00155   typedef CycleDataStageReader<CData> CDStageReader;
00156   typedef CycleDataLockedStageReader<CData> CDLockedStageReader;
00157   typedef CycleDataStageWriter<CData> CDStageWriter;
00158 
00159 public:
00160   // This class is returned from get_geoms().  It is similar to
00161   // PandaNode::get_children(); use this to walk through the list of
00162   // geoms faster than walking through the geoms directly from the
00163   // node.
00164   class EXPCL_PANDA_PGRAPH Geoms {
00165   public:
00166     INLINE Geoms();
00167     INLINE Geoms(const CData *cdata);
00168     INLINE Geoms(const Geoms &copy);
00169     INLINE void operator = (const Geoms &copy);
00170 
00171     INLINE int get_num_geoms() const;
00172     INLINE CPT(Geom) get_geom(int n) const;
00173     INLINE const RenderState *get_geom_state(int n) const;
00174 
00175   private:
00176     CPT(GeomList) _geoms;
00177   };
00178 
00179   INLINE Geoms get_geoms(Thread *current_thread = Thread::get_current_thread()) const;
00180 
00181   // This data is only needed when reading from a bam file.
00182   class BamAuxData : public BamReader::AuxData {
00183   public:
00184     // We just hold a pointer to the RenderState that may otherwise
00185     // lose its pointers before it can finalize.
00186     CPT(RenderState) _hold_state;
00187   };
00188 
00189 public:
00190   static void register_with_read_factory();
00191   virtual void write_datagram(BamWriter *manager, Datagram &dg);
00192 
00193   virtual void finalize(BamReader *manager);
00194 
00195 protected:
00196   static TypedWritable *make_from_bam(const FactoryParams &params);
00197   void fillin(DatagramIterator &scan, BamReader *manager);
00198   
00199 public:
00200   static TypeHandle get_class_type() {
00201     return _type_handle;
00202   }
00203   static void init_type() {
00204     GeomList::init_type();
00205     PandaNode::init_type();
00206     register_type(_type_handle, "GeomNode",
00207                   PandaNode::get_class_type());
00208   }
00209   virtual TypeHandle get_type() const {
00210     return get_class_type();
00211   }
00212   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00213 
00214 private:
00215   static TypeHandle _type_handle;
00216 
00217   friend class GeomTransformer;
00218 };
00219 
00220 #include "geomNode.I"
00221 
00222 #endif
 All Classes Functions Variables Enumerations