-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathasp_model.hpp
231 lines (193 loc) · 6.28 KB
/
asp_model.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#pragma once
// ================================================================================================
// -*- C++ -*-
// File: asp_model.hpp
// Author: Guilherme R. Lampert
// Created on: 20/12/14
// Brief: Handler for the Dungeon Siege "Aspects" (ASP) 3D model format.
//
// This project's source code is released under the MIT License.
// - http://opensource.org/licenses/MIT
//
// ================================================================================================
#include "siege/common.hpp"
#include "siege/helper_types.hpp"
namespace siege
{
// ========================================================
// AspModel:
// ========================================================
//
// Importer class for a Dungeon Siege ASPECT (.ASP) 3D model.
// ASP models are used for everything but the terrain.
//
// This importer is largely based on the 3D Max import/export scripts
// from the SiegeTheDay.org forums. Refer to ASPImport.ms for
// the original script code.
//
// You can import an ASP from a file or from a memory buffer.
// The asp2obj command line tool can convert an ASP into a
// static Wavefront OBJ model, which can then be opened in
// a variety of 3D modeling tools and viewers.
//
// Skeleton data is contained in the ASP, but animation frames
// are stored separately in another file with the .PRS extension.
//
// The term "Corner" is used here to mean an interleaved model
// vertex (position, tex-coords, normal, etc). This term was used
// by the Max scripts, so I decided to use it here as well to keep
// the C++ code as close to the original scripts as possible.
//
class AspModel final
: public utils::NonCopyable
{
public:
enum ImportFlags
{
// Default import mode. Loads most of the stuff but ignores some irrelevant data.
Default = 0,
// Load minimal data to import quickly for preview.
QuickImport = 1 << 1,
// Load and validate everything, even the unused stuff of the ASP format.
FullImport = 1 << 2
};
// Shorthand names for internal class usage:
using Vec2 = utils::Vec2;
using Vec3 = utils::Vec3;
using Vec4 = utils::Vec4;
using Vec4b = utils::Vec4b;
// Indexes into the shared corner (vertex) array for a face triangle.
struct TriIndex
{
uint32_t index[3];
};
// A model vertex, which can be thought of as a "corner"...
// "Corner" is the term used in the 3DMax export scripts.
struct WCornerInfo
{
Vec3 pos;
Vec3 normal;
Vec4 weight;
Vec2 texCoord;
Vec4b color;
Vec4b bone;
};
// A simpler model vertex (corner), without animation data.
// This was probably used for static geometry.
struct CornerInfo
{
uint32_t vtxIndex;
Vec3 normal;
Vec2 texCoord;
Vec4b color;
};
struct MatInfo
{
uint32_t textureIndex;
uint32_t faceSpan;
};
struct FaceInfo
{
std::vector<uint32_t> cornerStart;
std::vector<uint32_t> cornerSpan;
std::vector<TriIndex> cornerIndex;
};
struct BoneInfo
{
uint32_t parentIndex;
uint32_t flags;
std::string name;
};
struct SubMesh
{
uint32_t textureCount = 0;
uint32_t vertexCount = 0;
uint32_t cornerCount = 0;
uint32_t faceCount = 0;
uint32_t stitchCount = 0;
std::vector<MatInfo> matInfo; // BSMM
std::vector<Vec3> positions; // BVTX
std::vector<CornerInfo> corners; // BCRN
std::vector<WCornerInfo> wCorners; // WCRN
FaceInfo faceInfo; // BTRI
};
public:
// Construct an empty model.
AspModel() = default;
// Construct from ASP model file.
AspModel(std::string filename, uint32_t importFlags = Default);
// Construct from an ASP model file loaded into memory.
AspModel(ByteArray fileContents, uint32_t importFlags = Default, std::string filename = "");
// Load ASP model from file. Discards current, if any.
void initFromFile(std::string filename, uint32_t importFlags = Default);
// Load ASP model from memory. Discards current, if any.
void initFromMemory(ByteArray fileContents, uint32_t importFlags = Default, std::string filename = "");
// Disposes model data, making this class an empty/invalid model.
void dispose();
// Test if this object has valid model data.
bool isValid() const;
// Source file that originated this model. May be empty if the model was loaded from memory.
const std::string & getSourceFileName() const { return srcFileName; }
// Used by the OBJ exporter tool.
const std::vector<SubMesh> & getSubMeshes() const { return subMeshes; }
const std::vector<BoneInfo> & getBoneInfos() const { return boneInfos; }
const std::vector<std::string> & getTextureNames() const { return textureNames; }
private:
// Reading and handling the ASP data is fairly complex
// and requires some temporary state. This class facilitates that.
class AspImporter final
: public utils::NonCopyable
{
public:
// Imports a model from file data, writing to 'mdl'.
// Might throw an exception on error.
AspImporter(AspModel & mdl, ByteArray fileData,
uint32_t impFlags, const std::string & filename);
private:
// Reader helpers:
void readBytes(void * buffer, size_t numBytes);
uint32_t readU32();
float readF32();
Vec4b readColor();
Vec2 readTexCoord();
Vec3 readVec3();
Vec4 readVec4();
bool readFourCC(FourCC & fcc);
// Readers for each sections of the ASP file format:
void readBMSH();
void readBONH();
void readBSUB();
void readBSMM();
void readBVTX();
void readBCRN();
void readWCRN();
void readBVMP();
void readBTRI();
void readBVWL();
void readSTCH();
void readRPOS();
void readBBOX();
void readBEND();
// The main reading loop. This will branch on each
// ASP section and call one of the above handlers.
void importAspModel();
void validateVersion(const char * sectName, uint32_t version) const;
// Importer state:
AspModel & model;
uint32_t importFlags;
uint32_t currentSubMeshIndex;
size_t readPosition;
const ByteArray fileContents;
const std::string & srcFileName;
};
// Importer can access the internal data structures of AspModel.
friend class AspImporter;
// Model data:
std::vector<SubMesh> subMeshes;
std::vector<BoneInfo> boneInfos;
std::vector<std::string> textureNames;
// Source filename for debug printing.
// May be empty if the model was loaded from memory.
std::string srcFileName;
};
} // namespace siege {}