Panda3D
materialAttrib.cxx
1 // Filename: materialAttrib.cxx
2 // Created by: drose (04Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "materialAttrib.h"
16 #include "graphicsStateGuardianBase.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle MaterialAttrib::_type_handle;
23 int MaterialAttrib::_attrib_slot;
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: MaterialAttrib::make
27 // Access: Published, Static
28 // Description: Constructs a new MaterialAttrib object suitable for
29 // rendering the indicated material onto geometry.
30 ////////////////////////////////////////////////////////////////////
31 CPT(RenderAttrib) MaterialAttrib::
32 make(Material *material) {
33  MaterialAttrib *attrib = new MaterialAttrib;
34  attrib->_material = material;
35  material->set_attrib_lock();
36  return return_new(attrib);
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: MaterialAttrib::make_off
41 // Access: Published, Static
42 // Description: Constructs a new MaterialAttrib object suitable for
43 // rendering unmateriald geometry.
44 ////////////////////////////////////////////////////////////////////
45 CPT(RenderAttrib) MaterialAttrib::
46 make_off() {
47  MaterialAttrib *attrib = new MaterialAttrib;
48  return return_new(attrib);
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: MaterialAttrib::make_default
53 // Access: Published, Static
54 // Description: Returns a RenderAttrib that corresponds to whatever
55 // the standard default properties for render attributes
56 // of this type ought to be.
57 ////////////////////////////////////////////////////////////////////
58 CPT(RenderAttrib) MaterialAttrib::
59 make_default() {
60  return return_new(new MaterialAttrib);
61 }
62 
63 ////////////////////////////////////////////////////////////////////
64 // Function: MaterialAttrib::output
65 // Access: Public, Virtual
66 // Description:
67 ////////////////////////////////////////////////////////////////////
68 void MaterialAttrib::
69 output(ostream &out) const {
70  out << get_type() << ":";
71  if (is_off()) {
72  out << "(off)";
73  } else {
74  out << *_material;
75  }
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: MaterialAttrib::compare_to_impl
80 // Access: Protected, Virtual
81 // Description: Intended to be overridden by derived MaterialAttrib
82 // types to return a unique number indicating whether
83 // this MaterialAttrib is equivalent to the other one.
84 //
85 // This should return 0 if the two MaterialAttrib objects
86 // are equivalent, a number less than zero if this one
87 // should be sorted before the other one, and a number
88 // greater than zero otherwise.
89 //
90 // This will only be called with two MaterialAttrib
91 // objects whose get_type() functions return the same.
92 ////////////////////////////////////////////////////////////////////
93 int MaterialAttrib::
94 compare_to_impl(const RenderAttrib *other) const {
95  const MaterialAttrib *ta;
96  DCAST_INTO_R(ta, other, 0);
97 
98  // Comparing pointers by subtraction is problematic. Instead of
99  // doing this, we'll just depend on the built-in != and < operators
100  // for comparing pointers.
101  if (_material != ta->_material) {
102  return _material < ta->_material ? -1 : 1;
103  }
104  return 0;
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: MaterialAttrib::get_hash_impl
109 // Access: Protected, Virtual
110 // Description: Intended to be overridden by derived RenderAttrib
111 // types to return a unique hash for these particular
112 // properties. RenderAttribs that compare the same with
113 // compare_to_impl(), above, should return the same
114 // hash; RenderAttribs that compare differently should
115 // return a different hash.
116 ////////////////////////////////////////////////////////////////////
117 size_t MaterialAttrib::
118 get_hash_impl() const {
119  size_t hash = 0;
120  hash = pointer_hash::add_hash(hash, _material);
121  return hash;
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: MaterialAttrib::get_auto_shader_attrib_impl
126 // Access: Protected, Virtual
127 // Description:
128 ////////////////////////////////////////////////////////////////////
129 CPT(RenderAttrib) MaterialAttrib::
130 get_auto_shader_attrib_impl(const RenderState *state) const {
131  return this;
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function: MaterialAttrib::register_with_read_factory
136 // Access: Public, Static
137 // Description: Tells the BamReader how to create objects of type
138 // MaterialAttrib.
139 ////////////////////////////////////////////////////////////////////
140 void MaterialAttrib::
141 register_with_read_factory() {
142  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
143 }
144 
145 ////////////////////////////////////////////////////////////////////
146 // Function: MaterialAttrib::write_datagram
147 // Access: Public, Virtual
148 // Description: Writes the contents of this object to the datagram
149 // for shipping out to a Bam file.
150 ////////////////////////////////////////////////////////////////////
151 void MaterialAttrib::
153  RenderAttrib::write_datagram(manager, dg);
154 
155  manager->write_pointer(dg, _material);
156 }
157 
158 ////////////////////////////////////////////////////////////////////
159 // Function: MaterialAttrib::complete_pointers
160 // Access: Public, Virtual
161 // Description: Receives an array of pointers, one for each time
162 // manager->read_pointer() was called in fillin().
163 // Returns the number of pointers processed.
164 ////////////////////////////////////////////////////////////////////
167  int pi = RenderAttrib::complete_pointers(p_list, manager);
168 
169  TypedWritable *material = p_list[pi++];
170  if (material != (TypedWritable *)NULL) {
171  _material = DCAST(Material, material);
172  }
173 
174  return pi;
175 }
176 
177 ////////////////////////////////////////////////////////////////////
178 // Function: MaterialAttrib::make_from_bam
179 // Access: Protected, Static
180 // Description: This function is called by the BamReader's factory
181 // when a new object of type MaterialAttrib is encountered
182 // in the Bam file. It should create the MaterialAttrib
183 // and extract its information from the file.
184 ////////////////////////////////////////////////////////////////////
185 TypedWritable *MaterialAttrib::
186 make_from_bam(const FactoryParams &params) {
187  MaterialAttrib *attrib = new MaterialAttrib;
188  DatagramIterator scan;
189  BamReader *manager;
190 
191  parse_params(params, scan, manager);
192  attrib->fillin(scan, manager);
193  return attrib;
194 }
195 
196 ////////////////////////////////////////////////////////////////////
197 // Function: MaterialAttrib::fillin
198 // Access: Protected
199 // Description: This internal function is called by make_from_bam to
200 // read in all of the relevant data from the BamFile for
201 // the new MaterialAttrib.
202 ////////////////////////////////////////////////////////////////////
203 void MaterialAttrib::
204 fillin(DatagramIterator &scan, BamReader *manager) {
205  RenderAttrib::fillin(scan, manager);
206 
207  // Read the _material pointer.
208  manager->read_pointer(scan);
209 }
static size_t add_hash(size_t start, const void *key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:133
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:60
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
Indicates which, if any, material should be applied to geometry.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
virtual int complete_pointers(TypedWritable **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin()...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin()...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
Defines the way an object appears in the presence of lighting.
Definition: material.h:34
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
bool is_off() const
Returns true if the MaterialAttrib is an &#39;off&#39; MaterialAttrib, indicating that it should disable the ...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
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:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:279
void read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:658