xoreos  0.0.5
model_sonic.h
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 #ifndef GRAPHICS_AURORA_MODEL_SONIC_H
26 #define GRAPHICS_AURORA_MODEL_SONIC_H
27 
28 #include <vector>
29 #include <list>
30 #include <map>
31 
32 #include "glm/vec3.hpp"
33 #include "glm/vec4.hpp"
34 #include "glm/mat4x4.hpp"
35 
36 #include "src/aurora/nitrofile.h"
37 
40 
44 
45 namespace Graphics {
46 
47 namespace Aurora {
48 
49 class ModelNode_Sonic;
50 
116 class Model_Sonic : public Model, public ::Aurora::NitroFile {
117 public:
119  ~Model_Sonic();
120 
121  void render(RenderPass pass);
122 
123 private:
124  // === Loading-time ===
125 
127  kBoneNOP = 0x00,
128  kBoneEnd = 0x01,
133  kBoneConnect1 = 0x06,
139  kBoneConnect2 = 0x26,
140  kBoneEndPair = 0x2B,
143  kBoneConnect3 = 0x46,
145  };
146 
148  kPolygonNOP = 0x00,
149 
150  // Matrix commands
164 
165  // Vertex attributes
166  kPolygonColor = 0x20,
167  kPolygonNormal = 0x21,
169 
170  // Vertex coordinates
177 
180 
182 
183  // Lighting
189 
192 
194 
196 
200  };
201 
203  struct Info {
205 
208  };
209  typedef std::vector<Info> Infos;
210 
211  struct BoneCommand {
213 
214  std::vector<uint8> parameters;
215 
217  };
218  typedef std::list<BoneCommand> BoneCommands;
219 
220  struct PolygonCommand {
222 
223  std::vector<uint32> parameters;
224 
226  };
227  typedef std::vector<PolygonCommand> PolygonCommands;
228 
229  struct Bone {
231 
233  glm::mat4 transform;
234 
237 
240 
242  std::list<Bone *> children;
243 
245 
246 
247  Bone() : nodeID(0xFFFF), parentID(0xFFFF), nodeStack(0xFFFF), parentStack(0xFFFF), parent(0),
248  modelNode(0) { }
249  };
250  typedef std::vector<Bone> Bones;
251 
252  struct Material {
256 
259 
260  bool wrapX;
261  bool wrapY;
262  bool flipX;
263  bool flipY;
264 
267  };
268  typedef std::vector<Material> Materials;
269 
270  struct Polygon {
272 
275 
278 
280 
282  };
283  typedef std::vector<Polygon> Polygons;
284 
287  struct StackMix {
290  float ratio;
291 
293 
294  StackMix(uint16 i = 0xFFFF, uint16 s = 0xFFFF, float r = 0.0f) :
295  nodeID(i), nodeStack(s), ratio(r), node(0) { }
296  };
297  typedef std::vector<StackMix> StackMixes;
298  typedef std::map<uint32, StackMixes> StackMixMap;
299 
300  typedef std::list<Bone *> BoneList;
301  typedef std::map<uint8, Bone *> StackBoneMap;
302  typedef std::map<uint8, bool> BoneInvisible;
303 
304 
305  struct ParserContext {
307 
308  // .--- Offsets to different parts of the NSBMD file
311 
316 
319  // '---
320 
321  // .--- The various model parts
322  double defaultScale;
323 
326 
328 
330  // '---
331 
332  // .--- Processed model parts
334 
336 
339  // '---
340 
341  State *state;
342 
343  std::list<ModelNode_Sonic *> nodes;
344 
345  ParserContext(const Common::UString &name);
346  ~ParserContext();
347 
348  void clear();
349  };
350 
351  struct Geometry;
352  struct Primitive;
353 
354  // .--- Loading helpers
355  void newState(ParserContext &ctx);
356  void addState(ParserContext &ctx);
357 
358  void load(ParserContext &ctx);
359 
360  void readModel(ParserContext &ctx);
361  void readHeader(ParserContext &ctx);
362  void readModelHeader(ParserContext &ctx);
363 
364  void readBones(ParserContext &ctx);
365  void readBone(ParserContext &ctx, Bone &bone, Info &info);
366  void readBoneCommands(ParserContext &ctx);
367 
368  void readMaterials(ParserContext &ctx);
370  void readMaterialDefinition(ParserContext &ctx, Material &material, Info &info);
371  void readMaterialResource(ParserContext &ctx, uint textureOrPalette);
372 
373  void readPolygons(ParserContext &ctx);
374  void readPolygon(ParserContext &ctx, Polygon &polygon, Info &info);
375  void readPolygonCommands(ParserContext &ctx, Polygon &polygon, uint32 listSize);
376 
377  void parseBoneCommands(ParserContext &ctx);
378  void findRootBones(ParserContext &ctx);
379  void findStackBones(ParserContext &ctx);
380 
381  void createModelNodes(ParserContext &ctx);
382 
383  void findStackMixes(ParserContext &ctx);
384 
385  void createGeometry(ParserContext &ctx);
386  void createPrimitives(ParserContext &ctx, Geometry &geometry, Polygon &polygon);
387 
388  void createIndices(Primitive &primitive);
389  void createIndicesTriangles(Primitive &primitive);
390  void createIndicesTriangleStrip(Primitive &primitive);
391  void createIndicesQuads(Primitive &primitive);
392  void createIndicesQuadStrip(Primitive &primitive);
393 
394  void createBound();
395 
396  uint8 readInfoOffset(ParserContext &ctx, Infos &infos, uint32 offset);
397  uint8 readInfoOffsetCount(ParserContext &ctx, Infos &infos, uint32 offset);
398  // '---
399 
400  // .--- Utility methods
402  static double getFixedPoint(uint32 value, bool sign, uint8 iBits, uint8 fBits);
403 
407  static uint8 getBoneParameterCount(BoneCommandID cmd, uint8 count);
408 
410  static glm::mat4 createPivot(double a, double b, uint8 select, uint8 negate);
411  // '---
412 
413  // === Run-time ===
414 
420  };
421 
423  struct PrimitiveNode {
425  float ratio;
426 
427  PrimitiveNode(ModelNode_Sonic *n = 0, float r = 0.0f) : node(n), ratio(r) { }
428 
429  PrimitiveNode &operator=(const StackMix &mix) { node = mix.node; ratio = mix.ratio; return *this; }
430  PrimitiveNode(const StackMix &mix) { *this = mix; }
431  };
432  typedef std::vector<PrimitiveNode> PrimitiveNodes;
433 
437 
438  glm::vec3 vertex;
439  glm::vec3 normal;
440  glm::vec4 color;
441  glm::vec3 texCoord;
442 
443  PrimitiveVertex() : vertex(0.0f, 0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f),
444  color(1.0f, 1.0f, 1.0f, 1.0f), texCoord(0.0f, 0.0f, 0.0f) { }
445  };
446  typedef std::vector<PrimitiveVertex> PrimitiveVertices;
447  typedef std::vector<uint16> PrimitiveIndices;
448 
449  struct Primitive {
451 
452  bool invalid;
453 
456 
459 
461  };
462  typedef std::vector<Primitive> Primitives;
463 
464  struct Geometry {
466 
468  };
469  typedef std::vector<Geometry> Geometries;
470 
472 
473  void evaluateGeometry();
474  void evaluatePrimitive(Primitive &primitive);
475 
476  friend class ModelNode_Sonic;
477 };
478 
479 class ModelNode_Sonic : public ModelNode {
480 public:
481  ModelNode_Sonic(Model &model);
483 
485 
486 private:
488  void createAbsoluteBound();
489 
490  friend class Model_Sonic;
491 };
492 
493 } // End of namespace Aurora
494 
495 } // End of namespace Graphics
496 
497 #endif // GRAPHICS_AURORA_MODEL_SONIC_H
Connect bones, set child stack, inherit parent stack.
Definition: model_sonic.h:139
void createIndicesQuadStrip(Primitive &primitive)
void findStackBones(ParserContext &ctx)
void readBones(ParserContext &ctx)
std::vector< Geometry > Geometries
Definition: model_sonic.h:469
Vertex XYZ, 16bit fixed-point.
Definition: model_sonic.h:171
void findStackMixes(ParserContext &ctx)
This is a wrapper around SeekableSubReadStream, but it adds non-endian read methods whose endianness ...
Definition: readstream.h:383
Structure we use to represent the parameters of a LoadStack bone command.
Definition: model_sonic.h:287
void createIndices(Primitive &primitive)
void readBone(ParserContext &ctx, Bone &bone, Info &info)
void evaluatePrimitive(Primitive &primitive)
Vertex XY, 16bit fixed-point, re-use Z.
Definition: model_sonic.h:173
uint8 scaleY
Scale (1 or 2) the texture in Y direction.
Definition: model_sonic.h:266
PolygonCommand(PolygonCommandID cmd=kPolygonNOP)
Definition: model_sonic.h:225
Common::SeekableSubReadStreamEndian * nsbmd
Definition: model_sonic.h:306
A class holding an UTF-8 string.
Definition: ustring.h:48
Structure to represent a StackMix at run-time.
Definition: model_sonic.h:423
static double getFixedPoint(uint32 value, bool sign, uint8 iBits, uint8 fBits)
Convert the Nintendo DS fixed-point values into floating point doubles.
Connect bones, set parent stack, inherit child stack.
Definition: model_sonic.h:143
uint8 scaleX
Scale (1 or 2) the texture in X direction.
Definition: model_sonic.h:265
uint16 parentStack
Matrix stack position the parent uses.
Definition: model_sonic.h:239
A 3D model in Nintendo&#39;s NSBMD format.
Definition: model_sonic.h:116
uint8_t uint8
Definition: types.h:200
void readMaterialDefinitions(ParserContext &ctx)
std::vector< Bone > Bones
Definition: model_sonic.h:250
A real object in the game world.
Definition: types.h:52
Vertex XZ, 16bit fixed-point, re-use Y.
Definition: model_sonic.h:174
BoneCommand(BoneCommandID cmd=kBoneNOP)
Definition: model_sonic.h:216
std::vector< Polygon > Polygons
Definition: model_sonic.h:283
Multiply a line vector by the directional vector.
Definition: model_sonic.h:199
void createGeometry(ParserContext &ctx)
Set default stack position for a polygon.
Definition: model_sonic.h:130
End marker for bone command list.
Definition: model_sonic.h:128
std::vector< PolygonCommand > PolygonCommands
Definition: model_sonic.h:227
Push current matrix onto stack.
Definition: model_sonic.h:152
Multiply a line vector by the clip matrix.
Definition: model_sonic.h:198
uint16 nodeStack
Matrix stack position this bone uses.
Definition: model_sonic.h:238
void load(Model_Sonic::ParserContext &ctx, Model_Sonic::Bone &bone)
A index buffer.
std::map< uint8, bool > BoneInvisible
Definition: model_sonic.h:302
Bone * parent
Pointer to the parent bones.
Definition: model_sonic.h:241
A handle to an Aurora texture.
uint8 readInfoOffsetCount(ParserContext &ctx, Infos &infos, uint32 offset)
uint32 primitiveSize
Maximum length of a primitive from this polygon.
Definition: model_sonic.h:277
Connect bones, inherit child and parent stack.
Definition: model_sonic.h:133
bool wrapY
true: wrap, false: clamp.
Definition: model_sonic.h:261
Multiply current matrix by 3x3 values.
Definition: model_sonic.h:161
void readBoneCommands(ParserContext &ctx)
void readMaterials(ParserContext &ctx)
uint32 primitiveCount
Number of primitive this polygon produces.
Definition: model_sonic.h:276
std::list< ModelNode_Sonic * > nodes
Definition: model_sonic.h:343
void render(RenderPass pass)
Render the object.
Info structure, specifies names and offsets of all kinds of lists in Nintendo files.
Definition: model_sonic.h:203
void readMaterialDefinition(ParserContext &ctx, Material &material, Info &info)
Load 4x4 values into current matrix.
Definition: model_sonic.h:157
Store current matrix onto stack.
Definition: model_sonic.h:154
Connect bones, set child and parent stack.
Definition: model_sonic.h:144
RenderPass
Definition: types.h:97
std::vector< Primitive > Primitives
Definition: model_sonic.h:462
ModelType
The display type of a model.
Definition: types.h:51
std::vector< PrimitiveVertex > PrimitiveVertices
Definition: model_sonic.h:446
uint16_t uint16
Definition: types.h:202
Set specular reflection and emission colors.
Definition: model_sonic.h:185
uint8 readInfoOffset(ParserContext &ctx, Infos &infos, uint32 offset)
Test if box is inside view volume.
Definition: model_sonic.h:197
bool flipY
true: flip on every 2nd texture wrap.
Definition: model_sonic.h:263
void info(const char *s,...)
Definition: util.cpp:69
std::list< Bone * > BoneList
Definition: model_sonic.h:300
void readPolygonCommands(ParserContext &ctx, Polygon &polygon, uint32 listSize)
A node within a 3D model.
A 3D model of an object.
uint16 nodeID
ID of this bone.
Definition: model_sonic.h:235
Multiply current matrix by scale matrix.
Definition: model_sonic.h:162
Buffer containing vertex data.
Definition: vertexbuffer.h:68
Restore current matrix from stack.
Definition: model_sonic.h:155
Vertex XYZ, 10bit fixed-point.
Definition: model_sonic.h:172
void newState(ParserContext &ctx)
std::vector< uint16 > PrimitiveIndices
Definition: model_sonic.h:447
std::list< Bone * > children
Pointers to the child bones.
Definition: model_sonic.h:242
Load 4x3 values into current matrix.
Definition: model_sonic.h:158
std::vector< Material > Materials
Definition: model_sonic.h:268
Vertex YZ, 16bit fixed-point, re-use X.
Definition: model_sonic.h:175
Multiply current matrix by 4x4 values.
Definition: model_sonic.h:159
bool flipX
true: flip on every 2nd texture wrap.
Definition: model_sonic.h:262
void addState(ParserContext &ctx)
StackMix(uint16 i=0xFFFF, uint16 s=0xFFFF, float r=0.0f)
Definition: model_sonic.h:294
Set diffuse and ambient reflection colors.
Definition: model_sonic.h:184
ParserContext(const Common::UString &name)
Definition: model_sonic.cpp:83
void readMaterialResource(ParserContext &ctx, uint textureOrPalette)
glm::mat4 transform
Complete local transformation this bone specifies.
Definition: model_sonic.h:233
void createPrimitives(ParserContext &ctx, Geometry &geometry, Polygon &polygon)
uint16 parentID
ID of parent bone.
Definition: model_sonic.h:236
ModelNode_Sonic * modelNode
Model node this bone represents.
Definition: model_sonic.h:244
std::vector< PrimitiveNode > PrimitiveNodes
Definition: model_sonic.h:432
void readModelHeader(ParserContext &ctx)
Set specular reflection shininess.
Definition: model_sonic.h:188
void createIndicesTriangleStrip(Primitive &primitive)
Intermediate structure to (re)create the VBO/IBO from.
Definition: model_sonic.h:435
Multiply current matrix by 4x3 values.
Definition: model_sonic.h:160
uint32_t uint32
Definition: types.h:204
static uint8 getBoneParameterCount(BoneCommandID cmd, uint8 count)
Return the number of parameters required for this bone command.
PrimitiveNode & operator=(const StackMix &mix)
Definition: model_sonic.h:429
Base class for Nitro (Nintendo DS) files.
void parseBoneCommands(ParserContext &ctx)
void load(ParserContext &ctx)
bool wrapX
true: wrap, false: clamp.
Definition: model_sonic.h:260
std::vector< StackMix > StackMixes
Definition: model_sonic.h:297
void createIndicesTriangles(Primitive &primitive)
void readPolygon(ParserContext &ctx, Polygon &polygon, Info &info)
std::list< BoneCommand > BoneCommands
Definition: model_sonic.h:218
Buffer containing indices data.
Definition: indexbuffer.h:33
#define pass
Definition: fft.cpp:257
std::map< uint8, Bone * > StackBoneMap
Definition: model_sonic.h:301
void createAbsoluteBound()
Forward to ModelNode::createAbsoluteBound(), because we need this during the loading.
void createIndicesQuads(Primitive &primitive)
Common::UString texture
Name of texture within NSBTX.
Definition: model_sonic.h:254
void readPolygons(ParserContext &ctx)
void createModelNodes(ParserContext &ctx)
void findRootBones(ParserContext &ctx)
std::vector< Info > Infos
Definition: model_sonic.h:209
void readHeader(ParserContext &ctx)
std::map< uint32, StackMixes > StackMixMap
Definition: model_sonic.h:298
A handle to a texture.
Definition: texturehandle.h:51
Common::UString palette
Name of palette within NSBTX.
Definition: model_sonic.h:255
Primitive(PrimitiveType t=kPrimitiveTypeTriangles)
Definition: model_sonic.h:460
PrimitiveNode(ModelNode_Sonic *n=0, float r=0.0f)
Definition: model_sonic.h:427
Multiply current matrix by translation matrix.
Definition: model_sonic.h:163
A vertex buffer.
Model_Sonic(const Common::UString &name, ModelType type=kModelTypeObject)
void readModel(ParserContext &ctx)
static glm::mat4 createPivot(double a, double b, uint8 select, uint8 negate)
Create a specific pivot matrix.
unsigned int uint
Definition: types.h:211
static uint8 getPolygonParameterCount(PolygonCommandID cmd)
Return the number of parameters required for this geometry command.
Vertex XYZ delta, 10bit fixed-point.
Definition: model_sonic.h:176