Panda3D
|
00001 // Filename: geomNode.h 00002 // Created by: drose (22Feb02) 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 ©); 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 LMatrix4f &mat); 00050 virtual PandaNode *combine_with(PandaNode *other); 00051 virtual CPT(TransformState) 00052 calc_tight_bounds(LPoint3f &min_point, LPoint3f &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(const RenderState *state, 00064 PreparedGraphicsObjects *prepared_objects, 00065 Thread *current_thread); 00066 00067 PUBLISHED: 00068 INLINE void set_preserved(bool value); 00069 INLINE bool get_preserved() const; 00070 00071 INLINE int get_num_geoms() const; 00072 INLINE CPT(Geom) get_geom(int n) const; 00073 MAKE_SEQ(get_geoms, get_num_geoms, get_geom); 00074 INLINE PT(Geom) modify_geom(int n); 00075 INLINE const RenderState *get_geom_state(int n) const; 00076 MAKE_SEQ(get_geom_states, get_num_geoms, get_geom_state); 00077 INLINE void set_geom_state(int n, const RenderState *state); 00078 00079 void add_geom(Geom *geom, const RenderState *state = RenderState::make_empty()); 00080 void add_geoms_from(const GeomNode *other); 00081 void set_geom(int n, Geom *geom); 00082 INLINE void remove_geom(int n); 00083 INLINE void remove_all_geoms(); 00084 bool check_valid() const; 00085 00086 void decompose(); 00087 void unify(int max_indices, bool preserve_order); 00088 00089 void write_geoms(ostream &out, int indent_level) const; 00090 void write_verbose(ostream &out, int indent_level) const; 00091 00092 INLINE static CollideMask get_default_collide_mask(); 00093 00094 public: 00095 virtual void output(ostream &out) const; 00096 00097 virtual bool is_geom_node() const; 00098 00099 void do_premunge(GraphicsStateGuardianBase *gsg, 00100 const RenderState *node_state, 00101 GeomTransformer &transformer); 00102 00103 protected: 00104 virtual void r_mark_geom_bounds_stale(Thread *current_thread); 00105 virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds, 00106 int &internal_vertices, 00107 int pipeline_stage, 00108 Thread *current_thread) const; 00109 00110 public: 00111 // This must be declared public so that VC6 will allow the nested 00112 // CData class to access it. 00113 class GeomEntry { 00114 public: 00115 INLINE GeomEntry(Geom *geom, const RenderState *state); 00116 COWPT(Geom) _geom; 00117 CPT(RenderState) _state; 00118 }; 00119 00120 private: 00121 00122 bool _preserved; 00123 typedef CopyOnWriteObj< pvector<GeomEntry> > GeomList; 00124 typedef pmap<const InternalName *, int> NameCount; 00125 00126 INLINE void count_name(NameCount &name_count, const InternalName *name); 00127 INLINE int get_name_count(const NameCount &name_count, const InternalName *name); 00128 00129 // This is the data that must be cycled between pipeline stages. 00130 class EXPCL_PANDA_PGRAPH CData : public CycleData { 00131 public: 00132 INLINE CData(); 00133 CData(const CData ©); 00134 virtual CycleData *make_copy() const; 00135 virtual void write_datagram(BamWriter *manager, Datagram &dg) const; 00136 virtual int complete_pointers(TypedWritable **plist, BamReader *manager); 00137 virtual void fillin(DatagramIterator &scan, BamReader *manager); 00138 virtual TypeHandle get_parent_type() const { 00139 return GeomNode::get_class_type(); 00140 } 00141 00142 INLINE CPT(GeomList) get_geoms() const; 00143 INLINE PT(GeomList) modify_geoms(); 00144 INLINE void set_geoms(GeomList *geoms); 00145 00146 private: 00147 COWPT(GeomList) _geoms; 00148 }; 00149 00150 PipelineCycler<CData> _cycler; 00151 typedef CycleDataReader<CData> CDReader; 00152 typedef CycleDataWriter<CData> CDWriter; 00153 typedef CycleDataStageReader<CData> CDStageReader; 00154 typedef CycleDataLockedStageReader<CData> CDLockedStageReader; 00155 typedef CycleDataStageWriter<CData> CDStageWriter; 00156 00157 public: 00158 // This class is returned from get_geoms(). It is similar to 00159 // PandaNode::get_children(); use this to walk through the list of 00160 // geoms faster than walking through the geoms directly from the 00161 // node. 00162 class EXPCL_PANDA_PGRAPH Geoms { 00163 public: 00164 INLINE Geoms(); 00165 INLINE Geoms(const CData *cdata); 00166 INLINE Geoms(const Geoms ©); 00167 INLINE void operator = (const Geoms ©); 00168 00169 INLINE int get_num_geoms() const; 00170 INLINE CPT(Geom) get_geom(int n) const; 00171 INLINE const RenderState *get_geom_state(int n) const; 00172 00173 private: 00174 CPT(GeomList) _geoms; 00175 }; 00176 00177 INLINE Geoms get_geoms(Thread *current_thread = Thread::get_current_thread()) const; 00178 00179 // This data is only needed when reading from a bam file. 00180 class BamAuxData : public BamReader::AuxData { 00181 public: 00182 // We just hold a pointer to the RenderState that may otherwise 00183 // lose its pointers before it can finalize. 00184 CPT(RenderState) _hold_state; 00185 }; 00186 00187 public: 00188 static void register_with_read_factory(); 00189 virtual void write_datagram(BamWriter *manager, Datagram &dg); 00190 00191 virtual void finalize(BamReader *manager); 00192 00193 protected: 00194 static TypedWritable *make_from_bam(const FactoryParams ¶ms); 00195 void fillin(DatagramIterator &scan, BamReader *manager); 00196 00197 public: 00198 static TypeHandle get_class_type() { 00199 return _type_handle; 00200 } 00201 static void init_type() { 00202 GeomList::init_type(); 00203 PandaNode::init_type(); 00204 register_type(_type_handle, "GeomNode", 00205 PandaNode::get_class_type()); 00206 } 00207 virtual TypeHandle get_type() const { 00208 return get_class_type(); 00209 } 00210 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00211 00212 private: 00213 static TypeHandle _type_handle; 00214 00215 friend class GeomTransformer; 00216 }; 00217 00218 #include "geomNode.I" 00219 00220 #endif