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 translateViewpointStart() const; 173 void translateViewpointFinish() const; 174 175 char _name[64]; 176 Mesh *_mesh; 177 /** 178 * A value of 0x100 (256) specifies that when animating this node, keyframes should not be 179 * interpolated (lerped), but instead the transition from source to target is to occur 180 * discretely. 181 */ 182 int _flags; 183 /** 184 * Each KeyFrameAnim has a type identifier. This type field is a bitmask which is ANDed againts 185 * the type in the KeyFrameAnim to control which KeyFrameAnims animate on which nodes of the character. 186 * This enables selectively controlling the animations to act only on certain bones. 187 */ 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 210