xoreos  0.0.5
walkmesh.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/scopedptr.h"
26 
27 #include "src/aurora/resman.h"
28 
30 
31 namespace Engines {
32 
33 namespace KotOR {
34 
35 void Walkmesh::load(const Common::UString &resRef, ::Aurora::FileType type, const glm::mat4 &transform) {
36  _vertices.clear();
37  _indices.clear();
38  _faceWalkableMap.clear();
39 
40  Common::ScopedPtr<Common::SeekableReadStream> bwm(ResMan.getResource(resRef, type));
41 
42  if (!bwm) {
43  warning("Walkmesh file not found: %s", resRef.c_str());
44  return;
45  }
46 
47  try {
48  appendFromStream(*bwm, transform);
50  } catch (Common::Exception &e) {
51  warning("Walkmesh load failed: %s %s", resRef.c_str(), e.what());
52  }
53 }
54 
56  const glm::mat4 &transform) {
57  stream.seek(0);
58 
67  if (stream.readUint32BE() != MKTAG('B', 'W', 'M', ' ') ||
68  stream.readUint32BE() != MKTAG('V', '1', '.', '0'))
69  throw Common::Exception("Invalid BWM version");
70 
71  stream.skip(64);
72 
73  uint32 vertexCount = stream.readUint32LE();
74  uint32 vertexOffset = stream.readUint32LE();
75  uint32 faceCount = stream.readUint32LE();
76  uint32 faceOffset = stream.readUint32LE();
77  uint32 faceTypeOffset = stream.readUint32LE();
78 
79  //-
80 
81  appendFaceTypes(stream, faceCount, faceTypeOffset);
82  appendIndices(stream, faceCount, faceOffset);
83  appendVertices(stream, vertexCount, vertexOffset, transform);
84 }
85 
87  uint32 faceCount,
88  uint32 faceTypeOffset) {
89  stream.seek(faceTypeOffset);
90 
91  size_t prevFaceCount = _faceWalkableMap.size();
92  _faceWalkableMap.resize(prevFaceCount + faceCount);
93  uint32 index = prevFaceCount;
94 
95  for (uint32 i = 0; i < faceCount; ++i) {
96  uint32 faceType = stream.readUint32LE();
97  switch (faceType) {
98  case 0:
99  case 2:
100  case 7:
101  case 8:
102  _faceWalkableMap[index++] = false;
103  break;
104  default:
105  _faceWalkableMap[index++] = true;
106  break;
107  }
108  }
109 }
110 
112  uint32 faceCount,
113  uint32 faceOffset) {
114  stream.seek(faceOffset);
115 
116  size_t prevIndexCount = _indices.size();
117  uint32 indexCount = 3 * faceCount;
118  _indices.resize(prevIndexCount + indexCount);
119  uint32 index = prevIndexCount;
120  size_t prevVertexCount = _vertices.size() / 3;
121 
122  for (uint32 i = 0; i < indexCount; ++i) {
123  _indices[index++] = prevVertexCount + stream.readUint32LE();
124  }
125 }
126 
127 static void multiply(const float *v, const glm::mat4 &m, float *rv) {
128  rv[0] = v[0] * m[0][0] + v[1] * m[1][0] + v[2] * m[2][0] + m[3][0];
129  rv[1] = v[0] * m[0][1] + v[1] * m[1][1] + v[2] * m[2][1] + m[3][1];
130  rv[2] = v[0] * m[0][2] + v[1] * m[1][2] + v[2] * m[2][2] + m[3][2];
131  float w = v[0] * m[0][3] + v[1] * m[1][3] + v[2] * m[2][3] + m[3][3];
132  rv[0] /= w;
133  rv[1] /= w;
134  rv[2] /= w;
135 }
136 
138  uint32 vertexCount,
139  uint32 vertexOffset,
140  const glm::mat4 &transform) {
141  stream.seek(vertexOffset);
142 
143  size_t prevVerticesSize = _vertices.size();
144  uint32 verticesSize = 3 * vertexCount;
145  _vertices.resize(prevVerticesSize + verticesSize);
146  uint32 index = prevVerticesSize;
147 
148  for (uint32 i = 0; i < vertexCount; ++i) {
149  float v[3];
150  v[0] = stream.readIEEEFloatLE();
151  v[1] = stream.readIEEEFloatLE();
152  v[2] = stream.readIEEEFloatLE();
153 
154  float rv[3];
155  multiply(v, transform, rv);
156 
157  _vertices[index + 0] = rv[0];
158  _vertices[index + 1] = rv[1];
159  _vertices[index + 2] = rv[2];
160 
161  index += 3;
162  }
163 }
164 
165 } // End of namespace KotOR
166 
167 } // End of namespace Engines
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
#define MKTAG(a0, a1, a2, a3)
A wrapper macro used around four character constants, like &#39;DATA&#39;, to ensure portability.
Definition: endianness.h:140
static void multiply(const float *v, const glm::mat4 &m, float *rv)
Definition: walkmesh.cpp:127
void appendFromStream(Common::SeekableReadStream &stream, const glm::mat4 &transform)
Definition: walkmesh.cpp:55
uint32 readUint32LE()
Read an unsigned 32-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:133
A class holding an UTF-8 string.
Definition: ustring.h:48
std::vector< uint32 > _indices
Definition: walkmesh.h:70
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
void appendIndices(Common::SeekableReadStream &stream, uint32 faceCount, uint32 faceOffset)
Definition: walkmesh.cpp:111
void appendFaceTypes(Common::SeekableReadStream &stream, uint32 faceCount, uint32 faceTypeOffset)
Definition: walkmesh.cpp:86
Exception that provides a stack of explanations.
Definition: error.h:36
A simple scoped smart pointer template.
void appendVertices(Common::SeekableReadStream &stream, uint32 vertexCount, uint32 vertexOffset, const glm::mat4 &transform)
Definition: walkmesh.cpp:137
std::vector< bool > _faceWalkableMap
Definition: walkmesh.h:71
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
virtual size_t skip(ptrdiff_t offset)
Skip the specified number of bytes, adding that offset to the current position in the stream...
Definition: readstream.h:317
std::vector< float > _vertices
Definition: walkmesh.h:69
void warning(const char *s,...)
Definition: util.cpp:33
uint32 readUint32BE()
Read an unsigned 32-bit word stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:166
FORCEINLINE float readIEEEFloatLE()
Read a 32-bit IEEE float stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:230
KotOR walkmesh (BWM).
uint32_t uint32
Definition: types.h:204
FileType
Various file types used by the Aurora engine and found in archives.
Definition: types.h:56
const char * what() const
Definition: error.cpp:73
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void load(const Common::UString &resRef, ::Aurora::FileType type=::Aurora::kFileTypeWOK, const glm::mat4 &transform=glm::mat4())
Definition: walkmesh.cpp:35
The global resource manager for Aurora resources.