GXSL.LineShaders.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXSL.LineShaders;
  5. (*
  6. A shader that renders hidden (back-faced) lines differently from visible
  7. (front) lines. Polygon offset is used to displace fragments depths a little
  8. so that there is no z-fighting in rendering the same geometry multiple times.
  9. *)
  10. interface
  11. uses
  12. Winapi.OpenGL,
  13. System.Classes,
  14. Stage.OpenGLTokens,
  15. Stage.VectorTypes,
  16. GXS.Scene,
  17. GXS.Color,
  18. GXS.Material,
  19. GXS.BaseClasses,
  20. GXS.RenderContextInfo,
  21. GXS.State,
  22. GXS.Context;
  23. type
  24. TgxLineSettings = class(TgxUpdateAbleObject)
  25. private
  26. FColor: TgxColor;
  27. FWidth: Single;
  28. FPattern: TGLushort;
  29. FForceMaterial: Boolean;
  30. procedure SetPattern(const value: TGLushort);
  31. procedure SetColor(const v: TgxColor);
  32. procedure SetWidth(const Value: Single);
  33. procedure SetForceMaterial(v: boolean);
  34. public
  35. constructor Create(AOwner: TPersistent); override;
  36. destructor Destroy; override;
  37. procedure Apply(var rci: TgxRenderContextInfo);
  38. procedure UnApply(var rci: TgxRenderContextInfo);
  39. published
  40. property Width: Single read FWidth write SetWidth;
  41. property Color: TgxColor read FColor write SetColor;
  42. property Pattern: TGLushort read FPattern write SetPattern default $FFFF;
  43. (* Set ForceMaterial to true to enforce the application of the line settings
  44. for objects that sets their own color, line width and pattern. *)
  45. property ForceMaterial: Boolean read FForceMaterial write SetForceMaterial
  46. default false;
  47. end;
  48. TgxHiddenLineShader = class(TgxShader)
  49. private
  50. FPassCount: integer;
  51. FLineSmooth: Boolean;
  52. FSolid: Boolean;
  53. FBackGroundColor: TgxColor;
  54. FFrontLine: TgxLineSettings;
  55. FBackLine: TgxLineSettings;
  56. FLighting: Boolean;
  57. FShadeModel: TgxShadeModel;
  58. procedure SetlineSmooth(v: boolean);
  59. procedure SetSolid(v: boolean);
  60. procedure SetBackgroundColor(AColor: TgxColor);
  61. procedure SetLighting(v: boolean);
  62. procedure SetShadeModel(const val: TgxShadeModel);
  63. protected
  64. procedure DoApply(var rci: TgxRenderContextInfo; Sender: TObject); override;
  65. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  66. public
  67. constructor Create(AOwner: TComponent); override;
  68. destructor Destroy; override;
  69. published
  70. property FrontLine: TgxLineSettings read FFrontLine write FFrontLine;
  71. property BackLine: TgxLineSettings read FBackLine write FBackLine;
  72. // Line smoothing control
  73. property LineSmooth: Boolean read FlineSmooth write SetlineSmooth default
  74. false;
  75. // Solid controls if you can see through the front-line wireframe.
  76. property Solid: Boolean read FSolid write SetSolid default false;
  77. // Color used for solid fill.
  78. property BackgroundColor: TgxColor read FBackgroundColor write
  79. SetBackgroundColor;
  80. // When Solid is True, determines if lighting or background color is used.
  81. property SurfaceLit: Boolean read FLighting write SetLighting default true;
  82. // Shade model. Default is "Smooth".
  83. property ShadeModel: TgxShadeModel read FShadeModel write SetShadeModel
  84. default smDefault;
  85. end;
  86. TgxOutlineShader = class(TgxShader)
  87. private
  88. FPassCount: integer;
  89. FLineColor: TgxColor;
  90. FOutlineSmooth: Boolean;
  91. FOutlineWidth: Single;
  92. procedure SetOutlineWidth(v: single);
  93. procedure SetOutlineSmooth(v: boolean);
  94. protected
  95. procedure DoApply(var rci: TgxRenderContextInfo; Sender: TObject); override;
  96. function DoUnApply(var rci: TgxRenderContextInfo): Boolean; override;
  97. public
  98. constructor Create(AOwner: TComponent); override;
  99. destructor Destroy; override;
  100. published
  101. property LineColor: TgxColor read FLineColor write FLineColor;
  102. // Line smoothing control
  103. property LineSmooth: Boolean read FOutlineSmooth write SetOutlineSmooth
  104. default false;
  105. property LineWidth: Single read FOutlineWidth write SetOutlineWidth;
  106. end;
  107. // ------------------------------------------------------------------
  108. implementation
  109. // ------------------------------------------------------------------
  110. uses
  111. Stage.TextureFormat;
  112. // ------------------
  113. // ------------------ TgxLineSettings ------------------
  114. // ------------------
  115. constructor TgxLineSettings.Create(AOwner: TPersistent);
  116. begin
  117. inherited;
  118. FColor := TgxColor.Create(Self);
  119. FColor.Initialize(clrGray20);
  120. FWidth := 2;
  121. Pattern := $FFFF;
  122. ForceMaterial := false;
  123. end;
  124. destructor TgxLineSettings.Destroy;
  125. begin
  126. FColor.Free;
  127. inherited;
  128. end;
  129. procedure TgxLineSettings.SetPattern(const value: TGLushort);
  130. begin
  131. if FPattern <> value then
  132. begin
  133. FPattern := Value;
  134. NotifyChange(self);
  135. end;
  136. end;
  137. procedure TgxLineSettings.SetColor(const v: TgxColor);
  138. begin
  139. FColor.Color := v.Color;
  140. NotifyChange(Self);
  141. end;
  142. procedure TgxLineSettings.SetWidth(const Value: Single);
  143. begin
  144. FWidth := Value;
  145. NotifyChange(Self);
  146. end;
  147. var
  148. IgnoreMatSave: boolean;
  149. procedure TgxLineSettings.Apply(var rci: TgxRenderContextInfo);
  150. begin
  151. rci.gxStates.LineWidth := Width;
  152. glColor4fv(@Color.AsAddress^);
  153. if Pattern <> $FFFF then
  154. begin
  155. rci.gxStates.Enable(stLineStipple);
  156. rci.gxStates.LineStippleFactor := 1;
  157. rci.gxStates.LineStipplePattern := Pattern;
  158. end
  159. else
  160. rci.gxStates.Disable(stLineStipple);
  161. if ForceMaterial then
  162. begin
  163. IgnoreMatSave := rci.ignoreMaterials;
  164. rci.ignoreMaterials := true;
  165. end;
  166. end;
  167. procedure TgxLineSettings.UnApply(var rci: TgxRenderContextInfo);
  168. begin
  169. if ForceMaterial then
  170. rci.ignoreMaterials := IgnoreMatSave;
  171. end;
  172. procedure TgxLineSettings.SetForceMaterial(v: boolean);
  173. begin
  174. if FForceMaterial <> v then
  175. begin
  176. FForceMaterial := v;
  177. NotifyChange(self);
  178. end;
  179. end;
  180. // ------------------
  181. // ------------------ TgxHiddenLineShader ------------------
  182. // ------------------
  183. constructor TgxHiddenLineShader.Create(AOwner: TComponent);
  184. begin
  185. inherited;
  186. FFrontLine := TgxLineSettings.Create(self);
  187. FBackLine := TgxLineSettings.Create(self);
  188. FSolid := false;
  189. FBackgroundColor := TgxColor.Create(Self);
  190. FBackgroundColor.Initialize(clrBtnFace);
  191. FLineSmooth := False;
  192. FLighting := true;
  193. FShadeModel := smDefault;
  194. end;
  195. destructor TgxHiddenLineShader.Destroy;
  196. begin
  197. FFrontLine.Free;
  198. FBackLine.Free;
  199. FBackgroundColor.Free;
  200. inherited;
  201. end;
  202. procedure TgxHiddenLineShader.DoApply(var rci: TgxRenderContextInfo; Sender:
  203. TObject);
  204. begin
  205. FPassCount := 1;
  206. if solid then
  207. with rci.gxStates do
  208. begin
  209. // draw filled front faces in first pass
  210. PolygonMode := pmFill;
  211. CullFaceMode := cmBack;
  212. if FLighting then
  213. begin
  214. case ShadeModel of
  215. smDefault, smSmooth: glShadeModel(GL_SMOOTH);
  216. smFlat: glShadeModel(GL_FLAT);
  217. end
  218. end
  219. else
  220. begin
  221. Disable(stLighting);
  222. glColor4fv(@FBackgroundColor.AsAddress^); // use background color
  223. end;
  224. // enable and adjust polygon offset
  225. Enable(stPolygonOffsetFill);
  226. end
  227. else
  228. with rci.gxStates do
  229. begin
  230. Disable(stLighting);
  231. // draw back lines in first pass
  232. FBackLine.Apply(rci);
  233. CullFaceMode := cmFront;
  234. PolygonMode := pmLines;
  235. // enable and adjust polygon offset
  236. Enable(stPolygonOffsetLine);
  237. end;
  238. rci.gxStates.SetPolygonOffset(1, 2);
  239. end;
  240. function TgxHiddenLineShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  241. procedure SetLineSmoothBlend;
  242. begin
  243. with rci.gxStates do
  244. begin
  245. LineStippleFactor := 1;
  246. LineStipplePattern := $FFFF;
  247. if LineSmooth then
  248. begin
  249. LineSmoothHint := hintNicest;
  250. Enable(stLineSmooth);
  251. end
  252. else
  253. Disable(stLineSmooth);
  254. if LineSmooth or (FBackLine.FColor.Alpha < 1)
  255. or (FFrontLine.FColor.Alpha < 1) then
  256. begin
  257. Enable(stBlend);
  258. SetBlendFunc(bfSrcAlpha, bfOneMinusSrcAlpha);
  259. end
  260. else
  261. Disable(stBlend);
  262. end;
  263. end;
  264. begin
  265. case FPassCount of
  266. 1:
  267. with rci.gxStates do
  268. begin
  269. // draw front line in 2nd pass
  270. FPassCount := 2;
  271. FBackLine.UnApply(rci);
  272. FFrontLine.Apply(rci);
  273. SetLineSmoothBlend;
  274. if solid and FLighting then
  275. Disable(stLighting);
  276. PolygonMode := pmLines;
  277. CullFaceMode := cmBack;
  278. if solid then
  279. rci.gxStates.Disable(stPolygonOffsetFill)
  280. else
  281. rci.gxStates.Disable(stPolygonOffsetLine);
  282. Result := True;
  283. end;
  284. 2:
  285. begin
  286. FFrontLine.UnApply(rci);
  287. rci.gxStates.PolygonMode := pmFill;
  288. Result := false;
  289. end;
  290. else
  291. Assert(False);
  292. Result := False;
  293. end;
  294. end;
  295. procedure TgxHiddenLineShader.SetBackgroundColor(AColor: TgxColor);
  296. begin
  297. FBackgroundColor.Color := AColor.Color;
  298. NotifyChange(Self);
  299. end;
  300. procedure TgxHiddenLineShader.SetlineSmooth(v: boolean);
  301. begin
  302. if FlineSmooth <> v then
  303. begin
  304. FlineSmooth := v;
  305. NotifyChange(self);
  306. end;
  307. end;
  308. procedure TgxHiddenLineShader.SetLighting(v: boolean);
  309. begin
  310. if FLighting <> v then
  311. begin
  312. FLighting := v;
  313. NotifyChange(self);
  314. end;
  315. end;
  316. procedure TgxHiddenLineShader.SetSolid(v: boolean);
  317. begin
  318. if FSolid <> v then
  319. begin
  320. FSolid := v;
  321. NotifyChange(self);
  322. end;
  323. end;
  324. procedure TgxHiddenLineShader.SetShadeModel(const val: TgxShadeModel);
  325. begin
  326. if FShadeModel <> val then
  327. begin
  328. FShadeModel := val;
  329. NotifyChange(Self);
  330. end;
  331. end;
  332. (*--------------------------------------
  333. TgxOutlineShader
  334. --------------------------------------*)
  335. constructor TgxOutlineShader.Create(AOwner: TComponent);
  336. begin
  337. inherited;
  338. FOutlineSmooth := False;
  339. FOutLineWidth := 2;
  340. FLineColor := TgxColor.CreateInitialized(Self, clrBlack);
  341. ShaderStyle := ssLowLevel;
  342. end;
  343. destructor TgxOutlineShader.Destroy;
  344. begin
  345. FLineColor.Free;
  346. inherited;
  347. end;
  348. procedure TgxOutlineShader.DoApply(var rci: TgxRenderContextInfo; Sender:
  349. TObject);
  350. begin
  351. // We first draw the object as usual in the first pass. This allows objects
  352. // with color alpha < 1 to be rendered correctly with outline.
  353. FPassCount := 1;
  354. end;
  355. function TgxOutlineShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  356. begin
  357. if rci.ignoreMaterials or (stStencilTest in rci.gxStates.States) then
  358. begin
  359. Result := False;
  360. Exit;
  361. end;
  362. case FPassCount of
  363. 1:
  364. with rci.gxStates do
  365. begin
  366. // Now set up to draw the outline in the second pass
  367. Disable(stLighting);
  368. if FOutlineSmooth then
  369. begin
  370. LineSmoothHint := hintNicest;
  371. Enable(stLineSmooth);
  372. end
  373. else
  374. Disable(stLineSmooth);
  375. if FOutlineSmooth or (FlineColor.Alpha < 1) then
  376. begin
  377. Enable(stBlend);
  378. SetBlendFunc(bfSrcAlpha, bfOneMinusSrcAlpha);
  379. end
  380. else
  381. Disable(stBlend);
  382. glColor4fv(@FlineColor.AsAddress^);
  383. LineWidth := FOutlineWidth;
  384. Disable(stLineStipple);
  385. PolygonMode := pmLines;
  386. CullFaceMode := cmFront;
  387. DepthFunc := cfLEqual;
  388. ActiveTextureEnabled[ttTexture2D] := False;
  389. FPassCount := 2;
  390. Result := True; // go for next pass
  391. end;
  392. 2:
  393. with rci.gxStates do
  394. begin
  395. // Restore settings
  396. PolygonMode := pmFill;
  397. CullFaceMode := cmBack;
  398. DepthFunc := cfLequal;
  399. Result := False; // we're done
  400. end;
  401. else
  402. Assert(False);
  403. Result := False;
  404. end;
  405. end;
  406. procedure TgxOutlineShader.SetOutlineWidth(v: single);
  407. begin
  408. if FOutlineWidth <> v then
  409. begin
  410. FOutlineWidth := v;
  411. NotifyChange(self);
  412. end;
  413. end;
  414. procedure TgxOutlineShader.SetOutlineSmooth(v: boolean);
  415. begin
  416. if FOutlineSmooth <> v then
  417. begin
  418. FOutlineSmooth := v;
  419. NotifyChange(self);
  420. end;
  421. end;
  422. end.