Panda3D
fltLightSourceDefinition.cxx
1 // Filename: fltLightSourceDefinition.cxx
2 // Created by: drose (26Aug00)
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 "fltLightSourceDefinition.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 
19 TypeHandle FltLightSourceDefinition::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: FltLightSourceDefinition::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 FltLightSourceDefinition::
27 FltLightSourceDefinition(FltHeader *header) : FltRecord(header) {
28  _light_index = 0;
29  _ambient.set(0.0, 0.0, 0.0, 1.0);
30  _diffuse.set(1.0, 1.0, 1.0, 1.0);
31  _specular.set(0.0, 0.0, 0.0, 1.0);
32  _light_type = LT_infinite;
33  _exponential_dropoff = 1.0;
34  _cutoff_angle = 180.0;
35  _yaw = 0.0;
36  _pitch = 0.0;
37  _constant_coefficient = 0.0;
38  _linear_coefficient = 0.0;
39  _quadratic_coefficient = 1.0;
40  _modeling_light = false;
41 }
42 
43 ////////////////////////////////////////////////////////////////////
44 // Function: FltLightSourceDefinition::extract_record
45 // Access: Protected, Virtual
46 // Description: Fills in the information in this record based on the
47 // information given in the indicated datagram, whose
48 // opcode has already been read. Returns true on
49 // success, false if the datagram is invalid.
50 ////////////////////////////////////////////////////////////////////
51 bool FltLightSourceDefinition::
52 extract_record(FltRecordReader &reader) {
53  if (!FltRecord::extract_record(reader)) {
54  return false;
55  }
56 
57  nassertr(reader.get_opcode() == FO_light_definition, false);
58  DatagramIterator &iterator = reader.get_iterator();
59 
60  _light_index = iterator.get_be_int32();
61  iterator.skip_bytes(2*4);
62  _light_name = iterator.get_fixed_string(20);
63  iterator.skip_bytes(4);
64  _ambient[0] = iterator.get_be_float32();
65  _ambient[1] = iterator.get_be_float32();
66  _ambient[2] = iterator.get_be_float32();
67  _ambient[3] = iterator.get_be_float32();
68  _diffuse[0] = iterator.get_be_float32();
69  _diffuse[1] = iterator.get_be_float32();
70  _diffuse[2] = iterator.get_be_float32();
71  _diffuse[3] = iterator.get_be_float32();
72  _specular[0] = iterator.get_be_float32();
73  _specular[1] = iterator.get_be_float32();
74  _specular[2] = iterator.get_be_float32();
75  _specular[3] = iterator.get_be_float32();
76  _light_type = (LightType)iterator.get_be_int32();
77  iterator.skip_bytes(4*10);
78  _exponential_dropoff = iterator.get_be_float32();
79  _cutoff_angle = iterator.get_be_float32();
80  _yaw = iterator.get_be_float32();
81  _pitch = iterator.get_be_float32();
82  _constant_coefficient = iterator.get_be_float32();
83  _linear_coefficient = iterator.get_be_float32();
84  _quadratic_coefficient = iterator.get_be_float32();
85  _modeling_light = (iterator.get_be_int32() != 0);
86  iterator.skip_bytes(4*19);
87 
88  check_remaining_size(iterator);
89  return true;
90 }
91 
92 ////////////////////////////////////////////////////////////////////
93 // Function: FltLightSourceDefinition::build_record
94 // Access: Protected, Virtual
95 // Description: Fills up the current record on the FltRecordWriter with
96 // data for this record, but does not advance the
97 // writer. Returns true on success, false if there is
98 // some error.
99 ////////////////////////////////////////////////////////////////////
100 bool FltLightSourceDefinition::
101 build_record(FltRecordWriter &writer) const {
102  if (!FltRecord::build_record(writer)) {
103  return false;
104  }
105 
106  writer.set_opcode(FO_light_definition);
107  Datagram &datagram = writer.update_datagram();
108 
109  datagram.add_be_int32(_light_index);
110  datagram.pad_bytes(2*4);
111  datagram.add_fixed_string(_light_name, 20);
112  datagram.pad_bytes(4);
113  datagram.add_be_float32(_ambient[0]);
114  datagram.add_be_float32(_ambient[1]);
115  datagram.add_be_float32(_ambient[2]);
116  datagram.add_be_float32(_ambient[3]);
117  datagram.add_be_float32(_diffuse[0]);
118  datagram.add_be_float32(_diffuse[1]);
119  datagram.add_be_float32(_diffuse[2]);
120  datagram.add_be_float32(_diffuse[3]);
121  datagram.add_be_float32(_specular[0]);
122  datagram.add_be_float32(_specular[1]);
123  datagram.add_be_float32(_specular[2]);
124  datagram.add_be_float32(_specular[3]);
125  datagram.add_be_int32(_light_type);
126  datagram.pad_bytes(4*10);
127  datagram.add_be_float32(_exponential_dropoff);
128  datagram.add_be_float32(_cutoff_angle);
129  datagram.add_be_float32(_yaw);
130  datagram.add_be_float32(_pitch);
131  datagram.add_be_float32(_constant_coefficient);
132  datagram.add_be_float32(_linear_coefficient);
133  datagram.add_be_float32(_quadratic_coefficient);
134  datagram.add_be_int32(_modeling_light);
135  datagram.pad_bytes(4*19);
136 
137  return true;
138 }
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly...
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
void check_remaining_size(const DatagramIterator &di, const string &name=string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record...
Definition: fltRecord.cxx:313
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition: datagram.cxx:111
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:48
void add_be_float32(PN_float32 value)
Adds a 32-bit single-precision big-endian floating-point number to the datagram.
Definition: datagram.I:327
The base class for all kinds of records in a MultiGen OpenFlight file.
Definition: fltRecord.h:40
void add_be_int32(PN_int32 value)
Adds a signed 32-bit big-endian integer to the datagram.
Definition: datagram.I:267
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
void add_fixed_string(const string &str, size_t size)
Adds a fixed-length string to the datagram.
Definition: datagram.I:404
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
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
PN_int32 get_be_int32()
Extracts a signed 32-bit big-endian integer.