Panda3D
Loading...
Searching...
No Matches
fogAttrib.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 fogAttrib.cxx
10 * @author drose
11 * @date 2002-03-14
12 */
13
14#include "fogAttrib.h"
16#include "bamReader.h"
17#include "bamWriter.h"
18#include "datagram.h"
19#include "datagramIterator.h"
20
21TypeHandle FogAttrib::_type_handle;
22int FogAttrib::_attrib_slot;
23
24/**
25 * Constructs a new FogAttrib object suitable for rendering the indicated fog
26 * onto geometry.
27 */
28CPT(RenderAttrib) FogAttrib::
29make(Fog *fog) {
30 FogAttrib *attrib = new FogAttrib;
31 attrib->_fog = fog;
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 */
39CPT(RenderAttrib) FogAttrib::
40make_default() {
41 return return_new(new FogAttrib);
42}
43
44/**
45 * Constructs a new FogAttrib object suitable for rendering unfogd geometry.
46 */
47CPT(RenderAttrib) FogAttrib::
48make_off() {
49 FogAttrib *attrib = new FogAttrib;
50 return return_new(attrib);
51}
52
53/**
54 *
55 */
56void FogAttrib::
57output(std::ostream &out) const {
58 out << get_type() << ":";
59 if (is_off()) {
60 out << "(off)";
61 } else {
62 out << *_fog;
63 }
64}
65
66/**
67 * Intended to be overridden by derived FogAttrib types to return a unique
68 * number indicating whether this FogAttrib is equivalent to the other one.
69 *
70 * This should return 0 if the two FogAttrib objects are equivalent, a number
71 * less than zero if this one should be sorted before the other one, and a
72 * number greater than zero otherwise.
73 *
74 * This will only be called with two FogAttrib objects whose get_type()
75 * functions return the same.
76 */
77int FogAttrib::
78compare_to_impl(const RenderAttrib *other) const {
79 const FogAttrib *ta = (const FogAttrib *)other;
80
81 // Comparing pointers by subtraction is problematic. Instead of doing this,
82 // we'll just depend on the built-in != and < operators for comparing
83 // pointers.
84 if (_fog != ta->_fog) {
85 return _fog < ta->_fog ? -1 : 1;
86 }
87 return 0;
88}
89
90/**
91 * Intended to be overridden by derived RenderAttrib types to return a unique
92 * hash for these particular properties. RenderAttribs that compare the same
93 * with compare_to_impl(), above, should return the same hash; RenderAttribs
94 * that compare differently should return a different hash.
95 */
96size_t FogAttrib::
97get_hash_impl() const {
98 size_t hash = 0;
99 hash = pointer_hash::add_hash(hash, _fog);
100 return hash;
101}
102
103/**
104 * Tells the BamReader how to create objects of type FogAttrib.
105 */
108 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
109}
110
111/**
112 * Writes the contents of this object to the datagram for shipping out to a
113 * Bam file.
114 */
116write_datagram(BamWriter *manager, Datagram &dg) {
117 RenderAttrib::write_datagram(manager, dg);
118
119 manager->write_pointer(dg, _fog);
120}
121
122/**
123 * Receives an array of pointers, one for each time manager->read_pointer()
124 * was called in fillin(). Returns the number of pointers processed.
125 */
127complete_pointers(TypedWritable **p_list, BamReader *manager) {
128 int pi = RenderAttrib::complete_pointers(p_list, manager);
129
130 TypedWritable *fog = p_list[pi++];
131 if (fog != nullptr) {
132 _fog = DCAST(Fog, fog);
133 }
134
135 return pi;
136}
137
138/**
139 * This function is called by the BamReader's factory when a new object of
140 * type FogAttrib is encountered in the Bam file. It should create the
141 * FogAttrib and extract its information from the file.
142 */
143TypedWritable *FogAttrib::
144make_from_bam(const FactoryParams &params) {
145 FogAttrib *attrib = new FogAttrib;
146 DatagramIterator scan;
147 BamReader *manager;
148
149 parse_params(params, scan, manager);
150 attrib->fillin(scan, manager);
151
152 return attrib;
153}
154
155/**
156 * This internal function is called by make_from_bam to read in all of the
157 * relevant data from the BamFile for the new FogAttrib.
158 */
159void FogAttrib::
160fillin(DatagramIterator &scan, BamReader *manager) {
161 RenderAttrib::fillin(scan, manager);
162
163 // Read the _fog pointer.
164 manager->read_pointer(scan);
165}
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
bool read_pointer(DatagramIterator &scan)
The interface for reading a pointer to another object from a Bam file.
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
void write_pointer(Datagram &packet, const TypedWritable *dest)
The interface for writing a pointer to another object to a Bam file.
A class to retrieve the individual data elements previously stored in a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
An instance of this class is passed to the Factory when requesting it to do its business and construc...
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
Applies a Fog to the geometry at and below this node.
Definition fogAttrib.h:25
bool is_off() const
Returns true if the FogAttrib is an 'off' FogAttrib, indicating that it should disable fog.
Definition fogAttrib.I:26
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 void register_with_read_factory()
Tells the BamReader how to create objects of type FogAttrib.
virtual int complete_pointers(TypedWritable **plist, 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:41
This is the base class for a number of render attributes (other than transform) that may be set on sc...
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.
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().
static size_t add_hash(size_t start, const void *key)
Adds the indicated key into a running hash.
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.