GXS.Portal.pas 6.6 KB

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