Panda3D
Loading...
Searching...
No Matches
polylightNode.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 polylightNode.cxx
10 * @author sshodhan
11 * @date 2004-06-02
12 */
13
14#include "polylightNode.h"
15#include "config_pgraph.h"
16#include "nodePath.h"
17#include "clockObject.h"
18#include "bamReader.h"
19#include "bamWriter.h"
20#include "datagram.h"
21#include "datagramIterator.h"
22
23#include <time.h>
24#include <math.h>
25
26TypeHandle PolylightNode::_type_handle;
27
28/**
29 * Use PolylightNode() to construct a new PolylightNode object.
30 */
32PolylightNode(const std::string &name) :
33PandaNode(name)
34{
35 _enabled = true;
36 set_pos(0,0,0);
37 set_color(1,1,1);
38 _radius = 50;
39 set_attenuation(ALINEAR);
40 _a0 = 1.0;
41 _a1 = 0.1;
42 _a2 = 0.01;
43 _flickering = true;
44 set_flicker_type(FRANDOM);
45 _offset = -0.5;
46 _scale = 0.1;
47 _step_size = 0.1;
48 _sin_freq = 2.0;
49}
50
51/**
52 * Returns a newly-allocated Node that is a shallow copy of this one. It will
53 * be a different Node pointer, but its internal data may or may not be shared
54 * with that of the original Node.
55 */
57make_copy() const {
58 return new PolylightNode(*this);
59}
60
61/**
62 * Transforms the contents of this node by the indicated matrix, if it means
63 * anything to do so. For most kinds of nodes, this does nothing.
64 */
66xform(const LMatrix4 &mat) {
67 nassertv(!mat.is_nan());
68
69 if (mat.almost_equal(LMatrix4::ident_mat())) {
70 return;
71 }
72
73 _position = _position * mat;
74
75 // This is a little cheesy and fails miserably in the presence of a non-
76 // uniform scale.
77 LVector3 radius_v = LVector3(_radius, 0.0f, 0.0f) * mat;
78 _radius = length(radius_v);
79 mark_internal_bounds_stale();
80}
81
82/**
83 * If flickering is on, the do_poly_light function in PolylightNodeEffect will
84 * compute this light's color based on the variations applied in this function
85 * Variation can be sin or random Use offset, scale and step_size to tweak
86 * Future addition: custom function variations to flicker
87 */
88LColor PolylightNode::flicker() const {
89 PN_stdfloat r,g,b;
90
91 PN_stdfloat variation= 0.0;
92 LColor color = get_color(); //color = get_color_scenegraph();
93
94 r = color[0];
95 g = color[1];
96 b = color[2];
97
98 if (_flicker_type == FRANDOM) {
99 // srand((int)ClockObject::get_global_clock()->get_frame_time());
100 variation = (rand()%100); // a value between 0-99
101 variation /= 100.0;
102 if (polylight_info)
103 pgraph_cat.info() << "Random Variation: " << variation << std::endl;
104 } else if (_flicker_type == FSIN) {
106 variation = sinf(now*_sin_freq);
107 if (polylight_info)
108 pgraph_cat.info() << "Variation: " << variation << std::endl;
109 // can't use negative variation, so make it positive
110 if (variation < 0.0)
111 variation *= -1.0;
112 } else if (_flicker_type == FCUSTOM) {
113 // fixed point list of variation values coming soon... double index =
114 // (ClockObject::get_global_clock()->get_frame_time() % len(fixed_points))
115 // * ClockObject::get_global_clock()->get_dt(); index *= _speed;
116 /*if (!(int)index > len(fixed_points) {
117 variation = _fixed_points[(int)index];
118 variation += _offset;
119 variation *= _scale;
120 }*/
121 }
122
123 // variation += _offset; variation *= _scale;
124
125 // printf("Variation: %f\n",variation);
126 r += r * variation;
127 g += g * variation;
128 b += b * variation;
129
130 /* CLAMPING
131 if (fabs(r - color[0]) > 0.5 || fabs(g - color[1]) > 0.5 || fabs(b - color[2]) > 0.5) {
132 r = color[0];
133 g = color[1];
134 b = color[2];
135 }
136 */
137 pgraph_cat.debug() << "Color R:" << r << "; G:" << g << "; B:" << b << std::endl;
138 return LColor(r,g,b,1.0);
139}
140
141/**
142 * Returns a number less than zero if this PolylightNode sorts before the
143 * other one, greater than zero if it sorts after, or zero if they are
144 * equivalent.
145 *
146 * Two PolylightNodes are considered equivalent if they consist of exactly the
147 * same properties Otherwise, they are different; different PolylightNodes
148 * will be ranked in a consistent but undefined ordering; the ordering is
149 * useful only for placing the PolylightNodes in a sorted container like an
150 * STL set.
151 */
153compare_to(const PolylightNode &other) const {
154
155 if (_enabled != other._enabled) {
156 return _enabled ? 1 :-1;
157 }
158
159 if (_radius != other._radius) {
160 return _radius < other._radius ? -1 :1;
161 }
162 LVecBase3 position = get_pos();
163 LVecBase3 other_position = other.get_pos();
164 if (position != other_position) {
165 return position < other_position ? -1 :1;
166 }
167
168 LColor color = get_color();
169 LColor other_color = other.get_color();
170 if (color != other_color) {
171 return color < other_color ? -1 :1;
172 }
173
174 if (_attenuation_type != other._attenuation_type) {
175 return _attenuation_type < other._attenuation_type ? -1 :1;
176 }
177
178 if (_a0 != other._a0) {
179 return _a0 < other._a0 ? -1 :1;
180 }
181
182 if (_a1 != other._a1) {
183 return _a1 < other._a1 ? -1 :1;
184 }
185
186 if (_a2 != other._a2) {
187 return _a2 < other._a2 ? -1 :1;
188 }
189
190 if (_flickering != other._flickering) {
191 return _flickering ? 1 :-1;
192 }
193
194 if (_flicker_type != other._flicker_type) {
195 return _flicker_type < other._flicker_type ? -1 :1;
196 }
197
198 if (_offset != other._offset) {
199 return _offset < other._offset ? -1 :1;
200 }
201
202 if (_scale != other._scale) {
203 return _scale < other._scale ? -1 :1;
204 }
205
206 if (_step_size != other._step_size) {
207 return _step_size < other._step_size ? -1 :1;
208 }
209
210 if (_sin_freq != other._sin_freq) {
211 return _sin_freq < other._sin_freq ? -1 :1;
212 }
213
214
215 return 0;
216}
217
218
219
220
221/**
222 * Tells the BamReader how to create objects of type PolylightNode
223 */
226 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
227}
228
229/**
230 * Writes the contents of this object to the datagram for shipping out to a
231 * Bam file.
232 */
234write_datagram(BamWriter *manager, Datagram &dg) {
235 PandaNode::write_datagram(manager, dg);
236 _position.write_datagram(dg);
237 LRGBColor color;
238 color.set(_color[0], _color[1], _color[2]);
239 color.write_datagram(dg);
240 dg.add_stdfloat(_radius);
241}
242
243/**
244 * This function is called by the BamReader's factory when a new object of
245 * type CompassEffect is encountered in the Bam file. It should create the
246 * CompassEffect and extract its information from the file.
247 */
248TypedWritable *PolylightNode::
249make_from_bam(const FactoryParams &params) {
250 PolylightNode *light = new PolylightNode("");
251 DatagramIterator scan;
252 BamReader *manager;
253
254 parse_params(params, scan, manager);
255 light->fillin(scan, manager);
256
257 return light;
258}
259
260/**
261 * This internal function is called by make_from_bam to read in all of the
262 * relevant data from the BamFile for the new CompassEffect.
263 */
264void PolylightNode::
265fillin(DatagramIterator &scan, BamReader *manager) {
266 PandaNode::fillin(scan, manager);
267 _position.read_datagram(scan);
268 LRGBColor color;
269 color.read_datagram(scan);
270 set_color(color[0], color[1], color[2]);
271 _radius = scan.get_stdfloat();
272}
273
274
275
276/**
277 *
278 */
279void PolylightNode::
280output(std::ostream &out) const {
281 out << get_type() << ":";
282 // out << "Position: " << get_x() << " " << get_y() << " " << get_z() <<
283 // "\n"; out << "Color: " << get_r() << " " << get_g() << " " << get_b() <<
284 // "\n";
285 out << "Radius: " << get_radius() << "\n";
286}
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
get_frame_time
Returns the time in seconds as of the last time tick() was called (typically, this will be as of the ...
Definition clockObject.h:91
static ClockObject * get_global_clock()
Returns a pointer to the global ClockObject.
A class to retrieve the individual data elements previously stored in a Datagram.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition datagram.I:133
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
A basic node of the scene graph or data graph.
Definition pandaNode.h:65
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
A PolylightNode.
LPoint3 get_pos() const
Returns position as a LPoint3.
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
void set_pos(const LPoint3 &position)
Set this light's position.
PN_stdfloat get_radius() const
Get radius of the spherical light volume.
bool set_flicker_type(Flicker_Type type)
Flicker type can be FRANDOM or FSIN At a later point there might be a FCUSTOM Custom flicker will be ...
static void register_with_read_factory()
Tells the BamReader how to create objects of type PolylightNode.
LColor flicker() const
If flickering is on, the do_poly_light function in PolylightNodeEffect will compute this light's colo...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
LColor get_color() const
Returns the light's color as LColor.
PolylightNode(const std::string &name)
Use PolylightNode() to construct a new PolylightNode object.
bool set_attenuation(Attenuation_Type type)
Set ALINEAR or AQUADRATIC attenuation.
int compare_to(const PolylightNode &other) const
Returns a number less than zero if this PolylightNode sorts before the other one, greater than zero i...
void set_color(const LColor &color)
Set the light's color...
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this node by the indicated matrix, if it means anything to do so.
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.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.