[4074] | 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" |
---|
[4078] | 25 | #include "base_object.h" |
---|
[4146] | 26 | #include <vector> |
---|
[4148] | 27 | #include <math.h> |
---|
[4157] | 28 | #include <SDL_image.h> |
---|
[4074] | 29 | |
---|
[4146] | 30 | using namespace std; |
---|
| 31 | |
---|
[4142] | 32 | //template<class T> class tList; |
---|
[4146] | 33 | ; |
---|
[4074] | 34 | |
---|
| 35 | //! This is our 3D point class. CONFLICTING with Vector.cc |
---|
| 36 | class CVector3 |
---|
| 37 | { |
---|
| 38 | public: |
---|
| 39 | float x, y, z; |
---|
| 40 | }; |
---|
| 41 | |
---|
| 42 | //! This is our 2D point class. CONFLICTING with Vector.cc |
---|
| 43 | class CVector2 |
---|
| 44 | { |
---|
| 45 | public: |
---|
| 46 | float x, y; |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | // CONFLICTING with model.h |
---|
| 50 | struct tFace |
---|
| 51 | { |
---|
| 52 | int vertIndex[3]; // indicies for the verts that make up this triangle |
---|
| 53 | int coordIndex[3]; // indicies for the tex coords to texture this face |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | // CONFLICTING with material.cc, there are some small differences, i will use this struct and switch over later |
---|
| 57 | struct tMaterialInfo |
---|
| 58 | { |
---|
| 59 | char strName[255]; // The texture name |
---|
| 60 | char strFile[255]; // The texture file name (If this is set it's a texture map) |
---|
| 61 | byte color[3]; // The color of the object (R, G, B) |
---|
| 62 | int texureId; // the texture ID |
---|
| 63 | float uTile; // u tiling of texture |
---|
| 64 | float vTile; // v tiling of texture |
---|
| 65 | float uOffset; // u offset of texture |
---|
| 66 | float vOffset; // v offset of texture |
---|
| 67 | } ; |
---|
| 68 | |
---|
| 69 | //! properties of a 3D object |
---|
| 70 | struct t3DObject |
---|
| 71 | { |
---|
| 72 | int numOfVerts; // The number of verts in the model |
---|
| 73 | int numOfFaces; // The number of faces in the model |
---|
| 74 | int numTexVertex; // The number of texture coordinates |
---|
| 75 | int materialID; // The texture ID to use, which is the index into our texture array |
---|
| 76 | bool bHasTexture; // This is TRUE if there is a texture map for this object |
---|
| 77 | char strName[255]; // The name of the object |
---|
| 78 | CVector3 *pVerts; // The object's vertices |
---|
[4158] | 79 | CVector3 *pNormals; // The object's normals |
---|
| 80 | CVector2 *pTexVerts; // The texture's UV coordinates |
---|
[4074] | 81 | tFace *pFaces; // The faces information of the object |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | // CONFLICTING with animation.cc |
---|
| 85 | struct tAnimationInfo |
---|
| 86 | { |
---|
| 87 | char strName[255]; // This stores the name of the animation (Jump, Pain, etc..) |
---|
| 88 | int startFrame; // This stores the first frame number for this animation |
---|
| 89 | int endFrame; // This stores the last frame number for this animation |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | // CONFLICTING with animation.cc and vector.cc |
---|
| 93 | struct t3DModel |
---|
| 94 | { |
---|
| 95 | int numOfObjects; // The number of objects in the model |
---|
| 96 | int numOfMaterials; // The number of materials for the model |
---|
| 97 | int numOfAnimations; // The number of animations in this model (NEW) |
---|
| 98 | int currentAnim; // The current index into pAnimations list (NEW) |
---|
| 99 | int currentFrame; // The current frame of the current animation (NEW) |
---|
[4146] | 100 | vector<tAnimationInfo> animationList; // The list of animations (NEW) |
---|
| 101 | vector<tMaterialInfo> materialList; // The list of material information (Textures and colors) |
---|
| 102 | vector<t3DObject> objectList; // The object list for our model |
---|
[4074] | 103 | }; |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | //! This class defines the basic components of a model |
---|
[4078] | 108 | class AbstractModel : public BaseObject { |
---|
[4074] | 109 | |
---|
| 110 | public: |
---|
[4079] | 111 | AbstractModel() {} |
---|
| 112 | virtual ~AbstractModel() {} |
---|
[4074] | 113 | }; |
---|
| 114 | |
---|
[4148] | 115 | |
---|
| 116 | /* TESTING TESTING TESTING */ |
---|
| 117 | /* some mathematical functions that are already implemented by vector.cc class */ |
---|
| 118 | |
---|
| 119 | // magnitude of a normal |
---|
| 120 | #define Mag(Normal) (sqrt(Normal.x*Normal.x + Normal.y*Normal.y + Normal.z*Normal.z)) |
---|
| 121 | |
---|
[4149] | 122 | class MathHelp { |
---|
[4148] | 123 | |
---|
[4149] | 124 | public: |
---|
| 125 | // vector between two points |
---|
| 126 | static CVector3 VectorDiff(CVector3 vPoint1, CVector3 vPoint2) |
---|
| 127 | { |
---|
| 128 | CVector3 vVector; |
---|
[4148] | 129 | |
---|
[4149] | 130 | vVector.x = vPoint1.x - vPoint2.x; |
---|
| 131 | vVector.y = vPoint1.y - vPoint2.y; |
---|
| 132 | vVector.z = vPoint1.z - vPoint2.z; |
---|
[4148] | 133 | |
---|
[4149] | 134 | return vVector; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // adding vectors, returning result |
---|
| 138 | static CVector3 AddVector(CVector3 vVector1, CVector3 vVector2) |
---|
| 139 | { |
---|
| 140 | CVector3 vResult; |
---|
[4148] | 141 | |
---|
[4149] | 142 | vResult.x = vVector2.x + vVector1.x; |
---|
| 143 | vResult.y = vVector2.y + vVector1.y; |
---|
| 144 | vResult.z = vVector2.z + vVector1.z; |
---|
[4148] | 145 | |
---|
[4149] | 146 | return vResult; |
---|
| 147 | } |
---|
[4148] | 148 | |
---|
[4149] | 149 | // This divides a vector by a single number (scalar) and returns the result |
---|
| 150 | static CVector3 DivideVectorByScaler(CVector3 vVector1, float Scaler) |
---|
| 151 | { |
---|
| 152 | CVector3 vResult; |
---|
[4148] | 153 | |
---|
[4149] | 154 | vResult.x = vVector1.x / Scaler; |
---|
| 155 | vResult.y = vVector1.y / Scaler; |
---|
| 156 | vResult.z = vVector1.z / Scaler; |
---|
[4148] | 157 | |
---|
[4149] | 158 | return vResult; |
---|
| 159 | } |
---|
[4148] | 160 | |
---|
[4149] | 161 | // cross product between 2 vectors |
---|
| 162 | static CVector3 CrossProduct(CVector3 vVector1, CVector3 vVector2) |
---|
| 163 | { |
---|
| 164 | CVector3 vCross; |
---|
| 165 | vCross.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y)); |
---|
| 166 | vCross.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z)); |
---|
| 167 | vCross.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x)); |
---|
| 168 | return vCross; |
---|
| 169 | } |
---|
[4148] | 170 | |
---|
[4149] | 171 | // calculate the normal of a vector |
---|
| 172 | static CVector3 NormalizeVector(CVector3 vNormal) |
---|
| 173 | { |
---|
| 174 | double Magnitude; |
---|
| 175 | Magnitude = Mag(vNormal); |
---|
| 176 | vNormal.x /= (float)Magnitude; |
---|
| 177 | vNormal.y /= (float)Magnitude; |
---|
| 178 | vNormal.z /= (float)Magnitude; |
---|
[4148] | 179 | |
---|
[4149] | 180 | return vNormal; |
---|
| 181 | } |
---|
| 182 | }; |
---|
[4148] | 183 | |
---|
[4156] | 184 | class Helper |
---|
| 185 | { |
---|
| 186 | |
---|
[4157] | 187 | public: |
---|
| 188 | |
---|
| 189 | static void createTexture(unsigned int textureArray[], char* strFileName, int textureID) |
---|
| 190 | { |
---|
| 191 | SDL_Surface *image = NULL; |
---|
| 192 | |
---|
| 193 | if(!strFileName) |
---|
| 194 | return; |
---|
[4156] | 195 | |
---|
[4157] | 196 | image = IMG_Load(strFileName); |
---|
| 197 | if( !image) |
---|
| 198 | PRINTF(0)("IMG_Load: %s\n", IMG_GetError()); |
---|
[4156] | 199 | |
---|
[4157] | 200 | glGenTextures(1, &textureArray[textureID]); |
---|
| 201 | glPixelStorei (GL_UNPACK_ALIGNMENT, 1); |
---|
| 202 | glBindTexture(GL_TEXTURE_2D, textureArray[textureID]); |
---|
| 203 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); |
---|
| 204 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); |
---|
[4158] | 205 | gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->w, image->h, GL_RGB, GL_UNSIGNED_BYTE, image->pixels); |
---|
[4156] | 206 | |
---|
[4157] | 207 | SDL_FreeSurface(image); |
---|
| 208 | } |
---|
[4156] | 209 | |
---|
| 210 | }; |
---|
| 211 | |
---|
[4074] | 212 | #endif /* _ABSTRACT_MODEL_H */ |
---|