Panda3D
cLwoSurfaceBlock.cxx
1 // Filename: cLwoSurfaceBlock.cxx
2 // Created by: drose (26Apr01)
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 #include "cLwoSurfaceBlock.h"
16 #include "cLwoSurfaceBlockTMap.h"
17 #include "lwoToEggConverter.h"
18 
19 #include "lwoSurfaceBlockChannel.h"
20 #include "lwoSurfaceBlockEnabled.h"
21 #include "lwoSurfaceBlockImage.h"
22 #include "lwoSurfaceBlockRepeat.h"
23 #include "lwoSurfaceBlockVMapName.h"
24 #include "dcast.h"
25 
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: CLwoSurfaceBlock::Constructor
29 // Access: Public
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 CLwoSurfaceBlock::
33 CLwoSurfaceBlock(LwoToEggConverter *converter, const LwoSurfaceBlock *block) :
34  _converter(converter),
35  _block(block)
36 {
37  _block_type = _block->_header->get_id();
38  _ordinal = _block->_header->_ordinal;
39  _enabled = true;
40  _opacity_type = LwoSurfaceBlockOpacity::T_additive;
41  _opacity = 1.0;
42  _transform = LMatrix4d::ident_mat();
43  _inv_transform = LMatrix4d::ident_mat();
44  _projection_mode = LwoSurfaceBlockProjection::M_uv;
45  _axis = LwoSurfaceBlockAxis::A_y;
46  _clip_index = -1;
47  _w_wrap = LwoSurfaceBlockWrap::M_repeat;
48  _h_wrap = LwoSurfaceBlockWrap::M_repeat;
49  _w_repeat = 1.0;
50  _h_repeat = 1.0;
51  _tmap = (CLwoSurfaceBlockTMap *)NULL;
52 
53  // Scan the chunks in the header.
54  int num_hchunks = _block->_header->get_num_chunks();
55  for (int hi = 0; hi < num_hchunks; hi++) {
56  const IffChunk *hchunk = _block->_header->get_chunk(hi);
57 
58  if (hchunk->is_of_type(LwoSurfaceBlockChannel::get_class_type())) {
59  const LwoSurfaceBlockChannel *bc =
60  DCAST(LwoSurfaceBlockChannel, hchunk);
61  _channel_id = bc->_channel_id;
62 
63  } else if (hchunk->is_of_type(LwoSurfaceBlockEnabled::get_class_type())) {
64  const LwoSurfaceBlockEnabled *ec =
65  DCAST(LwoSurfaceBlockEnabled, hchunk);
66  _enabled = ec->_enabled;
67  }
68  }
69 
70  // Scan the chunks in the body.
71  int num_chunks = _block->get_num_chunks();
72  for (int i = 0; i < num_chunks; i++) {
73  const IffChunk *chunk = _block->get_chunk(i);
74 
75  if (chunk->is_of_type(LwoSurfaceBlockTMap::get_class_type())) {
76  const LwoSurfaceBlockTMap *lwo_tmap = DCAST(LwoSurfaceBlockTMap, chunk);
77  if (_tmap != (CLwoSurfaceBlockTMap *)NULL) {
78  nout << "Two TMAP chunks encountered within surface block.\n";
79  delete _tmap;
80  }
81  _tmap = new CLwoSurfaceBlockTMap(_converter, lwo_tmap);
82 
83  } else if (chunk->is_of_type(LwoSurfaceBlockProjection::get_class_type())) {
84  const LwoSurfaceBlockProjection *proj = DCAST(LwoSurfaceBlockProjection, chunk);
85  _projection_mode = proj->_mode;
86 
87  } else if (chunk->is_of_type(LwoSurfaceBlockAxis::get_class_type())) {
88  const LwoSurfaceBlockAxis *axis = DCAST(LwoSurfaceBlockAxis, chunk);
89  _axis = axis->_axis;
90 
91  } else if (chunk->is_of_type(LwoSurfaceBlockImage::get_class_type())) {
92  const LwoSurfaceBlockImage *image = DCAST(LwoSurfaceBlockImage, chunk);
93  _clip_index = image->_index;
94 
95  } else if (chunk->is_of_type(LwoSurfaceBlockWrap::get_class_type())) {
96  const LwoSurfaceBlockWrap *wrap = DCAST(LwoSurfaceBlockWrap, chunk);
97  _w_wrap = wrap->_width;
98  _h_wrap = wrap->_height;
99 
100  } else if (chunk->is_of_type(LwoSurfaceBlockWrap::get_class_type())) {
101  const LwoSurfaceBlockWrap *wrap = DCAST(LwoSurfaceBlockWrap, chunk);
102  _w_wrap = wrap->_width;
103  _h_wrap = wrap->_height;
104 
105  } else if (chunk->is_of_type(LwoSurfaceBlockVMapName::get_class_type())) {
106  const LwoSurfaceBlockVMapName *vmap = DCAST(LwoSurfaceBlockVMapName, chunk);
107  _uv_name = vmap->_name;
108 
109  } else if (chunk->is_of_type(LwoSurfaceBlockRepeat::get_class_type())) {
110  const LwoSurfaceBlockRepeat *repeat = DCAST(LwoSurfaceBlockRepeat, chunk);
111  if (repeat->get_id() == IffId("WRPW")) {
112  _w_repeat = repeat->_cycles;
113  } else if (repeat->get_id() == IffId("WRPH")) {
114  _h_repeat = repeat->_cycles;
115  }
116  }
117  }
118 
119  if (_tmap != (CLwoSurfaceBlockTMap *)NULL) {
120  _tmap->get_transform(_transform);
121  }
122 
123  // Also rotate the transform if we specify some axis other than Y.
124  // (All the map_* uv mapping functions are written to assume Y is
125  // the dominant axis.)
126  switch (_axis) {
127  case LwoSurfaceBlockAxis::A_x:
128  _transform = LMatrix4d::rotate_mat(90.0,
130  CS_yup_left) * _transform;
131  break;
132 
133  case LwoSurfaceBlockAxis::A_y:
134  break;
135 
136  case LwoSurfaceBlockAxis::A_z:
137  _transform = LMatrix4d::rotate_mat(-90.0,
139  CS_yup_left) * _transform;
140  break;
141  }
142 
143  _inv_transform.invert_from(_transform);
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: CLwoSurfaceBlock::Destructor
148 // Access: Public
149 // Description:
150 ////////////////////////////////////////////////////////////////////
151 CLwoSurfaceBlock::
152 ~CLwoSurfaceBlock() {
153  if (_tmap != (CLwoSurfaceBlockTMap *)NULL) {
154  delete _tmap;
155  }
156 }
Indicates which channel the texture in this LwoSurfaceBlock is applied to.
static LMatrix4d rotate_mat(double angle, const LVecBase3d &axis, CoordinateSystem cs=CS_default)
Returns a matrix that rotates by the given angle in degrees counterclockwise about the indicated vect...
Definition: lmatrix.h:6690
For cylindrical and spherical projections, this parameter controls how many times the image repeats o...
A texture layer or shader, part of a LwoSurface chunk.
The tMap chunk within a LwoSurfaceBlock chunk.
Specifies how the texture image appears for areas outside the image.
Specifies the name of a set of UV&#39;s defined on the polygons that use this model.
The basic kind of record in an EA "IFF" file, which the LightWave object file is based on...
Definition: iffChunk.h:32
static const LMatrix4d & ident_mat()
Returns an identity matrix.
Definition: lmatrix.h:5168
static const LVecBase3d & unit_z()
Returns a unit Z vector.
Definition: lvecBase3.h:1778
Indicates the projection mode for this particular shader.
bool invert_from(const LMatrix4d &other)
Computes the inverse of the other matrix, and stores the result in this matrix.
Definition: lmatrix.h:6438
Indicates the axis for this particular shader&#39;s projection.
This class supervises the construction of an EggData structure from the data represented by the LwoHe...
Indicates whether this particular layer or shader should be rendered or not.
static const LVecBase3d & unit_x()
Returns a unit X vector.
Definition: lvecBase3.h:1758
Specifies the particular image that is being applied as a texture.
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition: typedObject.I:63
This class is a wrapper around LwoSurfaceBlockTMap and stores additional information useful during th...
A four-byte chunk ID appearing in an "IFF" file.
Definition: iffId.h:29
IffId get_id() const
Returns the ID associated with this chunk.
Definition: iffChunk.I:31