GLSL.LineShaders.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. //
  2. // The graphics engine 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 Stage.Defines.inc}
  12. uses
  13. Winapi.OpenGL,
  14. System.Classes,
  15. Stage.OpenGLTokens,
  16. Stage.VectorTypes,
  17. GLS.Scene,
  18. GLS.Color,
  19. GLS.Material,
  20. GLS.BaseClasses,
  21. GLS.RenderContextInfo,
  22. GLS.State,
  23. GLS.Context;
  24. type
  25. TGLLineSettings = class(TGLUpdateAbleObject)
  26. private
  27. FColor: TGLColor;
  28. FWidth: Single;
  29. FPattern: TGLushort;
  30. FForceMaterial: Boolean;
  31. procedure SetPattern(const value: TGLushort);
  32. procedure SetColor(const v: TGLColor);
  33. procedure SetWidth(const Value: Single);
  34. procedure SetForceMaterial(v: boolean);
  35. public
  36. constructor Create(AOwner: TPersistent); override;
  37. destructor Destroy; override;
  38. procedure Apply(var rci: TGLRenderContextInfo);
  39. procedure UnApply(var rci: TGLRenderContextInfo);
  40. published
  41. property Width: Single read FWidth write SetWidth;
  42. property Color: TGLColor read FColor write SetColor;
  43. property Pattern: TGLushort read FPattern write SetPattern default $FFFF;
  44. (* Set ForceMaterial to true to enforce the application of the line settings
  45. for objects that sets their own color, line width and pattern. *)
  46. property ForceMaterial: Boolean read FForceMaterial write SetForceMaterial
  47. default false;
  48. end;
  49. TGLHiddenLineShader = class(TGLShader)
  50. private
  51. FPassCount: integer;
  52. FLineSmooth: Boolean;
  53. FSolid: Boolean;
  54. FBackGroundColor: TGLColor;
  55. FFrontLine: TGLLineSettings;
  56. FBackLine: TGLLineSettings;
  57. FLighting: Boolean;
  58. FShadeModel: TGLShadeModel;
  59. procedure SetlineSmooth(v: boolean);
  60. procedure SetSolid(v: boolean);
  61. procedure SetBackgroundColor(AColor: TGLColor);
  62. procedure SetLighting(v: boolean);
  63. procedure SetShadeModel(const val: TGLShadeModel);
  64. protected
  65. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  66. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  67. public
  68. constructor Create(AOwner: TComponent); override;
  69. destructor Destroy; override;
  70. published
  71. property FrontLine: TGLLineSettings read FFrontLine write FFrontLine;
  72. property BackLine: TGLLineSettings read FBackLine write FBackLine;
  73. // Line smoothing control
  74. property LineSmooth: Boolean read FlineSmooth write SetlineSmooth default
  75. false;
  76. // Solid controls if you can see through the front-line wireframe.
  77. property Solid: Boolean read FSolid write SetSolid default false;
  78. // Color used for solid fill.
  79. property BackgroundColor: TGLColor read FBackgroundColor write
  80. SetBackgroundColor;
  81. // When Solid is True, determines if lighting or background color is used.
  82. property SurfaceLit: Boolean read FLighting write SetLighting default true;
  83. // Shade model. Default is "Smooth".
  84. property ShadeModel: TGLShadeModel read FShadeModel write SetShadeModel
  85. default smDefault;
  86. end;
  87. TGLOutlineShader = class(TGLShader)
  88. private
  89. FPassCount: integer;
  90. FLineColor: TGLColor;
  91. FOutlineSmooth: Boolean;
  92. FOutlineWidth: Single;
  93. procedure SetOutlineWidth(v: single);
  94. procedure SetOutlineSmooth(v: boolean);
  95. protected
  96. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  97. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  98. public
  99. constructor Create(AOwner: TComponent); override;
  100. destructor Destroy; override;
  101. published
  102. property LineColor: TGLColor read FLineColor write FLineColor;
  103. // Line smoothing control
  104. property LineSmooth: Boolean read FOutlineSmooth write SetOutlineSmooth
  105. default false;
  106. property LineWidth: Single read FOutlineWidth write SetOutlineWidth;
  107. end;
  108. // ------------------------------------------------------------------
  109. implementation
  110. // ------------------------------------------------------------------
  111. uses
  112. Stage.TextureFormat;
  113. // ------------------
  114. // ------------------ TGLLineSettings ------------------
  115. // ------------------
  116. constructor TGLLineSettings.Create(AOwner: TPersistent);
  117. begin
  118. inherited;
  119. FColor := TGLColor.Create(Self);
  120. FColor.Initialize(clrGray20);
  121. FWidth := 2;
  122. Pattern := $FFFF;
  123. ForceMaterial := false;
  124. end;
  125. destructor TGLLineSettings.Destroy;
  126. begin
  127. FColor.Free;
  128. inherited;
  129. end;
  130. procedure TGLLineSettings.SetPattern(const value: TGLushort);
  131. begin
  132. if FPattern <> value then
  133. begin
  134. FPattern := Value;
  135. NotifyChange(self);
  136. end;
  137. end;
  138. procedure TGLLineSettings.SetColor(const v: TGLColor);
  139. begin
  140. FColor.Color := v.Color;
  141. NotifyChange(Self);
  142. end;
  143. procedure TGLLineSettings.SetWidth(const Value: Single);
  144. begin
  145. FWidth := Value;
  146. NotifyChange(Self);
  147. end;
  148. var
  149. IgnoreMatSave: boolean;
  150. procedure TGLLineSettings.Apply(var rci: TGLRenderContextInfo);
  151. begin
  152. rci.GLStates.LineWidth := Width;
  153. gl.Color4fv(Color.AsAddress);
  154. if Pattern <> $FFFF then
  155. begin
  156. rci.GLStates.Enable(stLineStipple);
  157. rci.GLStates.LineStippleFactor := 1;
  158. rci.GLStates.LineStipplePattern := Pattern;
  159. end
  160. else
  161. rci.GLStates.Disable(stLineStipple);
  162. if ForceMaterial then
  163. begin
  164. IgnoreMatSave := rci.ignoreMaterials;
  165. rci.ignoreMaterials := true;
  166. end;
  167. end;
  168. procedure TGLLineSettings.UnApply(var rci: TGLRenderContextInfo);
  169. begin
  170. if ForceMaterial then
  171. rci.ignoreMaterials := IgnoreMatSave;
  172. end;
  173. procedure TGLLineSettings.SetForceMaterial(v: boolean);
  174. begin
  175. if FForceMaterial <> v then
  176. begin
  177. FForceMaterial := v;
  178. NotifyChange(self);
  179. end;
  180. end;
  181. // ------------------
  182. // ------------------ TGLHiddenLineShader ------------------
  183. // ------------------
  184. constructor TGLHiddenLineShader.Create(AOwner: TComponent);
  185. begin
  186. inherited;
  187. FFrontLine := TGLLineSettings.Create(self);
  188. FBackLine := TGLLineSettings.Create(self);
  189. FSolid := false;
  190. FBackgroundColor := TGLColor.Create(Self);
  191. FBackgroundColor.Initialize(clrBtnFace);
  192. FLineSmooth := False;
  193. FLighting := true;
  194. FShadeModel := smDefault;
  195. end;
  196. destructor TGLHiddenLineShader.Destroy;
  197. begin
  198. FFrontLine.Free;
  199. FBackLine.Free;
  200. FBackgroundColor.Free;
  201. inherited;
  202. end;
  203. procedure TGLHiddenLineShader.DoApply(var rci: TGLRenderContextInfo; Sender:
  204. TObject);
  205. begin
  206. FPassCount := 1;
  207. if solid then
  208. with rci.GLStates do
  209. begin
  210. // draw filled front faces in first pass
  211. PolygonMode := pmFill;
  212. CullFaceMode := cmBack;
  213. if FLighting then
  214. begin
  215. case ShadeModel of
  216. smDefault, smSmooth: gl.ShadeModel(GL_SMOOTH);
  217. smFlat: gl.ShadeModel(GL_FLAT);
  218. end
  219. end
  220. else
  221. begin
  222. Disable(stLighting);
  223. gl.Color4fv(FBackgroundColor.AsAddress); // use background color
  224. end;
  225. // enable and adjust polygon offset
  226. Enable(stPolygonOffsetFill);
  227. end
  228. else
  229. with rci.GLStates do
  230. begin
  231. Disable(stLighting);
  232. // draw back lines in first pass
  233. FBackLine.Apply(rci);
  234. CullFaceMode := cmFront;
  235. PolygonMode := pmLines;
  236. // enable and adjust polygon offset
  237. Enable(stPolygonOffsetLine);
  238. end;
  239. rci.GLStates.SetPolygonOffset(1, 2);
  240. end;
  241. function TGLHiddenLineShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  242. procedure SetLineSmoothBlend;
  243. begin
  244. with rci.GLStates do
  245. begin
  246. LineStippleFactor := 1;
  247. LineStipplePattern := $FFFF;
  248. if LineSmooth then
  249. begin
  250. LineSmoothHint := hintNicest;
  251. Enable(stLineSmooth);
  252. end
  253. else
  254. Disable(stLineSmooth);
  255. if LineSmooth or (FBackLine.FColor.Alpha < 1)
  256. or (FFrontLine.FColor.Alpha < 1) then
  257. begin
  258. Enable(stBlend);
  259. SetBlendFunc(bfSrcAlpha, bfOneMinusSrcAlpha);
  260. end
  261. else
  262. Disable(stBlend);
  263. end;
  264. end;
  265. begin
  266. case FPassCount of
  267. 1:
  268. with rci.GLStates do 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.GLStates.Disable(stPolygonOffsetFill)
  280. else
  281. rci.GLStates.Disable(stPolygonOffsetLine);
  282. Result := True;
  283. end;
  284. 2:
  285. begin
  286. FFrontLine.UnApply(rci);
  287. rci.GLStates.PolygonMode := pmFill;
  288. Result := false;
  289. end;
  290. else
  291. Assert(False);
  292. Result := False;
  293. end;
  294. end;
  295. procedure TGLHiddenLineShader.SetBackgroundColor(AColor: TGLColor);
  296. begin
  297. FBackgroundColor.Color := AColor.Color;
  298. NotifyChange(Self);
  299. end;
  300. procedure TGLHiddenLineShader.SetlineSmooth(v: boolean);
  301. begin
  302. if FlineSmooth <> v then
  303. begin
  304. FlineSmooth := v;
  305. NotifyChange(self);
  306. end;
  307. end;
  308. procedure TGLHiddenLineShader.SetLighting(v: boolean);
  309. begin
  310. if FLighting <> v then
  311. begin
  312. FLighting := v;
  313. NotifyChange(self);
  314. end;
  315. end;
  316. procedure TGLHiddenLineShader.SetSolid(v: boolean);
  317. begin
  318. if FSolid <> v then
  319. begin
  320. FSolid := v;
  321. NotifyChange(self);
  322. end;
  323. end;
  324. procedure TGLHiddenLineShader.SetShadeModel(const val: TGLShadeModel);
  325. begin
  326. if FShadeModel <> val then
  327. begin
  328. FShadeModel := val;
  329. NotifyChange(Self);
  330. end;
  331. end;
  332. (*--------------------------------------
  333. TGLOutlineShader
  334. --------------------------------------*)
  335. constructor TGLOutlineShader.Create(AOwner: TComponent);
  336. begin
  337. inherited;
  338. FOutlineSmooth := False;
  339. FOutLineWidth := 2;
  340. FLineColor := TGLColor.CreateInitialized(Self, clrBlack);
  341. ShaderStyle := ssLowLevel;
  342. end;
  343. destructor TGLOutlineShader.Destroy;
  344. begin
  345. FLineColor.Free;
  346. inherited;
  347. end;
  348. procedure TGLOutlineShader.DoApply(var rci: TGLRenderContextInfo; 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 TGLOutlineShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  356. begin
  357. if rci.ignoreMaterials or (stStencilTest in rci.GLStates.States) then
  358. begin
  359. Result := False;
  360. Exit;
  361. end;
  362. case FPassCount of
  363. 1:
  364. with rci.GLStates 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. gl.Color4fv(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.GLStates 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 TGLOutlineShader.SetOutlineWidth(v: single);
  407. begin
  408. if FOutlineWidth <> v then
  409. begin
  410. FOutlineWidth := v;
  411. NotifyChange(self);
  412. end;
  413. end;
  414. procedure TGLOutlineShader.SetOutlineSmooth(v: boolean);
  415. begin
  416. if FOutlineSmooth <> v then
  417. begin
  418. FOutlineSmooth := v;
  419. NotifyChange(self);
  420. end;
  421. end;
  422. end.