GLS.Portal.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // The graphics engine GLScene
  3. //
  4. unit GLS.Portal;
  5. (*
  6. Portal Rendering support for GLScene.
  7. The portal structures are subclasses of the Mesh structures, with a "sector"
  8. being assimilated to a "MeshObject" and sector polygons to facegroups.
  9. The registered classes are:
  10. [TGLPortal, TGLSectorMeshObject, TFGPolygon, TFGPortalPolygon]
  11. *)
  12. interface
  13. {$I Stage.Defines.inc}
  14. uses
  15. System.Classes,
  16. System.SysUtils,
  17. GLS.PersistentClasses,
  18. Stage.VectorTypes,
  19. GLS.VectorFileObjects,
  20. GLS.Scene,
  21. GLS.Material,
  22. Stage.VectorGeometry,
  23. GLS.RenderContextInfo;
  24. type
  25. (* A mesh object list that handles portal rendering.
  26. The items are treated as being sectors. *)
  27. TGLPortalMeshObjectList = class(TGLMeshObjectList)
  28. public
  29. constructor CreateOwned(AOwner: TGLBaseMesh);
  30. destructor Destroy; override;
  31. procedure BuildList(var mrci: TGLRenderContextInfo); override;
  32. end;
  33. // A portal renderer sector.
  34. TGLSectorMeshObject = class(TGLMorphableMeshObject)
  35. private
  36. FRenderDone: Boolean;
  37. public
  38. constructor CreateOwned(AOwner: TGLMeshObjectList);
  39. destructor Destroy; override;
  40. procedure BuildList(var mrci: TGLRenderContextInfo); override;
  41. procedure Prepare; override;
  42. property RenderDone: Boolean read FRenderDone write FRenderDone;
  43. end;
  44. (* A portal polygon.
  45. This is the base class for portal polygons, the TFGPortalPolygon class
  46. implements the portal. *)
  47. TFGPolygon = class(TFGVertexNormalTexIndexList)
  48. public
  49. constructor CreateOwned(AOwner: TGLFaceGroups); override;
  50. destructor Destroy; override;
  51. procedure Prepare; override;
  52. end;
  53. (* A portal polygon. This is the base class for portal polygons,
  54. the TFGPortalPolygon class implements the portal. *)
  55. TFGPortalPolygon = class(TFGPolygon)
  56. private
  57. FDestinationSectorIndex: Integer;
  58. FCenter, FNormal: TAffineVector;
  59. FRadius: Single;
  60. public
  61. constructor CreateOwned(AOwner: TGLFaceGroups); override;
  62. destructor Destroy; override;
  63. procedure BuildList(var mrci: TGLRenderContextInfo); override;
  64. procedure Prepare; override;
  65. property DestinationSectorIndex: Integer read FDestinationSectorIndex
  66. write FDestinationSectorIndex;
  67. end;
  68. // Portal Renderer class.
  69. TGLPortal = class(TGLBaseMesh)
  70. public
  71. constructor Create(AOwner: TComponent); override;
  72. destructor Destroy; override;
  73. published
  74. property MaterialLibrary;
  75. end;
  76. // ------------------------------------------------------------------
  77. implementation
  78. // ------------------------------------------------------------------
  79. // ------------------
  80. // ------------------ TGLPortalMeshObjectList ------------------
  81. // ------------------
  82. constructor TGLPortalMeshObjectList.CreateOwned(AOwner: TGLBaseMesh);
  83. begin
  84. inherited CreateOwned(AOwner);
  85. end;
  86. destructor TGLPortalMeshObjectList.Destroy;
  87. begin
  88. inherited;
  89. end;
  90. procedure TGLPortalMeshObjectList.BuildList(var mrci: TGLRenderContextInfo);
  91. var
  92. i: Integer;
  93. startSector: TGLMeshObject;
  94. begin
  95. for i := 0 to Count - 1 do
  96. with TGLSectorMeshObject(Items[i]) do
  97. if InheritsFrom(TGLSectorMeshObject) then
  98. RenderDone := False;
  99. startSector := nil;
  100. for i := 0 to Count - 1 do
  101. begin
  102. if Items[i].PointInObject(PAffineVector(@mrci.cameraPosition)^) then
  103. begin
  104. startSector := Items[i];
  105. Break;
  106. end;
  107. end;
  108. if startSector <> nil then
  109. startSector.BuildList(mrci)
  110. else
  111. for i := 0 to Count - 1 do
  112. Items[i].BuildList(mrci);
  113. end;
  114. // ------------------
  115. // ------------------ TGLSectorMeshObject ------------------
  116. // ------------------
  117. constructor TGLSectorMeshObject.CreateOwned(AOwner: TGLMeshObjectList);
  118. begin
  119. inherited;
  120. Mode := momFaceGroups;
  121. end;
  122. destructor TGLSectorMeshObject.Destroy;
  123. begin
  124. inherited;
  125. end;
  126. procedure TGLSectorMeshObject.BuildList(var mrci: TGLRenderContextInfo);
  127. var
  128. i: Integer;
  129. libMat: TGLLibMaterial;
  130. begin
  131. if not RenderDone then
  132. begin
  133. RenderDone := True;
  134. // single pass : portals/polygons were sorted earlier
  135. if Assigned(mrci.MaterialLibrary) then
  136. begin
  137. for i := 0 to FaceGroups.Count - 1 do
  138. with FaceGroups[i] do
  139. begin
  140. if Length(MaterialName) > 0 then
  141. begin
  142. libMat := TGLMaterialLibrary(mrci.MaterialLibrary)
  143. .Materials.GetLibMaterialByName(MaterialName);
  144. if Assigned(libMat) then
  145. begin
  146. libMat.Apply(mrci);
  147. repeat
  148. BuildList(mrci);
  149. until not libMat.UnApply(mrci);
  150. end
  151. else
  152. BuildList(mrci);
  153. end
  154. else
  155. BuildList(mrci);
  156. end;
  157. end
  158. else
  159. for i := 0 to FaceGroups.Count - 1 do
  160. FaceGroups[i].BuildList(mrci);
  161. end;
  162. end;
  163. procedure TGLSectorMeshObject.Prepare;
  164. var
  165. i: Integer;
  166. begin
  167. for i := 0 to FaceGroups.Count - 1 do
  168. TFGPolygon(FaceGroups[i]).Prepare;
  169. FaceGroups.SortByMaterial; // this brings portals first
  170. end;
  171. // ------------------
  172. // ------------------ TFGPolygon ------------------
  173. // ------------------
  174. constructor TFGPolygon.CreateOwned(AOwner: TGLFaceGroups);
  175. begin
  176. inherited;
  177. Mode := fgmmTriangleFan;
  178. end;
  179. destructor TFGPolygon.Destroy;
  180. begin
  181. inherited;
  182. end;
  183. procedure TFGPolygon.Prepare;
  184. begin
  185. // nothing, ain't no portal !
  186. end;
  187. // ------------------
  188. // ------------------ TFGPortalPolygon ------------------
  189. // ------------------
  190. constructor TFGPortalPolygon.CreateOwned(AOwner: TGLFaceGroups);
  191. begin
  192. inherited;
  193. end;
  194. destructor TFGPortalPolygon.Destroy;
  195. begin
  196. inherited;
  197. end;
  198. procedure TFGPortalPolygon.BuildList(var mrci: TGLRenderContextInfo);
  199. var
  200. dir: TAffineVector;
  201. begin
  202. if FDestinationSectorIndex >= 0 then
  203. begin
  204. VectorSubtract(FCenter, PAffineVector(@mrci.rcci.origin)^, dir);
  205. if (VectorDotProduct(FNormal, dir) <= 0) and
  206. (not IsVolumeClipped(FCenter, FRadius, mrci.rcci.frustum)) then
  207. begin
  208. Owner.Owner.Owner.Items[FDestinationSectorIndex].BuildList(mrci);
  209. end
  210. end;
  211. end;
  212. procedure TFGPortalPolygon.Prepare;
  213. var
  214. min, max: TAffineVector;
  215. begin
  216. GetExtents(min, max);
  217. FNormal := GetNormal;
  218. VectorAdd(min, max, FCenter);
  219. ScaleVector(FCenter, 0.5);
  220. FRadius := VectorDistance(min, max) * 0.5;
  221. end;
  222. // ------------------
  223. // ------------------ TGLPortal ------------------
  224. // ------------------
  225. constructor TGLPortal.Create(AOwner: TComponent);
  226. begin
  227. FMeshObjects := TGLPortalMeshObjectList.CreateOwned(Self);
  228. inherited;
  229. ObjectStyle := ObjectStyle + [osDirectDraw];
  230. UseMeshMaterials := True;
  231. end;
  232. destructor TGLPortal.Destroy;
  233. begin
  234. inherited;
  235. end;
  236. // ------------------------------------------------------------------
  237. initialization
  238. // ------------------------------------------------------------------
  239. RegisterClasses([TGLPortal, TGLSectorMeshObject, TFGPolygon, TFGPortalPolygon]);
  240. end.