ScummVM API documentation
model.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef GRIM_MODEL_H
23 #define GRIM_MODEL_H
24 
25 #include "engines/grim/object.h"
26 
27 #include "math/matrix4.h"
28 #include "math/quat.h"
29 
30 namespace Common {
31 class SeekableReadStream;
32 }
33 
34 namespace Grim {
35 
36 class TextSplitter;
37 class Material;
38 class Mesh;
39 class ModelNode;
40 class CMap;
41 class Sprite;
42 
43 class Model : public Object {
44 public:
45  // Construct a 3D model from the given data.
46  Model(const Common::String &filename, Common::SeekableReadStream *data, CMap *cmap, Model *parent = NULL);
47 
48  void reload(CMap *cmap);
49  void draw() const;
50  Material *findMaterial(const char *name, CMap *cmap) const;
51 
52  ~Model();
53  const Common::String &getFilename() const { return _fname; }
54  const ObjectPtr<CMap> &getCMap() const { return _cmap; }
55 
56  ModelNode *getHierarchy() const;
57  int getNumNodes() const { return _numHierNodes; }
58 
59  struct Geoset {
60  void loadBinary(Common::SeekableReadStream *data, Material *materials[]);
61  void loadText(TextSplitter *ts, Material *materials[]);
62  void changeMaterials(Material *materials[]);
63  Geoset() : _numMeshes(0), _meshes(NULL) { }
64  ~Geoset();
65 
66  int _numMeshes;
67  Mesh *_meshes;
68  };
69 
70 //private:
71  void loadMaterial(int index, CMap *cmap);
72  void loadBinary(Common::SeekableReadStream *data);
73  void loadText(TextSplitter *ts);
74 
75  Common::String _fname;
76  ObjectPtr<CMap> _cmap;
77  Model *_parent;
78  int _numMaterials;
79  char (*_materialNames)[32];
80  Material **_materials;
81  bool *_materialsShared;
82  Math::Vector3d _insertOffset;
83  int _numGeosets;
84  Geoset *_geosets;
85  float _radius = 0.0f;
86  int _numHierNodes;
87  ModelNode *_rootHierNode;
88  Math::Vector3d _bboxPos;
89  Math::Vector3d _bboxSize;
90 };
91 
92 class MeshFace {
93 public:
94  MeshFace();
95  ~MeshFace();
96  void stealData(MeshFace &other);
97  int loadBinary(Common::SeekableReadStream *data, Material *materials[]);
98  int loadText(TextSplitter *ts, Material *materials[], int offset);
99  void draw(const Mesh *mesh) const;
100  void changeMaterial(Material *material);
101 
102  bool hasTexture() const { return _texVertices != nullptr; }
103 
104  const Math::Vector3d &getNormal() const { return _normal; }
105  void setNormal(const Math::Vector3d &normal) { _normal = normal; }
106  const Material *getMaterial() const { return _material; }
107  int getNumVertices() const { return _numVertices; }
108  int getVertex(int i) const { return _vertices[i]; }
109  int getTextureVertex(int i) const { return _texVertices[i]; }
110  int getLight() const { return _light; }
111 
112 private:
113  Material *_material;
114  int _type, _geo, _light, _tex;
115  float _extraLight;
116  int _numVertices;
117  int *_vertices, *_texVertices;
118  Math::Vector3d _normal;
119 
120 public:
121  void *_userData;
122 };
123 
124 class Mesh {
125 public:
126  void loadBinary(Common::SeekableReadStream *data, Material *materials[]);
127  void loadText(TextSplitter *ts, Material *materials[]);
128  void changeMaterials(Material *materials[]);
129  void draw() const;
130  void getBoundingBox(int *x1, int *y1, int *x2, int *y2) const;
131  void update();
132  Mesh();
133  ~Mesh();
134 
135  char _name[32];
136  float _radius;
137  int _shadow, _geometryMode, _lightingMode, _textureMode;
138 
139  int _numVertices;
140  int *_materialid;
141  float *_vertices; // sets of 3
142  float *_verticesI;
143  float *_vertNormals; // sets of 3
144 
145  int _numTextureVerts;
146  float *_textureVerts; // sets of 2
147 
148  int _numFaces;
149  MeshFace *_faces;
150  Math::Matrix4 _matrix;
151 
152  void *_userData;
153 
154 private:
155  void sortFaces();
156 };
157 
158 class ModelNode {
159 public:
160  ModelNode();
161  ~ModelNode();
162  void loadBinary(Common::SeekableReadStream *data, ModelNode *hierNodes, const Model::Geoset *g);
163  void draw() const;
164  void getBoundingBox(int *x1, int *y1, int *x2, int *y2) const;
165  void addChild(ModelNode *child);
166  void removeChild(ModelNode *child);
167  void setMatrix(const Math::Matrix4 &matrix);
168  void update();
169  void addSprite(Sprite *sprite);
170  void removeSprite(const Sprite *sprite);
171  void translateViewpoint() const;
172  void translateViewpointStart() const;
173  void translateViewpointFinish() const;
174 
175  char _name[64];
176  Mesh *_mesh;
182  int _flags;
188  int _type;
189  int _depth, _numChildren;
190  ModelNode *_parent, *_child, *_sibling;
191  // Specifies the bind pose for this node. This data is read from the model file and never altered
192  // (could be const).
193  Math::Vector3d _pos, _pivot;
194  Math::Quaternion _rot;
195  // Specifies the animated pose for this node.
196  Math::Vector3d _animPos;
197  Math::Quaternion _animRot;
198  bool _meshVisible, _hierVisible;
199  bool _initialized;
200  bool _needsUpdate;
201  Math::Matrix4 _matrix;
202  Math::Matrix4 _localMatrix;
203  Math::Matrix4 _pivotMatrix;
204  Sprite *_sprite;
205 };
206 
207 } // end of namespace Grim
208 
209 #endif
Definition: str.h:59
Definition: model.h:59
Definition: stream.h:745
Definition: actor.h:33
Definition: material.h:63
Definition: model.h:92
Definition: sprite.h:38
Definition: model.h:124
Definition: algorithm.h:29
Definition: colormap.h:35
int _flags
Definition: model.h:182
Definition: model.h:43
Definition: textsplit.h:35
Definition: model.h:158
Definition: object.h:69
int _type
Definition: model.h:188
Definition: object.h:32