Panda3D
cullBinAttrib.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file cullBinAttrib.cxx
10  * @author drose
11  * @date 2002-03-01
12  */
13 
14 #include "cullBinAttrib.h"
15 #include "bamReader.h"
16 #include "bamWriter.h"
17 #include "datagram.h"
18 #include "datagramIterator.h"
19 
20 TypeHandle CullBinAttrib::_type_handle;
21 int CullBinAttrib::_attrib_slot;
22 
23 /**
24  * Constructs a new CullBinAttrib assigning geometry into the named bin. If
25  * the bin name is the empty string, the default bin is used.
26  *
27  * The draw_order specifies further ordering information which is relevant
28  * only to certain kinds of bins (in particular CullBinFixed type bins).
29  */
30 CPT(RenderAttrib) CullBinAttrib::
31 make(const std::string &bin_name, int draw_order) {
32  CullBinAttrib *attrib = new CullBinAttrib;
33  attrib->_bin_name = bin_name;
34  attrib->_draw_order = draw_order;
35  return return_new(attrib);
36 }
37 
38 /**
39  * Returns a RenderAttrib that corresponds to whatever the standard default
40  * properties for render attributes of this type ought to be.
41  */
42 CPT(RenderAttrib) CullBinAttrib::
43 make_default() {
44  return return_new(new CullBinAttrib);
45 }
46 
47 /**
48  *
49  */
50 void CullBinAttrib::
51 output(std::ostream &out) const {
52  out << get_type() << ":";
53  if (_bin_name.empty()) {
54  out << "(default)";
55  } else {
56  out << _bin_name << "," << _draw_order;
57  }
58 }
59 
60 /**
61  * Intended to be overridden by derived CullBinAttrib types to return a unique
62  * number indicating whether this CullBinAttrib is equivalent to the other
63  * one.
64  *
65  * This should return 0 if the two CullBinAttrib objects are equivalent, a
66  * number less than zero if this one should be sorted before the other one,
67  * and a number greater than zero otherwise.
68  *
69  * This will only be called with two CullBinAttrib objects whose get_type()
70  * functions return the same.
71  */
72 int CullBinAttrib::
73 compare_to_impl(const RenderAttrib *other) const {
74  const CullBinAttrib *ta = (const CullBinAttrib *)other;
75 
76  if (_draw_order != ta->_draw_order) {
77  return _draw_order - ta->_draw_order;
78  }
79  return strcmp(_bin_name.c_str(), ta->_bin_name.c_str());
80 }
81 
82 /**
83  * Intended to be overridden by derived RenderAttrib types to return a unique
84  * hash for these particular properties. RenderAttribs that compare the same
85  * with compare_to_impl(), above, should return the same hash; RenderAttribs
86  * that compare differently should return a different hash.
87  */
88 size_t CullBinAttrib::
89 get_hash_impl() const {
90  size_t hash = 0;
91  hash = int_hash::add_hash(hash, _draw_order);
92  hash = string_hash::add_hash(hash, _bin_name);
93  return hash;
94 }
95 
96 /**
97  * Tells the BamReader how to create objects of type CullBinAttrib.
98  */
99 void CullBinAttrib::
101  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
102 }
103 
104 /**
105  * Writes the contents of this object to the datagram for shipping out to a
106  * Bam file.
107  */
108 void CullBinAttrib::
110  RenderAttrib::write_datagram(manager, dg);
111 
112  dg.add_string(_bin_name);
113  dg.add_int32(_draw_order);
114 }
115 
116 /**
117  * This function is called by the BamReader's factory when a new object of
118  * type CullBinAttrib is encountered in the Bam file. It should create the
119  * CullBinAttrib and extract its information from the file.
120  */
121 TypedWritable *CullBinAttrib::
122 make_from_bam(const FactoryParams &params) {
123  CullBinAttrib *attrib = new CullBinAttrib;
124  DatagramIterator scan;
125  BamReader *manager;
126 
127  parse_params(params, scan, manager);
128  attrib->fillin(scan, manager);
129 
130  return attrib;
131 }
132 
133 /**
134  * This internal function is called by make_from_bam to read in all of the
135  * relevant data from the BamFile for the new CullBinAttrib.
136  */
137 void CullBinAttrib::
138 fillin(DatagramIterator &scan, BamReader *manager) {
139  RenderAttrib::fillin(scan, manager);
140 
141  _bin_name = scan.get_string();
142  _draw_order = scan.get_int32();
143 }
CPT(RenderAttrib) CullBinAttrib
Constructs a new CullBinAttrib assigning geometry into the named bin.
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
int32_t get_int32()
Extracts a signed 32-bit integer.
std::string get_string()
Extracts a variable-length string.
static void register_with_read_factory()
Tells the BamReader how to create objects of type CullBinAttrib.
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:101
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_string(const std::string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:219
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Assigns geometry to a particular bin by name.
Definition: cullBinAttrib.h:27
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:67
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static size_t add_hash(size_t start, const Key &key)
Adds the elements of the indicated key into a running hash.
Definition: stl_compares.I:168
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.