1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM 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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef GRIM_MODEL_H
24 #define GRIM_MODEL_H
25 
26 #include "engines/grim/object.h"
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();
getFilename()53 	const Common::String &getFilename() const { return _fname; }
getCMap()54 	const ObjectPtr<CMap> &getCMap() const { return _cmap; }
55 
56 	ModelNode *getHierarchy() const;
getNumNodes()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[]);
GeosetGeoset63 		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;
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 
hasTexture()102 	bool hasTexture() const { return _texVertices != nullptr; }
103 
getNormal()104 	const Math::Vector3d &getNormal() const { return _normal; }
setNormal(const Math::Vector3d & normal)105 	void setNormal(const Math::Vector3d &normal) { _normal = normal; }
getMaterial()106 	const Material *getMaterial() const { return _material; }
getNumVertices()107 	int getNumVertices() const { return _numVertices; }
getVertex(int i)108 	int getVertex(int i) const { return _vertices[i]; }
getTextureVertex(int i)109 	int getTextureVertex(int i) const { return _texVertices[i]; }
getLight()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 translateViewpointBack() const;
173 
174 	char _name[64];
175 	Mesh *_mesh;
176 	/**
177 	 * A value of 0x100 (256) specifies that when animating this node, keyframes should not be
178 	 * interpolated (lerped), but instead the transition from source to target is to occur
179 	 * discretely.
180 	 */
181 	int _flags;
182 	/**
183 	 * Each KeyFrameAnim has a type identifier. This type field is a bitmask which is ANDed againts
184 	 * the type in the KeyFrameAnim to control which KeyFrameAnims animate on which nodes of the character.
185 	 * This enables selectively controlling the animations to act only on certain bones.
186 	 */
187 	int _type;
188 	int _depth, _numChildren;
189 	ModelNode *_parent, *_child, *_sibling;
190 	// Specifies the bind pose for this node. This data is read from the model file and never altered
191 	// (could be const).
192 	Math::Vector3d _pos, _pivot;
193 	Math::Quaternion _rot;
194 	// Specifies the animated pose for this node.
195 	Math::Vector3d _animPos;
196 	Math::Quaternion _animRot;
197 	bool _meshVisible, _hierVisible;
198 	bool _initialized;
199 	bool _needsUpdate;
200 	Math::Matrix4 _matrix;
201 	Math::Matrix4 _localMatrix;
202 	Math::Matrix4 _pivotMatrix;
203 	Sprite *_sprite;
204 };
205 
206 } // end of namespace Grim
207 
208 #endif
209