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