Niotso  git revision 558726a9f13d7c3423a683dd2f4323589b66c310
The New Implementation of The Sims Online
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
libvitaboy.hpp
Go to the documentation of this file.
1 /*
2  libvitaboy - Open source OpenGL TSO character animation library
3  libvitaboy.hpp - Copyright (c) 2012 Niotso Project <http://niotso.org/>
4  Author(s): Fatbag <X-Fi6@phppoll.org>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 
19 #ifndef LIBVITABOY_HPP
20 #define LIBVITABOY_HPP
21 
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <FileHandler.hpp>
27 
28 /****
29 ** Bytestream
30 */
31 
32 class VBFile_t {
33  private:
34  const uint8_t *Buffer, *Position;
35  unsigned Size;
36 
37  public:
38  inline void set(const void *_Buffer, unsigned _Size){
39  Buffer = (const uint8_t*) _Buffer;
40  Position = (const uint8_t*) _Buffer;
41  Size = _Size;
42  }
43 
44  inline unsigned getpos(){
45  return Position-Buffer;
46  }
47 
48  inline void seekto(unsigned offset){
49  Position = Buffer+offset;
50  }
51  inline void seekahead(unsigned count){
52  Position += count;
53  }
54  inline void seekback(unsigned count){
55  Position -= count;
56  }
57 
58  inline uint32_t readint32(){
59  uint32_t value = (uint32_t)((Position[0]<<(8*3)) | (Position[1]<<(8*2)) | (Position[2]<<(8*1)) | (Position[3]<<(8*0)));
60  Position += 4;
61  return value;
62  }
63 
64  inline uint32_t readint16(){
65  uint16_t value = (uint16_t)((Position[0]<<(8*1)) | (Position[1]<<(8*0)));
66  Position += 2;
67  return value;
68  }
69 
70  inline uint32_t readint8(){
71  uint8_t value = (uint8_t)((Position[0]<<(8*0)));
72  Position += 1;
73  return value;
74  }
75 
76  inline float readfloat(){
77  union { uint32_t i; float f; } value;
78  value.i = (uint32_t)((Position[0]<<(8*0)) | (Position[1]<<(8*1)) | (Position[2]<<(8*2)) | (Position[3]<<(8*3)));
79  Position += 4;
80  return value.f;
81  }
82 
83  inline void readbytes(void* Destination, unsigned length){
84  memcpy(Destination, Position, length);
85  Position += length;
86  }
87 
88  inline char* readstring(){
89  //Read a Pascal string with 1 length byte
90  unsigned length = readint8();
91  char *string = (char*) malloc(length+1);
92  readbytes(string, length);
93  string[length] = '\0';
94  return string;
95  }
96 
97  inline char* readstring2(){
98  //Read a Pascal string with 2 length bytes
99  unsigned length = readint16();
100  char *string = (char*) malloc(length+1);
101  readbytes(string, length);
102  string[length] = '\0';
103  return string;
104  }
105 };
106 
107 extern VBFile_t VBFile;
108 
109 /****
110 ** Common
111 */
112 
113 enum ReadGroup {
116 };
117 
119  float x, y, z;
120 };
121 
122 struct Rotation_t {
123  float x, y, z, w;
124 };
125 
127  char * Key;
128  char * Value;
129 };
130 
131 struct Prop_t {
132  uint32_t EntriesCount;
134 };
135 
136 struct PropsList_t {
137  uint32_t PropsCount;
139 };
140 
141 void ReadAsset(Asset_t& Asset, bool ReadGroup);
142 void ReadPropEntry(KeyValuePair_t& Entry);
143 void ReadPropEntries(Prop_t& Prop);
144 void ReadPropsList(PropsList_t& PropsList);
145 float DotProduct(Rotation_t * q1, Rotation_t * q2);
146 void Normalize(Rotation_t * q);
147 void CombineQuaternions(Rotation_t * Destination, Rotation_t * Source);
148 void FindQuaternionMatrix(float * Matrix, Rotation_t * Quaternion);
149 
150 
151 /****
152 ** Animation (*.anim)
153 */
154 
155 struct TimeProp_t {
156  uint32_t ID;
158 };
159 
161  uint32_t TimePropsCount;
163 };
164 
165 struct Motion_t {
166  uint32_t Unknown;
167  char * BoneName;
168  uint32_t FrameCount;
169  float Duration; //Converted to seconds
170  uint8_t HasTranslation;
171  uint8_t HasRotation;
173  uint32_t FirstRotation;
176 
177  uint8_t HasPropsLists;
178  uint32_t PropsListsCount;
180 
184 };
185 
186 struct Animation_t {
187  uint32_t Version;
188  char * Name;
189  float Duration; //Converted to seconds
190  float Distance;
191  uint8_t IsMoving;
193  uint32_t RotationsCount;
194  uint32_t MotionsCount;
195 
197  unsigned RotationsOffset;
198 
200 };
201 
203 void ReadMotion(Animation_t& Animation, Motion_t& Motion);
204 void ReadPropsLists(Motion_t& Motion);
205 void ReadTimePropsList(TimePropsList_t& TimePropsList);
206 void ReadTimePropsLists(Motion_t& Motion);
207 
208 
209 /****
210 ** Appearance (*.apr)
211 */
212 
213 struct Appearance_t {
214  uint32_t Version;
216  uint32_t BindingCount;
218 };
219 
220 void ReadAppearance(Appearance_t& Appearance);
221 
222 
223 /****
224 ** Binding (*.bnd)
225 */
226 
227 struct Binding_t {
228  uint32_t Version;
229  char * BoneName;
230  uint32_t MeshDef;
232  uint32_t AppearanceDef;
234 };
235 
236 void ReadBinding(Binding_t& Binding);
237 
238 
239 /****
240 ** Collection (*.col)
241 */
242 
243 struct PODef_t {
244  uint32_t Index;
246 };
247 
248 struct Collection_t {
249  uint32_t POCount;
251 };
252 
253 void ReadCollection(Collection_t& Collection);
254 
255 
256 /****
257 ** Hand Group (*.hag)
258 */
259 
260 struct HandGroup_t {
261  uint32_t Version;
263 };
264 
265 void ReadHandGroup(HandGroup_t& HandGroup);
266 
267 
268 /****
269 ** Mesh (*.mesh)
270 */
271 
273  float u, v;
274 };
275 
276 struct Coord_t {
277  float x, y, z;
278 };
279 
281  float u, v;
282 };
283 
285  float x, y, z;
286 };
287 
288 struct BlendData_t {
289  float Weight;
290  unsigned OtherVertex;
291 };
292 
293 struct Vertex_t {
297 
298  unsigned BoneIndex;
300 };
301 
302 struct Face_t {
303  unsigned VertexA, VertexB, VertexC;
304 };
305 
307  unsigned BoneIndex;
308  unsigned FirstRealVertex;
309  unsigned RealVertexCount;
312 };
313 
314 struct Mesh_t {
315  uint32_t Version;
316  uint32_t BoneCount;
317  char ** BoneNames;
318  uint32_t FaceCount;
320  uint32_t BindingCount;
322  uint32_t RealVertexCount;
327 };
328 
329 void ReadMesh(Mesh_t& Mesh);
330 
331 
332 /****
333 ** Outfit (*.oft)
334 */
335 
340 };
341 
345 };
346 
347 struct Outfit_t {
348  uint32_t Version;
349  uint32_t Unknown;
351  uint32_t Group;
352  uint32_t Region;
353 };
354 
355 void ReadOutfit(Outfit_t& Outfit);
356 
357 
358 /****
359 ** Purchasable Outfit (*.po)
360 */
361 
363  uint32_t Version;
364  uint32_t Unknown;
365  uint32_t OutfitDef;
367  uint32_t CollectionDef;
369 };
370 
371 void ReadPurchasableOutfit(PurchasableOutfit_t& PurchasableOutfit);
372 
373 
374 /****
375 ** Skeleton (*.skel)
376 */
377 
378 struct Bone_t {
379  uint32_t Unknown;
380  char * Name;
381  char * ParentsName;
382  uint8_t HasProps;
386  uint32_t CanTranslate;
387  uint32_t CanRotate;
388  uint32_t CanBlend;
389  float WiggleValue;
390  float WigglePower;
391 
392  unsigned ChildrenCount;
394 };
395 
396 struct Skeleton_t {
397  uint32_t Version;
398  char * Name;
399  uint16_t BoneCount;
401 };
402 
403 void ReadSkeleton(Skeleton_t& Bone);
404 void ReadBone(Skeleton_t& Skeleton, Bone_t& Bone, unsigned Index);
405 unsigned FindBone(Skeleton_t& Skeleton, const char * BoneName, unsigned Count);
406 
407 #endif