Panda3D
 All Classes Functions Variables Enumerations
shadeModelAttrib.cxx
1 // Filename: shadeModelAttrib.cxx
2 // Created by: drose (14Mar05)
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 "shadeModelAttrib.h"
16 #include "graphicsStateGuardianBase.h"
17 #include "dcast.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 #include "datagram.h"
21 #include "datagramIterator.h"
22 
23 TypeHandle ShadeModelAttrib::_type_handle;
24 int ShadeModelAttrib::_attrib_slot;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: ShadeModelAttrib::make
28 // Access: Published, Static
29 // Description: Constructs a new ShadeModelAttrib object that specifies
30 // whether to draw polygons with flat shading or with
31 // per-vertex (smooth) shading.
32 ////////////////////////////////////////////////////////////////////
34 make(ShadeModelAttrib::Mode mode) {
35  ShadeModelAttrib *attrib = new ShadeModelAttrib(mode);
36  return return_new(attrib);
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: ShadeModelAttrib::make_default
41 // Access: Published, Static
42 // Description: Returns a RenderAttrib that corresponds to whatever
43 // the standard default properties for render attributes
44 // of this type ought to be.
45 ////////////////////////////////////////////////////////////////////
47 make_default() {
48  return return_new(new ShadeModelAttrib(M_smooth));
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: ShadeModelAttrib::output
53 // Access: Public, Virtual
54 // Description:
55 ////////////////////////////////////////////////////////////////////
56 void ShadeModelAttrib::
57 output(ostream &out) const {
58  out << get_type() << ":";
59  switch (get_mode()) {
60  case M_flat:
61  out << "flat";
62  break;
63 
64  case M_smooth:
65  out << "smooth";
66  break;
67  }
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: ShadeModelAttrib::compare_to_impl
72 // Access: Protected, Virtual
73 // Description: Intended to be overridden by derived ShadeModelAttrib
74 // types to return a unique number indicating whether
75 // this ShadeModelAttrib is equivalent to the other one.
76 //
77 // This should return 0 if the two ShadeModelAttrib objects
78 // are equivalent, a number less than zero if this one
79 // should be sorted before the other one, and a number
80 // greater than zero otherwise.
81 //
82 // This will only be called with two ShadeModelAttrib
83 // objects whose get_type() functions return the same.
84 ////////////////////////////////////////////////////////////////////
85 int ShadeModelAttrib::
86 compare_to_impl(const RenderAttrib *other) const {
87  const ShadeModelAttrib *ta;
88  DCAST_INTO_R(ta, other, 0);
89  return (int)_mode - (int)ta->_mode;
90 }
91 
92 ////////////////////////////////////////////////////////////////////
93 // Function: ShadeModelAttrib::get_hash_impl
94 // Access: Protected, Virtual
95 // Description: Intended to be overridden by derived RenderAttrib
96 // types to return a unique hash for these particular
97 // properties. RenderAttribs that compare the same with
98 // compare_to_impl(), above, should return the same
99 // hash; RenderAttribs that compare differently should
100 // return a different hash.
101 ////////////////////////////////////////////////////////////////////
102 size_t ShadeModelAttrib::
103 get_hash_impl() const {
104  size_t hash = 0;
105  hash = int_hash::add_hash(hash, (int)_mode);
106  return hash;
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: ShadeModelAttrib::compose_impl
111 // Access: Protected, Virtual
112 // Description: Intended to be overridden by derived RenderAttrib
113 // types to specify how two consecutive RenderAttrib
114 // objects of the same type interact.
115 //
116 // This should return the result of applying the other
117 // RenderAttrib to a node in the scene graph below this
118 // RenderAttrib, which was already applied. In most
119 // cases, the result is the same as the other
120 // RenderAttrib (that is, a subsequent RenderAttrib
121 // completely replaces the preceding one). On the other
122 // hand, some kinds of RenderAttrib (for instance,
123 // ColorTransformAttrib) might combine in meaningful
124 // ways.
125 ////////////////////////////////////////////////////////////////////
127 compose_impl(const RenderAttrib *other) const {
128  const ShadeModelAttrib *ta;
129  DCAST_INTO_R(ta, other, 0);
130 
131  Mode mode = ta->get_mode();
132  return make(mode);
133 }
134 
135 ////////////////////////////////////////////////////////////////////
136 // Function: ShadeModelAttrib::register_with_read_factory
137 // Access: Public, Static
138 // Description: Tells the BamReader how to create objects of type
139 // ShadeModelAttrib.
140 ////////////////////////////////////////////////////////////////////
143  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: ShadeModelAttrib::write_datagram
148 // Access: Public, Virtual
149 // Description: Writes the contents of this object to the datagram
150 // for shipping out to a Bam file.
151 ////////////////////////////////////////////////////////////////////
154  RenderAttrib::write_datagram(manager, dg);
155 
156  dg.add_int8(_mode);
157 }
158 
159 ////////////////////////////////////////////////////////////////////
160 // Function: ShadeModelAttrib::make_from_bam
161 // Access: Protected, Static
162 // Description: This function is called by the BamReader's factory
163 // when a new object of type ShadeModelAttrib is encountered
164 // in the Bam file. It should create the ShadeModelAttrib
165 // and extract its information from the file.
166 ////////////////////////////////////////////////////////////////////
167 TypedWritable *ShadeModelAttrib::
168 make_from_bam(const FactoryParams &params) {
169  ShadeModelAttrib *attrib = new ShadeModelAttrib(M_smooth);
170  DatagramIterator scan;
171  BamReader *manager;
172 
173  parse_params(params, scan, manager);
174  attrib->fillin(scan, manager);
175 
176  return attrib;
177 }
178 
179 ////////////////////////////////////////////////////////////////////
180 // Function: ShadeModelAttrib::fillin
181 // Access: Protected
182 // Description: This internal function is called by make_from_bam to
183 // read in all of the relevant data from the BamFile for
184 // the new ShadeModelAttrib.
185 ////////////////////////////////////////////////////////////////////
186 void ShadeModelAttrib::
187 fillin(DatagramIterator &scan, BamReader *manager) {
188  RenderAttrib::fillin(scan, manager);
189 
190  _mode = (Mode)scan.get_int8();
191 }
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
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
Specifies whether flat shading (per-polygon) or smooth shading (per-vertex) is in effect...
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
Mode get_mode() const
Returns the shade mode.
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
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type ShadeModelAttrib.
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