FileQ3BSP.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit FileQ3BSP;
  5. (* Simple Quake III BSP file loader. *)
  6. interface
  7. uses
  8. System.Classes,
  9. System.SysUtils,
  10. GLVectorTypes;
  11. const FACE_POLYGON = 1;
  12. const MAX_TEXTURES = 1000;
  13. type
  14. TBSPHeader = record
  15. StrID : array [0..3] of AnsiChar; // This should always be 'IBSP'
  16. Version : Integer; // This should be 0x2e for Quake 3 files
  17. end;
  18. TBSPLump = record
  19. Offset : Integer; // The offset into the file for the start of this lump
  20. Length : Integer; // The length in bytes for this lump
  21. end;
  22. TBSPBBox = array [0..5] of Integer;
  23. TBSPNode = record
  24. Plane : Integer; // Space partitioning plane
  25. Children : array [0..1] of Integer; // Back and front child nodes
  26. BBox : TBSPBBox; // Bounding box of node
  27. end;
  28. TBSPLeaf = record
  29. Cluster : Integer; // Visibility cluster number
  30. Area : Integer; // Volume of the leaf
  31. BBox : TBSPBBox; // Bounding box of leaf
  32. FirstFace, NumFaces : Integer; // Lookup for the face list (indexes are for faces)
  33. FirstBrush, NumBrushes : Integer; // Lookup for the brush list (indexes are for brushes)
  34. end;
  35. TBSPModel = record
  36. BBox : TBSPBBox; // Bounding box of model
  37. FirstFace, NumFaces : Integer; // Lookup for the face list (indexes are for faces)
  38. FirstBrush, NumBrushes : Integer; // Lookup for the brush list (indexes are for brushes)
  39. end;
  40. TBSPVertex = record
  41. Position : TVector3f; // (x, y, z) position.
  42. TextureCoord : TVector2f; // (u, v) texture coordinate
  43. LightmapCoord : TVector2f; // (u, v) lightmap coordinate
  44. Normal : TVector3f; // (x, y, z) normal vector
  45. Color : array [0..3] of Byte // RGBA color for the vertex
  46. end;
  47. PBSPVertex = ^TBSPVertex;
  48. TBSPFace = record
  49. textureID : Integer; // The index into the texture array
  50. effect : Integer; // The index for the effects (or -1 = n/a)
  51. FaceType : Integer; // 1=polygon, 2=patch, 3=mesh, 4=billboard
  52. startVertIndex : Integer; // The starting index into this face's first vertex
  53. numOfVerts : Integer; // The number of vertices for this face
  54. meshVertIndex : Integer; // The index into the first meshvertex
  55. numMeshVerts : Integer; // The number of mesh vertices
  56. lightmapID : Integer; // The texture index for the lightmap
  57. lMapCorner : array [0..1] of Integer; // The face's lightmap corner in the image
  58. lMapSize : array [0..1] of Integer; // The size of the lightmap section
  59. lMapPos : TVector3f; // The 3D origin of lightmap.
  60. lMapVecs : array [0..1] of TVector3f; // The 3D space for s and t unit vectors.
  61. vNormal : TVector3f; // The face normal.
  62. Size : array [0..1] of Integer; // The bezier patch dimensions.
  63. end;
  64. PBSPFace = ^TBSPFace;
  65. TBSPTexture = record
  66. TextureName : array [0..63] of WideChar; // The name of the texture w/o the extension
  67. flags : Integer; // The surface flags (unknown)
  68. contents : Integer; // The content flags (unknown)
  69. end;
  70. PBSPTexture = ^TBSPTexture;
  71. TBSPLightmap = record
  72. imageBits : array [0..49151] of Byte; // The RGB data in a 128x128 image
  73. end;
  74. PBSPLightmap = ^TBSPLightmap;
  75. TBSPVisData = record
  76. numOfClusters : Integer;
  77. bytesPerCluster : Integer;
  78. bitSets : array of Byte;
  79. end;
  80. TQ3BSP = class (TObject)
  81. public
  82. Header : TBSPHeader;
  83. Lumps : array of TBSPLump;
  84. NumOfVerts : Integer;
  85. NumOfNodes : Integer;
  86. NumOfPlanes : Integer;
  87. NumOfLeaves : Integer;
  88. NumOfFaces : Integer;
  89. NumOfTextures : Integer;
  90. NumOfLightmaps : Integer;
  91. Vertices : array of TBSPVertex;
  92. Nodes : array of TBSPNode;
  93. Planes : array of TVector4f;
  94. Leaves : array of TBSPLeaf;
  95. Faces : array of TBSPFace;
  96. Textures : array of TBSPTexture; // texture names (without extension)
  97. Lightmaps : array of TBSPLightmap;
  98. VisData : TBSPVisData;
  99. constructor Create(bspStream : TStream);
  100. end;
  101. const
  102. kEntities = 0; // Stores player/object positions, etc...
  103. kTextures = 1; // Stores texture information
  104. kPlanes = 2; // Stores the splitting planes
  105. kNodes = 3; // Stores the BSP nodes
  106. kLeafs = 4; // Stores the leafs of the nodes
  107. kLeafFaces = 5; // Stores the leaf's indices into the faces
  108. kLeafBrushes = 6; // Stores the leaf's indices into the brushes
  109. kModels = 7; // Stores the info of world models
  110. kBrushes = 8; // Stores the brushes info (for collision)
  111. kBrushSides = 9; // Stores the brush surfaces info
  112. kVertices = 10; // Stores the level vertices
  113. kMeshVerts = 11; // Stores the model vertices offsets
  114. kShaders = 12; // Stores the shader files (blending, anims..)
  115. kFaces = 13; // Stores the faces for the level
  116. kLightmaps = 14; // Stores the lightmaps for the level
  117. kLightVolumes= 15; // Stores extra world lighting information
  118. kVisData = 16; // Stores PVS and cluster info (visibility)
  119. kMaxLumps = 17; // A constant to store the number of lumps
  120. // ------------------------------------------------------------------
  121. implementation
  122. // ------------------------------------------------------------------
  123. // ------------------
  124. // ------------------ TQ3BSP ------------------
  125. // ------------------
  126. constructor TQ3BSP.Create(bspStream : TStream);
  127. begin
  128. SetLength(Lumps, kMaxLumps);
  129. // Read in the header and lump data
  130. bspStream.Read(Header, SizeOf(Header));
  131. bspStream.Read(Lumps[0], kMaxLumps*SizeOf(TBSPLump));
  132. NumOfNodes:=Round(lumps[kNodes].length/SizeOf(TBSPNode));
  133. SetLength(Nodes, NumOfNodes);
  134. bspStream.Position:=lumps[kNodes].offset;
  135. bspStream.Read(Nodes[0], NumOfNodes*SizeOf(TBSPNode));
  136. NumOfPlanes:=Round(lumps[kPlanes].length/SizeOf(TVector4f));
  137. SetLength(Planes, NumOfPlanes);
  138. bspStream.Position:=lumps[kPlanes].offset;
  139. bspStream.Read(Planes[0], NumOfPlanes*SizeOf(TVector4f));
  140. NumOfLeaves:=Round(lumps[kLeafs].length/SizeOf(TBSPLeaf));
  141. SetLength(Leaves, NumOfLeaves);
  142. bspStream.Position:=lumps[kLeafs].offset;
  143. bspStream.Read(Leaves[0], NumOfLeaves*SizeOf(TBSPLeaf));
  144. NumOfVerts:=Round(lumps[kVertices].length/SizeOf(TBSPVertex));
  145. SetLength(Vertices, numOfVerts);
  146. bspStream.Position:=lumps[kVertices].offset;
  147. bspStream.Read(Vertices[0], numOfVerts*SizeOf(TBSPVertex));
  148. numOfFaces:=Round(lumps[kFaces].length/SizeOf(TBSPFace));
  149. SetLength(Faces, numOfFaces);
  150. bspStream.Position:=lumps[kFaces].offset;
  151. bspStream.Read(Faces[0], numOfFaces*SizeOf(TBSPFace));
  152. NumOfTextures:=Round(lumps[kTextures].length/SizeOf(TBSPTexture));
  153. SetLength(Textures, NumOfTextures);
  154. bspStream.Position:=lumps[kTextures].offset;
  155. bspStream.Read(Textures[0], NumOfTextures*SizeOf(TBSPTexture));
  156. NumOfLightmaps:=Round(lumps[kLightmaps].length/SizeOf(TBSPLightmap));
  157. SetLength(Lightmaps, NumOfLightmaps);
  158. bspStream.Position:=lumps[kLightmaps].offset;
  159. bspStream.Read(Lightmaps[0], NumOfLightmaps*SizeOf(TBSPLightmap));
  160. bspStream.Position:=lumps[kVisData].offset;
  161. bspStream.Read(VisData.numOfClusters, SizeOf(Integer));
  162. bspStream.Read(VisData.bytesPerCluster, SizeOf(Integer));
  163. if VisData.numOfClusters*VisData.bytesPerCluster>0 then begin
  164. SetLength(VisData.bitSets, VisData.numOfClusters*VisData.bytesPerCluster);
  165. bspStream.Read(VisData.bitSets[0], VisData.numOfClusters*VisData.bytesPerCluster);
  166. end;
  167. end;
  168. end.