GLPortal.pas 6.9 KB

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