Panda3D
fltHeader.h
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 fltHeader.h
10  * @author drose
11  * @date 2000-08-24
12  */
13 
14 #ifndef FLTHEADER_H
15 #define FLTHEADER_H
16 
17 #include "pandatoolbase.h"
18 
19 #include "fltBeadID.h"
20 #include "fltVertex.h"
21 #include "fltMaterial.h"
22 #include "fltTexture.h"
24 #include "fltEyepoint.h"
25 #include "fltTrackplane.h"
26 #include "fltInstanceDefinition.h"
27 
28 #include "pathReplace.h"
29 #include "pointerTo.h"
30 #include "filename.h"
31 #include "dSearchPath.h"
32 #include "distanceUnit.h"
33 #include "pvector.h"
34 #include "pset.h"
35 #include "pmap.h"
36 
37 /**
38  * This is the first bead in the file, the top of the bead hierarchy, and the
39  * primary interface to reading and writing a Flt file. You always read a Flt
40  * file by creating a header and calling read_flt(), which fills in its
41  * children beads automatically; you write a Flt file by creating a header,
42  * adding its children, and calling write_flt().
43  */
44 class FltHeader : public FltBeadID {
45 public:
46  FltHeader(PathReplace *path_replace);
47 
48  virtual void apply_converted_filenames();
49 
50  void set_path_replace(PathReplace *path_replace);
52  const PathReplace *get_path_replace() const;
53  Filename convert_path(const Filename &orig_filename,
54  const DSearchPath &additional_path = DSearchPath());
55 
56  void set_flt_filename(const Filename &flt_filename);
57  const Filename &get_flt_filename() const;
58 
59  FltError read_flt(Filename filename);
60  FltError read_flt(std::istream &in);
61  FltError write_flt(Filename filename);
62  FltError write_flt(std::ostream &out);
63 
64  enum AttrUpdate {
65  AU_none,
66  AU_if_missing,
67  AU_always
68  };
69 
70  void set_auto_attr_update(AttrUpdate attr);
71  AttrUpdate get_auto_attr_update() const;
72 
73  enum Units {
74  U_meters = 0,
75  U_kilometers = 1,
76  U_feet = 4,
77  U_inches = 5,
78  U_nautical_miles = 8
79  };
80 
81  enum Flags {
82  F_save_vertex_normals = 0x80000000
83  };
84 
85  enum ProjectionType {
86  PT_flat_earth = 0,
87  PT_trapezoidal = 1,
88  PT_round_earth = 2,
89  PT_lambert = 3,
90  PT_utm = 4
91  };
92 
93  enum VertexStorageType {
94  VTS_double = 1
95  };
96 
97  enum DatabaseOrigin {
98  DO_open_flight = 100,
99  DO_dig = 200,
100  DO_es_ct6 = 300,
101  DO_psp = 400,
102  DO_ge_civ = 600,
103  DO_es_gdf = 700,
104  };
105 
106  enum EarthModel {
107  EM_wgs84 = 0,
108  EM_wgs72 = 1,
109  EM_bessel = 2,
110  EM_clarke_1866 = 3,
111  EM_nad27 = 4
112  };
113 
114  int _format_revision_level;
115  int _edit_revision_level;
116  std::string _last_revision;
117  int _next_group_id;
118  int _next_lod_id;
119  int _next_object_id;
120  int _next_face_id;
121  int _unit_multiplier;
122  Units _vertex_units;
123  bool _texwhite_new;
124  unsigned int _flags;
125  ProjectionType _projection_type;
126  int _next_dof_id;
127  VertexStorageType _vertex_storage_type;
128  DatabaseOrigin _database_origin;
129  double _sw_x, _sw_y;
130  double _delta_x, _delta_y;
131  int _next_sound_id;
132  int _next_path_id;
133  int _next_clip_id;
134  int _next_text_id;
135  int _next_bsp_id;
136  int _next_switch_id;
137  double _sw_lat, _sw_long;
138  double _ne_lat, _ne_long;
139  double _origin_lat, _origin_long;
140  double _lambert_upper_lat, _lambert_lower_lat;
141  int _next_light_id;
142  int _next_road_id;
143  int _next_cat_id;
144  EarthModel _earth_model;
145  int _next_adaptive_id;
146  int _next_curve_id;
147  double _delta_z;
148  double _radius;
149  int _next_mesh_id;
150 
151 public:
152  int get_flt_version() const;
153  void set_flt_version(int version);
154  static int min_flt_version();
155  static int max_flt_version();
156  bool check_version() const;
157 
158  DistanceUnit get_units() const;
159 
160  // Accessors into the instance pool.
161  bool has_instance(int instance_index) const;
162  FltInstanceDefinition *get_instance(int instance_index) const;
163  void clear_instances();
164  void add_instance(FltInstanceDefinition *instance);
165  void remove_instance(int instance_index);
166 
167 
168  // Accessors into the vertex palette.
169  int get_num_vertices() const;
170  FltVertex *get_vertex(int n) const;
171  void clear_vertices();
172  void add_vertex(FltVertex *vertex);
173 
174  FltVertex *get_vertex_by_offset(int offset);
175  int get_offset_by_vertex(FltVertex *vertex);
176 
177 
178  // Accessors into the color palette. This is read-only; why would you want
179  // to mess with building a new color palette?
180  int get_num_colors() const;
181  LColor get_color(int color_index) const;
182  LRGBColor get_rgb(int color_index) const;
183  bool has_color_name(int color_index) const;
184  std::string get_color_name(int color_index) const;
185 
186  int get_closest_color(const LColor &color) const;
187  int get_closest_rgb(const LRGBColor &color) const;
188 
189  int get_num_color_entries() const;
190  int get_num_color_shades() const;
191 
192  // These functions are mainly used behind-the-scenes to decode the strange
193  // forest of color options defined for faces and vertices.
194  LColor get_color(int color_index, bool use_packed_color,
195  const FltPackedColor &packed_color,
196  int transparency);
197  LRGBColor get_rgb(int color_index, bool use_packed_color,
198  const FltPackedColor &packed_color);
199 
200  // Accessors into the material palette.
201  bool has_material(int material_index) const;
202  FltMaterial *get_material(int material_index) const;
203  void clear_materials();
204  void add_material(FltMaterial *material);
205  void remove_material(int material_index);
206 
207 
208  // Accessors into the texture palette.
209  bool has_texture(int texture_index) const;
210  FltTexture *get_texture(int texture_index) const;
211  void clear_textures();
212  void add_texture(FltTexture *texture);
213  void remove_texture(int texture_index);
214 
215 
216  // Accessors into the light source palette.
217  bool has_light_source(int light_index) const;
218  FltLightSourceDefinition *get_light_source(int light_index) const;
219  void clear_light_sources();
220  void add_light_source(FltLightSourceDefinition *light_source);
221  void remove_light_source(int light_index);
222 
223 
224  // Accessors into the eyepointtrackplane palette.
225  bool got_eyepoint_trackplane_palette() const;
226  void set_eyepoint_trackplane_palette(bool flag);
227 
228  int get_num_eyepoints() const;
229  FltEyepoint *get_eyepoint(int n);
230  int get_num_trackplanes() const;
232 
233 private:
234  // Instance subtrees. These are standalone subtrees, which may be
235  // referenced by various points in the hierarchy, stored by instance ID
236  // number.
237  typedef pmap<int, PT(FltInstanceDefinition)> Instances;
238  Instances _instances;
239 
240 
241  // Support for the vertex palette.
242  int update_vertex_lookups();
243 
244  typedef pvector<PT(FltVertex)> Vertices;
246 
249 
250  Vertices _vertices;
251  UniqueVertices _unique_vertices;
252  VerticesByOffset _vertices_by_offset;
253  OffsetsByVertex _offsets_by_vertex;
254 
255  bool _vertex_lookups_stale;
256 
257  // This is maintained while the header is being read, to map the vertices to
258  // their corresponding offsets in the vertex palette.
259  int _current_vertex_offset;
260 
261 
262  // Support for the color palette.
263  bool _got_color_palette;
266  Colors _colors;
267  ColorNames _color_names;
268 
269 
270  // Support for the material palette.
271  bool _got_14_material_palette;
272  typedef pmap<int, PT(FltMaterial)> Materials;
273  Materials _materials;
274  int _next_material_index;
275 
276 
277  // Support for the texture palette.
278  AttrUpdate _auto_attr_update;
279  typedef pmap<int, PT(FltTexture)> Textures;
280  Textures _textures;
281  int _next_pattern_index;
282 
283 
284  // Support for the light source palette.
285  typedef pmap<int, PT(FltLightSourceDefinition)> LightSources;
286  LightSources _light_sources;
287 
288 
289  // Support for the eyepointtrackplane palette.
290  bool _got_eyepoint_trackplane_palette;
291  FltEyepoint _eyepoints[10];
292  FltTrackplane _trackplanes[10];
293 
294  // This pointer is used to resolve references in the flt file.
295  PT(PathReplace) _path_replace;
296  Filename _flt_filename;
297 
298 protected:
299  virtual bool extract_record(FltRecordReader &reader);
300  virtual bool extract_ancillary(FltRecordReader &reader);
301 
302  virtual bool build_record(FltRecordWriter &writer) const;
303  virtual FltError write_ancillary(FltRecordWriter &writer) const;
304 
305 private:
306  bool extract_vertex(FltRecordReader &reader);
307  bool extract_color_palette(FltRecordReader &reader);
308  bool extract_material(FltRecordReader &reader);
309  bool extract_14_material_palette(FltRecordReader &reader);
310  bool extract_texture(FltRecordReader &reader);
311  bool extract_texture_map(FltRecordReader &reader);
312  bool extract_light_source(FltRecordReader &reader);
313  bool extract_eyepoint_palette(FltRecordReader &reader);
314 
315  FltError write_vertex_palette(FltRecordWriter &writer) const;
316  FltError write_color_palette(FltRecordWriter &writer) const;
317  FltError write_material_palette(FltRecordWriter &writer) const;
318  FltError write_texture_palette(FltRecordWriter &writer) const;
319  FltError write_light_source_palette(FltRecordWriter &writer) const;
320  FltError write_eyepoint_palette(FltRecordWriter &writer) const;
321 
322 public:
323  virtual TypeHandle get_type() const {
324  return get_class_type();
325  }
326  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
327  static TypeHandle get_class_type() {
328  return _type_handle;
329  }
330  static void init_type() {
331  FltBeadID::init_type();
332  register_type(_type_handle, "FltHeader",
333  FltBeadID::get_class_type());
334  }
335 
336 private:
337  static TypeHandle _type_handle;
338 };
339 
340 #endif
This class stores a list of directories that can be searched, in order, to locate a particular file.
Definition: dSearchPath.h:28
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
A base class for any of a broad family of flt beads that include an ID.
Definition: fltBeadID.h:24
A single eyepoint entry in the eyepoint/trackplane palette.
Definition: fltEyepoint.h:27
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
static int max_flt_version()
Returns the latest flt version number that this codebase is known to support (times 100).
Definition: fltHeader.cxx:345
void add_material(FltMaterial *material)
Defines a new material.
Definition: fltHeader.cxx:830
DistanceUnit get_units() const
Returns the units indicated by the flt header, or DU_invalid if the units in the header are not under...
Definition: fltHeader.cxx:387
int get_num_color_shades() const
Returns the number of shades of brightness of each entry in the color palette.
Definition: fltHeader.cxx:748
int get_offset_by_vertex(FltVertex *vertex)
Returns the byte offset into the vertex palette associated with the given vertex pointer.
Definition: fltHeader.cxx:526
void add_instance(FltInstanceDefinition *instance)
Defines a new instance subtree.
Definition: fltHeader.cxx:445
int get_num_eyepoints() const
Returns the number of eyepoints in the eyepoint/trackplane palette.
Definition: fltHeader.cxx:987
FltMaterial * get_material(int material_index) const
Returns the material associated with the given index, or NULL if there is no such material.
Definition: fltHeader.cxx:807
bool has_texture(int texture_index) const
Returns true if a texture with the given index has been defined.
Definition: fltHeader.cxx:857
const Filename & get_flt_filename() const
Returns the directory in which the flt file is considered to reside.
Definition: fltHeader.cxx:179
virtual void apply_converted_filenames()
Walks the hierarchy at this record and below and copies the _converted_filename record into the _orig...
Definition: fltHeader.cxx:113
FltEyepoint * get_eyepoint(int n)
Returns the nth eyepoint in the eyepoint/trackplane palette.
Definition: fltHeader.cxx:995
int get_flt_version() const
Returns the version number of the flt file as reported in the header, times 100.
Definition: fltHeader.cxx:309
bool has_light_source(int light_index) const
Returns true if a light source with the given index has been defined.
Definition: fltHeader.cxx:916
int get_num_color_entries() const
Returns the number of actual entries in the color palette.
Definition: fltHeader.cxx:738
void add_vertex(FltVertex *vertex)
Adds a new vertex to the end of the vertex palette.
Definition: fltHeader.cxx:491
AttrUpdate get_auto_attr_update() const
Returns the current setting of the auto_attr_update flag.
Definition: fltHeader.cxx:300
void set_auto_attr_update(AttrUpdate attr)
Controls whether texture .attr files are written automatically when write_flt() is called.
Definition: fltHeader.cxx:291
void set_flt_filename(const Filename &flt_filename)
Sets the filename–especially the directory part–in which the flt file is considered to reside.
Definition: fltHeader.cxx:171
void set_path_replace(PathReplace *path_replace)
Replaces the PathReplace object (which specifies how to mangle paths from the source to the destinati...
Definition: fltHeader.cxx:128
void add_texture(FltTexture *texture)
Defines a new texture.
Definition: fltHeader.cxx:889
void set_flt_version(int version)
Changes the version number of the flt file that will be reported in the header.
Definition: fltHeader.cxx:322
bool check_version() const
Verifies that the version number read from the header is an understood version number,...
Definition: fltHeader.cxx:357
std::string get_color_name(int color_index) const
Returns the name associated with the given color, if any.
Definition: fltHeader.cxx:604
FltError read_flt(Filename filename)
Opens the indicated filename for reading and attempts to read the complete Flt file.
Definition: fltHeader.cxx:188
FltHeader(PathReplace *path_replace)
The FltHeader constructor accepts a PathReplace pointer; it uses this object to automatically convert...
Definition: fltHeader.cxx:39
FltVertex * get_vertex_by_offset(int offset)
Returns the particular vertex pointer associated with the given byte offset into the vertex palette.
Definition: fltHeader.cxx:506
bool has_color_name(int color_index) const
Returns true if the given color is named, false otherwise.
Definition: fltHeader.cxx:596
LColor get_color(int color_index) const
Returns the four-component color corresponding to the given color index.
Definition: fltHeader.cxx:559
FltVertex * get_vertex(int n) const
Returns the nth vertex of the vertex palette.
Definition: fltHeader.cxx:469
LRGBColor get_rgb(int color_index) const
Returns the three-component color corresponding to the given color index, ignoring the alpha componen...
Definition: fltHeader.cxx:578
void remove_instance(int instance_index)
Removes a particular instance subtree from the pool, if it exists.
Definition: fltHeader.cxx:453
void clear_instances()
Removes all instance subtrees from the instance pool.
Definition: fltHeader.cxx:435
static int min_flt_version()
Returns the earliest flt version number that this codebase supports (times 100).
Definition: fltHeader.cxx:335
FltTrackplane * get_trackplane(int n)
Returns the nth trackplane in the eyepoint/trackplane palette.
Definition: fltHeader.cxx:1013
void remove_light_source(int light_index)
Removes a particular light source from the light source palette, if it exists.
Definition: fltHeader.cxx:957
void add_light_source(FltLightSourceDefinition *light_source)
Defines a new light source.
Definition: fltHeader.cxx:948
void clear_textures()
Removes all textures from the palette.
Definition: fltHeader.cxx:879
FltInstanceDefinition * get_instance(int instance_index) const
Returns the instance subtree associated with the given index, or NULL if there is no such instance.
Definition: fltHeader.cxx:422
PathReplace * get_path_replace()
Returns a pointer to the PathReplace object associated with this converter.
Definition: fltHeader.cxx:138
void remove_material(int material_index)
Removes a particular material from the material palette, if it exists.
Definition: fltHeader.cxx:849
FltError write_flt(Filename filename)
Opens the indicated filename for writing and attempts to write the complete Flt file.
Definition: fltHeader.cxx:237
void set_eyepoint_trackplane_palette(bool flag)
Sets the state of the eyepoint/trackplane palette flag.
Definition: fltHeader.cxx:978
int get_closest_rgb(const LRGBColor &color) const
Returns the color index of the nearest color in the palette that matches the given three-component co...
Definition: fltHeader.cxx:680
int get_num_colors() const
Returns the total number of different colors in the color palette.
Definition: fltHeader.cxx:550
void clear_materials()
Removes all materials from the palette.
Definition: fltHeader.cxx:820
FltLightSourceDefinition * get_light_source(int light_index) const
Returns the light source associated with the given index, or NULL if there is no such light source.
Definition: fltHeader.cxx:925
void remove_texture(int texture_index)
Removes a particular texture from the texture palette, if it exists.
Definition: fltHeader.cxx:908
int get_num_trackplanes() const
Returns the number of trackplanes in the eyepoint/trackplane palette.
Definition: fltHeader.cxx:1005
int get_closest_color(const LColor &color) const
Returns the color index of the nearest color in the palette that matches the given four-component col...
Definition: fltHeader.cxx:618
void clear_vertices()
Removes all vertices from the vertex palette.
Definition: fltHeader.cxx:478
void clear_light_sources()
Removes all light sources from the palette.
Definition: fltHeader.cxx:938
Filename convert_path(const Filename &orig_filename, const DSearchPath &additional_path=DSearchPath())
Uses the PathReplace object to convert the named filename as read from the flt record to its actual n...
Definition: fltHeader.cxx:157
bool has_material(int material_index) const
Returns true if a material with the given index has been defined.
Definition: fltHeader.cxx:798
int get_num_vertices() const
Returns the number of vertices in the vertex palette.
Definition: fltHeader.cxx:461
bool got_eyepoint_trackplane_palette() const
Returns true if we have read an eyepoint/trackplane palette, and at least some of the eyepoints and t...
Definition: fltHeader.cxx:967
FltTexture * get_texture(int texture_index) const
Returns the texture associated with the given index, or NULL if there is no such texture.
Definition: fltHeader.cxx:866
bool has_instance(int instance_index) const
Returns true if a instance subtree with the given index has been defined.
Definition: fltHeader.cxx:413
This special kind of record marks the top node of an instance subtree.
Represents a single entry in the light source palette.
Represents a single material in the material palette.
Definition: fltMaterial.h:28
A packed color record, A, B, G, R.
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly.
Represents a single texture in the texture palette.
Definition: fltTexture.h:27
A single trackplane entry in the eyepoint/trackplane palette.
Definition: fltTrackplane.h:27
Represents a single vertex in the vertex palette.
Definition: fltVertex.h:32
This encapsulates the user's command-line request to replace existing, incorrect pathnames to models ...
Definition: pathReplace.h:36
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DistanceUnit
This enumerated type lists all the kinds of units we're likely to come across in model conversion pro...
Definition: distanceUnit.h:23
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.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22