| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2006 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: bottac@ee.ethz.ch |
|---|
| 13 | |
|---|
| 14 | Inspired by: |
|---|
| 15 | Rendering Q3 Maps by Morgan McGuire http://graphics.cs.brown.edu/games/quake/quake3.html |
|---|
| 16 | Unofficial Quake 3 Map Specs by Kekoa Proudfoot http://graphics.stanford.edu/~kekoa/q3/ |
|---|
| 17 | Quake 3 Collision Detection by Nathan Ostgard http://www.devmaster.net/articles/quake3collision/ |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <vector> |
|---|
| 21 | #include "filesys/binary_file.h" |
|---|
| 22 | class SDL_Surface; |
|---|
| 23 | class BspTreeNode; |
|---|
| 24 | class Vector; |
|---|
| 25 | class Material; |
|---|
| 26 | class MoviePlayer; |
|---|
| 27 | class VertexArrayModel; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | struct bsp_lump { |
|---|
| 31 | int offset; |
|---|
| 32 | int length; |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | enum bsp_lumps { |
|---|
| 36 | Entities = 0, |
|---|
| 37 | Textures = 1, |
|---|
| 38 | Planes = 2, |
|---|
| 39 | Nodes = 3, |
|---|
| 40 | Leafs = 4, |
|---|
| 41 | Leaffaces = 5, |
|---|
| 42 | Leafbrushes = 6, |
|---|
| 43 | Models = 7, |
|---|
| 44 | Brushes = 8, |
|---|
| 45 | Brushsides = 9, |
|---|
| 46 | Vertices = 10, |
|---|
| 47 | Meshverts = 11, |
|---|
| 48 | Effects = 12, |
|---|
| 49 | Faces = 13, |
|---|
| 50 | Lightmaps = 14, |
|---|
| 51 | Lightvols = 15, |
|---|
| 52 | Visdata = 16 |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | struct bsp_header { |
|---|
| 56 | // The magic number always "IBSP" |
|---|
| 57 | char magic[4]; |
|---|
| 58 | // The version, 0x2e for quake 3 models |
|---|
| 59 | int version; |
|---|
| 60 | bsp_lump lumps[17]; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | struct plane |
|---|
| 64 | { |
|---|
| 65 | float x; //!< 1st component of the plane's normal |
|---|
| 66 | float y; //!< 2nd component of the plane's normal |
|---|
| 67 | float z; //!< 3rd component of the plane's normal |
|---|
| 68 | float d; //!< distance of the plane to the origin |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | typedef struct |
|---|
| 73 | { |
|---|
| 74 | float mins [ 3 ]; //!< Bounding box min coord. |
|---|
| 75 | float maxs [ 3 ]; //!< Bounding box max coord. |
|---|
| 76 | int face; //!< First face for model. |
|---|
| 77 | int n_faces; //!< Number of faces for model. |
|---|
| 78 | int brush; //!< First brush for model. |
|---|
| 79 | int n_brushes; //!< Number of brushes |
|---|
| 80 | } |
|---|
| 81 | model; |
|---|
| 82 | |
|---|
| 83 | typedef struct |
|---|
| 84 | { |
|---|
| 85 | int plane; //!< Plane index. |
|---|
| 86 | int left; //!< 1st Child index. Negative numbers are leaf indices: -(leaf+1). |
|---|
| 87 | int right; //!< 2nd Child index. Negative numbers are leaf indices: -(leaf+1). |
|---|
| 88 | int mins[ 3 ]; //!< Integer bounding box min coord. |
|---|
| 89 | int maxs[ 3 ]; //!< Integer bounding box max coord. |
|---|
| 90 | } |
|---|
| 91 | node; |
|---|
| 92 | |
|---|
| 93 | typedef struct |
|---|
| 94 | { |
|---|
| 95 | int cluster; //!< Visdata cluster index. |
|---|
| 96 | int area; //!< Areaportal area. |
|---|
| 97 | int mins[ 3 ]; //!< Integer bounding box min coord. |
|---|
| 98 | int maxs[ 3 ]; //!< Integer bounding box max coord. |
|---|
| 99 | int leafface; //!< First leafface for leaf. |
|---|
| 100 | int n_leaffaces; //!< Number of leaffaces for leaf. |
|---|
| 101 | int leafbrush_first; //!< leafbrush for leaf. |
|---|
| 102 | int n_leafbrushes; //!< Number of leafbrushes for leaf. |
|---|
| 103 | } |
|---|
| 104 | leaf; |
|---|
| 105 | |
|---|
| 106 | struct brush |
|---|
| 107 | { |
|---|
| 108 | int brushside; //!< First brushside for brush. |
|---|
| 109 | int n_brushsides; //!< Number of brushsides for brush. |
|---|
| 110 | int texture; //!< Texture index. |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | typedef struct |
|---|
| 114 | { |
|---|
| 115 | int plane; //!< Plane index. |
|---|
| 116 | int texture; //!< Texture index. |
|---|
| 117 | } |
|---|
| 118 | brushside; |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | struct face |
|---|
| 122 | { |
|---|
| 123 | int texture; //!< Texture index. |
|---|
| 124 | int effect; //!< Index into lump 12 (Effects), or -1. |
|---|
| 125 | int type; //!< Face type. 1=polygon, 2=patch, 3=mesh, 4=billboard |
|---|
| 126 | int vertex; //!< Index of first vertex. |
|---|
| 127 | int n_vertexes; //!< Number of vertices. |
|---|
| 128 | int meshvert; //!< Index of first meshvert. |
|---|
| 129 | int n_meshverts; //!< Number of meshverts. |
|---|
| 130 | int lm_index; //!< Lightmap index. |
|---|
| 131 | int lm_start [ 2 ]; //!< Corner of this face's lightmap image in lightmap. |
|---|
| 132 | int lm_size [ 2 ]; //!< Size of this face's lightmap image in lightmap. |
|---|
| 133 | float lm_origin [ 3 ] ; //!< World space origin of lightmap. |
|---|
| 134 | float lm_vecs [ 2 ][ 3 ]; //!< World space lightmap s and t unit vectors. |
|---|
| 135 | float normal[ 3 ]; //!< Surface normal. |
|---|
| 136 | int size [ 2 ] ; //!< Patch dimensions. |
|---|
| 137 | } ; |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | typedef struct |
|---|
| 141 | { |
|---|
| 142 | float position[ 3 ]; //!< Vertex position. |
|---|
| 143 | float texcoord[ 2 ][ 2 ]; //!< Vertex texture coordinates. [0][x]=surface, [1][x]=lightmap. |
|---|
| 144 | float normal[ 3 ]; //!< Vertex normal. |
|---|
| 145 | unsigned char color [ 4 ]; //!< Vertex color. RGBA. |
|---|
| 146 | } |
|---|
| 147 | BspVertex; |
|---|
| 148 | |
|---|
| 149 | typedef struct |
|---|
| 150 | { |
|---|
| 151 | int offset; |
|---|
| 152 | } |
|---|
| 153 | meshvert; //!< Integer offset to mesh vertex |
|---|
| 154 | |
|---|
| 155 | typedef struct |
|---|
| 156 | { |
|---|
| 157 | float position [ 3 ]; |
|---|
| 158 | } |
|---|
| 159 | BspVec; |
|---|
| 160 | |
|---|
| 161 | typedef struct |
|---|
| 162 | { |
|---|
| 163 | Material* mat; |
|---|
| 164 | MoviePlayer* aviMat; |
|---|
| 165 | bool alpha; |
|---|
| 166 | bool animated; |
|---|
| 167 | } |
|---|
| 168 | AMat; |
|---|
| 169 | typedef struct |
|---|
| 170 | { |
|---|
| 171 | unsigned char map [128][128][3]; |
|---|
| 172 | } |
|---|
| 173 | lightmap; |
|---|
| 174 | |
|---|
| 175 | typedef struct |
|---|
| 176 | { |
|---|
| 177 | char name[64]; |
|---|
| 178 | int flags; |
|---|
| 179 | int contents; |
|---|
| 180 | } |
|---|
| 181 | BspTexture; |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | class BspFile |
|---|
| 185 | { |
|---|
| 186 | friend class BspManager; |
|---|
| 187 | |
|---|
| 188 | public: |
|---|
| 189 | BspFile(); |
|---|
| 190 | ~BspFile(); |
|---|
| 191 | int read(const char* name ); |
|---|
| 192 | void build_tree(); |
|---|
| 193 | void load_textures(); |
|---|
| 194 | void tesselate( int iface ); |
|---|
| 195 | BspTreeNode* get_root(); |
|---|
| 196 | AMat loadMat( char* mat ); |
|---|
| 197 | AMat loadAVI(char * mat); |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | private: |
|---|
| 201 | void readEntities(); |
|---|
| 202 | void readTextures(); |
|---|
| 203 | void readPlanes(); |
|---|
| 204 | void readNodes(); |
|---|
| 205 | void readLeafs(); |
|---|
| 206 | void readLeafFaces(); |
|---|
| 207 | void readLeafBrushes(); |
|---|
| 208 | void readBrushSides(); |
|---|
| 209 | void readModels(); |
|---|
| 210 | void readBrushes(); |
|---|
| 211 | void readVertices(); |
|---|
| 212 | void readMeshVerts(); |
|---|
| 213 | void readEffects(); |
|---|
| 214 | void readFaces(); |
|---|
| 215 | void readLightmaps(); |
|---|
| 216 | void readLightvols(); |
|---|
| 217 | void readVisdata(); |
|---|
| 218 | BinaryFile file; |
|---|
| 219 | BspTreeNode* root; |
|---|
| 220 | bsp_header header; //!< Buffer for header of BSP-File |
|---|
| 221 | node* nodes; //!< Buffer to store BSP-Tree-Nodes |
|---|
| 222 | leaf* leaves; //!< Buffer to store BSP-Tree-Leaves |
|---|
| 223 | plane* planes; //!< Buffer to store planes separateing the space |
|---|
| 224 | model* bspModels; //!< Buffer to store BSP-Model-List |
|---|
| 225 | char* leafFaces; //!< Buffer to store leafFaces |
|---|
| 226 | face* faces; //!< |
|---|
| 227 | char* leafBrushes; //!< Buffer to store brush indice |
|---|
| 228 | brush* brushes; //!< Buffer to store brushes |
|---|
| 229 | brushside* brushSides; //!< |
|---|
| 230 | char* vertice; //!< |
|---|
| 231 | meshvert* meshverts; //!< Buffer to store meshverice |
|---|
| 232 | char* visData; //!< Buffer to store visibility data |
|---|
| 233 | char* textures; //!< Holds all the texture filename strings |
|---|
| 234 | char* patchVertice; //!< |
|---|
| 235 | char* patchIndexes; |
|---|
| 236 | char* patchTrianglesPerRow; |
|---|
| 237 | lightmap* lightMaps; //!< Buffer to store lightmap-images |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | int** patchRowIndexes; |
|---|
| 241 | VertexArrayModel** VertexArrayModels; |
|---|
| 242 | int patchOffset; |
|---|
| 243 | |
|---|
| 244 | int numNodes; |
|---|
| 245 | int numLeafs; |
|---|
| 246 | int numVertex; |
|---|
| 247 | int numPlanes; |
|---|
| 248 | int numBspModels; |
|---|
| 249 | int numLeafFaces; |
|---|
| 250 | int numFaces; |
|---|
| 251 | int numLeafBrushes; |
|---|
| 252 | int numTextures; |
|---|
| 253 | int numPatches; |
|---|
| 254 | int numBrushSides; |
|---|
| 255 | int numLightMaps; |
|---|
| 256 | |
|---|
| 257 | float scale; |
|---|
| 258 | |
|---|
| 259 | BspTreeNode* build_tree_rec( int i ); |
|---|
| 260 | unsigned int loadLightMapToGL(lightmap&); |
|---|
| 261 | AMat* Materials; |
|---|
| 262 | unsigned int* glLightMapTextures; |
|---|
| 263 | unsigned int whiteLightMap; |
|---|
| 264 | unsigned char whiteTexture[3]; |
|---|
| 265 | void swapAllBspCoordinates(); |
|---|
| 266 | void swapCoords(int * array); |
|---|
| 267 | void swapCoords(float * array); |
|---|
| 268 | SDL_Surface* testSurf; |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | ::std::vector<MoviePlayer* > MovieMaterials; //!< Movieplayer Materials |
|---|
| 272 | }; |
|---|
| 273 | |
|---|