1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | /*! |
---|
17 | \file abstract_model.h |
---|
18 | \brief Definition of an abstract model. containing all needed for other model |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef _ABSTRACT_MODEL_H |
---|
22 | #define _ABSTRACT_MODEL_H |
---|
23 | |
---|
24 | #include "stdincl.h" |
---|
25 | #include "base_object.h" |
---|
26 | #include <vector> |
---|
27 | #include <math.h> |
---|
28 | #include <SDL_image.h> |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | //! this is a small and performant 3D vector |
---|
35 | typedef float sVec3D[3]; |
---|
36 | |
---|
37 | //! small and performant 2D vector |
---|
38 | typedef float sVec2D[2]; |
---|
39 | |
---|
40 | //! compressed vertex data: char insetead of float, the value will be expanded by the scale value. only for loading |
---|
41 | typedef struct |
---|
42 | { |
---|
43 | byte v[3]; |
---|
44 | unsigned char lightNormalIndex; |
---|
45 | } sVertex; |
---|
46 | |
---|
47 | //! compressed texture offset data: coords scaled by the texture size. Only for loading |
---|
48 | typedef struct |
---|
49 | { |
---|
50 | short s,t; |
---|
51 | } sTexCoor; |
---|
52 | |
---|
53 | |
---|
54 | //! holds tha informations about a md2 frame |
---|
55 | typedef struct |
---|
56 | { |
---|
57 | sVec3D scale; //!< scales values of the model |
---|
58 | sVec3D translate; //!< translates the model |
---|
59 | char name[16]; //!< frame name: something like "run32" |
---|
60 | sVertex pVertices[1]; //!< first vertex of thes frame |
---|
61 | } sFrame; |
---|
62 | |
---|
63 | |
---|
64 | //! holds the information about a triangle |
---|
65 | typedef struct |
---|
66 | { |
---|
67 | short indexToVertices[3]; //!< index to the verteces of the triangle |
---|
68 | short indexToTexCoor[3]; //!< index to the texture coordinates |
---|
69 | } sTriangle; |
---|
70 | |
---|
71 | |
---|
72 | //! a md2 animation definition |
---|
73 | typedef struct |
---|
74 | { |
---|
75 | int firstFrame; //!< first frame of the animation |
---|
76 | int lastFrame; //!< last frame of the animation |
---|
77 | int fps; //!< speed: number of frames per second |
---|
78 | } sAnim; |
---|
79 | |
---|
80 | //! animation state definition |
---|
81 | typedef struct |
---|
82 | { |
---|
83 | int startFrame; |
---|
84 | int endFrame; |
---|
85 | int fps; |
---|
86 | |
---|
87 | float localTime; |
---|
88 | float lastTime; |
---|
89 | float interpolationState; //!< the state of the animation [0..1] |
---|
90 | |
---|
91 | int type; //!< animation type |
---|
92 | |
---|
93 | int currentFrame; |
---|
94 | int nextFrame; |
---|
95 | } sAnimState; |
---|
96 | |
---|
97 | |
---|
98 | //! This is our 3D point class. CONFLICTING with Vector.cc |
---|
99 | class CVector3 |
---|
100 | { |
---|
101 | public: |
---|
102 | float x, y, z; |
---|
103 | }; |
---|
104 | |
---|
105 | //! This is our 2D point class. CONFLICTING with Vector.cc |
---|
106 | class CVector2 |
---|
107 | { |
---|
108 | public: |
---|
109 | float x, y; |
---|
110 | }; |
---|
111 | |
---|
112 | // CONFLICTING with model.h |
---|
113 | struct tFace |
---|
114 | { |
---|
115 | int vertIndex[3]; // indicies for the verts that make up this triangle |
---|
116 | int coordIndex[3]; // indicies for the tex coords to texture this face |
---|
117 | }; |
---|
118 | |
---|
119 | // CONFLICTING with material.cc, there are some small differences, i will use this struct and switch over later |
---|
120 | struct tMaterialInfo |
---|
121 | { |
---|
122 | char strName[255]; // The texture name |
---|
123 | char strFile[255]; // The texture file name (If this is set it's a texture map) |
---|
124 | byte color[3]; // The color of the object (R, G, B) |
---|
125 | int texureId; // the texture ID |
---|
126 | float uTile; // u tiling of texture |
---|
127 | float vTile; // v tiling of texture |
---|
128 | float uOffset; // u offset of texture |
---|
129 | float vOffset; // v offset of texture |
---|
130 | } ; |
---|
131 | |
---|
132 | //! properties of a 3D object |
---|
133 | struct t3DObject |
---|
134 | { |
---|
135 | int numOfVerts; // The number of verts in the model |
---|
136 | int numOfFaces; // The number of faces in the model |
---|
137 | int numTexVertex; // The number of texture coordinates |
---|
138 | int materialID; // The texture ID to use, which is the index into our texture array |
---|
139 | bool bHasTexture; // This is TRUE if there is a texture map for this object |
---|
140 | char strName[255]; // The name of the object |
---|
141 | CVector3 *pVerts; // The object's vertices |
---|
142 | CVector3 *pNormals; // The object's normals |
---|
143 | CVector2 *pTexVerts; // The texture's UV coordinates |
---|
144 | tFace *pFaces; // The faces information of the object |
---|
145 | }; |
---|
146 | |
---|
147 | // CONFLICTING with animation.cc |
---|
148 | struct tAnimationInfo |
---|
149 | { |
---|
150 | char strName[255]; // This stores the name of the animation (Jump, Pain, etc..) |
---|
151 | int startFrame; // This stores the first frame number for this animation |
---|
152 | int endFrame; // This stores the last frame number for this animation |
---|
153 | }; |
---|
154 | |
---|
155 | // CONFLICTING with animation.cc and vector.cc |
---|
156 | struct t3DModel |
---|
157 | { |
---|
158 | int numOfObjects; // The number of objects in the model |
---|
159 | int numOfMaterials; // The number of materials for the model |
---|
160 | int numOfAnimations; // The number of animations in this model (NEW) |
---|
161 | int currentAnim; // The current index into pAnimations list (NEW) |
---|
162 | int currentFrame; // The current frame of the current animation (NEW) |
---|
163 | vector<tAnimationInfo> animationList; // The list of animations (NEW) |
---|
164 | vector<tMaterialInfo> materialList; // The list of material information (Textures and colors) |
---|
165 | vector<t3DObject> objectList; // The object list for our model |
---|
166 | }; |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | //! This class defines the basic components of a model |
---|
171 | class AbstractModel : public BaseObject { |
---|
172 | |
---|
173 | public: |
---|
174 | AbstractModel() {} |
---|
175 | virtual ~AbstractModel() {} |
---|
176 | }; |
---|
177 | |
---|
178 | |
---|
179 | /* TESTING TESTING TESTING */ |
---|
180 | /* some mathematical functions that are already implemented by vector.cc class */ |
---|
181 | |
---|
182 | // magnitude of a normal |
---|
183 | #define Mag(Normal) (sqrt(Normal.x*Normal.x + Normal.y*Normal.y + Normal.z*Normal.z)) |
---|
184 | |
---|
185 | class MathHelp { |
---|
186 | |
---|
187 | public: |
---|
188 | // vector between two points |
---|
189 | static CVector3 VectorDiff(CVector3 vPoint1, CVector3 vPoint2) |
---|
190 | { |
---|
191 | CVector3 vVector; |
---|
192 | |
---|
193 | vVector.x = vPoint1.x - vPoint2.x; |
---|
194 | vVector.y = vPoint1.y - vPoint2.y; |
---|
195 | vVector.z = vPoint1.z - vPoint2.z; |
---|
196 | |
---|
197 | return vVector; |
---|
198 | } |
---|
199 | |
---|
200 | // adding vectors, returning result |
---|
201 | static CVector3 AddVector(CVector3 vVector1, CVector3 vVector2) |
---|
202 | { |
---|
203 | CVector3 vResult; |
---|
204 | |
---|
205 | vResult.x = vVector2.x + vVector1.x; |
---|
206 | vResult.y = vVector2.y + vVector1.y; |
---|
207 | vResult.z = vVector2.z + vVector1.z; |
---|
208 | |
---|
209 | return vResult; |
---|
210 | } |
---|
211 | |
---|
212 | // This divides a vector by a single number (scalar) and returns the result |
---|
213 | static CVector3 DivideVectorByScaler(CVector3 vVector1, float Scaler) |
---|
214 | { |
---|
215 | CVector3 vResult; |
---|
216 | |
---|
217 | vResult.x = vVector1.x / Scaler; |
---|
218 | vResult.y = vVector1.y / Scaler; |
---|
219 | vResult.z = vVector1.z / Scaler; |
---|
220 | |
---|
221 | return vResult; |
---|
222 | } |
---|
223 | |
---|
224 | // cross product between 2 vectors |
---|
225 | static CVector3 CrossProduct(CVector3 vVector1, CVector3 vVector2) |
---|
226 | { |
---|
227 | CVector3 vCross; |
---|
228 | vCross.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y)); |
---|
229 | vCross.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z)); |
---|
230 | vCross.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x)); |
---|
231 | return vCross; |
---|
232 | } |
---|
233 | |
---|
234 | // calculate the normal of a vector |
---|
235 | static CVector3 NormalizeVector(CVector3 vNormal) |
---|
236 | { |
---|
237 | double Magnitude; |
---|
238 | Magnitude = Mag(vNormal); |
---|
239 | vNormal.x /= (float)Magnitude; |
---|
240 | vNormal.y /= (float)Magnitude; |
---|
241 | vNormal.z /= (float)Magnitude; |
---|
242 | |
---|
243 | return vNormal; |
---|
244 | } |
---|
245 | }; |
---|
246 | |
---|
247 | |
---|
248 | #endif /* _ABSTRACT_MODEL_H */ |
---|