fltRecord.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 fltRecord.cxx
  10. * @author drose
  11. * @date 2000-08-24
  12. */
  13. #include "fltRecord.h"
  14. #include "fltRecordReader.h"
  15. #include "fltRecordWriter.h"
  16. #include "fltHeader.h"
  17. #include "fltGroup.h"
  18. #include "fltObject.h"
  19. #include "fltFace.h"
  20. #include "fltCurve.h"
  21. #include "fltMesh.h"
  22. #include "fltLocalVertexPool.h"
  23. #include "fltMeshPrimitive.h"
  24. #include "fltVertexList.h"
  25. #include "fltLOD.h"
  26. #include "fltInstanceDefinition.h"
  27. #include "fltInstanceRef.h"
  28. #include "fltUnsupportedRecord.h"
  29. #include "fltExternalReference.h"
  30. #include "fltVectorRecord.h"
  31. #include "config_flt.h"
  32. #include "dcast.h"
  33. #include "indent.h"
  34. #include "datagramIterator.h"
  35. #include <assert.h>
  36. TypeHandle FltRecord::_type_handle;
  37. /**
  38. *
  39. */
  40. FltRecord::
  41. FltRecord(FltHeader *header) :
  42. _header(header)
  43. {
  44. }
  45. /**
  46. *
  47. */
  48. FltRecord::
  49. ~FltRecord() {
  50. }
  51. /**
  52. * Returns the number of child records of this record. This reflects the
  53. * normal scene graph hierarchy.
  54. */
  55. int FltRecord::
  56. get_num_children() const {
  57. return _children.size();
  58. }
  59. /**
  60. * Returns the nth child of this record.
  61. */
  62. FltRecord *FltRecord::
  63. get_child(int n) const {
  64. nassertr(n >= 0 && n < (int)_children.size(), nullptr);
  65. return _children[n];
  66. }
  67. /**
  68. * Removes all children from this record.
  69. */
  70. void FltRecord::
  71. clear_children() {
  72. _children.clear();
  73. }
  74. /**
  75. * Adds a new child to the end of the list of children for this record.
  76. */
  77. void FltRecord::
  78. add_child(FltRecord *child) {
  79. _children.push_back(child);
  80. }
  81. /**
  82. * Returns the number of subface records of this record. Normally, subfaces
  83. * will only be present on object records, although it is logically possible
  84. * for them to appear anywhere.
  85. */
  86. int FltRecord::
  87. get_num_subfaces() const {
  88. return _subfaces.size();
  89. }
  90. /**
  91. * Returns the nth subface of this record.
  92. */
  93. FltRecord *FltRecord::
  94. get_subface(int n) const {
  95. nassertr(n >= 0 && n < (int)_subfaces.size(), nullptr);
  96. return _subfaces[n];
  97. }
  98. /**
  99. * Removes all subfaces from this record.
  100. */
  101. void FltRecord::
  102. clear_subfaces() {
  103. _subfaces.clear();
  104. }
  105. /**
  106. * Adds a new subface to the end of the list of subfaces for this record.
  107. */
  108. void FltRecord::
  109. add_subface(FltRecord *subface) {
  110. _subfaces.push_back(subface);
  111. }
  112. /**
  113. * Returns the number of extension attribute records for this object. These
  114. * are auxiliary nodes, presumably of type FO_extension, that have some local
  115. * meaning to the object.
  116. */
  117. int FltRecord::
  118. get_num_extensions() const {
  119. return _extensions.size();
  120. }
  121. /**
  122. * Returns the nth extension of this record.
  123. */
  124. FltRecord *FltRecord::
  125. get_extension(int n) const {
  126. nassertr(n >= 0 && n < (int)_extensions.size(), nullptr);
  127. return _extensions[n];
  128. }
  129. /**
  130. * Removes all extensions from this record.
  131. */
  132. void FltRecord::
  133. clear_extensions() {
  134. _extensions.clear();
  135. }
  136. /**
  137. * Adds a new extension to the end of the list of extensions for this record.
  138. * This should be a record of type FO_extension.
  139. */
  140. void FltRecord::
  141. add_extension(FltRecord *extension) {
  142. _extensions.push_back(extension);
  143. }
  144. /**
  145. * Returns the number of unsupported ancillary records of this record. These
  146. * are ancillary records that appeared following this record in the flt file
  147. * but that aren't directly understood by the flt loader--normally, an
  148. * ancillary record is examined and decoded on the spot, and no pointer to it
  149. * is kept.
  150. */
  151. int FltRecord::
  152. get_num_ancillary() const {
  153. return _ancillary.size();
  154. }
  155. /**
  156. * Returns the nth unsupported ancillary record of this record. See
  157. * get_num_ancillary().
  158. */
  159. FltRecord *FltRecord::
  160. get_ancillary(int n) const {
  161. nassertr(n >= 0 && n < (int)_ancillary.size(), nullptr);
  162. return _ancillary[n];
  163. }
  164. /**
  165. * Removes all unsupported ancillary records from this record. See
  166. * get_num_ancillary().
  167. */
  168. void FltRecord::
  169. clear_ancillary() {
  170. _ancillary.clear();
  171. }
  172. /**
  173. * Adds a new unsupported ancillary record to the end of the list of ancillary
  174. * records for this record. This record will be written to the flt file
  175. * following this record, without attempting to understand what is in it.
  176. *
  177. * Normally, there is no reason to use this function; if the data stored in
  178. * the FltRecord requires one or more ancillary record, the appropriate
  179. * records will automatically be generated when the record is written. This
  180. * function is only required to output a record whose type is not supported by
  181. * the flt loader. But it would be better to extend the flt loader to know
  182. * about this new kind of data record.
  183. */
  184. void FltRecord::
  185. add_ancillary(FltRecord *ancillary) {
  186. _ancillary.push_back(ancillary);
  187. }
  188. /**
  189. * Returns true if this record has a nonempty comment, false otherwise.
  190. */
  191. bool FltRecord::
  192. has_comment() const {
  193. return !_comment.empty();
  194. }
  195. /**
  196. * Retrieves the comment for this record, or empty string if the record has no
  197. * comment.
  198. */
  199. const std::string &FltRecord::
  200. get_comment() const {
  201. return _comment;
  202. }
  203. /**
  204. * Removes the comment for this record.
  205. */
  206. void FltRecord::
  207. clear_comment() {
  208. _comment = "";
  209. }
  210. /**
  211. * Changes the comment for this record.
  212. */
  213. void FltRecord::
  214. set_comment(const std::string &comment) {
  215. _comment = comment;
  216. }
  217. /**
  218. * Checks that the iterator has no bytes left, as it should at the end of a
  219. * successfully read record. If there *are* remaining bytes, print a warning
  220. * message but otherwise don't worry about it.
  221. *
  222. * If we are attempting to read a flt file whose version is newer than the
  223. * newest this program understands, don't even print a warning message, since
  224. * this is exactly the sort of thing we expect.
  225. */
  226. void FltRecord::
  227. check_remaining_size(const DatagramIterator &di, const std::string &name) const {
  228. if (di.get_remaining_size() == 0) {
  229. return;
  230. }
  231. if (_header->get_flt_version() <= _header->max_flt_version()) {
  232. nout << "Warning! Ignoring extra " << di.get_remaining_size()
  233. << " bytes at the end of a ";
  234. if (name.empty()) {
  235. nout << get_type();
  236. } else {
  237. nout << name;
  238. }
  239. nout << " record.\n";
  240. }
  241. }
  242. /**
  243. * Walks the hierarchy at this record and below and copies the
  244. * _converted_filename record into the _orig_filename record, so the flt file
  245. * will be written out with the converted filename instead of what was
  246. * originally read in.
  247. */
  248. void FltRecord::
  249. apply_converted_filenames() {
  250. Records::const_iterator ci;
  251. for (ci = _subfaces.begin(); ci != _subfaces.end(); ++ci) {
  252. (*ci)->apply_converted_filenames();
  253. }
  254. for (ci = _children.begin(); ci != _children.end(); ++ci) {
  255. (*ci)->apply_converted_filenames();
  256. }
  257. }
  258. /**
  259. * Writes a quick one-line description of the record, but not its children.
  260. * This is a human-readable description, primarily for debugging; to write a
  261. * flt file, use FltHeader::write_flt().
  262. */
  263. void FltRecord::
  264. output(std::ostream &out) const {
  265. out << get_type();
  266. }
  267. /**
  268. * Writes a multiple-line description of the record and all of its children.
  269. * This is a human-readable description, primarily for debugging; to write a
  270. * flt file, use FltHeader::write_flt().
  271. */
  272. void FltRecord::
  273. write(std::ostream &out, int indent_level) const {
  274. indent(out, indent_level) << *this;
  275. write_children(out, indent_level);
  276. }
  277. /**
  278. * Assuming the current write position has been left at the end of the last
  279. * line of the record description, writes out the list of children.
  280. */
  281. void FltRecord::
  282. write_children(std::ostream &out, int indent_level) const {
  283. if (!_ancillary.empty()) {
  284. out << " + " << _ancillary.size() << " ancillary";
  285. }
  286. if (!_extensions.empty()) {
  287. out << " + " << _extensions.size() << " extensions";
  288. }
  289. if (!_subfaces.empty()) {
  290. out << " [";
  291. Records::const_iterator ci;
  292. for (ci = _subfaces.begin(); ci != _subfaces.end(); ++ci) {
  293. out << " " << *(*ci);
  294. }
  295. out << " ]";
  296. }
  297. if (!_children.empty()) {
  298. out << " {\n";
  299. Records::const_iterator ci;
  300. for (ci = _children.begin(); ci != _children.end(); ++ci) {
  301. (*ci)->write(out, indent_level + 2);
  302. }
  303. indent(out, indent_level) << "}\n";
  304. } else {
  305. out << "\n";
  306. }
  307. }
  308. /*
  309. virtual void write(ostream &out) const;
  310. virtual void build_record(Datagram &datagram) const;
  311. */
  312. /**
  313. * Returns true if the indicated opcode corresponds to an ancillary record
  314. * type, false otherwise. In general, this function is used to identify
  315. * ancillary records that are not presently supported by the FltReader; these
  316. * will be ignored. Normally, ancillary records will be detected and
  317. * processed by extract_ancillary().
  318. */
  319. bool FltRecord::
  320. is_ancillary(FltOpcode opcode) {
  321. switch (opcode) {
  322. case FO_comment:
  323. case FO_long_id:
  324. case FO_multitexture:
  325. case FO_uv_list:
  326. case FO_replicate:
  327. case FO_road_zone:
  328. case FO_transform_matrix:
  329. case FO_rotate_about_edge:
  330. case FO_translate:
  331. case FO_scale:
  332. case FO_rotate_about_point:
  333. case FO_rotate_and_scale:
  334. case FO_put:
  335. case FO_general_matrix:
  336. case FO_vector:
  337. case FO_bounding_box:
  338. case FO_bounding_sphere:
  339. case FO_bounding_cylinder:
  340. case FO_bv_center:
  341. case FO_bv_orientation:
  342. case FO_local_vertex_pool:
  343. case FO_cat_data:
  344. case FO_14_material_palette:
  345. case FO_vertex_palette:
  346. case FO_vertex_c:
  347. case FO_vertex_cn:
  348. case FO_vertex_cnu:
  349. case FO_vertex_cu:
  350. case FO_color_palette:
  351. case FO_name_table:
  352. case FO_15_material:
  353. case FO_texture:
  354. case FO_eyepoint_palette:
  355. case FO_light_definition:
  356. case FO_texture_map_palette:
  357. return true;
  358. case FO_header:
  359. case FO_mesh:
  360. case FO_mesh_primitive:
  361. case FO_group:
  362. case FO_object:
  363. case FO_face:
  364. case FO_light_point:
  365. case FO_dof:
  366. case FO_vertex_list:
  367. case FO_morph_list:
  368. case FO_bsp:
  369. case FO_external_ref:
  370. case FO_lod:
  371. case FO_sound:
  372. case FO_light_source:
  373. case FO_road_segment:
  374. case FO_road_construction:
  375. case FO_road_path:
  376. case FO_clip_region:
  377. case FO_text:
  378. case FO_switch:
  379. case FO_cat:
  380. case FO_extension:
  381. case FO_curve:
  382. return false;
  383. case FO_push:
  384. case FO_pop:
  385. case FO_push_face:
  386. case FO_pop_face:
  387. case FO_push_attribute:
  388. case FO_pop_attribute:
  389. case FO_push_extension:
  390. case FO_pop_extension:
  391. case FO_instance:
  392. case FO_instance_ref:
  393. return false;
  394. default:
  395. nout << "Don't know whether " << opcode << " is ancillary.\n";
  396. return false;
  397. }
  398. }
  399. /**
  400. * Creates a new FltRecord corresponding to the opcode. If the opcode is
  401. * unknown, creates a FltUnsupportedRecord.
  402. */
  403. FltRecord *FltRecord::
  404. create_new_record(FltOpcode opcode) const {
  405. switch (opcode) {
  406. case FO_group:
  407. return new FltGroup(_header);
  408. case FO_object:
  409. return new FltObject(_header);
  410. case FO_face:
  411. return new FltFace(_header);
  412. case FO_curve:
  413. return new FltCurve(_header);
  414. case FO_mesh:
  415. return new FltMesh(_header);
  416. case FO_local_vertex_pool:
  417. return new FltLocalVertexPool(_header);
  418. case FO_mesh_primitive:
  419. return new FltMeshPrimitive(_header);
  420. case FO_vertex_list:
  421. return new FltVertexList(_header);
  422. case FO_lod:
  423. return new FltLOD(_header);
  424. case FO_instance:
  425. return new FltInstanceDefinition(_header);
  426. case FO_instance_ref:
  427. return new FltInstanceRef(_header);
  428. case FO_external_ref:
  429. return new FltExternalReference(_header);
  430. case FO_vector:
  431. return new FltVectorRecord(_header);
  432. default:
  433. nout << "Ignoring unsupported record " << opcode << "\n";
  434. return new FltUnsupportedRecord(_header);
  435. }
  436. }
  437. /**
  438. * Extracts this record information from the current record presented in the
  439. * reader, then advances the reader and continues to read any children, if
  440. * present. On return, the reader is position on the next sibling record to
  441. * this record.
  442. *
  443. * Returns FE_ok if successful, otherwise on error.
  444. */
  445. FltError FltRecord::
  446. read_record_and_children(FltRecordReader &reader) {
  447. if (!extract_record(reader)) {
  448. nout << "Could not extract record for " << *this << "\n";
  449. assert(!flt_error_abort);
  450. return FE_invalid_record;
  451. }
  452. FltError result = reader.advance();
  453. if (result == FE_end_of_file) {
  454. return FE_ok;
  455. } else if (result != FE_ok) {
  456. return result;
  457. }
  458. while (true) {
  459. if (extract_ancillary(reader)) {
  460. // Ok, a known ancillary record. Fine.
  461. } else if (reader.get_opcode() == FO_push) {
  462. // A push begins a new list of children.
  463. result = reader.advance();
  464. if (result != FE_ok) {
  465. return result;
  466. }
  467. while (reader.get_opcode() != FO_pop) {
  468. PT(FltRecord) child = create_new_record(reader.get_opcode());
  469. FltError result = child->read_record_and_children(reader);
  470. if (result != FE_ok) {
  471. return result;
  472. }
  473. if (child->is_of_type(FltInstanceDefinition::get_class_type())) {
  474. // A special case for an instance definition. These shouldn't
  475. // appear in the hierarchy, but should instead be added directly to
  476. // the header.
  477. _header->add_instance(DCAST(FltInstanceDefinition, child));
  478. } else {
  479. add_child(child);
  480. }
  481. if (reader.eof() || reader.error()) {
  482. assert(!flt_error_abort);
  483. return FE_end_of_file;
  484. }
  485. }
  486. } else if (reader.get_opcode() == FO_push_face) {
  487. // A push subface begins a new list of subfaces.
  488. result = reader.advance();
  489. if (result != FE_ok) {
  490. return result;
  491. }
  492. while (reader.get_opcode() != FO_pop_face) {
  493. PT(FltRecord) subface = create_new_record(reader.get_opcode());
  494. FltError result = subface->read_record_and_children(reader);
  495. if (result != FE_ok) {
  496. return result;
  497. }
  498. add_subface(subface);
  499. if (reader.eof() || reader.error()) {
  500. assert(!flt_error_abort);
  501. return FE_end_of_file;
  502. }
  503. }
  504. } else if (reader.get_opcode() == FO_push_extension) {
  505. // A push extension begins a new list of extensions.
  506. result = reader.advance();
  507. if (result != FE_ok) {
  508. return result;
  509. }
  510. while (reader.get_opcode() != FO_pop_extension) {
  511. PT(FltRecord) extension = create_new_record(reader.get_opcode());
  512. FltError result = extension->read_record_and_children(reader);
  513. if (result != FE_ok) {
  514. return result;
  515. }
  516. add_extension(extension);
  517. if (reader.eof() || reader.error()) {
  518. assert(!flt_error_abort);
  519. return FE_end_of_file;
  520. }
  521. }
  522. } else if (is_ancillary(reader.get_opcode())) {
  523. // An unsupported ancillary record. Skip it.
  524. PT(FltRecord) ancillary = create_new_record(reader.get_opcode());
  525. ancillary->extract_record(reader);
  526. _ancillary.push_back(ancillary);
  527. } else {
  528. // None of the above: we're done.
  529. return FE_ok;
  530. }
  531. // Skip to the next record. If that's the end, fine.
  532. result = reader.advance(true);
  533. if (reader.eof() || result != FE_ok) {
  534. return result;
  535. }
  536. }
  537. }
  538. /**
  539. * Fills in the information in this record based on the information given in
  540. * the indicated datagram, whose opcode has already been read. Returns true
  541. * on success, false if the datagram is invalid.
  542. */
  543. bool FltRecord::
  544. extract_record(FltRecordReader &) {
  545. return true;
  546. }
  547. /**
  548. * Checks whether the given record, which follows this record sequentially in
  549. * the file, is an ancillary record of this record. If it is, extracts the
  550. * relevant information and returns true; otherwise, leaves it alone and
  551. * returns false.
  552. */
  553. bool FltRecord::
  554. extract_ancillary(FltRecordReader &reader) {
  555. if (reader.get_opcode() == FO_comment) {
  556. DatagramIterator &di = reader.get_iterator();
  557. _comment = di.get_fixed_string(di.get_remaining_size());
  558. return true;
  559. }
  560. return false;
  561. }
  562. /**
  563. * Writes this record out to the flt file, along with all of its ancillary
  564. * records and children records. Returns FE_ok on success, or something else
  565. * on error.
  566. */
  567. FltError FltRecord::
  568. write_record_and_children(FltRecordWriter &writer) const {
  569. // First, write the record.
  570. if (!build_record(writer)) {
  571. assert(!flt_error_abort);
  572. return FE_bad_data;
  573. }
  574. FltError result = writer.advance();
  575. if (result != FE_ok) {
  576. return result;
  577. }
  578. // Then the ancillary data.
  579. result = write_ancillary(writer);
  580. if (result != FE_ok) {
  581. return result;
  582. }
  583. Records::const_iterator ci;
  584. for (ci = _ancillary.begin(); ci != _ancillary.end(); ++ci) {
  585. if (!(*ci)->build_record(writer)) {
  586. assert(!flt_error_abort);
  587. return FE_bad_data;
  588. }
  589. result = writer.advance();
  590. if (result != FE_ok) {
  591. return result;
  592. }
  593. }
  594. // Any extensions?
  595. if (!_extensions.empty()) {
  596. result = writer.write_record(FO_push_face);
  597. if (result != FE_ok) {
  598. return result;
  599. }
  600. for (ci = _extensions.begin(); ci != _extensions.end(); ++ci) {
  601. (*ci)->write_record_and_children(writer);
  602. }
  603. result = writer.write_record(FO_pop_face);
  604. if (result != FE_ok) {
  605. return result;
  606. }
  607. }
  608. // Finally, write all the children.
  609. if (!_children.empty()) {
  610. result = writer.write_record(FO_push);
  611. if (result != FE_ok) {
  612. return result;
  613. }
  614. for (ci = _children.begin(); ci != _children.end(); ++ci) {
  615. (*ci)->write_record_and_children(writer);
  616. }
  617. result = writer.write_record(FO_pop);
  618. if (result != FE_ok) {
  619. return result;
  620. }
  621. }
  622. // We must write subfaces *after* the list of children, or Creator will
  623. // crash trying to load the file.
  624. if (!_subfaces.empty()) {
  625. result = writer.write_record(FO_push_face);
  626. if (result != FE_ok) {
  627. return result;
  628. }
  629. for (ci = _subfaces.begin(); ci != _subfaces.end(); ++ci) {
  630. (*ci)->write_record_and_children(writer);
  631. }
  632. result = writer.write_record(FO_pop_face);
  633. if (result != FE_ok) {
  634. return result;
  635. }
  636. }
  637. return FE_ok;
  638. }
  639. /**
  640. * Fills up the current record on the FltRecordWriter with data for this
  641. * record, but does not advance the writer. Returns true on success, false if
  642. * there is some error.
  643. */
  644. bool FltRecord::
  645. build_record(FltRecordWriter &) const {
  646. return true;
  647. }
  648. /**
  649. * Writes whatever ancillary records are required for this record. Returns
  650. * FE_ok on success, or something else if there is some error.
  651. */
  652. FltError FltRecord::
  653. write_ancillary(FltRecordWriter &writer) const {
  654. if (!_comment.empty()) {
  655. Datagram dc(_comment.data(), _comment.size());
  656. FltError result = writer.write_record(FO_comment, dc);
  657. if (result != FE_ok) {
  658. return result;
  659. }
  660. }
  661. return FE_ok;
  662. }