ScummVM API documentation
spriter.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
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  * Spriter - 3D Renderer
22  */
23 
24 #ifndef TINSEL_SPRITER_H
25 #define TINSEL_SPRITER_H
26 
27 #include "tinsel/dw.h"
28 #include "common/rect.h"
29 #include "common/stack.h"
30 #include "common/str.h"
31 
32 #include "math/vector3d.h"
33 #include "math/vector2d.h"
34 #include "math/matrix4.h"
35 
36 #if defined(USE_TINYGL)
37 #include "graphics/tinygl/tinygl.h"
38 #endif
39 
40 namespace Tinsel {
41 
42 typedef Common::FixedStack<Math::Matrix4, 30> MatrixStack;
43 
44 enum RenderProgramOp : uint16 {
45  MATRIX_DUPLICATE = 1,
46  MATRIX_REMOVE = 2,
47  UNUSED = 3,
48  TRANSFORM = 4,
49  TRANSLATE_X = 5,
50  TRANSLATE_Y = 6,
51  TRANSLATE_Z = 7,
52  TRANSLATE_XYZ = 8,
53  ROTATE_X = 9,
54  ROTATE_Y = 10,
55  ROTATE_Z = 11,
56  ROTATE_XYZ = 17,
57  STOP = 16,
58 };
59 
60 struct AnimationInfo {
61  Common::String name;
62 
63  uint meshNum;
64 
65  uint translateTablesHunk;
66  uint translateTables;
67  uint translateNum;
68 
69  uint rotateTablesHunk;
70  uint rotateTables;
71  uint rotateNum;
72 
73  uint scaleTablesHunk;
74  uint scaleTables;
75  uint scaleNum;
76 
77  uint maxFrame;
78 };
79 
80 struct MeshInfo {
81  uint meshTablesHunk;
82  uint meshTables;
83 
84  uint programHunk;
85  uint program;
86 };
87 
88 struct Hunk {
90  Common::Array<uint> mappingIdx;
91  uint size;
92  uint flags;
93 };
94 
96 
98 
99 struct Primitive {
100  uint indices[8];
101  uint color;
102  Math::Vector2d uv[4];
103  uint texture;
104 };
105 
106 enum MeshPartType {
107  MESH_PART_TYPE_COLOR,
108  MESH_PART_TYPE_SOLID,
109  MESH_PART_TYPE_TEXTURE,
110 };
111 
112 struct MeshPart {
113  MeshPartType type;
114  uint cull;
115  uint numVertices;
116  Common::Array<Primitive> primitives;
117 };
118 
119 struct Mesh {
124 };
125 
126 struct Meshes {
127  uint vertexCount;
128  uint normalCount;
129 
130  Common::Array<Mesh> meshes;
131 };
132 
134 
135 struct ModelTables {
136  Vectors translations;
137  Vectors rotations;
138  Vectors scales;
139  Meshes meshes;
140 };
141 
142 enum ModelFlags {
143  MODEL_HAS_TRANSLATION_TABLE = 1,
144  MODEL_HAS_SCALE_TABLE = 2,
145  MODEL_HAS_ROTATION_TABLE = 4
146 };
147 
148 struct Model {
149  Hunks hunks;
150  Hunks hunksOverlay; // The game script can load additional data to supplement the main model. E.g. a special animation.
151  uint animationCount;
152  uint field_0xe;
153  uint field_0xf;
154  uint8* program;
155 
156  // animation tables
157  AnimationData startTranslateTables;
158  AnimationData startRotateTables;
159  AnimationData startScaleTables;
160  AnimationData endTranslateTables;
161  AnimationData endRotateTables;
162  AnimationData endScaleTables;
163  int startFrame;
164  int endFrame;
165 
166  uint flags;
167  uint field_0x32;
168  uint field_0x33;
169 
170  ModelTables tables;
171 
172  Math::Vector3d position;
173  Math::Vector3d rotation;
174  Math::Vector3d scale;
175 
176  uint time; // interpolant
177 };
178 
179 struct Viewport {
180  int ap;
181  float width;
182  float height;
183 
184  Common::Rect rect;
185 };
186 
187 struct View {
188  int centerX;
189  int centerY;
190 
191  Common::Rect viewRect;
192  Common::Rect screenRect;
193 
194  Viewport viewport;
195 
196  Math::Vector3d position;
197  Math::Vector3d rotation;
198 };
199 
200 class Spriter {
201 private:
202  MatrixStack _modelMatrix;
203  MatrixStack* _currentMatrix;
204 
206  AnimationInfo _animShadow;
207 
208  MeshInfo _meshMain;
209  Common::Array<MeshInfo> _meshShadow;
210 
211  View _view;
212 
213  Common::Array<uint8> _palette;
214  Common::Array<uint8> _textureData;
215 
216  bool _textureGenerated;
217  uint _texture[4];
218 
219  bool _modelIdle;
220 
221  uint _animId;
222  uint _animSpeed;
223  uint _animDelay;
224  uint _animDelayMax;
225 
226  uint _sequencesCount;
227 
228  uint _direction;
229 
230 public:
231  Model _modelMain;
232  Model _modelShadow;
233 
234 public:
235  Spriter();
236  virtual ~Spriter();
237 
238  void Init(int width, int height);
239  void SetCamera(int rotX, int rotY, int rotZ, int posX, int posY, int posZ, int cameraAp);
240  void TransformSceneXYZ(int x, int y, int z, int& xOut, int& yOut);
241  void Load(const Common::String& modelName, const Common::String& textureName);
242 
243  void SetPalette(SCNHANDLE hPalette);
244 
245  void SetSequence(uint animId, uint delay);
246  Common::Rect Draw(int direction, int x, int y, int z, int tDelta);
247 
248 private:
249  const Math::Matrix4& MatrixCurrent() const;
250 
251  void MatrixReset();
252 
253  void MatrixPop();
254  void MatrixPush();
255  void MatrixTranslate(float x, float y, float z);
256  void MatrixScale(float x, float y, float z);
257  void MatrixRotateX(float angle);
258  void MatrixRotateY(float angle);
259  void MatrixRotateZ(float angle);
260 
261  void SetViewport(int ap);
262 
263  // Loading of the model
264  void LoadH(const Common::String& modelName);
265  void LoadGBL(const Common::String& modelName);
266  void LoadRBH(const Common::String& modelName, Hunks& hunks);
267  void LoadVMC(const Common::String& textureName);
268 
269  void UpdateTextures();
270 
271  Meshes LoadMeshes(const Hunks &hunks, uint hunk, uint offset, int frame);
272  template<bool convert>
273  AnimationData LoadAnimationData(const Hunks &hunks, uint hunk, uint offset);
274  void InitModel(Model& model, MeshInfo& meshInfo, Common::Array<AnimationInfo>& animInfo, uint flags);
275 
276  // Processing of the model
277  void RunRenderProgram(Model &model, bool preprocess);
278 
279  void FindSimilarVertices(Mesh& mesh, Vectors& vertices, Common::Array<uint16>& sameVertices) const;
280  void MergeVertices(Mesh& mesh, Common::Array<uint16>& sameVertices);
281 
282  void TransformMesh(Mesh& mesh, Vectors& vertices);
283  void CalculateNormals(Mesh& mesh, Vectors& vertices, Vectors &normals);
284 
285  // Rendering
286  void RenderModel(Model& model);
287  void RenderMesh(Mesh& mesh, Vectors& vertices, Vectors &normals);
288  void RenderMeshPartColor(MeshPart& part, Vectors& vertices, Vectors &normals);
289  void RenderMeshPartTexture(MeshPart& part, Vectors& vertices, Vectors &normals);
290 
291  // Animation
292  bool SetStartFrame(Model &model, const AnimationInfo &anim, int frame);
293  bool SetEndFrame(Model &model, const AnimationInfo &anim, int frame);
294 };
295 
296 } // End of namespace Tinsel
297 
298 #endif // TINSEL_SPRITER_H
Definition: spriter.h:99
Definition: str.h:59
Definition: spriter.h:88
uint32 SCNHANDLE
Definition: dw.h:31
Definition: spriter.h:119
Definition: spriter.h:187
Definition: rect.h:144
Definition: spriter.h:135
Definition: spriter.h:179
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
Definition: actors.h:36
Definition: spriter.h:148
Definition: spriter.h:60
Definition: spriter.h:80
Definition: spriter.h:126
Definition: spriter.h:200
Definition: spriter.h:112