Panda3D
rescaleNormalAttrib.cxx
1 // Filename: rescaleNormalAttrib.cxx
2 // Created by: drose (30Dec04)
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 "rescaleNormalAttrib.h"
16 #include "graphicsStateGuardianBase.h"
17 #include "string_utils.h"
18 #include "dcast.h"
19 #include "bamReader.h"
20 #include "bamWriter.h"
21 #include "datagram.h"
22 #include "datagramIterator.h"
23 #include "configVariableEnum.h"
24 #include "config_pgraph.h"
25 
26 TypeHandle RescaleNormalAttrib::_type_handle;
27 int RescaleNormalAttrib::_attrib_slot;
28 
29 // This variable is defined here instead of in config_pgraph.cxx,
30 // because it depends on rescaleNormalAttrib.h having already been
31 // included.
33 ("rescale-normals", RescaleNormalAttrib::M_auto,
34  PRC_DESC("Specifies the kind of RescaleNormalAttrib that should be "
35  "created for the top of the scene graph. This can automatically "
36  "ensure that your lighting normals are unit-length, which may be "
37  "particularly necessary in the presence of scales in the scene "
38  "graph. Turning it off ('none') may produce a small performance "
39  "benefit."));
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: RescaleNormalAttrib::make
43 // Access: Published, Static
44 // Description: Constructs a new RescaleNormalAttrib object that
45 // specifies whether to rescale normals to compensate
46 // for transform scales or incorrectly defined normals.
47 ////////////////////////////////////////////////////////////////////
48 CPT(RenderAttrib) RescaleNormalAttrib::
49 make(RescaleNormalAttrib::Mode mode) {
50  RescaleNormalAttrib *attrib = new RescaleNormalAttrib(mode);
51  return return_new(attrib);
52 }
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function: RescaleNormalAttrib::make_default
56 // Access: Published, Static
57 // Description: Constructs a RescaleNoramlAttrib object that's
58 // suitable for putting at the top of a scene graph.
59 // This will contain whatever attrib was suggested by
60 // the user's rescale-normals Config variable.
61 ////////////////////////////////////////////////////////////////////
62 CPT(RenderAttrib) RescaleNormalAttrib::
63 make_default() {
64  RescaleNormalAttrib *attrib = new RescaleNormalAttrib(rescale_normals);
65  return return_new(attrib);
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: RescaleNormalAttrib::output
70 // Access: Public, Virtual
71 // Description:
72 ////////////////////////////////////////////////////////////////////
73 void RescaleNormalAttrib::
74 output(ostream &out) const {
75  out << get_type() << ":" << get_mode();
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: RescaleNormalAttrib::compare_to_impl
80 // Access: Protected, Virtual
81 // Description: Intended to be overridden by derived RescaleNormalAttrib
82 // types to return a unique number indicating whether
83 // this RescaleNormalAttrib is equivalent to the other one.
84 //
85 // This should return 0 if the two RescaleNormalAttrib 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 RescaleNormalAttrib
91 // objects whose get_type() functions return the same.
92 ////////////////////////////////////////////////////////////////////
93 int RescaleNormalAttrib::
94 compare_to_impl(const RenderAttrib *other) const {
95  const RescaleNormalAttrib *ta;
96  DCAST_INTO_R(ta, other, 0);
97  return (int)_mode - (int)ta->_mode;
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function: RescaleNormalAttrib::get_hash_impl
102 // Access: Protected, Virtual
103 // Description: Intended to be overridden by derived RenderAttrib
104 // types to return a unique hash for these particular
105 // properties. RenderAttribs that compare the same with
106 // compare_to_impl(), above, should return the same
107 // hash; RenderAttribs that compare differently should
108 // return a different hash.
109 ////////////////////////////////////////////////////////////////////
110 size_t RescaleNormalAttrib::
111 get_hash_impl() const {
112  size_t hash = 0;
113  hash = int_hash::add_hash(hash, (int)_mode);
114  return hash;
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: RescaleNormalAttrib::register_with_read_factory
119 // Access: Public, Static
120 // Description: Tells the BamReader how to create objects of type
121 // RescaleNormalAttrib.
122 ////////////////////////////////////////////////////////////////////
125  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: RescaleNormalAttrib::write_datagram
130 // Access: Public, Virtual
131 // Description: Writes the contents of this object to the datagram
132 // for shipping out to a Bam file.
133 ////////////////////////////////////////////////////////////////////
136  RenderAttrib::write_datagram(manager, dg);
137 
138  dg.add_int8(_mode);
139 }
140 
141 ////////////////////////////////////////////////////////////////////
142 // Function: RescaleNormalAttrib::make_from_bam
143 // Access: Protected, Static
144 // Description: This function is called by the BamReader's factory
145 // when a new object of type RescaleNormalAttrib is encountered
146 // in the Bam file. It should create the RescaleNormalAttrib
147 // and extract its information from the file.
148 ////////////////////////////////////////////////////////////////////
149 TypedWritable *RescaleNormalAttrib::
150 make_from_bam(const FactoryParams &params) {
151  RescaleNormalAttrib *attrib = new RescaleNormalAttrib(M_none);
152  DatagramIterator scan;
153  BamReader *manager;
154 
155  parse_params(params, scan, manager);
156  attrib->fillin(scan, manager);
157 
158  return attrib;
159 }
160 
161 ////////////////////////////////////////////////////////////////////
162 // Function: RescaleNormalAttrib::fillin
163 // Access: Protected
164 // Description: This internal function is called by make_from_bam to
165 // read in all of the relevant data from the BamFile for
166 // the new RescaleNormalAttrib.
167 ////////////////////////////////////////////////////////////////////
168 void RescaleNormalAttrib::
169 fillin(DatagramIterator &scan, BamReader *manager) {
170  RenderAttrib::fillin(scan, manager);
171 
172  _mode = (Mode)scan.get_int8();
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: RescaleNormalAttrib::Mode output operator
177 // Description:
178 ////////////////////////////////////////////////////////////////////
179 ostream &
180 operator << (ostream &out, RescaleNormalAttrib::Mode mode) {
181  switch (mode) {
182  case RescaleNormalAttrib::M_none:
183  return out << "none";
184 
185  case RescaleNormalAttrib::M_rescale:
186  return out << "rescale";
187 
188  case RescaleNormalAttrib::M_normalize:
189  return out << "normalize";
190 
191  case RescaleNormalAttrib::M_auto:
192  return out << "auto";
193  }
194 
195  return out << "(**invalid RescaleNormalAttrib::Mode(" << (int)mode << ")**)";
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: RescaleNormalAttrib::Mode input operator
200 // Description:
201 ////////////////////////////////////////////////////////////////////
202 istream &
203 operator >> (istream &in, RescaleNormalAttrib::Mode &mode) {
204  string word;
205  in >> word;
206 
207  if (cmp_nocase(word, "none") == 0) {
208  mode = RescaleNormalAttrib::M_none;
209 
210  } else if (cmp_nocase(word, "rescale") == 0) {
211  mode = RescaleNormalAttrib::M_rescale;
212 
213  } else if (cmp_nocase(word, "normalize") == 0) {
214  mode = RescaleNormalAttrib::M_normalize;
215 
216  } else if (cmp_nocase(word, "auto") == 0) {
217  mode = RescaleNormalAttrib::M_auto;
218 
219  } else {
220  pgraph_cat.error()
221  << "Invalid RescaleNormalAttrib::Mode value: " << word << "\n";
222  mode = RescaleNormalAttrib::M_none;
223  }
224 
225  return in;
226 }
PN_int8 get_int8()
Extracts a signed 8-bit integer.
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
void add_int8(PN_int8 value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:128
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
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:122
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.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This class specializes ConfigVariable as an enumerated type.
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
Specifies how polygons are to be drawn.
A class to retrieve the individual data elements previously stored in a Datagram. ...
Mode get_mode() const
Returns the render mode.
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type RescaleNormalAttrib.