Panda3D
fltMaterial.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 fltMaterial.cxx
10  * @author drose
11  * @date 2000-08-25
12  */
13 
14 #include "fltMaterial.h"
15 #include "fltRecordReader.h"
16 #include "fltRecordWriter.h"
17 
18 TypeHandle FltMaterial::_type_handle;
19 
20 /**
21  *
22  */
23 FltMaterial::
24 FltMaterial(FltHeader *header) : FltRecord(header) {
25  _material_index = -1;
26  _flags = 0;
27  _ambient.set(0.0, 0.0, 0.0);
28  _diffuse.set(0.0, 0.0, 0.0);
29  _specular.set(0.0, 0.0, 0.0);
30  _emissive.set(0.0, 0.0, 0.0);
31  _shininess = 0.0;
32  _alpha = 1.0;
33 }
34 
35 /**
36  * Fills in the information in this record based on the information given in
37  * the indicated datagram, whose opcode has already been read. Returns true
38  * on success, false if the datagram is invalid.
39  */
40 bool FltMaterial::
41 extract_record(FltRecordReader &reader) {
42  if (!FltRecord::extract_record(reader)) {
43  return false;
44  }
45 
46  nassertr(reader.get_opcode() == FO_15_material, false);
47  DatagramIterator &iterator = reader.get_iterator();
48 
49  _material_index = iterator.get_be_int32();
50  _material_name = iterator.get_fixed_string(12);
51  _flags = iterator.get_be_uint32();
52  _ambient[0] = iterator.get_be_float32();
53  _ambient[1] = iterator.get_be_float32();
54  _ambient[2] = iterator.get_be_float32();
55  _diffuse[0] = iterator.get_be_float32();
56  _diffuse[1] = iterator.get_be_float32();
57  _diffuse[2] = iterator.get_be_float32();
58  _specular[0] = iterator.get_be_float32();
59  _specular[1] = iterator.get_be_float32();
60  _specular[2] = iterator.get_be_float32();
61  _emissive[0] = iterator.get_be_float32();
62  _emissive[1] = iterator.get_be_float32();
63  _emissive[2] = iterator.get_be_float32();
64  _shininess = iterator.get_be_float32();
65  _alpha = iterator.get_be_float32();
66  iterator.skip_bytes(4);
67 
68  check_remaining_size(iterator);
69  return true;
70 }
71 
72 /**
73  * Fills up the current record on the FltRecordWriter with data for this
74  * record, but does not advance the writer. Returns true on success, false if
75  * there is some error.
76  */
77 bool FltMaterial::
78 build_record(FltRecordWriter &writer) const {
79  if (!FltRecord::build_record(writer)) {
80  return false;
81  }
82 
83  writer.set_opcode(FO_15_material);
84  Datagram &datagram = writer.update_datagram();
85 
86  datagram.add_be_int32(_material_index);
87  datagram.add_fixed_string(_material_name, 12);
88  datagram.add_be_uint32(_flags);
89  datagram.add_be_float32(_ambient[0]);
90  datagram.add_be_float32(_ambient[1]);
91  datagram.add_be_float32(_ambient[2]);
92  datagram.add_be_float32(_diffuse[0]);
93  datagram.add_be_float32(_diffuse[1]);
94  datagram.add_be_float32(_diffuse[2]);
95  datagram.add_be_float32(_specular[0]);
96  datagram.add_be_float32(_specular[1]);
97  datagram.add_be_float32(_specular[2]);
98  datagram.add_be_float32(_emissive[0]);
99  datagram.add_be_float32(_emissive[1]);
100  datagram.add_be_float32(_emissive[2]);
101  datagram.add_be_float32(_shininess);
102  datagram.add_be_float32(_alpha);
103  datagram.pad_bytes(4);
104 
105  return true;
106 }
107 
108 /**
109  * Fills in the information in this record based on the information from the
110  * current position within the v14 material palette. Leaves the iterator at
111  * the beginning of the next material.
112  */
113 bool FltMaterial::
115  _material_index = index;
116 
117  _ambient[0] = di.get_be_float32();
118  _ambient[1] = di.get_be_float32();
119  _ambient[2] = di.get_be_float32();
120  _diffuse[0] = di.get_be_float32();
121  _diffuse[1] = di.get_be_float32();
122  _diffuse[2] = di.get_be_float32();
123  _specular[0] = di.get_be_float32();
124  _specular[1] = di.get_be_float32();
125  _specular[2] = di.get_be_float32();
126  _emissive[0] = di.get_be_float32();
127  _emissive[1] = di.get_be_float32();
128  _emissive[2] = di.get_be_float32();
129  _shininess = di.get_be_float32();
130  _alpha = di.get_be_float32();
131  _flags = di.get_be_uint32();
132  _material_name = di.get_fixed_string(12);
133  di.skip_bytes(4 * 28);
134 
135  return true;
136 }
137 
138 /**
139  * Fills up the current record on the FltRecordWriter with data for this
140  * record, formatted as a part of a v14 material palette. Returns true on
141  * success, false if there is some error.
142  */
143 bool FltMaterial::
145  datagram.add_be_float32(_ambient[0]);
146  datagram.add_be_float32(_ambient[1]);
147  datagram.add_be_float32(_ambient[2]);
148  datagram.add_be_float32(_diffuse[0]);
149  datagram.add_be_float32(_diffuse[1]);
150  datagram.add_be_float32(_diffuse[2]);
151  datagram.add_be_float32(_specular[0]);
152  datagram.add_be_float32(_specular[1]);
153  datagram.add_be_float32(_specular[2]);
154  datagram.add_be_float32(_emissive[0]);
155  datagram.add_be_float32(_emissive[1]);
156  datagram.add_be_float32(_emissive[2]);
157  datagram.add_be_float32(_shininess);
158  datagram.add_be_float32(_alpha);
159  datagram.add_be_uint32(_flags);
160  datagram.add_fixed_string(_material_name, 12);
161  datagram.pad_bytes(4 * 28);
162 
163  return true;
164 }
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 ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void check_remaining_size(const DatagramIterator &di, const std::string &name=std::string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record.
Definition: fltRecord.cxx:254
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:99
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
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:200
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_be_uint32(uint32_t value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition: datagram.I:181
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
A class to retrieve the individual data elements previously stored in a Datagram.
void add_fixed_string(const std::string &str, size_t size)
Adds a fixed-length string to the datagram.
Definition: datagram.I:263
void add_be_int32(int32_t value)
Adds a signed 32-bit big-endian integer to the datagram.
Definition: datagram.I:154
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
uint32_t get_be_uint32()
Extracts an unsigned 32-bit big-endian integer.
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:38
std::string get_fixed_string(size_t size)
Extracts a fixed-length string.