GLSL.LineShaders.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLSL.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. {$I GLScene.inc}
  12. uses
  13. Winapi.OpenGL,
  14. System.Classes,
  15. GLS.VectorTypes,
  16. GLS.Scene,
  17. GLS.Color,
  18. GLS.Material,
  19. GLS.BaseClasses,
  20. GLS.RenderContextInfo,
  21. GLS.State,
  22. GLS.Context;
  23. type
  24. TGLLineSettings = class(TGLUpdateAbleObject)
  25. private
  26. FColor: TGLColor;
  27. FWidth: Single;
  28. FPattern: TGLushort;
  29. FForceMaterial: Boolean;
  30. procedure SetPattern(const value: TGLushort);
  31. procedure SetColor(const v: TGLColor);
  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: TGLRenderContextInfo);
  38. procedure UnApply(var rci: TGLRenderContextInfo);
  39. published
  40. property Width: Single read FWidth write SetWidth;
  41. property Color: TGLColor 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. TGLHiddenLineShader = class(TGLShader)
  49. private
  50. FPassCount: integer;
  51. FLineSmooth: Boolean;
  52. FSolid: Boolean;
  53. FBackGroundColor: TGLColor;
  54. FFrontLine: TGLLineSettings;
  55. FBackLine: TGLLineSettings;
  56. FLighting: Boolean;
  57. FShadeModel: TGLShadeModel;
  58. procedure SetlineSmooth(v: boolean);
  59. procedure SetSolid(v: boolean);
  60. procedure SetBackgroundColor(AColor: TGLColor);
  61. procedure SetLighting(v: boolean);
  62. procedure SetShadeModel(const val: TGLShadeModel);
  63. protected
  64. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  65. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  66. public
  67. constructor Create(AOwner: TComponent); override;
  68. destructor Destroy; override;
  69. published
  70. property FrontLine: TGLLineSettings read FFrontLine write FFrontLine;
  71. property BackLine: TGLLineSettings 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: TGLColor 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: TGLShadeModel read FShadeModel write SetShadeModel
  84. default smDefault;
  85. end;
  86. TGLOutlineShader = class(TGLShader)
  87. private
  88. FPassCount: integer;
  89. FLineColor: TGLColor;
  90. FOutlineSmooth: Boolean;
  91. FOutlineWidth: Single;
  92. procedure SetOutlineWidth(v: single);
  93. procedure SetOutlineSmooth(v: boolean);
  94. protected
  95. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  96. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  97. public
  98. constructor Create(AOwner: TComponent); override;
  99. destructor Destroy; override;
  100. published
  101. property LineColor: TGLColor 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. GLS.TextureFormat;
  112. // ------------------
  113. // ------------------ TGLLineSettings ------------------
  114. // ------------------
  115. constructor TGLLineSettings.Create(AOwner: TPersistent);
  116. begin
  117. inherited;
  118. FColor := TGLColor.Create(Self);
  119. FColor.Initialize(clrGray20);
  120. FWidth := 2;
  121. Pattern := $FFFF;
  122. ForceMaterial := false;
  123. end;
  124. destructor TGLLineSettings.Destroy;
  125. begin
  126. FColor.Free;
  127. inherited;
  128. end;
  129. procedure TGLLineSettings.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 TGLLineSettings.SetColor(const v: TGLColor);
  138. begin
  139. FColor.Color := v.Color;
  140. NotifyChange(Self);
  141. end;
  142. procedure TGLLineSettings.SetWidth(const Value: Single);
  143. begin
  144. FWidth := Value;
  145. NotifyChange(Self);
  146. end;
  147. var
  148. IgnoreMatSave: boolean;
  149. procedure TGLLineSettings.Apply(var rci: TGLRenderContextInfo);
  150. begin
  151. rci.GLStates.LineWidth := Width;
  152. gl.Color4fv(Color.AsAddress);
  153. if Pattern <> $FFFF then
  154. begin
  155. rci.GLStates.Enable(stLineStipple);
  156. rci.GLStates.LineStippleFactor := 1;
  157. rci.GLStates.LineStipplePattern := Pattern;
  158. end
  159. else
  160. rci.GLStates.Disable(stLineStipple);
  161. if ForceMaterial then
  162. begin
  163. IgnoreMatSave := rci.ignoreMaterials;
  164. rci.ignoreMaterials := true;
  165. end;
  166. end;
  167. procedure TGLLineSettings.UnApply(var rci: TGLRenderContextInfo);
  168. begin
  169. if ForceMaterial then
  170. rci.ignoreMaterials := IgnoreMatSave;
  171. end;
  172. procedure TGLLineSettings.SetForceMaterial(v: boolean);
  173. begin
  174. if FForceMaterial <> v then
  175. begin
  176. FForceMaterial := v;
  177. NotifyChange(self);
  178. end;
  179. end;
  180. // ------------------
  181. // ------------------ TGLHiddenLineShader ------------------
  182. // ------------------
  183. constructor TGLHiddenLineShader.Create(AOwner: TComponent);
  184. begin
  185. inherited;
  186. FFrontLine := TGLLineSettings.Create(self);
  187. FBackLine := TGLLineSettings.Create(self);
  188. FSolid := false;
  189. FBackgroundColor := TGLColor.Create(Self);
  190. FBackgroundColor.Initialize(clrBtnFace);
  191. FLineSmooth := False;
  192. FLighting := true;
  193. FShadeModel := smDefault;
  194. end;
  195. destructor TGLHiddenLineShader.Destroy;
  196. begin
  197. FFrontLine.Free;
  198. FBackLine.Free;
  199. FBackgroundColor.Free;
  200. inherited;
  201. end;
  202. procedure TGLHiddenLineShader.DoApply(var rci: TGLRenderContextInfo; Sender:
  203. TObject);
  204. begin
  205. FPassCount := 1;
  206. if solid then
  207. with rci.GLStates 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: gl.ShadeModel(GL_SMOOTH);
  216. smFlat: gl.ShadeModel(GL_FLAT);
  217. end
  218. end
  219. else
  220. begin
  221. Disable(stLighting);
  222. gl.Color4fv(FBackgroundColor.AsAddress); // use background color
  223. end;
  224. // enable and adjust polygon offset
  225. Enable(stPolygonOffsetFill);
  226. end
  227. else
  228. with rci.GLStates 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.GLStates.SetPolygonOffset(1, 2);
  239. end;
  240. function TGLHiddenLineShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  241. procedure SetLineSmoothBlend;
  242. begin
  243. with rci.GLStates 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.GLStates do begin
  268. // draw front line in 2nd pass
  269. FPassCount := 2;
  270. FBackLine.UnApply(rci);
  271. FFrontLine.Apply(rci);
  272. SetLineSmoothBlend;
  273. if solid and FLighting then
  274. Disable(stLighting);
  275. PolygonMode := pmLines;
  276. CullFaceMode := cmBack;
  277. if solid then
  278. rci.GLStates.Disable(stPolygonOffsetFill)
  279. else
  280. rci.GLStates.Disable(stPolygonOffsetLine);
  281. Result := True;
  282. end;
  283. 2:
  284. begin
  285. FFrontLine.UnApply(rci);
  286. rci.GLStates.PolygonMode := pmFill;
  287. Result := false;
  288. end;
  289. else
  290. Assert(False);
  291. Result := False;
  292. end;
  293. end;
  294. procedure TGLHiddenLineShader.SetBackgroundColor(AColor: TGLColor);
  295. begin
  296. FBackgroundColor.Color := AColor.Color;
  297. NotifyChange(Self);
  298. end;
  299. procedure TGLHiddenLineShader.SetlineSmooth(v: boolean);
  300. begin
  301. if FlineSmooth <> v then
  302. begin
  303. FlineSmooth := v;
  304. NotifyChange(self);
  305. end;
  306. end;
  307. procedure TGLHiddenLineShader.SetLighting(v: boolean);
  308. begin
  309. if FLighting <> v then
  310. begin
  311. FLighting := v;
  312. NotifyChange(self);
  313. end;
  314. end;
  315. procedure TGLHiddenLineShader.SetSolid(v: boolean);
  316. begin
  317. if FSolid <> v then
  318. begin
  319. FSolid := v;
  320. NotifyChange(self);
  321. end;
  322. end;
  323. procedure TGLHiddenLineShader.SetShadeModel(const val: TGLShadeModel);
  324. begin
  325. if FShadeModel <> val then
  326. begin
  327. FShadeModel := val;
  328. NotifyChange(Self);
  329. end;
  330. end;
  331. (*--------------------------------------
  332. TGLOutlineShader
  333. --------------------------------------*)
  334. constructor TGLOutlineShader.Create(AOwner: TComponent);
  335. begin
  336. inherited;
  337. FOutlineSmooth := False;
  338. FOutLineWidth := 2;
  339. FLineColor := TGLColor.CreateInitialized(Self, clrBlack);
  340. ShaderStyle := ssLowLevel;
  341. end;
  342. destructor TGLOutlineShader.Destroy;
  343. begin
  344. FLineColor.Free;
  345. inherited;
  346. end;
  347. procedure TGLOutlineShader.DoApply(var rci: TGLRenderContextInfo; Sender:
  348. TObject);
  349. begin
  350. // We first draw the object as usual in the first pass. This allows objects
  351. // with color alpha < 1 to be rendered correctly with outline.
  352. FPassCount := 1;
  353. end;
  354. function TGLOutlineShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  355. begin
  356. if rci.ignoreMaterials or (stStencilTest in rci.GLStates.States) then
  357. begin
  358. Result := False;
  359. Exit;
  360. end;
  361. case FPassCount of
  362. 1:
  363. with rci.GLStates do
  364. begin
  365. // Now set up to draw the outline in the second pass
  366. Disable(stLighting);
  367. if FOutlineSmooth then
  368. begin
  369. LineSmoothHint := hintNicest;
  370. Enable(stLineSmooth);
  371. end
  372. else
  373. Disable(stLineSmooth);
  374. if FOutlineSmooth or (FlineColor.Alpha < 1) then
  375. begin
  376. Enable(stBlend);
  377. SetBlendFunc(bfSrcAlpha, bfOneMinusSrcAlpha);
  378. end
  379. else
  380. Disable(stBlend);
  381. gl.Color4fv(FlineColor.AsAddress);
  382. LineWidth := FOutlineWidth;
  383. Disable(stLineStipple);
  384. PolygonMode := pmLines;
  385. CullFaceMode := cmFront;
  386. DepthFunc := cfLEqual;
  387. ActiveTextureEnabled[ttTexture2D] := False;
  388. FPassCount := 2;
  389. Result := True; // go for next pass
  390. end;
  391. 2:
  392. with rci.GLStates do
  393. begin
  394. // Restore settings
  395. PolygonMode := pmFill;
  396. CullFaceMode := cmBack;
  397. DepthFunc := cfLequal;
  398. Result := False; // we're done
  399. end;
  400. else
  401. Assert(False);
  402. Result := False;
  403. end;
  404. end;
  405. procedure TGLOutlineShader.SetOutlineWidth(v: single);
  406. begin
  407. if FOutlineWidth <> v then
  408. begin
  409. FOutlineWidth := v;
  410. NotifyChange(self);
  411. end;
  412. end;
  413. procedure TGLOutlineShader.SetOutlineSmooth(v: boolean);
  414. begin
  415. if FOutlineSmooth <> v then
  416. begin
  417. FOutlineSmooth := v;
  418. NotifyChange(self);
  419. end;
  420. end;
  421. end.