GXS.HiddenLineShader.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXS.HiddenLineShader;
  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. Winapi.OpenGLext,
  15. System.Classes,
  16. GXS.Material,
  17. GXS.Scene,
  18. GXS.Color,
  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: GLushort;
  29. FForceMaterial: Boolean;
  30. procedure SetPattern(const value: GLushort);
  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: GLushort 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. // ------------------------------------------------------------------
  87. implementation
  88. // ------------------
  89. // ------------------ TgxLineSettings ------------------
  90. // ------------------
  91. constructor TgxLineSettings.Create(AOwner: TPersistent);
  92. begin
  93. inherited;
  94. FColor := TgxColor.Create(Self);
  95. FColor.Initialize(clrGray20);
  96. FWidth := 2;
  97. Pattern := $FFFF;
  98. ForceMaterial := false;
  99. end;
  100. destructor TgxLineSettings.Destroy;
  101. begin
  102. FColor.Free;
  103. inherited;
  104. end;
  105. procedure TgxLineSettings.SetPattern(const value: GLushort);
  106. begin
  107. if FPattern <> value then
  108. begin
  109. FPattern := Value;
  110. NotifyChange(self);
  111. end;
  112. end;
  113. procedure TgxLineSettings.SetColor(const v: TgxColor);
  114. begin
  115. FColor.Color := v.Color;
  116. NotifyChange(Self);
  117. end;
  118. procedure TgxLineSettings.SetWidth(const Value: Single);
  119. begin
  120. FWidth := Value;
  121. NotifyChange(Self);
  122. end;
  123. var
  124. IgnoreMatSave: boolean;
  125. procedure TgxLineSettings.Apply(var rci: TgxRenderContextInfo);
  126. begin
  127. rci.gxStates.LineWidth := Width;
  128. glColor4fv(@Color.AsAddress^);
  129. if Pattern <> $FFFF then
  130. begin
  131. rci.gxStates.Enable(stLineStipple);
  132. rci.gxStates.LineStippleFactor := 1;
  133. rci.gxStates.LineStipplePattern := Pattern;
  134. end
  135. else
  136. rci.gxStates.Disable(stLineStipple);
  137. if ForceMaterial then
  138. begin
  139. IgnoreMatSave := rci.ignoreMaterials;
  140. rci.ignoreMaterials := true;
  141. end;
  142. end;
  143. procedure TgxLineSettings.UnApply(var rci: TgxRenderContextInfo);
  144. begin
  145. if ForceMaterial then
  146. rci.ignoreMaterials := IgnoreMatSave;
  147. end;
  148. procedure TgxLineSettings.SetForceMaterial(v: boolean);
  149. begin
  150. if FForceMaterial <> v then
  151. begin
  152. FForceMaterial := v;
  153. NotifyChange(self);
  154. end;
  155. end;
  156. // ------------------
  157. // ------------------ TgxHiddenLineShader ------------------
  158. // ------------------
  159. constructor TgxHiddenLineShader.Create(AOwner: TComponent);
  160. begin
  161. inherited;
  162. FFrontLine := TgxLineSettings.Create(self);
  163. FBackLine := TgxLineSettings.Create(self);
  164. FSolid := false;
  165. FBackgroundColor := TgxColor.Create(Self);
  166. FBackgroundColor.Initialize(clrBtnFace);
  167. FLineSmooth := False;
  168. FLighting := true;
  169. FShadeModel := smDefault;
  170. end;
  171. destructor TgxHiddenLineShader.Destroy;
  172. begin
  173. FFrontLine.Free;
  174. FBackLine.Free;
  175. FBackgroundColor.Free;
  176. inherited;
  177. end;
  178. procedure TgxHiddenLineShader.DoApply(var rci: TgxRenderContextInfo; Sender:
  179. TObject);
  180. begin
  181. FPassCount := 1;
  182. if solid then
  183. with rci.gxStates do
  184. begin
  185. // draw filled front faces in first pass
  186. PolygonMode := pmFill;
  187. CullFaceMode := cmBack;
  188. if FLighting then
  189. begin
  190. case ShadeModel of
  191. smDefault, smSmooth: glShadeModel(GL_SMOOTH);
  192. smFlat: glShadeModel(GL_FLAT);
  193. end
  194. end
  195. else
  196. begin
  197. Disable(stLighting);
  198. glColor4fv(@FBackgroundColor.AsAddress^); // use background color
  199. end;
  200. // enable and adjust polygon offset
  201. Enable(stPolygonOffsetFill);
  202. end
  203. else
  204. with rci.gxStates do
  205. begin
  206. Disable(stLighting);
  207. // draw back lines in first pass
  208. FBackLine.Apply(rci);
  209. CullFaceMode := cmFront;
  210. PolygonMode := pmLines;
  211. // enable and adjust polygon offset
  212. Enable(stPolygonOffsetLine);
  213. end;
  214. rci.gxStates.SetPolygonOffset(1, 2);
  215. end;
  216. function TgxHiddenLineShader.DoUnApply(var rci: TgxRenderContextInfo): Boolean;
  217. procedure SetLineSmoothBlend;
  218. begin
  219. with rci.gxStates do
  220. begin
  221. LineStippleFactor := 1;
  222. LineStipplePattern := $FFFF;
  223. if LineSmooth then
  224. begin
  225. LineSmoothHint := hintNicest;
  226. Enable(stLineSmooth);
  227. end
  228. else
  229. Disable(stLineSmooth);
  230. if LineSmooth or (FBackLine.FColor.Alpha < 1)
  231. or (FFrontLine.FColor.Alpha < 1) then
  232. begin
  233. Enable(stBlend);
  234. SetBlendFunc(bfSrcAlpha, bfOneMinusSrcAlpha);
  235. end
  236. else
  237. Disable(stBlend);
  238. end;
  239. end;
  240. begin
  241. case FPassCount of
  242. 1:
  243. with rci.gxStates do begin
  244. // draw front line in 2nd pass
  245. FPassCount := 2;
  246. FBackLine.UnApply(rci);
  247. FFrontLine.Apply(rci);
  248. SetLineSmoothBlend;
  249. if solid and FLighting then
  250. Disable(stLighting);
  251. PolygonMode := pmLines;
  252. CullFaceMode := cmBack;
  253. if solid then
  254. rci.gxStates.Disable(stPolygonOffsetFill)
  255. else
  256. rci.gxStates.Disable(stPolygonOffsetLine);
  257. Result := True;
  258. end;
  259. 2:
  260. begin
  261. FFrontLine.UnApply(rci);
  262. rci.gxStates.PolygonMode := pmFill;
  263. Result := false;
  264. end;
  265. else
  266. Assert(False);
  267. Result := False;
  268. end;
  269. end;
  270. procedure TgxHiddenLineShader.SetBackgroundColor(AColor: TgxColor);
  271. begin
  272. FBackgroundColor.Color := AColor.Color;
  273. NotifyChange(Self);
  274. end;
  275. procedure TgxHiddenLineShader.SetlineSmooth(v: boolean);
  276. begin
  277. if FlineSmooth <> v then
  278. begin
  279. FlineSmooth := v;
  280. NotifyChange(self);
  281. end;
  282. end;
  283. procedure TgxHiddenLineShader.SetLighting(v: boolean);
  284. begin
  285. if FLighting <> v then
  286. begin
  287. FLighting := v;
  288. NotifyChange(self);
  289. end;
  290. end;
  291. procedure TgxHiddenLineShader.SetSolid(v: boolean);
  292. begin
  293. if FSolid <> v then
  294. begin
  295. FSolid := v;
  296. NotifyChange(self);
  297. end;
  298. end;
  299. procedure TgxHiddenLineShader.SetShadeModel(const val: TgxShadeModel);
  300. begin
  301. if FShadeModel <> val then
  302. begin
  303. FShadeModel := val;
  304. NotifyChange(Self);
  305. end;
  306. end;
  307. end.