Panda3D
 All Classes Functions Variables Enumerations
fltMaterial.cxx
1 // Filename: fltMaterial.cxx
2 // Created by: drose (25Aug00)
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 "fltMaterial.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 
19 TypeHandle FltMaterial::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: FltMaterial::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 FltMaterial::
27 FltMaterial(FltHeader *header) : FltRecord(header) {
28  _material_index = -1;
29  _flags = 0;
30  _ambient.set(0.0, 0.0, 0.0);
31  _diffuse.set(0.0, 0.0, 0.0);
32  _specular.set(0.0, 0.0, 0.0);
33  _emissive.set(0.0, 0.0, 0.0);
34  _shininess = 0.0;
35  _alpha = 1.0;
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: FltMaterial::extract_record
40 // Access: Protected, Virtual
41 // Description: Fills in the information in this record based on the
42 // information given in the indicated datagram, whose
43 // opcode has already been read. Returns true on
44 // success, false if the datagram is invalid.
45 ////////////////////////////////////////////////////////////////////
46 bool FltMaterial::
47 extract_record(FltRecordReader &reader) {
48  if (!FltRecord::extract_record(reader)) {
49  return false;
50  }
51 
52  nassertr(reader.get_opcode() == FO_15_material, false);
53  DatagramIterator &iterator = reader.get_iterator();
54 
55  _material_index = iterator.get_be_int32();
56  _material_name = iterator.get_fixed_string(12);
57  _flags = iterator.get_be_uint32();
58  _ambient[0] = iterator.get_be_float32();
59  _ambient[1] = iterator.get_be_float32();
60  _ambient[2] = iterator.get_be_float32();
61  _diffuse[0] = iterator.get_be_float32();
62  _diffuse[1] = iterator.get_be_float32();
63  _diffuse[2] = iterator.get_be_float32();
64  _specular[0] = iterator.get_be_float32();
65  _specular[1] = iterator.get_be_float32();
66  _specular[2] = iterator.get_be_float32();
67  _emissive[0] = iterator.get_be_float32();
68  _emissive[1] = iterator.get_be_float32();
69  _emissive[2] = iterator.get_be_float32();
70  _shininess = iterator.get_be_float32();
71  _alpha = iterator.get_be_float32();
72  iterator.skip_bytes(4);
73 
74  check_remaining_size(iterator);
75  return true;
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: FltMaterial::build_record
80 // Access: Protected, Virtual
81 // Description: Fills up the current record on the FltRecordWriter with
82 // data for this record, but does not advance the
83 // writer. Returns true on success, false if there is
84 // some error.
85 ////////////////////////////////////////////////////////////////////
86 bool FltMaterial::
87 build_record(FltRecordWriter &writer) const {
88  if (!FltRecord::build_record(writer)) {
89  return false;
90  }
91 
92  writer.set_opcode(FO_15_material);
93  Datagram &datagram = writer.update_datagram();
94 
95  datagram.add_be_int32(_material_index);
96  datagram.add_fixed_string(_material_name, 12);
97  datagram.add_be_uint32(_flags);
98  datagram.add_be_float32(_ambient[0]);
99  datagram.add_be_float32(_ambient[1]);
100  datagram.add_be_float32(_ambient[2]);
101  datagram.add_be_float32(_diffuse[0]);
102  datagram.add_be_float32(_diffuse[1]);
103  datagram.add_be_float32(_diffuse[2]);
104  datagram.add_be_float32(_specular[0]);
105  datagram.add_be_float32(_specular[1]);
106  datagram.add_be_float32(_specular[2]);
107  datagram.add_be_float32(_emissive[0]);
108  datagram.add_be_float32(_emissive[1]);
109  datagram.add_be_float32(_emissive[2]);
110  datagram.add_be_float32(_shininess);
111  datagram.add_be_float32(_alpha);
112  datagram.pad_bytes(4);
113 
114  return true;
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: FltMaterial::extract_14_record
119 // Access: Public
120 // Description: Fills in the information in this record based on the
121 // information from the current position within the v14
122 // material palette. Leaves the iterator at the
123 // beginning of the next material.
124 ////////////////////////////////////////////////////////////////////
125 bool FltMaterial::
127  _material_index = index;
128 
129  _ambient[0] = di.get_be_float32();
130  _ambient[1] = di.get_be_float32();
131  _ambient[2] = di.get_be_float32();
132  _diffuse[0] = di.get_be_float32();
133  _diffuse[1] = di.get_be_float32();
134  _diffuse[2] = di.get_be_float32();
135  _specular[0] = di.get_be_float32();
136  _specular[1] = di.get_be_float32();
137  _specular[2] = di.get_be_float32();
138  _emissive[0] = di.get_be_float32();
139  _emissive[1] = di.get_be_float32();
140  _emissive[2] = di.get_be_float32();
141  _shininess = di.get_be_float32();
142  _alpha = di.get_be_float32();
143  _flags = di.get_be_uint32();
144  _material_name = di.get_fixed_string(12);
145  di.skip_bytes(4 * 28);
146 
147  return true;
148 }
149 
150 ////////////////////////////////////////////////////////////////////
151 // Function: FltMaterial::build_14_record
152 // Access: Public
153 // Description: Fills up the current record on the FltRecordWriter
154 // with data for this record, formatted as a part of a
155 // v14 material palette. Returns true on success, false
156 // if there is some error.
157 ////////////////////////////////////////////////////////////////////
158 bool FltMaterial::
160  datagram.add_be_float32(_ambient[0]);
161  datagram.add_be_float32(_ambient[1]);
162  datagram.add_be_float32(_ambient[2]);
163  datagram.add_be_float32(_diffuse[0]);
164  datagram.add_be_float32(_diffuse[1]);
165  datagram.add_be_float32(_diffuse[2]);
166  datagram.add_be_float32(_specular[0]);
167  datagram.add_be_float32(_specular[1]);
168  datagram.add_be_float32(_specular[2]);
169  datagram.add_be_float32(_emissive[0]);
170  datagram.add_be_float32(_emissive[1]);
171  datagram.add_be_float32(_emissive[2]);
172  datagram.add_be_float32(_shininess);
173  datagram.add_be_float32(_alpha);
174  datagram.add_be_uint32(_flags);
175  datagram.add_fixed_string(_material_name, 12);
176  datagram.pad_bytes(4 * 28);
177 
178  return true;
179 }
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...
bool build_14_record(Datagram &datagram)
Fills up the current record on the FltRecordWriter with data for this record, formatted as a part of ...
PN_float32 get_be_float32()
Extracts a 32-bit big-endian single-precision floating-point number.
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
bool extract_14_record(int index, DatagramIterator &di)
Fills in the information in this record based on the information from the current position within the...
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
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
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
PN_uint32 get_be_uint32()
Extracts an unsigned 32-bit big-endian integer.
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
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. ...
void add_be_uint32(PN_uint32 value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition: datagram.I:303
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
string get_fixed_string(size_t size)
Extracts a fixed-length string.