Panda3D
rescaleNormalAttrib.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 rescaleNormalAttrib.cxx
10  * @author drose
11  * @date 2004-12-30
12  */
13 
14 #include "rescaleNormalAttrib.h"
16 #include "string_utils.h"
17 #include "dcast.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 #include "datagram.h"
21 #include "datagramIterator.h"
22 #include "configVariableEnum.h"
23 #include "config_pgraph.h"
24 
25 using std::istream;
26 using std::ostream;
27 using std::string;
28 
29 TypeHandle RescaleNormalAttrib::_type_handle;
30 int RescaleNormalAttrib::_attrib_slot;
31 CPT(RenderAttrib) RescaleNormalAttrib::_attribs[RescaleNormalAttrib::M_auto + 1];
32 
33 /**
34  * Constructs a new RescaleNormalAttrib object that specifies whether to
35  * rescale normals to compensate for transform scales or incorrectly defined
36  * normals.
37  */
38 CPT(RenderAttrib) RescaleNormalAttrib::
39 make(RescaleNormalAttrib::Mode mode) {
40  if (_attribs[mode].is_null()) {
41  // Don't bother with return_new, since this is the only way a
42  // RescaleNormalAttrib can be made anyway.
43  _attribs[mode] = new RescaleNormalAttrib(mode);
44  }
45  return _attribs[mode];
46 }
47 
48 /**
49  *
50  */
51 void RescaleNormalAttrib::
52 output(ostream &out) const {
53  out << get_type() << ":" << get_mode();
54 }
55 
56 /**
57  * Intended to be overridden by derived RescaleNormalAttrib types to return a
58  * unique number indicating whether this RescaleNormalAttrib is equivalent to
59  * the other one.
60  *
61  * This should return 0 if the two RescaleNormalAttrib objects are equivalent,
62  * a number less than zero if this one should be sorted before the other one,
63  * and a number greater than zero otherwise.
64  *
65  * This will only be called with two RescaleNormalAttrib objects whose
66  * get_type() functions return the same.
67  */
68 int RescaleNormalAttrib::
69 compare_to_impl(const RenderAttrib *other) const {
70  const RescaleNormalAttrib *ta = (const RescaleNormalAttrib *)other;
71  return (int)_mode - (int)ta->_mode;
72 }
73 
74 /**
75  * Intended to be overridden by derived RenderAttrib types to return a unique
76  * hash for these particular properties. RenderAttribs that compare the same
77  * with compare_to_impl(), above, should return the same hash; RenderAttribs
78  * that compare differently should return a different hash.
79  */
80 size_t RescaleNormalAttrib::
81 get_hash_impl() const {
82  size_t hash = 0;
83  hash = int_hash::add_hash(hash, (int)_mode);
84  return hash;
85 }
86 
87 /**
88  * Tells the BamReader how to create objects of type RescaleNormalAttrib.
89  */
92  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
93 }
94 
95 /**
96  * Writes the contents of this object to the datagram for shipping out to a
97  * Bam file.
98  */
101  RenderAttrib::write_datagram(manager, dg);
102 
103  dg.add_int8(_mode);
104 }
105 
106 /**
107  * This function is called by the BamReader's factory when a new object of
108  * type RescaleNormalAttrib is encountered in the Bam file. It should create
109  * the RescaleNormalAttrib and extract its information from the file.
110  */
111 TypedWritable *RescaleNormalAttrib::
112 make_from_bam(const FactoryParams &params) {
113  RescaleNormalAttrib *attrib = new RescaleNormalAttrib(M_none);
114  DatagramIterator scan;
115  BamReader *manager;
116 
117  parse_params(params, scan, manager);
118  attrib->fillin(scan, manager);
119 
120  return attrib;
121 }
122 
123 /**
124  * This internal function is called by make_from_bam to read in all of the
125  * relevant data from the BamFile for the new RescaleNormalAttrib.
126  */
127 void RescaleNormalAttrib::
128 fillin(DatagramIterator &scan, BamReader *manager) {
129  RenderAttrib::fillin(scan, manager);
130 
131  _mode = (Mode)scan.get_int8();
132 }
133 
134 /**
135  *
136  */
137 void RescaleNormalAttrib::
138 init_type() {
139  RenderAttrib::init_type();
140  register_type(_type_handle, "RescaleNormalAttrib",
141  RenderAttrib::get_class_type());
142 
143  // This is defined here, since we have otherwise no guarantee that the
144  // config var has already been constructed by the time we call init_type()
145  // at static init time.
146  static ConfigVariableEnum<RescaleNormalAttrib::Mode> rescale_normals
147  ("rescale-normals", RescaleNormalAttrib::M_auto,
148  PRC_DESC("Specifies the kind of RescaleNormalAttrib that should be "
149  "created for the top of the scene graph. This can automatically "
150  "ensure that your lighting normals are unit-length, which may be "
151  "particularly necessary in the presence of scales in the scene "
152  "graph. Turning it off ('none') may produce a small performance "
153  "benefit."));
154 
155  Mode mode = rescale_normals;
156  RescaleNormalAttrib *attrib = new RescaleNormalAttrib(mode);
157  _attrib_slot = register_slot(_type_handle, 100, attrib);
158  _attribs[mode] = attrib;
159 }
160 
161 /**
162  *
163  */
164 ostream &
165 operator << (ostream &out, RescaleNormalAttrib::Mode mode) {
166  switch (mode) {
167  case RescaleNormalAttrib::M_none:
168  return out << "none";
169 
170  case RescaleNormalAttrib::M_rescale:
171  return out << "rescale";
172 
173  case RescaleNormalAttrib::M_normalize:
174  return out << "normalize";
175 
176  case RescaleNormalAttrib::M_auto:
177  return out << "auto";
178  }
179 
180  return out << "(**invalid RescaleNormalAttrib::Mode(" << (int)mode << ")**)";
181 }
182 
183 /**
184  *
185  */
186 istream &
187 operator >> (istream &in, RescaleNormalAttrib::Mode &mode) {
188  string word;
189  in >> word;
190 
191  if (cmp_nocase(word, "none") == 0) {
192  mode = RescaleNormalAttrib::M_none;
193 
194  } else if (cmp_nocase(word, "rescale") == 0) {
195  mode = RescaleNormalAttrib::M_rescale;
196 
197  } else if (cmp_nocase(word, "normalize") == 0) {
198  mode = RescaleNormalAttrib::M_normalize;
199 
200  } else if (cmp_nocase(word, "auto") == 0) {
201  mode = RescaleNormalAttrib::M_auto;
202 
203  } else {
204  pgraph_cat.error()
205  << "Invalid RescaleNormalAttrib::Mode value: " << word << "\n";
206  mode = RescaleNormalAttrib::M_none;
207  }
208 
209  return in;
210 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
get_mode
Returns the render mode.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
CPT(RenderAttrib) RescaleNormalAttrib CPT(RenderAttrib) RescaleNormalAttrib
Constructs a new RescaleNormalAttrib object that specifies whether to rescale normals to compensate f...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:42
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 void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static int register_slot(TypeHandle type_handle, int sort, RenderAttrib *default_attrib)
Adds the indicated TypeHandle to the registry, if it is not there already, and returns a unique slot ...
Definition: renderAttrib.I:101
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
This class specializes ConfigVariable as an enumerated type.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
Specifies how polygons are to be drawn.
A class to retrieve the individual data elements previously stored in a Datagram.
int8_t get_int8()
Extracts a signed 8-bit integer.
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type RescaleNormalAttrib.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.