cLwoSurfaceBlock.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 cLwoSurfaceBlock.cxx
  10. * @author drose
  11. * @date 2001-04-26
  12. */
  13. #include "cLwoSurfaceBlock.h"
  14. #include "cLwoSurfaceBlockTMap.h"
  15. #include "lwoToEggConverter.h"
  16. #include "lwoSurfaceBlockChannel.h"
  17. #include "lwoSurfaceBlockEnabled.h"
  18. #include "lwoSurfaceBlockImage.h"
  19. #include "lwoSurfaceBlockRepeat.h"
  20. #include "lwoSurfaceBlockVMapName.h"
  21. #include "dcast.h"
  22. /**
  23. *
  24. */
  25. CLwoSurfaceBlock::
  26. CLwoSurfaceBlock(LwoToEggConverter *converter, const LwoSurfaceBlock *block) :
  27. _converter(converter),
  28. _block(block)
  29. {
  30. _block_type = _block->_header->get_id();
  31. _ordinal = _block->_header->_ordinal;
  32. _enabled = true;
  33. _opacity_type = LwoSurfaceBlockOpacity::T_additive;
  34. _opacity = 1.0;
  35. _transform = LMatrix4d::ident_mat();
  36. _inv_transform = LMatrix4d::ident_mat();
  37. _projection_mode = LwoSurfaceBlockProjection::M_uv;
  38. _axis = LwoSurfaceBlockAxis::A_y;
  39. _clip_index = -1;
  40. _w_wrap = LwoSurfaceBlockWrap::M_repeat;
  41. _h_wrap = LwoSurfaceBlockWrap::M_repeat;
  42. _w_repeat = 1.0;
  43. _h_repeat = 1.0;
  44. _tmap = nullptr;
  45. // Scan the chunks in the header.
  46. int num_hchunks = _block->_header->get_num_chunks();
  47. for (int hi = 0; hi < num_hchunks; hi++) {
  48. const IffChunk *hchunk = _block->_header->get_chunk(hi);
  49. if (hchunk->is_of_type(LwoSurfaceBlockChannel::get_class_type())) {
  50. const LwoSurfaceBlockChannel *bc =
  51. DCAST(LwoSurfaceBlockChannel, hchunk);
  52. _channel_id = bc->_channel_id;
  53. } else if (hchunk->is_of_type(LwoSurfaceBlockEnabled::get_class_type())) {
  54. const LwoSurfaceBlockEnabled *ec =
  55. DCAST(LwoSurfaceBlockEnabled, hchunk);
  56. _enabled = ec->_enabled;
  57. }
  58. }
  59. // Scan the chunks in the body.
  60. int num_chunks = _block->get_num_chunks();
  61. for (int i = 0; i < num_chunks; i++) {
  62. const IffChunk *chunk = _block->get_chunk(i);
  63. if (chunk->is_of_type(LwoSurfaceBlockTMap::get_class_type())) {
  64. const LwoSurfaceBlockTMap *lwo_tmap = DCAST(LwoSurfaceBlockTMap, chunk);
  65. if (_tmap != nullptr) {
  66. nout << "Two TMAP chunks encountered within surface block.\n";
  67. delete _tmap;
  68. }
  69. _tmap = new CLwoSurfaceBlockTMap(_converter, lwo_tmap);
  70. } else if (chunk->is_of_type(LwoSurfaceBlockProjection::get_class_type())) {
  71. const LwoSurfaceBlockProjection *proj = DCAST(LwoSurfaceBlockProjection, chunk);
  72. _projection_mode = proj->_mode;
  73. } else if (chunk->is_of_type(LwoSurfaceBlockAxis::get_class_type())) {
  74. const LwoSurfaceBlockAxis *axis = DCAST(LwoSurfaceBlockAxis, chunk);
  75. _axis = axis->_axis;
  76. } else if (chunk->is_of_type(LwoSurfaceBlockImage::get_class_type())) {
  77. const LwoSurfaceBlockImage *image = DCAST(LwoSurfaceBlockImage, chunk);
  78. _clip_index = image->_index;
  79. } else if (chunk->is_of_type(LwoSurfaceBlockWrap::get_class_type())) {
  80. const LwoSurfaceBlockWrap *wrap = DCAST(LwoSurfaceBlockWrap, chunk);
  81. _w_wrap = wrap->_width;
  82. _h_wrap = wrap->_height;
  83. } else if (chunk->is_of_type(LwoSurfaceBlockWrap::get_class_type())) {
  84. const LwoSurfaceBlockWrap *wrap = DCAST(LwoSurfaceBlockWrap, chunk);
  85. _w_wrap = wrap->_width;
  86. _h_wrap = wrap->_height;
  87. } else if (chunk->is_of_type(LwoSurfaceBlockVMapName::get_class_type())) {
  88. const LwoSurfaceBlockVMapName *vmap = DCAST(LwoSurfaceBlockVMapName, chunk);
  89. _uv_name = vmap->_name;
  90. } else if (chunk->is_of_type(LwoSurfaceBlockRepeat::get_class_type())) {
  91. const LwoSurfaceBlockRepeat *repeat = DCAST(LwoSurfaceBlockRepeat, chunk);
  92. if (repeat->get_id() == IffId("WRPW")) {
  93. _w_repeat = repeat->_cycles;
  94. } else if (repeat->get_id() == IffId("WRPH")) {
  95. _h_repeat = repeat->_cycles;
  96. }
  97. }
  98. }
  99. if (_tmap != nullptr) {
  100. _tmap->get_transform(_transform);
  101. }
  102. // Also rotate the transform if we specify some axis other than Y. (All the
  103. // map_* uv mapping functions are written to assume Y is the dominant axis.)
  104. switch (_axis) {
  105. case LwoSurfaceBlockAxis::A_x:
  106. _transform = LMatrix4d::rotate_mat(90.0,
  107. LVecBase3d::unit_z(),
  108. CS_yup_left) * _transform;
  109. break;
  110. case LwoSurfaceBlockAxis::A_y:
  111. break;
  112. case LwoSurfaceBlockAxis::A_z:
  113. _transform = LMatrix4d::rotate_mat(-90.0,
  114. LVecBase3d::unit_x(),
  115. CS_yup_left) * _transform;
  116. break;
  117. }
  118. _inv_transform.invert_from(_transform);
  119. }
  120. /**
  121. *
  122. */
  123. CLwoSurfaceBlock::
  124. ~CLwoSurfaceBlock() {
  125. delete _tmap;
  126. }