Panda3D
fogAttrib.cxx
1 // Filename: fogAttrib.cxx
2 // Created by: drose (14Mar02)
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 "fogAttrib.h"
16 #include "graphicsStateGuardianBase.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle FogAttrib::_type_handle;
23 int FogAttrib::_attrib_slot;
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: FogAttrib::make
27 // Access: Published, Static
28 // Description: Constructs a new FogAttrib object suitable for
29 // rendering the indicated fog onto geometry.
30 ////////////////////////////////////////////////////////////////////
31 CPT(RenderAttrib) FogAttrib::
32 make(Fog *fog) {
33  FogAttrib *attrib = new FogAttrib;
34  attrib->_fog = fog;
35  return return_new(attrib);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: FogAttrib::make_default
40 // Access: Published, Static
41 // Description: Returns a RenderAttrib that corresponds to whatever
42 // the standard default properties for render attributes
43 // of this type ought to be.
44 ////////////////////////////////////////////////////////////////////
45 CPT(RenderAttrib) FogAttrib::
46 make_default() {
47  return return_new(new FogAttrib);
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: FogAttrib::make_off
52 // Access: Published, Static
53 // Description: Constructs a new FogAttrib object suitable for
54 // rendering unfogd geometry.
55 ////////////////////////////////////////////////////////////////////
56 CPT(RenderAttrib) FogAttrib::
57 make_off() {
58  FogAttrib *attrib = new FogAttrib;
59  return return_new(attrib);
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: FogAttrib::output
64 // Access: Public, Virtual
65 // Description:
66 ////////////////////////////////////////////////////////////////////
67 void FogAttrib::
68 output(ostream &out) const {
69  out << get_type() << ":";
70  if (is_off()) {
71  out << "(off)";
72  } else {
73  out << *_fog;
74  }
75 }
76 
77 ////////////////////////////////////////////////////////////////////
78 // Function: FogAttrib::compare_to_impl
79 // Access: Protected, Virtual
80 // Description: Intended to be overridden by derived FogAttrib
81 // types to return a unique number indicating whether
82 // this FogAttrib is equivalent to the other one.
83 //
84 // This should return 0 if the two FogAttrib objects
85 // are equivalent, a number less than zero if this one
86 // should be sorted before the other one, and a number
87 // greater than zero otherwise.
88 //
89 // This will only be called with two FogAttrib
90 // objects whose get_type() functions return the same.
91 ////////////////////////////////////////////////////////////////////
92 int FogAttrib::
93 compare_to_impl(const RenderAttrib *other) const {
94  const FogAttrib *ta;
95  DCAST_INTO_R(ta, other, 0);
96 
97  // Comparing pointers by subtraction is problematic. Instead of
98  // doing this, we'll just depend on the built-in != and < operators
99  // for comparing pointers.
100  if (_fog != ta->_fog) {
101  return _fog < ta->_fog ? -1 : 1;
102  }
103  return 0;
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: FogAttrib::get_hash_impl
108 // Access: Protected, Virtual
109 // Description: Intended to be overridden by derived RenderAttrib
110 // types to return a unique hash for these particular
111 // properties. RenderAttribs that compare the same with
112 // compare_to_impl(), above, should return the same
113 // hash; RenderAttribs that compare differently should
114 // return a different hash.
115 ////////////////////////////////////////////////////////////////////
116 size_t FogAttrib::
117 get_hash_impl() const {
118  size_t hash = 0;
119  hash = pointer_hash::add_hash(hash, _fog);
120  return hash;
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: FogAttrib::get_auto_shader_attrib_impl
125 // Access: Protected, Virtual
126 // Description:
127 ////////////////////////////////////////////////////////////////////
128 CPT(RenderAttrib) FogAttrib::
129 get_auto_shader_attrib_impl(const RenderState *state) const {
130  return this;
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: FogAttrib::register_with_read_factory
135 // Access: Public, Static
136 // Description: Tells the BamReader how to create objects of type
137 // FogAttrib.
138 ////////////////////////////////////////////////////////////////////
139 void FogAttrib::
140 register_with_read_factory() {
141  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: FogAttrib::write_datagram
146 // Access: Public, Virtual
147 // Description: Writes the contents of this object to the datagram
148 // for shipping out to a Bam file.
149 ////////////////////////////////////////////////////////////////////
150 void FogAttrib::
152  RenderAttrib::write_datagram(manager, dg);
153 
154  manager->write_pointer(dg, _fog);
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: FogAttrib::complete_pointers
159 // Access: Public, Virtual
160 // Description: Receives an array of pointers, one for each time
161 // manager->read_pointer() was called in fillin().
162 // Returns the number of pointers processed.
163 ////////////////////////////////////////////////////////////////////
164 int FogAttrib::
166  int pi = RenderAttrib::complete_pointers(p_list, manager);
167 
168  TypedWritable *fog = p_list[pi++];
169  if (fog != (TypedWritable *)NULL) {
170  _fog = DCAST(Fog, fog);
171  }
172 
173  return pi;
174 }
175 
176 ////////////////////////////////////////////////////////////////////
177 // Function: FogAttrib::make_from_bam
178 // Access: Protected, Static
179 // Description: This function is called by the BamReader's factory
180 // when a new object of type FogAttrib is encountered
181 // in the Bam file. It should create the FogAttrib
182 // and extract its information from the file.
183 ////////////////////////////////////////////////////////////////////
184 TypedWritable *FogAttrib::
185 make_from_bam(const FactoryParams &params) {
186  FogAttrib *attrib = new FogAttrib;
187  DatagramIterator scan;
188  BamReader *manager;
189 
190  parse_params(params, scan, manager);
191  attrib->fillin(scan, manager);
192 
193  return attrib;
194 }
195 
196 ////////////////////////////////////////////////////////////////////
197 // Function: FogAttrib::fillin
198 // Access: Protected
199 // Description: This internal function is called by make_from_bam to
200 // read in all of the relevant data from the BamFile for
201 // the new FogAttrib.
202 ////////////////////////////////////////////////////////////////////
203 void FogAttrib::
204 fillin(DatagramIterator &scan, BamReader *manager) {
205  RenderAttrib::fillin(scan, manager);
206 
207  // Read the _fog pointer.
208  manager->read_pointer(scan);
209 }
static size_t add_hash(size_t start, const void *key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:133
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
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
Applies a Fog to the geometry at and below this node.
Definition: fogAttrib.h:27
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()...
Definition: fogAttrib.cxx:165
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()...
Specifies how atmospheric fog effects are applied to geometry.
Definition: fog.h:46
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
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
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
bool is_off() const
Returns true if the FogAttrib is an &#39;off&#39; FogAttrib, indicating that it should disable fog...
Definition: fogAttrib.I:33
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: fogAttrib.cxx:151
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
Definition: bamWriter.cxx:279
void read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
Definition: bamReader.cxx:658