[7353] | 1 | /* |
---|
[7385] | 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 |
---|
[8081] | 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/ |
---|
[7353] | 18 | */ |
---|
| 19 | |
---|
[8490] | 20 | #include <vector> |
---|
[9324] | 21 | #include "filesys/binary_file.h" |
---|
[7385] | 22 | class SDL_Surface; |
---|
[7353] | 23 | class BspTreeNode; |
---|
| 24 | class Vector; |
---|
| 25 | class Material; |
---|
[7801] | 26 | class MoviePlayer; |
---|
[7353] | 27 | class VertexArrayModel; |
---|
| 28 | |
---|
[7801] | 29 | |
---|
[9324] | 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 |
---|
[7353] | 64 | { |
---|
[7395] | 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 |
---|
[8030] | 69 | }; |
---|
[7353] | 70 | |
---|
[9324] | 71 | |
---|
[7353] | 72 | typedef struct |
---|
| 73 | { |
---|
[7395] | 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 |
---|
[7385] | 80 | } |
---|
| 81 | model; |
---|
[7353] | 82 | |
---|
| 83 | typedef struct |
---|
| 84 | { |
---|
[7395] | 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. |
---|
[7385] | 90 | } |
---|
| 91 | node; |
---|
[7353] | 92 | |
---|
| 93 | typedef struct |
---|
| 94 | { |
---|
[7395] | 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. |
---|
[7385] | 103 | } |
---|
| 104 | leaf; |
---|
[7353] | 105 | |
---|
[7563] | 106 | struct brush |
---|
[7353] | 107 | { |
---|
[7395] | 108 | int brushside; //!< First brushside for brush. |
---|
| 109 | int n_brushsides; //!< Number of brushsides for brush. |
---|
| 110 | int texture; //!< Texture index. |
---|
[7563] | 111 | }; |
---|
[7353] | 112 | |
---|
| 113 | typedef struct |
---|
| 114 | { |
---|
[7395] | 115 | int plane; //!< Plane index. |
---|
| 116 | int texture; //!< Texture index. |
---|
[7385] | 117 | } |
---|
| 118 | brushside; |
---|
[7353] | 119 | |
---|
[9324] | 120 | |
---|
[7385] | 121 | struct face |
---|
[7353] | 122 | { |
---|
[7395] | 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. |
---|
[7465] | 132 | int lm_size [ 2 ]; //!< Size of this face's lightmap image in lightmap. |
---|
[7395] | 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. |
---|
[7353] | 137 | } ; |
---|
| 138 | |
---|
[9324] | 139 | |
---|
[7353] | 140 | typedef struct |
---|
| 141 | { |
---|
[7395] | 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. |
---|
[7385] | 146 | } |
---|
| 147 | BspVertex; |
---|
[7353] | 148 | |
---|
| 149 | typedef struct |
---|
| 150 | { |
---|
[7385] | 151 | int offset; |
---|
| 152 | } |
---|
[7395] | 153 | meshvert; //!< Integer offset to mesh vertex |
---|
[7353] | 154 | |
---|
[7385] | 155 | typedef struct |
---|
[7353] | 156 | { |
---|
[7385] | 157 | float position [ 3 ]; |
---|
| 158 | } |
---|
| 159 | BspVec; |
---|
[7353] | 160 | |
---|
[7385] | 161 | typedef struct |
---|
[7353] | 162 | { |
---|
[7385] | 163 | Material* mat; |
---|
[7801] | 164 | MoviePlayer* aviMat; |
---|
[7385] | 165 | bool alpha; |
---|
[7801] | 166 | bool animated; |
---|
[7385] | 167 | } |
---|
| 168 | AMat; |
---|
[7465] | 169 | typedef struct |
---|
| 170 | { |
---|
| 171 | unsigned char map [128][128][3]; |
---|
| 172 | } |
---|
| 173 | lightmap; |
---|
| 174 | |
---|
[7563] | 175 | typedef struct |
---|
| 176 | { |
---|
| 177 | char name[64]; |
---|
| 178 | int flags; |
---|
| 179 | int contents; |
---|
| 180 | } |
---|
| 181 | BspTexture; |
---|
| 182 | |
---|
[9324] | 183 | |
---|
[7385] | 184 | class BspFile |
---|
| 185 | { |
---|
| 186 | friend class BspManager; |
---|
[7353] | 187 | |
---|
[7385] | 188 | public: |
---|
| 189 | BspFile(); |
---|
[9003] | 190 | ~BspFile(); |
---|
[7596] | 191 | int read(const char* name ); |
---|
[7385] | 192 | void build_tree(); |
---|
| 193 | void load_textures(); |
---|
| 194 | void tesselate( int iface ); |
---|
| 195 | BspTreeNode* get_root(); |
---|
| 196 | AMat loadMat( char* mat ); |
---|
[7801] | 197 | AMat loadAVI(char * mat); |
---|
| 198 | |
---|
[7353] | 199 | |
---|
[7385] | 200 | private: |
---|
[9324] | 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 |
---|
[7465] | 238 | |
---|
| 239 | |
---|
[9324] | 240 | int** patchRowIndexes; |
---|
| 241 | VertexArrayModel** VertexArrayModels; |
---|
| 242 | int patchOffset; |
---|
[7385] | 243 | |
---|
[9324] | 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; |
---|
[7385] | 256 | |
---|
[9324] | 257 | float scale; |
---|
[7385] | 258 | |
---|
[9324] | 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 |
---|
[7353] | 272 | }; |
---|
| 273 | |
---|