FileQ3BSP.pas 8.3 KB

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