Panda3D
materialAttrib.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 materialAttrib.cxx
10  * @author drose
11  * @date 2002-03-04
12  */
13 
14 #include "materialAttrib.h"
16 #include "bamReader.h"
17 #include "bamWriter.h"
18 #include "datagram.h"
19 #include "datagramIterator.h"
20 
21 TypeHandle MaterialAttrib::_type_handle;
22 int MaterialAttrib::_attrib_slot;
23 
24 /**
25  * Constructs a new MaterialAttrib object suitable for rendering the indicated
26  * material onto geometry.
27  */
28 CPT(RenderAttrib) MaterialAttrib::
29 make(Material *material) {
30  MaterialAttrib *attrib = new MaterialAttrib;
31  attrib->_material = material;
32  material->set_attrib_lock();
33  return return_new(attrib);
34 }
35 
36 /**
37  * Constructs a new MaterialAttrib object suitable for rendering unmateriald
38  * geometry.
39  */
40 CPT(RenderAttrib) MaterialAttrib::
41 make_off() {
42  MaterialAttrib *attrib = new MaterialAttrib;
43  return return_new(attrib);
44 }
45 
46 /**
47  * Returns a RenderAttrib that corresponds to whatever the standard default
48  * properties for render attributes of this type ought to be.
49  */
50 CPT(RenderAttrib) MaterialAttrib::
51 make_default() {
52  return return_new(new MaterialAttrib);
53 }
54 
55 /**
56  *
57  */
58 void MaterialAttrib::
59 output(std::ostream &out) const {
60  out << get_type() << ":";
61  if (_material != nullptr) {
62  out << *_material;
63  } else if (is_off()) {
64  out << "(off)";
65  }
66 }
67 
68 /**
69  * Intended to be overridden by derived MaterialAttrib types to return a
70  * unique number indicating whether this MaterialAttrib is equivalent to the
71  * other one.
72  *
73  * This should return 0 if the two MaterialAttrib objects are equivalent, a
74  * number less than zero if this one should be sorted before the other one,
75  * and a number greater than zero otherwise.
76  *
77  * This will only be called with two MaterialAttrib objects whose get_type()
78  * functions return the same.
79  */
80 int MaterialAttrib::
81 compare_to_impl(const RenderAttrib *other) const {
82  const MaterialAttrib *ta = (const MaterialAttrib *)other;
83 
84  // Comparing pointers by subtraction is problematic. Instead of doing this,
85  // we'll just depend on the built-in != and < operators for comparing
86  // pointers.
87  if (_material != ta->_material) {
88  return _material < ta->_material ? -1 : 1;
89  }
90  return 0;
91 }
92 
93 /**
94  * Intended to be overridden by derived RenderAttrib types to return a unique
95  * hash for these particular properties. RenderAttribs that compare the same
96  * with compare_to_impl(), above, should return the same hash; RenderAttribs
97  * that compare differently should return a different hash.
98  */
99 size_t MaterialAttrib::
100 get_hash_impl() const {
101  return pointer_hash::add_hash(0, _material);
102 }
103 
104 /**
105  * Tells the BamReader how to create objects of type MaterialAttrib.
106  */
109  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
110 }
111 
112 /**
113  * Writes the contents of this object to the datagram for shipping out to a
114  * Bam file.
115  */
117 write_datagram(BamWriter *manager, Datagram &dg) {
118  RenderAttrib::write_datagram(manager, dg);
119 
120  manager->write_pointer(dg, _material);
121 }
122 
123 /**
124  * Receives an array of pointers, one for each time manager->read_pointer()
125  * was called in fillin(). Returns the number of pointers processed.
126  */
128 complete_pointers(TypedWritable **p_list, BamReader *manager) {
129  int pi = RenderAttrib::complete_pointers(p_list, manager);
130 
131  TypedWritable *material = p_list[pi++];
132  _material = DCAST(Material, material);
133 
134  return pi;
135 }
136 
137 /**
138  * This function is called by the BamReader's factory when a new object of
139  * type MaterialAttrib is encountered in the Bam file. It should create the
140  * MaterialAttrib and extract its information from the file.
141  */
142 TypedWritable *MaterialAttrib::
143 make_from_bam(const FactoryParams &params) {
144  MaterialAttrib *attrib = new MaterialAttrib;
145  DatagramIterator scan;
146  BamReader *manager;
147 
148  parse_params(params, scan, manager);
149  attrib->fillin(scan, manager);
150  return attrib;
151 }
152 
153 /**
154  * This internal function is called by make_from_bam to read in all of the
155  * relevant data from the BamFile for the new MaterialAttrib.
156  */
157 void MaterialAttrib::
158 fillin(DatagramIterator &scan, BamReader *manager) {
159  RenderAttrib::fillin(scan, manager);
160 
161  // Read the _material pointer.
162  manager->read_pointer(scan);
163 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:610
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:317
A class to retrieve the individual data elements previously stored in a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
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
Indicates which, if any, material should be applied to geometry.
bool is_off() const
Returns true if the MaterialAttrib is an 'off' MaterialAttrib, indicating that it should disable the ...
static void register_with_read_factory()
Tells the BamReader how to create objects of type MaterialAttrib.
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 **plist, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
Defines the way an object appears in the presence of lighting.
Definition: material.h:43
void set_attrib_lock()
Definition: material.I:337
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
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().
static size_t add_hash(size_t start, const void *key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:110
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
CPT(RenderAttrib) MaterialAttrib
Constructs a new MaterialAttrib object suitable for rendering the indicated material onto geometry.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.