Panda3D
shadeModelAttrib.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 shadeModelAttrib.cxx
10  * @author drose
11  * @date 2005-03-14
12  */
13 
14 #include "shadeModelAttrib.h"
16 #include "dcast.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle ShadeModelAttrib::_type_handle;
23 int ShadeModelAttrib::_attrib_slot;
24 
25 /**
26  * Constructs a new ShadeModelAttrib object that specifies whether to draw
27  * polygons with flat shading or with per-vertex (smooth) shading.
28  */
29 CPT(RenderAttrib) ShadeModelAttrib::
30 make(ShadeModelAttrib::Mode mode) {
31  ShadeModelAttrib *attrib = new ShadeModelAttrib(mode);
32  return return_new(attrib);
33 }
34 
35 /**
36  * Returns a RenderAttrib that corresponds to whatever the standard default
37  * properties for render attributes of this type ought to be.
38  */
39 CPT(RenderAttrib) ShadeModelAttrib::
40 make_default() {
41  return return_new(new ShadeModelAttrib(M_smooth));
42 }
43 
44 /**
45  *
46  */
47 void ShadeModelAttrib::
48 output(std::ostream &out) const {
49  out << get_type() << ":";
50  switch (get_mode()) {
51  case M_flat:
52  out << "flat";
53  break;
54 
55  case M_smooth:
56  out << "smooth";
57  break;
58  }
59 }
60 
61 /**
62  * Intended to be overridden by derived ShadeModelAttrib types to return a
63  * unique number indicating whether this ShadeModelAttrib is equivalent to the
64  * other one.
65  *
66  * This should return 0 if the two ShadeModelAttrib objects are equivalent, a
67  * number less than zero if this one should be sorted before the other one,
68  * and a number greater than zero otherwise.
69  *
70  * This will only be called with two ShadeModelAttrib objects whose get_type()
71  * functions return the same.
72  */
73 int ShadeModelAttrib::
74 compare_to_impl(const RenderAttrib *other) const {
75  const ShadeModelAttrib *ta = (const ShadeModelAttrib *)other;
76  return (int)_mode - (int)ta->_mode;
77 }
78 
79 /**
80  * Intended to be overridden by derived RenderAttrib types to return a unique
81  * hash for these particular properties. RenderAttribs that compare the same
82  * with compare_to_impl(), above, should return the same hash; RenderAttribs
83  * that compare differently should return a different hash.
84  */
85 size_t ShadeModelAttrib::
86 get_hash_impl() const {
87  size_t hash = 0;
88  hash = int_hash::add_hash(hash, (int)_mode);
89  return hash;
90 }
91 
92 /**
93  * Intended to be overridden by derived RenderAttrib types to specify how two
94  * consecutive RenderAttrib objects of the same type interact.
95  *
96  * This should return the result of applying the other RenderAttrib to a node
97  * in the scene graph below this RenderAttrib, which was already applied. In
98  * most cases, the result is the same as the other RenderAttrib (that is, a
99  * subsequent RenderAttrib completely replaces the preceding one). On the
100  * other hand, some kinds of RenderAttrib (for instance, ColorTransformAttrib)
101  * might combine in meaningful ways.
102  */
103 CPT(RenderAttrib) ShadeModelAttrib::
104 compose_impl(const RenderAttrib *other) const {
105  const ShadeModelAttrib *ta = (const ShadeModelAttrib *)other;
106 
107  Mode mode = ta->get_mode();
108  return make(mode);
109 }
110 
111 /**
112  * Tells the BamReader how to create objects of type ShadeModelAttrib.
113  */
114 void ShadeModelAttrib::
115 register_with_read_factory() {
116  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
117 }
118 
119 /**
120  * Writes the contents of this object to the datagram for shipping out to a
121  * Bam file.
122  */
124 write_datagram(BamWriter *manager, Datagram &dg) {
125  RenderAttrib::write_datagram(manager, dg);
126 
127  dg.add_int8(_mode);
128 }
129 
130 /**
131  * This function is called by the BamReader's factory when a new object of
132  * type ShadeModelAttrib is encountered in the Bam file. It should create the
133  * ShadeModelAttrib and extract its information from the file.
134  */
135 TypedWritable *ShadeModelAttrib::
136 make_from_bam(const FactoryParams &params) {
137  ShadeModelAttrib *attrib = new ShadeModelAttrib(M_smooth);
138  DatagramIterator scan;
139  BamReader *manager;
140 
141  parse_params(params, scan, manager);
142  attrib->fillin(scan, manager);
143 
144  return attrib;
145 }
146 
147 /**
148  * This internal function is called by make_from_bam to read in all of the
149  * relevant data from the BamFile for the new ShadeModelAttrib.
150  */
151 void ShadeModelAttrib::
152 fillin(DatagramIterator &scan, BamReader *manager) {
153  RenderAttrib::fillin(scan, manager);
154 
155  _mode = (Mode)scan.get_int8();
156 }
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
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
A class to retrieve the individual data elements previously stored in a Datagram.
int8_t get_int8()
Extracts a signed 8-bit integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:42
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
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.
Specifies whether flat shading (per-polygon) or smooth shading (per-vertex) is in effect.
get_mode
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.
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
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:101
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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) ShadeModelAttrib
Constructs a new ShadeModelAttrib object that specifies whether to draw polygons with flat shading or...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.