GLSL.ShapeShaders.pas 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  1. //
  2. // The graphics engine GLScene
  3. //
  4. unit GLSL.ShapeShaders;
  5. (*
  6. Erosion shader Erode surface object and render with Anisotropic Specular Reflection
  7. At this time one light source is supported
  8. *)
  9. interface
  10. {$I Stage.Defines.inc}
  11. uses
  12. Winapi.OpenGL,
  13. Winapi.OpenGLext,
  14. System.Classes,
  15. System.SysUtils,
  16. Stage.OpenGLTokens,
  17. GLS.Scene,
  18. GLS.BaseClasses,
  19. GLS.State,
  20. GLS.Context,
  21. GLS.Graphics,
  22. GLS.RenderContextInfo,
  23. GLS.Coordinates,
  24. Stage.VectorGeometry,
  25. Stage.VectorTypes,
  26. Stage.TextureFormat,
  27. GLS.Color,
  28. GLS.Texture,
  29. GLS.Material,
  30. GLSL.Shader,
  31. GLSL.CustomShader;
  32. //------------------ Cel Shader --------------------------
  33. type
  34. (* A shader that applies cel shading through a vertex program
  35. and shade definition texture.
  36. Cel shading options:
  37. csoOutlines: Render a second outline pass.
  38. csoTextured: Allows for a primary texture that the cel shading
  39. is modulated with and forces the shade definition
  40. to render as a second texture. *)
  41. TGLCelShaderOption = (csoOutlines, csoTextured, csoNoBuildShadeTexture);
  42. TGLCelShaderOptions = set of TGLCelShaderOption;
  43. // An event for user defined cel intensity.
  44. TGLCelShaderGetIntensity = procedure(Sender: TObject; var intensity: Byte) of
  45. object;
  46. // A generic cel shader.
  47. TGLCelShader = class(TGLShader)
  48. private
  49. FOutlineWidth: Single;
  50. FCelShaderOptions: TGLCelShaderOptions;
  51. FVPHandle: TGLARBVertexProgramHandle;
  52. FShadeTexture: TGLTexture;
  53. FOnGetIntensity: TGLCelShaderGetIntensity;
  54. FOutlinePass,
  55. FUnApplyShadeTexture: Boolean;
  56. FOutlineColor: TGLColor;
  57. protected
  58. procedure SetCelShaderOptions(const val: TGLCelShaderOptions);
  59. procedure SetOutlineWidth(const val: Single);
  60. procedure SetOutlineColor(const val: TGLColor);
  61. procedure BuildShadeTexture;
  62. procedure Loaded; override;
  63. function GenerateVertexProgram: string;
  64. public
  65. constructor Create(AOwner: TComponent); override;
  66. destructor Destroy; override;
  67. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  68. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  69. property ShadeTexture: TGLTexture read FShadeTexture;
  70. published
  71. property CelShaderOptions: TGLCelShaderOptions read FCelShaderOptions write
  72. SetCelShaderOptions;
  73. property OutlineColor: TGLColor read FOutlineColor write SetOutlineColor;
  74. property OutlineWidth: Single read FOutlineWidth write SetOutlineWidth;
  75. property OnGetIntensity: TGLCelShaderGetIntensity read FOnGetIntensity write
  76. FOnGetIntensity;
  77. end;
  78. //------------- Erosion Shader ---------------------
  79. type
  80. (* Custom class for a shader that Erode surface object *)
  81. TGLCustomGLSLSimpleErosionShader = class(TGLCustomGLSLShader)
  82. private
  83. FMaterialLibrary: TGLAbstractMaterialLibrary;
  84. FMainTex : TGLTexture;
  85. FNoiseTex : TGLTexture;
  86. FErosionTex : TGLTexture;
  87. FMainTexName : TGLLibMaterialName;
  88. FNoiseTexName : TGLLibMaterialName;
  89. FErosionTexName : TGLLibMaterialName;
  90. FErosionScale: Single;
  91. FErosionFactor: Single;
  92. FIntensityFactor1: Single;
  93. FIntensityFactor2: Single;
  94. FSpecularColor : TGLColor;
  95. FAmbientColor : TGLColor;
  96. FAmbientFactor : Single;
  97. FDiffuseFactor : Single;
  98. FSpecularFactor : Single;
  99. FSpecularRoughness : Single;
  100. FAnisotropicRoughness : Single;
  101. function GetMaterialLibrary: TGLAbstractMaterialLibrary;
  102. procedure SetMainTexTexture(const Value: TGLTexture);
  103. procedure SetNoiseTexTexture(const Value: TGLTexture);
  104. procedure SetErosionTexTexture(const Value: TGLTexture);
  105. function GetMainTexName: TGLLibMaterialName;
  106. procedure SetMainTexName(const Value: TGLLibMaterialName);
  107. function GetNoiseTexName: TGLLibMaterialName;
  108. procedure SetNoiseTexName(const Value: TGLLibMaterialName);
  109. function GetErosionTexName: TGLLibMaterialName;
  110. procedure SetErosionTexName(const Value: TGLLibMaterialName);
  111. procedure SetAmbientColor(AValue: TGLColor);
  112. procedure SetSpecularColor(AValue: TGLColor);
  113. protected
  114. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  115. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  116. procedure SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary); virtual;
  117. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  118. public
  119. constructor Create(AOwner : TComponent); override;
  120. destructor Destroy; override;
  121. // property Color1: TGLColor read FColor1;
  122. // property Color2: TGLColor read FColor2;
  123. property MaterialLibrary: TGLAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  124. property MainTexture: TGLTexture read FMainTex write SetMainTexTexture;
  125. property MainTextureName: TGLLibMaterialName read GetMainTexName write SetMainTexName;
  126. property NoiseTexture: TGLTexture read FNoiseTex write SetNoiseTexTexture;
  127. property NoiseTextureName: TGLLibMaterialName read GetNoiseTexName write SetNoiseTexName;
  128. property ErosionTexture: TGLTexture read FErosionTex write SetErosionTexTexture;
  129. property ErosionTextureName: TGLLibMaterialName read GetErosionTexName write SetErosionTexName;
  130. property ErosionFactor: Single read FErosionFactor write FErosionFactor;
  131. property ErosionScale: Single read FErosionFactor write FErosionFactor;
  132. property IntensityFactor1: Single read FIntensityFactor1 write FIntensityFactor1;
  133. property IntensityFactor2: Single read FIntensityFactor2 write FIntensityFactor2;
  134. property SpecularColor : TGLColor Read FSpecularColor Write setSpecularColor;
  135. property AmbientColor : TGLColor Read FAmbientColor Write setAmbientColor;
  136. property AmbientFactor : Single Read FAmbientFactor Write FAmbientFactor;
  137. property DiffuseFactor : Single Read FDiffuseFactor Write FDiffuseFactor;
  138. property SpecularFactor : Single Read FSpecularFactor Write FSpecularFactor;
  139. property SpecularRoughness : Single Read FSpecularRoughness Write FSpecularRoughness;
  140. property AnisotropicRoughness : Single Read FAnisotropicRoughness Write FAnisotropicRoughness;
  141. end;
  142. TGLSLSimpleErosionShader = class(TGLCustomGLSLSimpleErosionShader)
  143. published
  144. // property Color1;
  145. // property Color2;
  146. property MaterialLibrary;
  147. property MainTexture;
  148. property MainTextureName;
  149. property NoiseTexture;
  150. property NoiseTextureName;
  151. property ErosionTexture;
  152. property ErosionTextureName;
  153. property ErosionScale;
  154. property ErosionFactor;
  155. property IntensityFactor1;
  156. property IntensityFactor2;
  157. property SpecularColor;
  158. property AmbientColor;
  159. property AmbientFactor;
  160. property DiffuseFactor;
  161. property SpecularFactor;
  162. property SpecularRoughness;
  163. property AnisotropicRoughness;
  164. end;
  165. //-------------- Glass Shader --------------------
  166. type
  167. (* Custom class for Glass shader:
  168. Environment mapping and refraction mapping using the fresnel terms *)
  169. TGLCustomGLSLGlassShader = class(TGLCustomGLSLShader)
  170. private
  171. FDiffuseColor: TGLColor;
  172. FDepth: Single;
  173. FMix: Single;
  174. FAlpha: Single;
  175. FMaterialLibrary: TGLAbstractMaterialLibrary;
  176. FMainTexture: TGLTexture; // EnvMap
  177. FMainTexName: TGLLibMaterialName;
  178. FRefractionTexture: TGLTexture;
  179. FRefractionTexName: TGLLibMaterialName;
  180. FOwnerObject: TGLBaseSceneObject;
  181. FBlendSrc: TGLBlendFunction;
  182. FBlendDst: TGLBlendFunction;
  183. function GetMaterialLibrary: TGLAbstractMaterialLibrary;
  184. procedure SetMainTexTexture(const Value: TGLTexture);
  185. function GetMainTexName: TGLLibMaterialName;
  186. procedure SetMainTexName(const Value: TGLLibMaterialName);
  187. procedure SetRefractionTexTexture(const Value: TGLTexture);
  188. function GetRefractionTexName: TGLLibMaterialName;
  189. procedure SetRefractionTexName(const Value: TGLLibMaterialName);
  190. procedure SetDiffuseColor(AValue: TGLColor);
  191. protected
  192. procedure DoApply(var rci: TGLRenderContextInfo; Sender: TObject); override;
  193. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  194. procedure SetMaterialLibrary(const Value
  195. : TGLAbstractMaterialLibrary); virtual;
  196. procedure Notification(AComponent: TComponent;
  197. Operation: TOperation); override;
  198. public
  199. constructor Create(AOwner: TComponent); override;
  200. destructor Destroy; override;
  201. property DiffuseColor: TGLColor read FDiffuseColor Write SetDiffuseColor;
  202. property Depth: Single read FDepth write FDepth;
  203. property Mix: Single read FMix write FMix;
  204. property Alpha: Single read FAlpha write FAlpha;
  205. property MaterialLibrary: TGLAbstractMaterialLibrary read GetMaterialLibrary
  206. write SetMaterialLibrary;
  207. property MainTexture: TGLTexture read FMainTexture write SetMainTexTexture;
  208. property MainTextureName: TGLLibMaterialName read GetMainTexName
  209. write SetMainTexName;
  210. property RefractionTexture: TGLTexture read FRefractionTexture
  211. write SetRefractionTexTexture;
  212. property RefractionTextureName: TGLLibMaterialName read GetRefractionTexName
  213. write SetRefractionTexName;
  214. property OwnerObject: TGLBaseSceneObject read FOwnerObject
  215. write FOwnerObject;
  216. property BlendSrc: TGLBlendFunction read FBlendSrc write FBlendSrc
  217. default bfSrcAlpha;
  218. property BlendDst: TGLBlendFunction read FBlendDst write FBlendDst
  219. default bfDstAlpha;
  220. end;
  221. TGLSLGlassShader = class(TGLCustomGLSLGlassShader)
  222. published
  223. property DiffuseColor;
  224. property Depth;
  225. property Mix;
  226. property Alpha;
  227. property MaterialLibrary;
  228. property MainTexture;
  229. property MainTextureName;
  230. property RefractionTexture;
  231. property RefractionTextureName;
  232. property OwnerObject;
  233. property BlendSrc;
  234. property BlendDst;
  235. end;
  236. //-----------Gooch Shader -----------------------
  237. type
  238. (* Custom class for Gooch Shader *)
  239. TGLCustomGLSLSimpleGoochShader = class(TGLCustomGLSLShader)
  240. private
  241. FDiffuseColor : TGLColor;
  242. FWarmColor : TGLColor;
  243. FCoolColor : TGLColor;
  244. FSpecularColor : TGLColor;
  245. FAmbientColor : TGLColor;
  246. FDiffuseWarm : Single;
  247. FDiffuseCool : Single;
  248. FAmbientFactor : Single;
  249. FDiffuseFactor : Single;
  250. FSpecularFactor : Single;
  251. FBlendingMode: TGLBlendingModeEx;
  252. procedure SetDiffuseColor(AValue: TGLColor);
  253. procedure SetAmbientColor(AValue: TGLColor);
  254. procedure SetSpecularColor(AValue: TGLColor);
  255. procedure SetWarmColor(AValue: TGLColor);
  256. procedure SetCoolColor(AValue: TGLColor);
  257. protected
  258. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  259. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  260. public
  261. constructor Create(AOwner : TComponent); override;
  262. destructor Destroy; override;
  263. property DiffuseColor : TGLColor read FDiffuseColor Write setDiffuseColor;
  264. property WarmColor : TGLColor read FWarmColor Write setWarmColor;
  265. property CoolColor : TGLColor Read FCoolColor Write setCoolColor;
  266. property SpecularColor : TGLColor Read FSpecularColor Write setSpecularColor;
  267. property AmbientColor : TGLColor Read FAmbientColor Write setAmbientColor;
  268. property WarmFactor : Single Read FDiffuseWarm Write FDiffuseWarm;
  269. property CoolFactor : Single Read FDiffuseCool Write FDiffuseCool;
  270. property AmbientFactor : Single Read FAmbientFactor Write FAmbientFactor;
  271. property DiffuseFactor : Single Read FDiffuseFactor Write FDiffuseFactor;
  272. property SpecularFactor : Single Read FSpecularFactor Write FSpecularFactor;
  273. property BlendingMode: TGLBlendingModeEx read FBlendingMode write FBlendingMode default bmxOpaque;
  274. end;
  275. type
  276. TGLSLSimpleGoochShader = class(TGLCustomGLSLSimpleGoochShader)
  277. published
  278. property DiffuseColor;
  279. property WarmColor;
  280. property CoolColor;
  281. property SpecularColor;
  282. property AmbientColor;
  283. property WarmFactor;
  284. property CoolFactor;
  285. property AmbientFactor;
  286. property DiffuseFactor;
  287. property SpecularFactor;
  288. end;
  289. //------------Fur Shader ------------------
  290. type
  291. (* Custom class for Fur Shader *)
  292. TGLCustomGLSLFurShader = class(TGLCustomGLSLShader)
  293. private
  294. FMaterialLibrary: TGLAbstractMaterialLibrary;
  295. FCurrentPass: Integer;
  296. FPassCount: Single;
  297. FFurLength: Single;
  298. FMaxFurLength: Single;
  299. FFurScale: Single;
  300. FRandomFurLength : Boolean;
  301. FColorScale: TGLColor;
  302. FAmbient: TGLColor;
  303. FGravity : TGLCoordinates;
  304. FLightIntensity : Single;
  305. FMainTex : TGLTexture;
  306. FNoiseTex : TGLTexture;
  307. FNoiseTexName : TGLLibMaterialName;
  308. FMainTexName : TGLLibMaterialName;
  309. FBlendSrc : TGLBlendFunction;
  310. FBlendDst : TGLBlendFunction;
  311. // FBlendEquation : TGLBlendEquation;
  312. function GetMaterialLibrary: TGLAbstractMaterialLibrary;
  313. procedure SetMainTexTexture(const Value: TGLTexture);
  314. procedure SetNoiseTexTexture(const Value: TGLTexture);
  315. function GetNoiseTexName: TGLLibMaterialName;
  316. procedure SetNoiseTexName(const Value: TGLLibMaterialName);
  317. function GetMainTexName: TGLLibMaterialName;
  318. procedure SetMainTexName(const Value: TGLLibMaterialName);
  319. procedure SetGravity(APosition:TGLCoordinates);
  320. procedure SetAmbient(AValue: TGLColor);
  321. procedure SetColorScale(AValue: TGLColor);
  322. protected
  323. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  324. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  325. procedure SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary); virtual;
  326. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  327. public
  328. //Common stuff
  329. constructor Create(AOwner : TComponent); override;
  330. destructor Destroy; override;
  331. property PassCount: Single read FPassCount write FPassCount;
  332. property FurLength: Single read FFurLength write FFurLength;
  333. property MaxFurLength: Single read FMaxFurLength write FMaxFurLength;
  334. property FurDensity: Single read FFurScale write FFurScale;
  335. property RandomFurLength : Boolean read FRandomFurLength Write FRandomFurLength;
  336. property ColorScale: TGLColor read FColorScale Write setColorScale;
  337. property Ambient: TGLColor read FAmbient write setAmbient;
  338. property MaterialLibrary: TGLAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  339. property MainTexture: TGLTexture read FMainTex write SetMainTexTexture;
  340. property MainTextureName: TGLLibMaterialName read GetMainTexName write SetMainTexName;
  341. property NoiseTexture: TGLTexture read FNoiseTex write SetNoiseTexTexture;
  342. property NoiseTextureName: TGLLibMaterialName read GetNoiseTexName write SetNoiseTexName;
  343. //property BlendEquation : TBlendEquation read FBlendEquation write FBlendEquation default beMin;
  344. property BlendSrc : TGLBlendFunction read FBlendSrc write FBlendSrc default bfSrcColor;
  345. property BlendDst : TGLBlendFunction read FBlendDst write FBlendDst default bfOneMinusDstColor;
  346. property Gravity : TGLCoordinates Read FGravity write setGravity;
  347. property LightIntensity : Single read FLightIntensity Write FLightIntensity;
  348. end;
  349. TGLSLFurShader = class(TGLCustomGLSLFurShader)
  350. published
  351. property PassCount;
  352. property FurLength;
  353. property MaxFurLength;
  354. property FurDensity;
  355. property RandomFurLength;
  356. property ColorScale;
  357. property Ambient;
  358. property LightIntensity;
  359. property Gravity;
  360. property BlendSrc;
  361. property BlendDst;
  362. property MainTexture;
  363. property MainTextureName;
  364. property NoiseTexture;
  365. property NoiseTextureName;
  366. end;
  367. //------------ Ivory Shader ----------------
  368. type
  369. (* Custom class for a shader that simulate Ivory Material *)
  370. TGLCustomGLSLIvoryShader = class(TGLCustomGLSLShader)
  371. protected
  372. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  373. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  374. public
  375. constructor Create(AOwner : TComponent); override;
  376. destructor Destroy; override;
  377. end;
  378. type
  379. TGLSLIvoryShader = class(TGLCustomGLSLIvoryShader)
  380. end;
  381. //------------- Lattice Shader ----------------------
  382. type
  383. (* Custom class for a shader that simulate Lattice *)
  384. TGLCustomGLSLSimpleLatticeShader = class(TGLCustomGLSLShader)
  385. private
  386. FLatticeScale: TGLCoordinates2;
  387. FLatticeThreshold: TGLCoordinates2;
  388. procedure SetLatticeScale(const Value: TGLCoordinates2);
  389. procedure SetLatticeThreshold(const Value: TGLCoordinates2);
  390. protected
  391. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  392. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  393. public
  394. constructor Create(AOwner : TComponent); override;
  395. destructor Destroy; override;
  396. property LatticeScale: TGLCoordinates2 read FLatticeScale write SetLatticeScale;
  397. property LatticeThreshold: TGLCoordinates2 read FLatticeThreshold write SetLatticeThreshold;
  398. end;
  399. (* Custom class for GLSLLatticeShader.
  400. A shader that simulate Lattice with Diffuse/Specular and support Texture *)
  401. TGLCustomGLSLLatticeShader = class(TGLCustomGLSLSimpleLatticeShader)
  402. private
  403. FAmbientColor: TGLColor;
  404. FDiffuseColor: TGLColor;
  405. FSpecularColor: TGLColor;
  406. FMaterialLibrary: TGLAbstractMaterialLibrary;
  407. FMainTexture: TGLTexture;
  408. FMainTexName : TGLLibMaterialName;
  409. FSpecularPower: Single;
  410. FLightPower: Single;
  411. function GetMaterialLibrary: TGLAbstractMaterialLibrary;
  412. procedure SetMainTexTexture(const Value: TGLTexture);
  413. function GetMainTexName: TGLLibMaterialName;
  414. procedure SetMainTexName(const Value: TGLLibMaterialName);
  415. procedure SetDiffuseColor(AValue: TGLColor);
  416. procedure SetAmbientColor(AValue: TGLColor);
  417. procedure SetSpecularColor(AValue: TGLColor);
  418. protected
  419. procedure DoInitialize(var rci : TGLRenderContextInfo; Sender : TObject); override;
  420. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  421. procedure SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary); virtual;
  422. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  423. public
  424. constructor Create(AOwner : TComponent); override;
  425. destructor Destroy; override;
  426. property DiffuseColor : TGLColor read FDiffuseColor Write setDiffuseColor;
  427. property SpecularColor : TGLColor Read FSpecularColor Write setSpecularColor;
  428. property AmbientColor : TGLColor Read FAmbientColor Write setAmbientColor;
  429. property MaterialLibrary: TGLAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  430. property MainTexture: TGLTexture read FMainTexture write SetMainTexTexture;
  431. property MainTextureName: TGLLibMaterialName read GetMainTexName write SetMainTexName;
  432. property SpecularPower: Single read FSpecularPower write FSpecularPower;
  433. property LightPower: Single read FLightPower write FLightPower;
  434. end;
  435. TGLSLSimpleLatticeShader = class(TGLCustomGLSLSimpleLatticeShader)
  436. published
  437. property LatticeScale;
  438. property LatticeThreshold;
  439. end;
  440. TGLSLLatticeShader = class(TGLCustomGLSLLatticeShader)
  441. published
  442. property LatticeScale;
  443. property LatticeThreshold;
  444. property AmbientColor;
  445. property DiffuseColor;
  446. property SpecularColor;
  447. property MainTexture;
  448. property SpecularPower;
  449. property LightPower;
  450. end;
  451. //----------------- SEM Shader ---------------------
  452. type
  453. (* Custom class for SEM Shader : Spherical Environment Mapping *)
  454. TGLCustomGLSLSemShader = class(TGLCustomGLSLShader)
  455. private
  456. FAmbientColor: TGLColor;
  457. // FDiffuseColor: TGLColor;
  458. FSpecularColor: TGLColor;
  459. FAmbientFactor : Single;
  460. FDiffuseFactor : Single;
  461. FSpecularFactor : Single;
  462. FMaterialLibrary: TGLAbstractMaterialLibrary;
  463. FMainTexture: TGLTexture;
  464. FMainTexName : TGLLibMaterialName;
  465. // FSpecularPower: Single;
  466. // FLightPower: Single;
  467. function GetMaterialLibrary: TGLAbstractMaterialLibrary;
  468. procedure SetMainTexTexture(const Value: TGLTexture);
  469. function GetMainTexName: TGLLibMaterialName;
  470. procedure SetMainTexName(const Value: TGLLibMaterialName);
  471. //procedure SetDiffuseColor(AValue: TGLColor);
  472. procedure SetAmbientColor(AValue: TGLColor);
  473. procedure SetSpecularColor(AValue: TGLColor);
  474. protected
  475. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  476. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  477. procedure SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary); virtual;
  478. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  479. public
  480. constructor Create(AOwner : TComponent); override;
  481. destructor Destroy; override;
  482. // property DiffuseColor : TGLColor read FDiffuseColor Write setDiffuseColor;
  483. property SpecularColor : TGLColor Read FSpecularColor Write setSpecularColor;
  484. property AmbientColor : TGLColor Read FAmbientColor Write setAmbientColor;
  485. property AmbientFactor : Single Read FAmbientFactor Write FAmbientFactor;
  486. property DiffuseFactor : Single Read FDiffuseFactor Write FDiffuseFactor;
  487. property SpecularFactor : Single Read FSpecularFactor Write FSpecularFactor;
  488. property MaterialLibrary: TGLAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  489. property MainTexture: TGLTexture read FMainTexture write SetMainTexTexture;
  490. property MainTextureName: TGLLibMaterialName read GetMainTexName write SetMainTexName;
  491. // property SpecularPower: Single read FSpecularPower write FSpecularPower;
  492. // property LightPower: Single read FLightPower write FLightPower;
  493. end;
  494. TGLSLSemShader = class(TGLCustomGLSLSemShader)
  495. published
  496. property AmbientColor;
  497. // property DiffuseColor;
  498. property SpecularColor;
  499. property AmbientFactor;
  500. property DiffuseFactor;
  501. property SpecularFactor;
  502. property MaterialLibrary;
  503. property MainTexture;
  504. property MainTextureName;
  505. end;
  506. //----------------- Toon Shader ---------------------
  507. type
  508. (* Custom class for Toon Shader *)
  509. TGLCustomGLSLToonShader = class(TGLCustomGLSLShader)
  510. private
  511. FHighlightColor : TGLColor;
  512. FMidColor : TGLColor;
  513. FLightenShadowColor : TGLColor;
  514. FDarkenShadowColor : TGLColor;
  515. FOutlineColor : TGLColor;
  516. FHighlightSize : Single;
  517. FMidSize : Single;
  518. FShadowSize : Single;
  519. FOutlineWidth : Single;
  520. procedure SetHighLightColor(AValue: TGLColor);
  521. procedure SetMidColor(AValue: TGLColor);
  522. procedure SetLightenShadowColor(AValue: TGLColor);
  523. procedure SetDarkenShadowColor(AValue: TGLColor);
  524. procedure SetOutlineColor(AValue: TGLColor);
  525. protected
  526. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  527. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  528. public
  529. constructor Create(AOwner : TComponent); override;
  530. destructor Destroy; override;
  531. property HighlightColor : TGLColor read FHighlightColor Write setHighlightColor;
  532. property MidColor : TGLColor read FMidColor Write setMidColor;
  533. property LightenShadowColor : TGLColor Read FLightenShadowColor Write setLightenShadowColor;
  534. property DarkenShadowrColor : TGLColor Read FDarkenShadowColor Write setDarkenShadowColor;
  535. property OutlinetColor : TGLColor Read FOutlineColor Write setOutlineColor;
  536. property HighlightSize : Single read FHighlightSize write FHighlightSize;
  537. property MidSize : Single read FMidSize write FMidSize;
  538. property ShadowSize : Single read FShadowSize write FShadowSize;
  539. property OutlineWidth : Single read FOutlineWidth write FOutlineWidth;
  540. end;
  541. type
  542. TGLSLToonShader = class(TGLCustomGLSLToonShader)
  543. published
  544. property HighlightColor;
  545. property MidColor;
  546. property LightenShadowColor;
  547. property DarkenShadowrColor;
  548. property OutlinetColor;
  549. property HighlightSize;
  550. property MidSize;
  551. property ShadowSize;
  552. property OutlineWidth;
  553. end;
  554. //----------- Vertex Displacement Shader -----------
  555. (*
  556. VertexDisplacement shader: Basic Vertex Displacement with Perlin Noise
  557. You can Improved it:
  558. The vertex displacement can be done by reading a 2D or 3D texture.
  559. It can be done along the normal or the tangent.
  560. It can be scaled, twisted, modulated, inverted...
  561. Converted from : https://www.clicktorelease.com/blog/vertex-displacement-noise-3d-webgl-glsl-three-js
  562. At this time only one light source is supported
  563. *)
  564. TGLCustomGLSLVertexDisplacementShader = class(TGLCustomGLSLShader)
  565. private
  566. FAmbientColor: TGLColor;
  567. // FDiffuseColor: TGLColor;
  568. FSpecularColor: TGLColor;
  569. FAmbientFactor : Single;
  570. FDiffuseFactor : Single;
  571. FSpecularFactor : Single;
  572. FMaterialLibrary: TGLAbstractMaterialLibrary;
  573. FMainTexture: TGLTexture;
  574. FMainTexName : TGLLibMaterialName;
  575. FElapsedTime : Single;
  576. FNoise : Single;
  577. FDisplacementScale : Single;
  578. FNoiseScale : Single;
  579. FTurbulenceFactor : Single;
  580. FNoisePeriod : Single;
  581. FTimeFactor : Single;
  582. function GetMaterialLibrary: TGLAbstractMaterialLibrary;
  583. procedure SetMainTexTexture(const Value: TGLTexture);
  584. function GetMainTexName: TGLLibMaterialName;
  585. procedure SetMainTexName(const Value: TGLLibMaterialName);
  586. //procedure SetDiffuseColor(AValue: TGLColor);
  587. procedure SetAmbientColor(AValue: TGLColor);
  588. procedure SetSpecularColor(AValue: TGLColor);
  589. protected
  590. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  591. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  592. procedure SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary); virtual;
  593. procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  594. public
  595. constructor Create(AOwner : TComponent); override;
  596. destructor Destroy; override;
  597. // property DiffuseColor : TGLColor read FDiffuseColor Write setDiffuseColor;
  598. property SpecularColor : TGLColor Read FSpecularColor Write setSpecularColor;
  599. property AmbientColor : TGLColor Read FAmbientColor Write setAmbientColor;
  600. property AmbientFactor : Single Read FAmbientFactor Write FAmbientFactor;
  601. property DiffuseFactor : Single Read FDiffuseFactor Write FDiffuseFactor;
  602. property SpecularFactor : Single Read FSpecularFactor Write FSpecularFactor;
  603. property MaterialLibrary: TGLAbstractMaterialLibrary read getMaterialLibrary write SetMaterialLibrary;
  604. property MainTexture: TGLTexture read FMainTexture write SetMainTexTexture;
  605. property MainTextureName: TGLLibMaterialName read GetMainTexName write SetMainTexName;
  606. property ElapsedTime: Single read FElapsedTime write FElapsedTime;
  607. property NoiseFactor : Single read FNoise write FNoise;
  608. property NoiseScale : Single read FNoiseScale write FNoiseScale;
  609. property TurbulenceFactor : Single read FTurbulenceFactor write FTurbulenceFactor;
  610. property NoisePeriod : Single read FNoisePeriod write FNoisePeriod;
  611. property DisplacementScale : Single read FDisplacementScale write FDisplacementScale;
  612. property TimeFactor : Single read FTimeFactor write FTimeFactor;
  613. end;
  614. TGLSLVertexDisplacementShader = class(TGLCustomGLSLVertexDisplacementShader)
  615. published
  616. property AmbientColor;
  617. // property DiffuseColor;
  618. property SpecularColor;
  619. property AmbientFactor;
  620. property DiffuseFactor;
  621. property SpecularFactor;
  622. property MaterialLibrary;
  623. property MainTexture;
  624. property MainTextureName;
  625. property ElapsedTime;
  626. property NoiseFactor;
  627. property NoiseScale;
  628. property TurbulenceFactor;
  629. property NoisePeriod;
  630. property DisplacementScale;
  631. property TimeFactor;
  632. end;
  633. //----------------------------------------------------------
  634. implementation
  635. //----------------------------------------------------------
  636. const
  637. fBuffSize: Integer = 512;
  638. (***************************************************
  639. TGLCelShader
  640. ***************************************************)
  641. constructor TGLCelShader.Create(AOwner: TComponent);
  642. begin
  643. inherited;
  644. FOutlineWidth := 3;
  645. FCelShaderOptions := [csoOutlines];
  646. FShadeTexture := TGLTexture.Create(Self);
  647. with FShadeTexture do
  648. begin
  649. Enabled := True;
  650. MinFilter := miNearest;
  651. MagFilter := maNearest;
  652. TextureWrap := twNone;
  653. TextureMode := tmModulate;
  654. end;
  655. FOutlineColor := TGLColor.Create(Self);
  656. FOutlineColor.OnNotifyChange := NotifyChange;
  657. FOutlineColor.Initialize(clrBlack);
  658. ShaderStyle := ssLowLevel;
  659. FVPHandle := TGLARBVertexProgramHandle.Create;
  660. end;
  661. destructor TGLCelShader.Destroy;
  662. begin
  663. FVPHandle.Free;
  664. FShadeTexture.Free;
  665. FOutlineColor.Free;
  666. inherited;
  667. end;
  668. procedure TGLCelShader.Loaded;
  669. begin
  670. inherited;
  671. BuildShadeTexture;
  672. end;
  673. procedure TGLCelShader.BuildShadeTexture;
  674. var
  675. bmp32: TGLBitmap32;
  676. i: Integer;
  677. intensity: Byte;
  678. begin
  679. if csoNoBuildShadeTexture in FCelShaderOptions then
  680. exit;
  681. with FShadeTexture do
  682. begin
  683. ImageClassName := 'TGLBlankImage';
  684. TGLBlankImage(Image).Width := 128;
  685. TGLBlankImage(Image).Height := 2;
  686. end;
  687. bmp32 := FShadeTexture.Image.GetBitmap32;
  688. bmp32.Blank := false;
  689. for i := 0 to bmp32.Width - 1 do
  690. begin
  691. intensity := i * (256 div bmp32.Width);
  692. if Assigned(FOnGetIntensity) then
  693. FOnGetIntensity(Self, intensity)
  694. else
  695. begin
  696. if intensity > 230 then
  697. intensity := 255
  698. else if intensity > 150 then
  699. intensity := 230
  700. else if intensity > 100 then
  701. intensity := intensity + 50
  702. else
  703. intensity := 150;
  704. end;
  705. bmp32.Data^[i].r := intensity;
  706. bmp32.Data^[i].g := intensity;
  707. bmp32.Data^[i].b := intensity;
  708. bmp32.Data^[i].a := 1;
  709. bmp32.Data^[i + bmp32.Width] := bmp32.Data^[i];
  710. end;
  711. end;
  712. function TGLCelShader.GenerateVertexProgram: string;
  713. var
  714. VP: TStringList;
  715. begin
  716. VP := TStringList.Create;
  717. VP.Add('!!ARBvp1.0');
  718. VP.Add('OPTION ARB_position_invariant;');
  719. VP.Add('PARAM mvinv[4] = { state.matrix.modelview.inverse };');
  720. VP.Add('PARAM lightPos = program.local[0];');
  721. VP.Add('TEMP temp, light, normal;');
  722. VP.Add(' DP4 light.x, mvinv[0], lightPos;');
  723. VP.Add(' DP4 light.y, mvinv[1], lightPos;');
  724. VP.Add(' DP4 light.z, mvinv[2], lightPos;');
  725. VP.Add(' ADD light, light, -vertex.position;');
  726. VP.Add(' DP3 temp.x, light, light;');
  727. VP.Add(' RSQ temp.x, temp.x;');
  728. VP.Add(' MUL light, temp.x, light;');
  729. VP.Add(' DP3 temp, vertex.normal, vertex.normal;');
  730. VP.Add(' RSQ temp.x, temp.x;');
  731. VP.Add(' MUL normal, temp.x, vertex.normal;');
  732. VP.Add(' MOV result.color, state.material.diffuse;');
  733. if csoTextured in FCelShaderOptions then
  734. begin
  735. VP.Add(' MOV result.texcoord[0], vertex.texcoord[0];');
  736. VP.Add(' DP3 result.texcoord[1].x, normal, light;');
  737. end
  738. else
  739. begin
  740. VP.Add(' DP3 result.texcoord[0].x, normal, light;');
  741. end;
  742. VP.Add('END');
  743. Result := VP.Text;
  744. VP.Free;
  745. end;
  746. procedure TGLCelShader.DoApply(var rci: TGLRenderContextInfo; Sender: TObject);
  747. var
  748. light: TGLVector;
  749. begin
  750. if (csDesigning in ComponentState) then
  751. exit;
  752. FVPHandle.AllocateHandle;
  753. if FVPHandle.IsDataNeedUpdate then
  754. begin
  755. FVPHandle.LoadARBProgram(GenerateVertexProgram);
  756. Enabled := FVPHandle.Ready;
  757. FVPHandle.NotifyDataUpdated;
  758. if not Enabled then
  759. Abort;
  760. end;
  761. rci.GLStates.Disable(stLighting);
  762. gl.GetLightfv(GL_LIGHT0, GL_POSITION, @light.X);
  763. FVPHandle.Enable;
  764. FVPHandle.Bind;
  765. gl.ProgramLocalParameter4fv(GL_VERTEX_PROGRAM_NV, 0, @light.X);
  766. if (csoTextured in FCelShaderOptions) then
  767. FShadeTexture.ApplyAsTexture2(rci, nil)
  768. else
  769. FShadeTexture.Apply(rci);
  770. FOutlinePass := csoOutlines in FCelShaderOptions;
  771. FUnApplyShadeTexture := True;
  772. end;
  773. function TGLCelShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  774. begin
  775. Result := False;
  776. if (csDesigning in ComponentState) then
  777. exit;
  778. FVPHandle.Disable;
  779. if FUnApplyShadeTexture then
  780. begin
  781. if (csoTextured in FCelShaderOptions) then
  782. FShadeTexture.UnApplyAsTexture2(rci, false)
  783. else
  784. FShadeTexture.UnApply(rci);
  785. FUnApplyShadeTexture := False;
  786. end;
  787. if FOutlinePass then
  788. with rci.GLStates do
  789. begin
  790. ActiveTexture := 0;
  791. ActiveTextureEnabled[ttTexture2D] := False;
  792. Enable(stBlend);
  793. Enable(stLineSmooth);
  794. Disable(stLineStipple);
  795. Enable(stCullFace);
  796. PolygonMode := pmLines;
  797. LineWidth := FOutlineWidth;
  798. CullFaceMode := cmFront;
  799. LineSmoothHint := hintNicest;
  800. SetBlendFunc(bfSrcAlpha, bfOneMinusSrcAlpha);
  801. DepthFunc := cfLEqual;
  802. gl.Color4fv(FOutlineColor.AsAddress);
  803. Result := True;
  804. FOutlinePass := False;
  805. Exit;
  806. end
  807. else
  808. with rci.GLStates do
  809. begin
  810. rci.GLStates.PolygonMode := pmFill;
  811. rci.GLStates.CullFaceMode := cmBack;
  812. rci.GLStates.DepthFunc := cfLEqual;
  813. end;
  814. end;
  815. procedure TGLCelShader.SetCelShaderOptions(const val: TGLCelShaderOptions);
  816. begin
  817. if val <> FCelShaderOptions then
  818. begin
  819. FCelShaderOptions := val;
  820. BuildShadeTexture;
  821. FVPHandle.NotifyChangesOfData;
  822. NotifyChange(Self);
  823. end;
  824. end;
  825. procedure TGLCelShader.SetOutlineWidth(const val: Single);
  826. begin
  827. if val <> FOutlineWidth then
  828. begin
  829. FOutlineWidth := val;
  830. NotifyChange(Self);
  831. end;
  832. end;
  833. procedure TGLCelShader.SetOutlineColor(const val: TGLColor);
  834. begin
  835. if val <> FOutlineColor then
  836. begin
  837. FOutlineColor.Assign(val);
  838. NotifyChange(Self);
  839. end;
  840. end;
  841. (****************************************
  842. TGLCustomGLSLSimpleErosionShader
  843. *****************************************)
  844. constructor TGLCustomGLSLSimpleErosionShader.Create(AOwner: TComponent);
  845. begin
  846. inherited;
  847. with VertexProgram.Code do
  848. begin
  849. Add('uniform float Scale; ');
  850. Add('varying vec3 normal; ');
  851. Add('varying vec2 vTexCoord; ');
  852. Add('varying vec3 lightVec; ');
  853. Add('varying vec3 viewVec; ');
  854. Add('varying vec3 Position; ');
  855. Add(' ');
  856. Add('void main(void) { ');
  857. // Add(' mat4 mWorld = gl_ModelViewMatrix; ');
  858. Add(' vec3 Normal = gl_Normal; ');
  859. Add(' vec4 lightPos = gl_LightSource[0].position;');
  860. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  861. Add(' normal = gl_NormalMatrix * gl_Normal; ');
  862. Add(' Position = vec3(gl_Vertex)*Scale; ');
  863. Add(' vTexCoord = gl_MultiTexCoord0; ');
  864. Add(' lightVec = vec3(lightPos - vert); ');
  865. Add(' viewVec = -vec3(vert); ');
  866. Add(' gl_Position = ftransform(); ');
  867. Add('} ');
  868. end;
  869. with FragmentProgram.Code do
  870. begin
  871. Add('uniform float ErosionFactor; ');
  872. Add('uniform float IntensityFactor1; ');
  873. Add('uniform float IntensityFactor2; ');
  874. Add('uniform sampler2D MainTexture; ');
  875. Add('uniform sampler2D Noise2d; ');
  876. Add('uniform sampler2D ErosionTexture; ');
  877. Add('uniform vec4 SpecularColor; ');
  878. Add('uniform vec4 AmbientColor; ');
  879. Add('uniform float DiffuseIntensity; ');
  880. Add('uniform float AmbientIntensity; ');
  881. Add('uniform float SpecularIntensity; ');
  882. Add('uniform float SpecularRoughness; ');
  883. Add('uniform float AnisoRoughness; ');
  884. Add('varying vec3 normal; ');
  885. Add('varying vec2 vTexCoord; ');
  886. Add('varying vec3 lightVec; ');
  887. Add('varying vec3 viewVec; ');
  888. Add('varying vec3 Position; ');
  889. Add(' ');
  890. Add('void main (void) ');
  891. Add('{ ');
  892. Add(' vec3 offset = vec3(- ErosionFactor, - ErosionFactor + 0.06, - ErosionFactor * 0.92); ');
  893. Add(' vec4 DiffuseColor; ');
  894. Add(' vec4 Color1 = texture2D(MainTexture,vTexCoord); ');
  895. Add(' vec4 Color2 = texture2D(ErosionTexture,vTexCoord); ');
  896. Add(' // Compute noise ');
  897. Add(' vec3 noiseCoord = Position.xyz + offset; ');
  898. Add(' vec4 noiseVec = texture2D(Noise2d, noiseCoord.xy); ');
  899. Add(' float intensity = (abs(noiseVec[0] - 0.25) + ');
  900. Add(' abs(noiseVec[1] - 0.125) + ');
  901. Add(' abs(noiseVec[2] - 0.0625) + ');
  902. Add(' abs(noiseVec[3] - 0.03125)); ');
  903. Add(' // continue noise evaluation');
  904. Add(' intensity = IntensityFactor1 * (noiseVec.x + noiseVec.y+ noiseVec.z + noiseVec.w); ');
  905. Add(' intensity = IntensityFactor2 * abs(2.0 * intensity -1.0); ');
  906. Add(' // discard pixels in a psuedo-random fashion (noise) ');
  907. Add(' if (intensity < fract(0.5 - offset.x - offset.y - offset.z)) discard; ');
  908. Add(' // color fragments different colors using noise ');
  909. Add(' clamp(intensity, 0.0, 1.0); ');
  910. Add(' Color2.a =1.0-intensity; ');
  911. Add(' Color1.a =1.0; ');
  912. Add(' DiffuseColor = mix(Color2, Color1, intensity); ');
  913. Add(' DiffuseColor.a = intensity; ');
  914. Add(' // Anisotropic Specular Lighting Reflection ');
  915. // Anisotropic Specular Reflection
  916. // This is useful for depicting surfaces
  917. // such as velvet or brushed metal,
  918. // it allows you to stretch the highlight along a
  919. // SpecDirection vector (in object space)
  920. // add new var and replace the follow line
  921. // vec3 T = cross(norm,V) by vec3 T = cross(norm,normalize(SpecDirection));
  922. Add(' vec3 norm = normalize(normal); ');
  923. Add(' vec3 L = normalize(lightVec); ');
  924. Add(' vec3 V = normalize(viewVec); ');
  925. Add(' vec3 halfAngle = normalize(L + V); ');
  926. Add(' vec3 T = cross(norm,V); ');
  927. Add(' float NdotL = dot(L, norm); ');
  928. Add(' float NdotH = clamp(dot(halfAngle, norm), 0.0, 1.0); ');
  929. Add(' // "Half-Lambert" technique for more pleasing diffuse term ');
  930. Add(' float diffuse = 0.5 * NdotL + 0.5; ');
  931. Add(' float specular = pow(NdotH,1.0/SpecularRoughness); '); //54
  932. Add(' // Heidrich-Seidel anisotropic distribution ');
  933. Add(' float ldott = dot(L,T); ');
  934. Add(' float vdott = dot(V,T); ');
  935. Add(' float aniso = pow(sin(ldott)*sin(vdott) + cos(ldott)*cos(vdott),1.0/AnisoRoughness); ');
  936. Add(' vec3 FinalColour = AmbientColor*AmbientIntensity + ');
  937. Add(' DiffuseColor*diffuse*DiffuseIntensity + ');
  938. Add(' SpecularColor*aniso*specular*SpecularIntensity; ');
  939. Add(' gl_FragColor = vec4(FinalColour,intensity); ');
  940. Add('} ');
  941. end;
  942. //setup initial parameters
  943. FAmbientColor := TGLColor.Create(self);
  944. FAmbientColor.SetColor(0.2,0.2,0.2,1.0);
  945. FSpecularColor := TGLColor.Create(self);
  946. FSpecularColor.SetColor(0.75,0.75,0.75,1.0);
  947. FAmbientFactor := 0.8;
  948. FDiffuseFactor :=0.9;
  949. FSpecularFactor :=0.8;
  950. FSpecularRoughness :=0.45;
  951. FAnisotropicRoughness :=0.35;
  952. FErosionScale := 0.03;
  953. FErosionFactor := 0.35;
  954. FIntensityFactor1 := 0.75;
  955. FIntensityFactor2 := 1.95;
  956. end;
  957. destructor TGLCustomGLSLSimpleErosionShader.Destroy;
  958. begin
  959. FAmbientColor.Free;
  960. FSpecularColor.Free;
  961. inherited;
  962. end;
  963. procedure TGLCustomGLSLSimpleErosionShader.DoApply(var rci : TGLRenderContextInfo; Sender : TObject);
  964. begin
  965. GetGLSLProg.UseProgramObject;
  966. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  967. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  968. param['AmbientIntensity'].AsVector1f := FAmbientFactor;
  969. param['DiffuseIntensity'].AsVector1f := FDiffuseFactor;
  970. param['SpecularIntensity'].AsVector1f := FSpecularFactor;
  971. param['SpecularRoughness'].AsVector1f := FSpecularRoughness;
  972. param['AnisoRoughness'].AsVector1f := FAnisotropicRoughness;
  973. param['ErosionFactor'].AsVector1f := FErosionFactor;
  974. param['IntensityFactor1'].AsVector1f := FIntensityFactor1;
  975. param['IntensityFactor2'].AsVector1f := FIntensityFactor2;
  976. param['Scale'].AsVector1f := FErosionScale;
  977. param['MainTexture'].AsTexture2D[0] := FMainTex;
  978. param['Noise2d'].AsTexture2D[1] := FNoiseTex;
  979. param['ErosionTexture'].AsTexture2D[2] := FErosionTex;
  980. // GetGLSLProg.UniformTextureHandle['Noise2d', 1, GL_TEXTURE_2D] := FNoiseTexture.Handle;
  981. end;
  982. function TGLCustomGLSLSimpleErosionShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  983. begin
  984. GetGLSLProg.EndUseProgramObject;
  985. //gl.PopAttrib;
  986. Result := False;
  987. end;
  988. function TGLCustomGLSLSimpleErosionShader.GetMaterialLibrary: TGLAbstractMaterialLibrary;
  989. begin
  990. Result := FMaterialLibrary;
  991. end;
  992. procedure TGLCustomGLSLSimpleErosionShader.SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary);
  993. begin
  994. if FMaterialLibrary <> nil then FMaterialLibrary.RemoveFreeNotification(Self);
  995. FMaterialLibrary := Value;
  996. if (FMaterialLibrary <> nil)
  997. and (FMaterialLibrary is TGLAbstractMaterialLibrary) then
  998. FMaterialLibrary.FreeNotification(Self);
  999. end;
  1000. procedure TGLCustomGLSLSimpleErosionShader.SetMainTexTexture(const Value: TGLTexture);
  1001. begin
  1002. if FMainTex = Value then Exit;
  1003. FMainTex := Value;
  1004. NotifyChange(Self)
  1005. end;
  1006. procedure TGLCustomGLSLSimpleErosionShader.SetNoiseTexTexture(const Value: TGLTexture);
  1007. begin
  1008. if FNoiseTex = Value then Exit;
  1009. FNoiseTex := Value;
  1010. NotifyChange(Self);
  1011. end;
  1012. procedure TGLCustomGLSLSimpleErosionShader.SetErosionTexTexture(const Value: TGLTexture);
  1013. begin
  1014. if FErosionTex = Value then Exit;
  1015. FErosionTex := Value;
  1016. NotifyChange(Self);
  1017. end;
  1018. function TGLCustomGLSLSimpleErosionShader.GetNoiseTexName: TGLLibMaterialName;
  1019. begin
  1020. Result := TGLMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FNoiseTex);
  1021. if Result = '' then Result := FNoiseTexName;
  1022. end;
  1023. procedure TGLCustomGLSLSimpleErosionShader.SetNoiseTexName(const Value: TGLLibMaterialName);
  1024. begin
  1025. //Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1026. if FNoiseTexName = Value then Exit;
  1027. FNoiseTexName := Value;
  1028. FNoiseTex := TGLMaterialLibrary(FMaterialLibrary).TextureByName(FNoiseTexName);
  1029. NotifyChange(Self);
  1030. end;
  1031. function TGLCustomGLSLSimpleErosionShader.GetMainTexName: TGLLibMaterialName;
  1032. begin
  1033. Result := TGLMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTex);
  1034. if Result = '' then Result := FMainTexName;
  1035. end;
  1036. procedure TGLCustomGLSLSimpleErosionShader.SetMainTexName(const Value: TGLLibMaterialName);
  1037. begin
  1038. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1039. if FMainTexName = Value then Exit;
  1040. FMainTexName := Value;
  1041. FMainTex := TGLMaterialLibrary(FMaterialLibrary).TextureByName(FMainTexName);
  1042. NotifyChange(Self);
  1043. end;
  1044. function TGLCustomGLSLSimpleErosionShader.GetErosionTexName: TGLLibMaterialName;
  1045. begin
  1046. Result := TGLMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FErosionTex);
  1047. if Result = '' then Result := FErosionTexName;
  1048. end;
  1049. procedure TGLCustomGLSLSimpleErosionShader.SetErosionTexName(const Value: TGLLibMaterialName);
  1050. begin
  1051. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1052. if FErosionTexName = Value then Exit;
  1053. FErosionTexName := Value;
  1054. FErosionTex := TGLMaterialLibrary(FMaterialLibrary).TextureByName(FErosionTexName);
  1055. NotifyChange(Self);
  1056. end;
  1057. procedure TGLCustomGLSLSimpleErosionShader.SetAmbientColor(AValue: TGLColor);
  1058. begin
  1059. FAmbientColor.DirectColor := AValue.Color;
  1060. end;
  1061. procedure TGLCustomGLSLSimpleErosionShader.SetSpecularColor(AValue: TGLColor);
  1062. begin
  1063. FSpecularColor.DirectColor := AValue.Color;
  1064. end;
  1065. procedure TGLCustomGLSLSimpleErosionShader.Notification(AComponent: TComponent; Operation: TOperation);
  1066. var
  1067. Index: Integer;
  1068. begin
  1069. inherited;
  1070. if Operation = opRemove then
  1071. if AComponent = FMaterialLibrary then
  1072. if FMaterialLibrary <> nil then
  1073. begin
  1074. // Need to nil the textures that were owned by it
  1075. if FNoiseTex <> nil then
  1076. begin
  1077. Index := TGLMaterialLibrary(FMaterialLibrary).Materials.GetTextureIndex(FNoiseTex);
  1078. if Index <> -1 then
  1079. SetNoiseTexTexture(nil);
  1080. end;
  1081. if FMainTex <> nil then
  1082. begin
  1083. Index := TGLMaterialLibrary(FMaterialLibrary).Materials.GetTextureIndex(FMainTex);
  1084. if Index <> -1 then
  1085. SetMainTexTexture(nil);
  1086. end;
  1087. if FErosionTex <> nil then
  1088. begin
  1089. Index := TGLMaterialLibrary(FMaterialLibrary).Materials.GetTextureIndex(FErosionTex);
  1090. if Index <> -1 then
  1091. SetErosionTexTexture(nil);
  1092. end;
  1093. FMaterialLibrary := nil;
  1094. end;
  1095. end;
  1096. (* ---------------------------------
  1097. TGLCustomGLSLGlassShader
  1098. ---------------------------------- *)
  1099. constructor TGLCustomGLSLGlassShader.Create(AOwner: TComponent);
  1100. begin
  1101. inherited;
  1102. with VertexProgram.Code do
  1103. begin
  1104. clear;
  1105. Add('varying vec3 Normal; ');
  1106. Add('varying vec3 EyeDir; ');
  1107. Add('varying vec4 EyePos; ');
  1108. Add('varying float LightIntensity; ');
  1109. Add('void main(void) ');
  1110. Add('{ ');
  1111. Add(' gl_Position = ftransform(); ');
  1112. Add(' vec3 LightPos = gl_LightSource[0].position.xyz;');
  1113. Add(' Normal = normalize(gl_NormalMatrix * gl_Normal); ');
  1114. Add(' vec4 pos = gl_ModelViewMatrix * gl_Vertex; ');
  1115. Add(' EyeDir = -pos.xyz; ');
  1116. Add(' EyePos = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  1117. Add(' LightIntensity = max(dot(normalize(LightPos - EyeDir), Normal), 0.0); ');
  1118. Add('} ');
  1119. end;
  1120. with FragmentProgram.Code do
  1121. begin
  1122. clear;
  1123. Add('const vec3 Xunitvec = vec3 (1.0, 0.0, 0.0); ');
  1124. Add('const vec3 Yunitvec = vec3 (0.0, 1.0, 0.0); ');
  1125. Add('uniform vec4 BaseColor; ');
  1126. Add('uniform float Depth; ');
  1127. Add('uniform float MixRatio; ');
  1128. Add('uniform float AlphaIntensity; ');
  1129. // need to scale our framebuffer - it has a fixed width/height of 2048
  1130. Add('uniform float FrameWidth; ');
  1131. Add('uniform float FrameHeight; ');
  1132. Add('uniform sampler2D EnvMap; ');
  1133. Add('uniform sampler2D RefractionMap; ');
  1134. Add('varying vec3 Normal; ');
  1135. Add('varying vec3 EyeDir; ');
  1136. Add('varying vec4 EyePos; ');
  1137. Add('varying float LightIntensity; ');
  1138. Add('void main (void) ');
  1139. Add('{ ');
  1140. // Compute reflection vector
  1141. Add(' vec3 reflectDir = reflect(EyeDir, Normal); ');
  1142. // Compute altitude and azimuth angles
  1143. Add(' vec2 index; ');
  1144. Add(' index.y = dot(normalize(reflectDir), Yunitvec); ');
  1145. Add(' reflectDir.y = 0.0; ');
  1146. Add(' index.x = dot(normalize(reflectDir), Xunitvec) * 0.5; ');
  1147. // Translate index values into proper range
  1148. Add(' if (reflectDir.z >= 0.0) ');
  1149. Add(' index = (index + 1.0) * 0.5; ');
  1150. Add(' else ');
  1151. Add(' { ');
  1152. Add(' index.t = (index.t + 1.0) * 0.5; ');
  1153. Add(' index.s = (-index.s) * 0.5 + 1.0; ');
  1154. Add(' } ');
  1155. // if reflectDir.z >= 0.0, s will go from 0.25 to 0.75
  1156. // if reflectDir.z < 0.0, s will go from 0.75 to 1.25, and
  1157. // that's OK, because we've set the texture to wrap.
  1158. // Do a lookup into the environment map.
  1159. Add(' vec4 envColor = texture2D(EnvMap, index); ');
  1160. // calc fresnels term. This allows a view dependant blend of reflection/refraction
  1161. Add(' float fresnel = abs(dot(normalize(EyeDir), Normal)); ');
  1162. Add(' fresnel *= MixRatio; ');
  1163. Add(' fresnel = clamp(fresnel, 0.1, 0.9); ');
  1164. // calc refraction
  1165. Add(' vec3 refractionDir = normalize(EyeDir) - normalize(Normal); ');
  1166. // Scale the refraction so the z element is equal to depth
  1167. Add(' float depthVal = Depth / -refractionDir.z; ');
  1168. // perform the div by w
  1169. Add(' float recipW = 1.0 / EyePos.w; ');
  1170. Add(' vec2 eye = EyePos.xy * vec2(recipW); ');
  1171. // calc the refraction lookup
  1172. Add(' index.s = (eye.x + refractionDir.x * depthVal); ');
  1173. Add(' index.t = (eye.y + refractionDir.y * depthVal); ');
  1174. // scale and shift so we're in the range 0-1
  1175. Add(' index.s = index.s / 2.0 + 0.5; ');
  1176. Add(' index.t = index.t / 2.0 + 0.5; ');
  1177. // as we're looking at the framebuffer, we want it clamping at the edge of the rendered scene, not the edge of the texture,
  1178. // so we clamp before scaling to fit
  1179. Add(' float recip1k = 1.0 / 2048.0; ');
  1180. Add(' index.s = clamp(index.s, 0.0, 1.0 - recip1k); ');
  1181. Add(' index.t = clamp(index.t, 0.0, 1.0 - recip1k); ');
  1182. // scale the texture so we just see the rendered framebuffer
  1183. Add(' index.s = index.s * FrameWidth * recip1k; ');
  1184. Add(' index.t = index.t * FrameHeight * recip1k; ');
  1185. Add(' vec4 RefractionColor = texture2D(RefractionMap, index.st); ');
  1186. // Add(' RefractionColor.a = 0.9; ');
  1187. // Add(' RefractionColor = RefractionColor+vec3(0.75,0.75,0.75); ');//
  1188. // Add lighting to base color and mix
  1189. // Add(' vec4 base = LightIntensity * BaseColor; ');
  1190. Add(' envColor = mix(envColor, BaseColor,LightIntensity); ');
  1191. Add(' envColor = mix(envColor, RefractionColor, fresnel); ');
  1192. Add(' envColor.a = AlphaIntensity; ');
  1193. Add(' gl_FragColor = envColor; //vec4 (envColor.rgb, 0.3); ');
  1194. Add('} ');
  1195. end;
  1196. // FMainTexture := TGLTexture.Create(nil);
  1197. // FMainTexture.Disabled := False;
  1198. // FMainTexture.Enabled := True;
  1199. // setup initial parameters
  1200. FDiffuseColor := TGLColor.Create(Self);
  1201. FDepth := 0.1;
  1202. FMix := 1.0;
  1203. FAlpha := 1.0;
  1204. FDiffuseColor.SetColor(0.15, 0.15, 0.15, 1.0);
  1205. FBlendSrc := bfSrcAlpha;
  1206. FBlendDst := bfDstAlpha;
  1207. end;
  1208. destructor TGLCustomGLSLGlassShader.Destroy;
  1209. begin
  1210. FDiffuseColor.Destroy;
  1211. inherited;
  1212. end;
  1213. procedure TGLCustomGLSLGlassShader.DoApply(var rci: TGLRenderContextInfo;
  1214. Sender: TObject);
  1215. begin
  1216. // Auto Render EnvMap
  1217. // capture and create material from framebuffer
  1218. // I don't say why but We need to reset and reaffect our texture otherwise one of the texture is broken
  1219. with FMainTexture do
  1220. begin
  1221. PrepareBuildList;
  1222. gl.ActiveTexture(GL_TEXTURE0_ARB);
  1223. gl.BindTexture(GL_TEXTURE_2D, Handle);
  1224. gl.ActiveTexture(GL_TEXTURE0_ARB);
  1225. end;
  1226. with FRefractionTexture do
  1227. begin
  1228. PrepareBuildList;
  1229. gl.ActiveTexture(GL_TEXTURE1_ARB);
  1230. gl.BindTexture(GL_TEXTURE_2D, Handle);
  1231. gl.ActiveTexture(GL_TEXTURE0_ARB);
  1232. end;
  1233. FOwnerObject.Visible := False;
  1234. TGLSceneBuffer(rci.buffer).CopyToTexture(FMainTexture);
  1235. FOwnerObject.Visible := True;
  1236. GetGLSLProg.UseProgramObject;
  1237. // GetGLSLProg.Uniform4f['BaseColor'] := FDiffuseColor.Color;
  1238. // GetGLSLProg.Uniform1f['Depth'] := FDepth;
  1239. // GetGLSLProg.Uniform1f['MixRatio'] := FMix; // 0 - 2
  1240. // GetGLSLProg.Uniform1f['FrameWidth'] := fBuffSize * 3.125;
  1241. // GetGLSLProg.Uniform1f['FrameHeight'] := fBuffSize * 3.125;
  1242. // SetTex('EnvMap',FMainTexture); --> BUG
  1243. // SetTex('RefractionMap',FRefractionTexture);
  1244. param['BaseColor'].AsVector4f := FDiffuseColor.Color;
  1245. param['Depth'].AsVector1f := FDepth; // 0 - 0.3
  1246. param['MixRatio'].AsVector1f := FMix; // 0 - 2
  1247. param['AlphaIntensity'].AsVector1f := FAlpha; // 0 - 2
  1248. param['FrameWidth'].AsVector1f := fBuffSize * 3.75;
  1249. param['FrameHeight'].AsVector1f := fBuffSize * 3.75;
  1250. param['EnvMap'].AsTexture2D[0] := FMainTexture;
  1251. param['RefractionMap'].AsTexture2D[1] := FRefractionTexture;
  1252. gl.Enable(GL_BLEND);
  1253. gl.BlendFunc(cGLBlendFunctionToGLEnum[FBlendSrc],
  1254. cGLBlendFunctionToGLEnum[FBlendDst]);
  1255. end;
  1256. function TGLCustomGLSLGlassShader.DoUnApply
  1257. (var rci: TGLRenderContextInfo): Boolean;
  1258. begin
  1259. gl.Disable(GL_BLEND);
  1260. GetGLSLProg.EndUseProgramObject;
  1261. Result := False;
  1262. end;
  1263. function TGLCustomGLSLGlassShader.GetMaterialLibrary
  1264. : TGLAbstractMaterialLibrary;
  1265. begin
  1266. Result := FMaterialLibrary;
  1267. end;
  1268. procedure TGLCustomGLSLGlassShader.SetMaterialLibrary
  1269. (const Value: TGLAbstractMaterialLibrary);
  1270. begin
  1271. if FMaterialLibrary <> nil then
  1272. FMaterialLibrary.RemoveFreeNotification(Self);
  1273. FMaterialLibrary := Value;
  1274. if (FMaterialLibrary <> nil) and
  1275. (FMaterialLibrary is TGLAbstractMaterialLibrary) then
  1276. FMaterialLibrary.FreeNotification(Self);
  1277. end;
  1278. procedure TGLCustomGLSLGlassShader.SetMainTexTexture(const Value: TGLTexture);
  1279. begin
  1280. if FMainTexture = Value then
  1281. Exit;
  1282. FMainTexture := Value;
  1283. NotifyChange(Self)
  1284. end;
  1285. function TGLCustomGLSLGlassShader.GetMainTexName: TGLLibMaterialName;
  1286. begin
  1287. Result := TGLMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTexture);
  1288. if Result = '' then
  1289. Result := FMainTexName;
  1290. end;
  1291. procedure TGLCustomGLSLGlassShader.SetMainTexName
  1292. (const Value: TGLLibMaterialName);
  1293. begin
  1294. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1295. if FMainTexName = Value then
  1296. Exit;
  1297. FMainTexName := Value;
  1298. FMainTexture := TGLMaterialLibrary(FMaterialLibrary)
  1299. .TextureByName(FMainTexName);
  1300. NotifyChange(Self);
  1301. end;
  1302. procedure TGLCustomGLSLGlassShader.SetRefractionTexTexture
  1303. (const Value: TGLTexture);
  1304. begin
  1305. if FRefractionTexture = Value then
  1306. Exit;
  1307. FRefractionTexture := Value;
  1308. NotifyChange(Self)
  1309. end;
  1310. function TGLCustomGLSLGlassShader.GetRefractionTexName: TGLLibMaterialName;
  1311. begin
  1312. Result := TGLMaterialLibrary(FMaterialLibrary)
  1313. .GetNameOfTexture(FRefractionTexture);
  1314. if Result = '' then
  1315. Result := FRefractionTexName;
  1316. end;
  1317. procedure TGLCustomGLSLGlassShader.SetRefractionTexName
  1318. (const Value: TGLLibMaterialName);
  1319. begin
  1320. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1321. if FRefractionTexName = Value then
  1322. Exit;
  1323. FRefractionTexName := Value;
  1324. FRefractionTexture := TGLMaterialLibrary(FMaterialLibrary)
  1325. .TextureByName(FRefractionTexName);
  1326. NotifyChange(Self);
  1327. end;
  1328. procedure TGLCustomGLSLGlassShader.SetDiffuseColor(AValue: TGLColor);
  1329. begin
  1330. FDiffuseColor.DirectColor := AValue.Color;
  1331. end;
  1332. procedure TGLCustomGLSLGlassShader.Notification(AComponent: TComponent;
  1333. Operation: TOperation);
  1334. var
  1335. Index: Integer;
  1336. begin
  1337. inherited;
  1338. if Operation = opRemove then
  1339. if AComponent = FMaterialLibrary then
  1340. if FMaterialLibrary <> nil then
  1341. begin
  1342. if FMainTexture <> nil then
  1343. begin
  1344. Index := TGLMaterialLibrary(FMaterialLibrary)
  1345. .Materials.GetTextureIndex(FMainTexture);
  1346. if Index <> -1 then
  1347. SetMainTexTexture(nil);
  1348. end;
  1349. if FRefractionTexture <> nil then
  1350. begin
  1351. Index := TGLMaterialLibrary(FMaterialLibrary)
  1352. .Materials.GetTextureIndex(FRefractionTexture);
  1353. if Index <> -1 then
  1354. SetRefractionTexTexture(nil);
  1355. end;
  1356. FMaterialLibrary := nil;
  1357. end;
  1358. end;
  1359. //-------------------------------------------------------------
  1360. // TGLCustomGLSLSimpleGoochShader
  1361. //-------------------------------------------------------------
  1362. constructor TGLCustomGLSLSimpleGoochShader.Create(AOwner: TComponent);
  1363. begin
  1364. inherited;
  1365. with VertexProgram.Code do
  1366. begin
  1367. Clear;
  1368. Add('varying vec3 vNormal; ');
  1369. Add('varying vec3 lightVec; ');
  1370. Add('varying vec3 viewVec; ');
  1371. Add('varying vec3 ReflectVec; ');
  1372. Add(' ');
  1373. Add('void main() ');
  1374. Add('{ ');
  1375. Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  1376. Add(' vec4 lightPos = gl_LightSource[0].position;');
  1377. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  1378. Add(' vec3 normal = gl_NormalMatrix * gl_Normal; ');
  1379. Add(' vNormal = normalize(normal); ');
  1380. Add(' lightVec = vec3(lightPos - vert); ');
  1381. Add(' ReflectVec = normalize(reflect(-lightVec, vNormal)); ');
  1382. Add(' viewVec = -vec3(vert); ');
  1383. Add('} ');
  1384. end;
  1385. with FragmentProgram.Code do
  1386. begin
  1387. Clear;
  1388. Add('uniform vec4 SurfaceColor; ');
  1389. Add('uniform vec4 WarmColor; ');
  1390. Add('uniform vec4 CoolColor; ');
  1391. Add('uniform vec4 SpecularColor; ');
  1392. Add('uniform vec4 AmbientColor; ');
  1393. Add('uniform float DiffuseWarm; ');
  1394. Add('uniform float DiffuseCool; ');
  1395. Add('uniform float AmbientFactor; ');
  1396. Add('uniform float DiffuseFactor; ');
  1397. Add('uniform float SpecularFactor; ');
  1398. Add('varying vec3 vNormal; ');
  1399. Add('varying vec3 lightVec; ');
  1400. Add('varying vec3 viewVec; ');
  1401. Add('varying vec3 ReflectVec; ');
  1402. Add(' ');
  1403. Add('void main() ');
  1404. Add('{ ');
  1405. Add('vec3 L = normalize(lightVec); ');
  1406. Add('vec3 V = normalize(viewVec); ');
  1407. Add('vec3 halfAngle = normalize(L + V); ');
  1408. Add('float NdotL = (dot(L, vNormal) + 1.0) * 0.5; ');
  1409. Add('float NdotH = clamp(dot(halfAngle, vNormal), 0.0, 1.0); ');
  1410. Add('// "Half-Lambert" technique for more pleasing diffuse term ');
  1411. Add('float diffuse = 0.5 * NdotL + 0.5; ');
  1412. Add('vec3 nreflect = normalize(ReflectVec); ');
  1413. Add('float specular = max(dot(nreflect, V), 0.0); ');
  1414. Add('specular = pow(specular, 64.0); ');
  1415. Add('vec4 kCool = min(CoolColor + DiffuseCool * SurfaceColor, 1.0); ');
  1416. Add('vec4 kWarm = min(WarmColor + DiffuseWarm * SurfaceColor, 1.0); ');
  1417. Add('vec4 Cgooch = mix(kWarm, kCool, diffuse); ');
  1418. Add('vec3 result = AmbientFactor * AmbientColor.rgb + DiffuseFactor * Cgooch.rgb + SpecularColor.rgb * SpecularFactor *specular; ');
  1419. Add('gl_FragColor = vec4(result,SurfaceColor.a); ');
  1420. Add('} ');
  1421. end;
  1422. // Initial stuff.
  1423. FDiffuseColor := TGLColor.Create(self);
  1424. FDiffuseColor.SetColor(0.75,0.75,0.75,1.0);
  1425. FWarmColor := TGLColor.Create(self);
  1426. FWarmColor.SetColor(0.88,0.81,0.49,1.0);
  1427. FCoolColor := TGLColor.Create(self);
  1428. FCoolColor.SetColor(0.58,0.10,0.76,1.0);
  1429. FAmbientColor := TGLColor.Create(self);
  1430. FAmbientColor.SetColor(0.3,0.3,0.3,1.0);
  1431. FSpecularColor := TGLColor.Create(self);
  1432. FSpecularColor.SetColor(1.0,1.0,1.0,1.0);
  1433. FDiffuseWarm := 0.55;
  1434. FDiffuseCool := 0.30;
  1435. FAmbientFactor := 1.0;
  1436. FDiffuseFactor :=0.8;
  1437. FSpecularFactor :=0.9;
  1438. FBlendingMode:=bmxOpaque;
  1439. end;
  1440. destructor TGLCustomGLSLSimpleGoochShader.Destroy;
  1441. begin
  1442. FDiffuseColor.Free;
  1443. FWarmColor.Free;
  1444. FCoolColor.Free;
  1445. FSpecularColor.Free;
  1446. FAmbientColor.Free;
  1447. inherited;
  1448. end;
  1449. procedure TGLCustomGLSLSimpleGoochShader.DoApply(var rci: TGLRenderContextInfo;
  1450. Sender: TObject);
  1451. begin
  1452. GetGLSLProg.UseProgramObject;
  1453. param['SurfaceColor'].AsVector4f := FDiffuseColor.Color;
  1454. param['WarmColor'].AsVector4f := FWarmColor.Color;
  1455. param['CoolColor'].AsVector4f := FCoolColor.Color;
  1456. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  1457. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  1458. param['DiffuseWarm'].AsVector1f := FDiffuseWarm;
  1459. param['DiffuseCool'].AsVector1f := FDiffuseCool;
  1460. param['AmbientFactor'].AsVector1f := FAmbientFactor;
  1461. param['DiffuseFactor'].AsVector1f := FDiffuseFactor;
  1462. param['SpecularFactor'].AsVector1f := FSpecularFactor;
  1463. // gl.PushAttrib(GL_COLOR_BUFFER_BIT);
  1464. ApplyBlendingModeEx(FBlendingMode);
  1465. // gl.Enable(GL_BLEND);
  1466. // gl.BlendFunc(cGLBlendFunctionToGLEnum[FBlendSrc],cGLBlendFunctionToGLEnum[FBlendDst]);
  1467. end;
  1468. function TGLCustomGLSLSimpleGoochShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  1469. begin
  1470. gl.ActiveTexture(GL_TEXTURE0_ARB);
  1471. GetGLSLProg.EndUseProgramObject;
  1472. UnApplyBlendingModeEx;
  1473. // gl.PopAttrib;
  1474. Result := False;
  1475. end;
  1476. procedure TGLCustomGLSLSimpleGoochShader.SetDiffuseColor(AValue: TGLColor);
  1477. begin
  1478. FDiffuseColor.DirectColor := AValue.Color;
  1479. end;
  1480. procedure TGLCustomGLSLSimpleGoochShader.SetAmbientColor(AValue: TGLColor);
  1481. begin
  1482. FAmbientColor.DirectColor := AValue.Color;
  1483. end;
  1484. procedure TGLCustomGLSLSimpleGoochShader.SetSpecularColor(AValue: TGLColor);
  1485. begin
  1486. FSpecularColor.DirectColor := AValue.Color;
  1487. end;
  1488. procedure TGLCustomGLSLSimpleGoochShader.SetWarmColor(AValue: TGLColor);
  1489. begin
  1490. FWarmColor.DirectColor := AValue.Color;
  1491. end;
  1492. procedure TGLCustomGLSLSimpleGoochShader.SetCoolColor(AValue: TGLColor);
  1493. begin
  1494. FCoolColor.DirectColor := AValue.Color;
  1495. end;
  1496. (*------------------------------------------
  1497. // TGLCustomGLSLFurShader
  1498. ------------------------------------------*)
  1499. constructor TGLCustomGLSLFurShader.Create(AOwner: TComponent);
  1500. begin
  1501. inherited;
  1502. with VertexProgram.Code do
  1503. begin
  1504. clear;
  1505. Add('uniform float fFurLength; ');
  1506. Add('uniform float fFurMaxLength; ');
  1507. Add('uniform float pass_index; ');
  1508. Add('uniform int UseRandomLength; ');
  1509. Add('uniform float fLayer; // 0 to 1 for the level ');
  1510. Add('uniform vec3 vGravity; ');
  1511. Add('varying vec3 normal; ');
  1512. Add('varying vec2 vTexCoord; ');
  1513. Add('varying vec3 lightVec; ');
  1514. // Add('varying vec3 viewVec; ');
  1515. Add('float rand(vec2 co){ ');
  1516. Add(' return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); ');
  1517. Add('} ');
  1518. Add('void main() ');
  1519. Add('{ ');
  1520. Add(' mat4 mWorld = gl_ModelViewMatrix; ');
  1521. Add(' vec3 Normal = gl_Normal; ');
  1522. Add(' vec4 Position = gl_Vertex; ');
  1523. Add(' vec4 lightPos = gl_LightSource[0].position;');
  1524. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  1525. Add(' normal = gl_NormalMatrix * gl_Normal; ');
  1526. // Additional Gravit/Force Code
  1527. Add(' vec3 vGravity2 = vGravity *mat3(mWorld ); ');
  1528. // We use the pow function, so that only the tips of the hairs bend
  1529. Add(' float k = pow(fLayer, 3.0); ');
  1530. // Random the Hair length perhaps will can use a texture map for controling.
  1531. Add(' vec3 vNormal = normalize( Normal * mat3(mWorld )); ');
  1532. Add(' float RandomFurLength; ');
  1533. Add(' if (UseRandomLength == 1) { RandomFurLength = fFurLength+fFurLength*rand(vNormal.xy); } ');
  1534. Add(' else { RandomFurLength = fFurLength ; } ');
  1535. Add(' RandomFurLength = pass_index*(RandomFurLength * vNormal); ');
  1536. Add(' if (RandomFurLength > fFurMaxLength ) { RandomFurLength = fFurMaxLength; } ');
  1537. Add(' Position.xyz += RandomFurLength +(vGravity2 * k); ');
  1538. Add(' Position.xyz += pass_index*(fFurLength * Normal)+(vGravity2 * k); ');
  1539. Add(' vTexCoord = gl_MultiTexCoord0; ');
  1540. Add(' ');
  1541. Add(' gl_Position = gl_ModelViewProjectionMatrix * Position; ');
  1542. Add(' lightVec = vec3(lightPos - vert); ');
  1543. // Add(' viewVec = -vec3(vert); ');
  1544. Add('normal = vNormal; ');
  1545. Add('} ');
  1546. end;
  1547. with FragmentProgram.Code do
  1548. begin
  1549. clear;
  1550. Add('uniform vec4 fcolorScale; ');
  1551. Add('uniform float pass_index; ');
  1552. Add('uniform float fFurScale; ');
  1553. Add('uniform vec4 vAmbient; ');
  1554. Add('uniform float fLayer; // 0 to 1 for the level ');
  1555. Add('uniform float vLightIntensity; ');
  1556. Add('uniform sampler2D FurTexture; ');
  1557. Add('uniform sampler2D ColourTexture; ');
  1558. //textures
  1559. Add('varying vec2 vTexCoord; ');
  1560. Add('varying vec3 normal; ');
  1561. Add('varying vec3 lightVec; ');
  1562. // Add('varying vec3 viewVec; ');
  1563. Add('void main() ');
  1564. Add('{ ');
  1565. // A Faking shadow
  1566. Add(' vec4 fAlpha = texture2D( FurTexture, vTexCoord*fFurScale ); ');
  1567. Add(' float fakeShadow = mix(0.3, 1.0, fAlpha.a-fLayer); ');
  1568. Add(' ');
  1569. Add(' vec4 FinalColour = vec4(0.0,0.0,0.0,1.0); ');
  1570. Add('FinalColour = (fcolorScale*texture2D( ColourTexture, vTexCoord))*fakeShadow; ');
  1571. // This comment part it's for controling if we must draw the hair according the red channel and the alpha in NoiseMap
  1572. // Don' t work well a this time the NoiseMap must be perfect
  1573. // Add('float visibility = 0.0; ');
  1574. // Add('if (pass_index == 1.0) ');
  1575. // Add('{ ');
  1576. // Add(' visibility = 1.0; ');
  1577. // Add('} ');
  1578. // Add('else ');
  1579. // Add('{ ');
  1580. // Add(' if (fAlpha.a<fAlpha.r) { visibility = 0.0; } ');
  1581. // Add(' else { visibility =mix(0.1,1.0,(1.02-fLayer)); } //-1.0; ');
  1582. // Add('} ');
  1583. Add('float visibility =mix(0.1,1.0,(1.02-fLayer)); '); // The Last past must be transparent
  1584. // Simply Lighting - For this time only ONE light source is supported
  1585. Add('vec4 ambient = vAmbient*FinalColour; ');
  1586. Add('vec4 diffuse = FinalColour; ');
  1587. Add('vec3 L = normalize(lightVec); ');
  1588. Add('float NdotL = dot(L, normal); ');
  1589. Add('// "Half-Lambert" technique for more pleasing diffuse term ');
  1590. Add('diffuse = diffuse*(0.5*NdotL+0.5); ');
  1591. Add('FinalColour = vLightIntensity*(ambient+ diffuse); // + no specular; ');
  1592. Add('FinalColour.a = visibility ; ');
  1593. Add(' // Return the calculated color ');
  1594. Add(' gl_FragColor= FinalColour; ');
  1595. Add('} ');
  1596. end;
  1597. //Fur stuff
  1598. FPassCount := 16; // More is greater more the fur is dense
  1599. FFurLength := 0.3000; // The minimal Hair length
  1600. FMaxFurLength := 3.0;
  1601. FRandomFurLength := false;
  1602. FFurScale:=1.0;
  1603. FColorScale := TGLColor.Create(Self);
  1604. FColorScale.SetColor(0.2196,0.2201,0.2201,1.0);
  1605. FAmbient := TGLColor.Create(Self);
  1606. FAmbient.SetColor(1.0,1.0,1.0,1.0);
  1607. // The Blend Funcs are very important for realistic fur rendering it can vary follow your textures
  1608. FBlendSrc := bfOneMinusSrcColor;
  1609. FBlendDst := bfOneMinusSrcAlpha;
  1610. FGravity := TGLCoordinates.Create(self);
  1611. FGravity.AsAffineVector := AffinevectorMake(0.0,0.0,0.0);
  1612. FLightIntensity := 2.5;
  1613. end;
  1614. destructor TGLCustomGLSLFurShader.Destroy;
  1615. begin
  1616. Enabled:=false;
  1617. FGravity.Free;
  1618. FColorScale.Destroy;
  1619. FAmbient.Destroy;
  1620. inherited;
  1621. end;
  1622. procedure TGLCustomGLSLFurShader.DoApply(var rci: TGLRenderContextInfo;Sender: TObject);
  1623. begin
  1624. GetGLSLProg.UseProgramObject;
  1625. //Fur stuff
  1626. FCurrentPass := 1;
  1627. param['pass_index'].AsVector1f := 1.0;
  1628. param['fFurLength'].AsVector1f := FFurLength;
  1629. param['fFurMaxLength'].AsVector1f := FMaxFurLength;
  1630. param['fFurScale'].AsVector1f := FFurScale;
  1631. if FRandomFurLength then param['UseRandomLength'].AsVector1i := 1
  1632. else param['UseRandomLength'].AsVector1i := 0;
  1633. param['fcolorScale'].AsVector4f := FColorScale.Color;
  1634. param['FurTexture'].AsTexture2D[0] := FNoiseTex;
  1635. param['ColourTexture'].AsTexture2D[1] := FMainTex;
  1636. param['vGravity'].AsVector3f := FGravity.AsAffineVector;
  1637. param['vAmbient'].AsVector4f := FAmbient.Color; //vectorMake(0.5,0.5,0.5,1.0);
  1638. param['fLayer'].AsVector1f := 1/PassCount;
  1639. param['vLightIntensity'].AsVector1f := FLightIntensity;
  1640. gl.PushAttrib(GL_COLOR_BUFFER_BIT);
  1641. gl.Enable(GL_BLEND);
  1642. gl.BlendFunc(cGLBlendFunctionToGLEnum[FBlendSrc],cGLBlendFunctionToGLEnum[FBlendDst]);
  1643. // gl.BlendFunc(GL_SRC_ALPHA, cGLBlendFunctionToGLEnum[FBlendSrc]);
  1644. // gl.BlendFunc(GL_DST_ALPHA,cGLBlendFunctionToGLEnum[FBlendDst]);
  1645. // gl.BlendEquation(cGLBlendEquationToGLEnum[BlendEquation]);
  1646. end;
  1647. function TGLCustomGLSLFurShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  1648. begin
  1649. if FCurrentPass < PassCount then
  1650. begin
  1651. Inc(FCurrentPass);
  1652. //GetGLSLProg.Uniform1f['pass_index'] := FCurrentPass;
  1653. param['pass_index'].AsVector1f := FCurrentPass;
  1654. param['fLayer'].AsVector1f := FCurrentPass/PassCount;
  1655. Result := True;
  1656. end
  1657. else
  1658. begin
  1659. // glActiveTextureARB(GL_TEXTURE0_ARB);
  1660. gl.ActiveTexture(GL_TEXTURE0_ARB);
  1661. GetGLSLProg.EndUseProgramObject;
  1662. gl.PopAttrib;
  1663. Result := False;
  1664. end;
  1665. end;
  1666. function TGLCustomGLSLFurShader.GetMaterialLibrary: TGLAbstractMaterialLibrary;
  1667. begin
  1668. Result := FMaterialLibrary;
  1669. end;
  1670. procedure TGLCustomGLSLFurShader.SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary);
  1671. begin
  1672. if FMaterialLibrary <> nil then FMaterialLibrary.RemoveFreeNotification(Self);
  1673. FMaterialLibrary := Value;
  1674. if (FMaterialLibrary <> nil)
  1675. and (FMaterialLibrary is TGLAbstractMaterialLibrary) then
  1676. FMaterialLibrary.FreeNotification(Self);
  1677. end;
  1678. procedure TGLCustomGLSLFurShader.SetMainTexTexture(const Value: TGLTexture);
  1679. begin
  1680. if FMainTex = Value then
  1681. Exit;
  1682. FMainTex := Value;
  1683. NotifyChange(self)
  1684. end;
  1685. procedure TGLCustomGLSLFurShader.SetNoiseTexTexture(const Value: TGLTexture);
  1686. begin
  1687. if FNoiseTex = Value then
  1688. Exit;
  1689. FNoiseTex := Value;
  1690. NotifyChange(self);
  1691. end;
  1692. function TGLCustomGLSLFurShader.GetNoiseTexName: TGLLibMaterialName;
  1693. begin
  1694. Result := TGLMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FNoiseTex);
  1695. if Result = '' then
  1696. Result := FNoiseTexName;
  1697. end;
  1698. procedure TGLCustomGLSLFurShader.SetNoiseTexName
  1699. (const Value: TGLLibMaterialName);
  1700. begin
  1701. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1702. if FNoiseTexName = Value then
  1703. Exit;
  1704. FNoiseTexName := Value;
  1705. FNoiseTex := TGLMaterialLibrary(FMaterialLibrary)
  1706. .TextureByName(FNoiseTexName);
  1707. NotifyChange(self);
  1708. end;
  1709. function TGLCustomGLSLFurShader.GetMainTexName: TGLLibMaterialName;
  1710. begin
  1711. Result := TGLMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTex);
  1712. if Result = '' then
  1713. Result := FMainTexName;
  1714. end;
  1715. procedure TGLCustomGLSLFurShader.SetMainTexName
  1716. (const Value: TGLLibMaterialName);
  1717. begin
  1718. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  1719. if FMainTexName = Value then
  1720. Exit;
  1721. FMainTexName := Value;
  1722. FMainTex := TGLMaterialLibrary(FMaterialLibrary)
  1723. .TextureByName(FMainTexName);
  1724. NotifyChange(self);
  1725. end;
  1726. procedure TGLCustomGLSLFurShader.Notification(AComponent: TComponent;
  1727. Operation: TOperation);
  1728. var
  1729. Index: Integer;
  1730. begin
  1731. inherited;
  1732. if Operation = opRemove then
  1733. if AComponent = FMaterialLibrary then
  1734. if FMaterialLibrary <> nil then
  1735. begin
  1736. // Need to nil the textures that were owned by it
  1737. if FNoiseTex <> nil then
  1738. begin
  1739. Index := TGLMaterialLibrary(FMaterialLibrary)
  1740. .Materials.GetTextureIndex(FNoiseTex);
  1741. if Index <> -1 then
  1742. SetNoiseTexTexture(nil);
  1743. end;
  1744. if FMainTex <> nil then
  1745. begin
  1746. Index := TGLMaterialLibrary(FMaterialLibrary)
  1747. .Materials.GetTextureIndex(FMainTex);
  1748. if Index <> -1 then
  1749. SetMainTexTexture(nil);
  1750. end;
  1751. FMaterialLibrary := nil;
  1752. end;
  1753. end;
  1754. procedure TGLCustomGLSLFurShader.SetGravity(APosition: TGLCoordinates);
  1755. begin
  1756. FGravity.SetPoint(APosition.DirectX, APosition.DirectY, APosition.DirectZ);
  1757. end;
  1758. procedure TGLCustomGLSLFurShader.SetAmbient(AValue: TGLColor);
  1759. begin
  1760. FAmbient.DirectColor := AValue.Color;
  1761. end;
  1762. procedure TGLCustomGLSLFurShader.SetColorScale(AValue: TGLColor);
  1763. begin
  1764. FColorScale.DirectColor := AValue.Color;
  1765. end;
  1766. (*****************************************************
  1767. TGLCustomGLSLIvoryShader
  1768. ****************************************************)
  1769. constructor TGLCustomGLSLIvoryShader.Create(AOwner: TComponent);
  1770. begin
  1771. inherited;
  1772. with VertexProgram.Code do
  1773. begin
  1774. clear;
  1775. Add('varying vec3 normal; ');
  1776. Add('varying vec3 lightVec; ');
  1777. Add('varying vec3 viewVec; ');
  1778. Add(' ');
  1779. Add('void main() ');
  1780. Add('{ ');
  1781. Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  1782. Add(' vec4 lightPos = gl_LightSource[0].position;');
  1783. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  1784. Add(' normal = gl_NormalMatrix * gl_Normal; ');
  1785. Add(' lightVec = vec3(lightPos - vert); ');
  1786. Add(' viewVec = -vec3(vert); ');
  1787. Add('} ');
  1788. end;
  1789. with FragmentProgram.Code do
  1790. begin
  1791. clear;
  1792. Add('varying vec3 normal; ');
  1793. Add('varying vec3 lightVec; ');
  1794. Add('varying vec3 viewVec; ');
  1795. Add(' ');
  1796. Add('void main() ');
  1797. Add('{ ');
  1798. Add('vec3 norm = normalize(normal); ');
  1799. Add('vec3 L = normalize(lightVec); ');
  1800. Add('vec3 V = normalize(viewVec); ');
  1801. Add('vec3 halfAngle = normalize(L + V); ');
  1802. Add('float NdotL = dot(L, norm); ');
  1803. Add('float NdotH = clamp(dot(halfAngle, norm), 0.0, 1.0); ');
  1804. Add('// "Half-Lambert" technique for more pleasing diffuse term ');
  1805. Add('float diffuse = 0.5 * NdotL + 0.5; ');
  1806. Add('float specular = pow(NdotH, 64.0); ');
  1807. Add('float result = diffuse + specular; ');
  1808. Add('gl_FragColor = vec4(result); ');
  1809. Add('} ');
  1810. end;
  1811. // Initial stuff.
  1812. end;
  1813. destructor TGLCustomGLSLIvoryShader.Destroy;
  1814. begin
  1815. inherited;
  1816. end;
  1817. procedure TGLCustomGLSLIvoryShader.DoApply(var rci: TGLRenderContextInfo;
  1818. Sender: TObject);
  1819. begin
  1820. GetGLSLProg.UseProgramObject;
  1821. end;
  1822. function TGLCustomGLSLIvoryShader.DoUnApply
  1823. (var rci: TGLRenderContextInfo): Boolean;
  1824. begin
  1825. Result := False;
  1826. GetGLSLProg.EndUseProgramObject;
  1827. end;
  1828. (*****************************************************
  1829. TGLCustomGLSLSimpleLatticeShader
  1830. *****************************************************)
  1831. constructor TGLCustomGLSLSimpleLatticeShader.Create(AOwner: TComponent);
  1832. begin
  1833. inherited;
  1834. with FragmentProgram.Code do
  1835. begin
  1836. clear;
  1837. Add(' uniform vec2 Scale; ');
  1838. Add(' uniform vec2 Threshold; ');
  1839. Add(' ');
  1840. Add(' void main (void) ');
  1841. Add('{ ');
  1842. Add(' float ss = fract(gl_TexCoord[0].s * Scale.s); ');
  1843. Add(' float tt = fract(gl_TexCoord[0].t * Scale.t); ');
  1844. Add(' ');
  1845. Add(' if ((ss > Threshold.s) && (tt > Threshold.t)) discard; ');
  1846. Add(' gl_FragColor = gl_Color;');
  1847. Add('} ');
  1848. end;
  1849. // Initial stuff.
  1850. FLatticeScale := TGLCoordinates2.Create(self);
  1851. FLatticeThreshold := TGLCoordinates2.Create(self);
  1852. FLatticeScale.SetPoint2D(10, 40);
  1853. FLatticeThreshold.SetPoint2D(0.15, 0.3);
  1854. end;
  1855. destructor TGLCustomGLSLSimpleLatticeShader.Destroy;
  1856. begin
  1857. FLatticeScale.Destroy;
  1858. FLatticeThreshold.Destroy;
  1859. inherited;
  1860. end;
  1861. procedure TGLCustomGLSLSimpleLatticeShader.DoApply
  1862. (var rci: TGLRenderContextInfo; Sender: TObject);
  1863. begin
  1864. GetGLSLProg.UseProgramObject;
  1865. param['Scale'].AsVector2f := FLatticeScale.AsPoint2D;
  1866. param['Threshold'].AsVector2f := FLatticeThreshold.AsPoint2D;
  1867. end;
  1868. function TGLCustomGLSLSimpleLatticeShader.DoUnApply
  1869. (var rci: TGLRenderContextInfo): Boolean;
  1870. begin
  1871. Result := False;
  1872. // gl.ActiveTexture(GL_TEXTURE0_ARB);
  1873. GetGLSLProg.EndUseProgramObject;
  1874. end;
  1875. procedure TGLCustomGLSLSimpleLatticeShader.SetLatticeScale
  1876. (const Value: TGLCoordinates2);
  1877. begin
  1878. FLatticeScale.Assign(Value);
  1879. end;
  1880. procedure TGLCustomGLSLSimpleLatticeShader.SetLatticeThreshold
  1881. (const Value: TGLCoordinates2);
  1882. begin
  1883. FLatticeThreshold.Assign(Value);
  1884. end;
  1885. // TGLCustomGLSLLatticeShader
  1886. constructor TGLCustomGLSLLatticeShader.Create(AOwner: TComponent);
  1887. begin
  1888. inherited;
  1889. FAmbientColor := TGLColor.Create(self);
  1890. FDiffuseColor := TGLColor.Create(self);
  1891. FSpecularColor := TGLColor.Create(self);
  1892. // setup initial parameters
  1893. FAmbientColor.SetColor(0.15, 0.15, 0.15, 1);
  1894. FDiffuseColor.SetColor(1, 1, 1, 1);
  1895. FSpecularColor.SetColor(1, 1, 1, 1);
  1896. FSpecularPower := 8; // 6
  1897. FLightPower := 1;
  1898. end;
  1899. destructor TGLCustomGLSLLatticeShader.Destroy;
  1900. begin
  1901. FAmbientColor.Destroy;
  1902. FDiffuseColor.Destroy;
  1903. FSpecularColor.Destroy;
  1904. inherited;
  1905. end;
  1906. procedure TGLCustomGLSLLatticeShader.DoApply(var rci: TGLRenderContextInfo;
  1907. Sender: TObject);
  1908. begin
  1909. inherited;
  1910. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  1911. param['DiffuseColor'].AsVector4f := FDiffuseColor.Color;
  1912. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  1913. param['SpecPower'].AsVector1f := FSpecularPower;
  1914. param['LightIntensity'].AsVector1f := FLightPower;
  1915. param['MainTexture'].AsTexture2D[0] := FMainTexture;
  1916. end;
  1917. procedure TGLCustomGLSLLatticeShader.DoInitialize
  1918. (var rci: TGLRenderContextInfo; Sender: TObject);
  1919. begin
  1920. with VertexProgram.Code do
  1921. begin
  1922. clear;
  1923. Add('varying vec3 Normal; ');
  1924. Add('varying vec3 LightVector; ');
  1925. Add('varying vec3 CameraVector; ');
  1926. Add('varying vec2 Texcoord; ');
  1927. Add(' ');
  1928. Add(' ');
  1929. Add('void main(void) ');
  1930. Add('{ ');
  1931. Add(' gl_Position = ftransform(); ');
  1932. Add(' Texcoord = gl_MultiTexCoord0.xy; ');
  1933. Add(' Normal = normalize(gl_NormalMatrix * gl_Normal); ');
  1934. Add(' vec3 p = (gl_ModelViewMatrix * gl_Vertex).xyz; ');
  1935. Add(' LightVector = normalize(gl_LightSource[0].position.xyz - p); ');
  1936. Add(' CameraVector = normalize(p); ');
  1937. Add('} ');
  1938. end;
  1939. with FragmentProgram.Code do
  1940. begin
  1941. clear;
  1942. Add(' uniform vec2 Scale; ');
  1943. Add(' uniform vec2 Threshold; ');
  1944. Add(' ');
  1945. Add('uniform vec4 AmbientColor; ');
  1946. Add('uniform vec4 DiffuseColor; ');
  1947. Add('uniform vec4 SpecularColor; ');
  1948. Add(' ');
  1949. Add('uniform float LightIntensity; ');
  1950. Add('uniform float SpecPower; ');
  1951. Add('uniform sampler2D MainTexture; ');
  1952. Add(' ');
  1953. Add('varying vec3 Normal; ');
  1954. Add('varying vec3 LightVector; ');
  1955. Add('varying vec3 CameraVector; ');
  1956. Add('varying vec2 Texcoord; ');
  1957. Add(' ');
  1958. Add('void main(void) ');
  1959. Add('{ ');
  1960. Add(' float ss = fract(Texcoord[0] * Scale.s); ');
  1961. Add(' float tt = fract(Texcoord[1] * Scale.t); ');
  1962. Add(' ');
  1963. Add(' if ((ss > Threshold.s) && (tt > Threshold.t)) discard; ');
  1964. Add(' ');
  1965. Add(' vec4 TextureContrib = texture2D(MainTexture, Texcoord); ');
  1966. Add(' vec4 DiffuseContrib = clamp(DiffuseColor * dot(LightVector, Normal), 0.0, 1.0); ');
  1967. Add(' ');
  1968. Add(' vec3 reflect_vec = reflect(CameraVector, -Normal); ');
  1969. Add(' float Temp = dot(reflect_vec, LightVector); ');
  1970. Add(' vec4 SpecContrib = SpecularColor * clamp(pow(Temp, SpecPower), 0.0, 0.95); ');
  1971. Add(' ');
  1972. Add(' gl_FragColor = TextureContrib * LightIntensity * (AmbientColor + DiffuseContrib) + LightIntensity * SpecContrib; ');
  1973. Add('} ');
  1974. end;
  1975. inherited;
  1976. end;
  1977. function TGLCustomGLSLLatticeShader.GetMaterialLibrary
  1978. : TGLAbstractMaterialLibrary;
  1979. begin
  1980. Result := FMaterialLibrary;
  1981. end;
  1982. procedure TGLCustomGLSLLatticeShader.SetMaterialLibrary
  1983. (const Value: TGLAbstractMaterialLibrary);
  1984. begin
  1985. if FMaterialLibrary <> nil then
  1986. FMaterialLibrary.RemoveFreeNotification(self);
  1987. FMaterialLibrary := Value;
  1988. if (FMaterialLibrary <> nil) and
  1989. (FMaterialLibrary is TGLAbstractMaterialLibrary) then
  1990. FMaterialLibrary.FreeNotification(self);
  1991. end;
  1992. procedure TGLCustomGLSLLatticeShader.SetMainTexTexture
  1993. (const Value: TGLTexture);
  1994. begin
  1995. if FMainTexture = Value then
  1996. Exit;
  1997. FMainTexture := Value;
  1998. NotifyChange(self)
  1999. end;
  2000. function TGLCustomGLSLLatticeShader.GetMainTexName: TGLLibMaterialName;
  2001. begin
  2002. Result := TGLMaterialLibrary(FMaterialLibrary)
  2003. .GetNameOfTexture(FMainTexture);
  2004. if Result = '' then
  2005. Result := FMainTexName;
  2006. end;
  2007. procedure TGLCustomGLSLLatticeShader.SetMainTexName
  2008. (const Value: TGLLibMaterialName);
  2009. begin
  2010. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  2011. if FMainTexName = Value then
  2012. Exit;
  2013. FMainTexName := Value;
  2014. FMainTexture := TGLMaterialLibrary(FMaterialLibrary)
  2015. .TextureByName(FMainTexName);
  2016. NotifyChange(self);
  2017. end;
  2018. procedure TGLCustomGLSLLatticeShader.SetDiffuseColor(AValue: TGLColor);
  2019. begin
  2020. FDiffuseColor.DirectColor := AValue.Color;
  2021. end;
  2022. procedure TGLCustomGLSLLatticeShader.SetAmbientColor(AValue: TGLColor);
  2023. begin
  2024. FAmbientColor.DirectColor := AValue.Color;
  2025. end;
  2026. procedure TGLCustomGLSLLatticeShader.SetSpecularColor(AValue: TGLColor);
  2027. begin
  2028. FSpecularColor.DirectColor := AValue.Color;
  2029. end;
  2030. procedure TGLCustomGLSLLatticeShader.Notification(AComponent: TComponent;
  2031. Operation: TOperation);
  2032. var
  2033. Index: Integer;
  2034. begin
  2035. inherited;
  2036. if Operation = opRemove then
  2037. if AComponent = FMaterialLibrary then
  2038. if FMaterialLibrary <> nil then
  2039. begin
  2040. if FMainTexture <> nil then
  2041. begin
  2042. Index := TGLMaterialLibrary(FMaterialLibrary)
  2043. .Materials.GetTextureIndex(FMainTexture);
  2044. if Index <> -1 then
  2045. SetMainTexTexture(nil);
  2046. end;
  2047. FMaterialLibrary := nil;
  2048. end;
  2049. end;
  2050. (*************************************************
  2051. TGLCustomGLSLSemShader
  2052. ************************************************)
  2053. constructor TGLCustomGLSLSemShader.Create(AOwner: TComponent);
  2054. begin
  2055. inherited;
  2056. with VertexProgram.Code do
  2057. begin
  2058. clear;
  2059. Add('varying vec3 viewVec; ');
  2060. Add('varying vec3 normal; ');
  2061. Add('varying vec3 lightVec; ');
  2062. Add('void main() { ');
  2063. Add(' vec4 p = gl_ModelViewMatrix * gl_Vertex; ');
  2064. Add(' vec4 lightPos = gl_LightSource[0].position;');
  2065. Add(' lightVec = vec3(lightPos - p); ');
  2066. Add(' viewVec = -vec3(p); ');
  2067. Add(' normal = normalize(gl_NormalMatrix * gl_Normal ); ');
  2068. Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  2069. Add('} ');
  2070. end;
  2071. with FragmentProgram.Code do
  2072. begin
  2073. clear;
  2074. Add('uniform vec4 AmbientColor; ');
  2075. Add('uniform vec4 SpecularColor; ');
  2076. Add('uniform float DiffuseIntensity; ');
  2077. Add('uniform float AmbientIntensity; ');
  2078. Add('uniform float SpecularIntensity; ');
  2079. Add('uniform sampler2D MainTexture; ');
  2080. Add('varying vec3 viewVec; ');
  2081. Add('varying vec3 normal; ');
  2082. Add('varying vec3 lightVec; ');
  2083. Add('void main() { ');
  2084. Add(' vec3 V = normalize(viewVec); ');
  2085. Add(' vec3 r = reflect( V, normal ); ');
  2086. Add(' float m = 2.0 * sqrt( pow( r.x, 2.0 ) + pow( r.y, 2.0 ) + pow( r.z + 1.0, 2.0 ) ); ');
  2087. Add(' vec2 vN = r.xy / m + 0.5; ');
  2088. Add(' vec4 DiffuseColor; ');
  2089. Add(' DiffuseColor = texture2D( MainTexture, vN ); //.rgb; ');
  2090. // Simple Lighting
  2091. Add(' vec3 L = normalize(lightVec); ');
  2092. Add(' vec3 halfAngle = normalize(L + V); ');
  2093. Add(' float NdotL = dot(L, normal); ');
  2094. Add(' float NdotH = clamp(dot(halfAngle, normal), 0.0, 1.0); ');
  2095. Add(' // "Half-Lambert" technique for more pleasing diffuse term ');
  2096. Add(' float diffuse = DiffuseColor*(0.5 * NdotL + 0.5); ');
  2097. Add(' float specular = pow(NdotH, 64.0); ');
  2098. Add(' vec4 FinalColour = AmbientColor*AmbientIntensity + ');
  2099. Add(' DiffuseColor*diffuse*DiffuseIntensity + ');
  2100. Add(' SpecularColor*specular*SpecularIntensity; ');
  2101. Add(' gl_FragColor = FinalColour; //vec4( FinalColour, 1.0 ); ');
  2102. Add('} ');
  2103. end;
  2104. FAmbientColor := TGLColor.Create(self);
  2105. // FDiffuseColor := TGLColor.Create(Self);
  2106. FSpecularColor := TGLColor.Create(self);
  2107. // setup initial parameters
  2108. FAmbientColor.SetColor(0.15, 0.15, 0.15, 1.0);
  2109. // FDiffuseColor.SetColor(1, 1, 1, 1);
  2110. FSpecularColor.SetColor(1.0, 1.0, 1.0, 1.0);
  2111. FAmbientFactor := 0.8;
  2112. FDiffuseFactor := 0.9;
  2113. FSpecularFactor := 0.8;
  2114. end;
  2115. destructor TGLCustomGLSLSemShader.Destroy;
  2116. begin
  2117. FAmbientColor.Destroy;
  2118. // FDiffuseColor.Destroy;
  2119. FSpecularColor.Destroy;
  2120. inherited;
  2121. end;
  2122. procedure TGLCustomGLSLSemShader.DoApply(var rci: TGLRenderContextInfo;
  2123. Sender: TObject);
  2124. begin
  2125. GetGLSLProg.UseProgramObject;
  2126. // Param['DiffuseColor'].AsVector4f := FDiffuseColor.Color;
  2127. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  2128. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  2129. param['AmbientIntensity'].AsVector1f := FAmbientFactor;
  2130. param['DiffuseIntensity'].AsVector1f := FDiffuseFactor;
  2131. param['SpecularIntensity'].AsVector1f := FSpecularFactor;
  2132. // Param['SpecPower'].AsVector1f := FSpecularPower;
  2133. // Param['LightIntensity'].AsVector1f := FLightPower;
  2134. param['MainTexture'].AsTexture2D[0] := FMainTexture;
  2135. end;
  2136. function TGLCustomGLSLSemShader.DoUnApply
  2137. (var rci: TGLRenderContextInfo): Boolean;
  2138. begin
  2139. gl.ActiveTexture(GL_TEXTURE0_ARB);
  2140. GetGLSLProg.EndUseProgramObject;
  2141. Result := False;
  2142. end;
  2143. function TGLCustomGLSLSemShader.GetMaterialLibrary
  2144. : TGLAbstractMaterialLibrary;
  2145. begin
  2146. Result := FMaterialLibrary;
  2147. end;
  2148. procedure TGLCustomGLSLSemShader.SetMaterialLibrary
  2149. (const Value: TGLAbstractMaterialLibrary);
  2150. begin
  2151. if FMaterialLibrary <> nil then
  2152. FMaterialLibrary.RemoveFreeNotification(self);
  2153. FMaterialLibrary := Value;
  2154. if (FMaterialLibrary <> nil) and
  2155. (FMaterialLibrary is TGLAbstractMaterialLibrary) then
  2156. FMaterialLibrary.FreeNotification(self);
  2157. end;
  2158. procedure TGLCustomGLSLSemShader.SetMainTexTexture(const Value: TGLTexture);
  2159. begin
  2160. if FMainTexture = Value then
  2161. Exit;
  2162. FMainTexture := Value;
  2163. NotifyChange(self)
  2164. end;
  2165. function TGLCustomGLSLSemShader.GetMainTexName: TGLLibMaterialName;
  2166. begin
  2167. Result := TGLMaterialLibrary(FMaterialLibrary)
  2168. .GetNameOfTexture(FMainTexture);
  2169. if Result = '' then
  2170. Result := FMainTexName;
  2171. end;
  2172. procedure TGLCustomGLSLSemShader.SetMainTexName
  2173. (const Value: TGLLibMaterialName);
  2174. begin
  2175. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  2176. if FMainTexName = Value then
  2177. Exit;
  2178. FMainTexName := Value;
  2179. FMainTexture := TGLMaterialLibrary(FMaterialLibrary)
  2180. .TextureByName(FMainTexName);
  2181. NotifyChange(self);
  2182. end;
  2183. // procedure TGLCustomGLSLSemShader.SetDiffuseColor(AValue: TGLColor);
  2184. // begin
  2185. // FDiffuseColor.DirectColor := AValue.Color;
  2186. // end;
  2187. procedure TGLCustomGLSLSemShader.SetAmbientColor(AValue: TGLColor);
  2188. begin
  2189. FAmbientColor.DirectColor := AValue.Color;
  2190. end;
  2191. procedure TGLCustomGLSLSemShader.SetSpecularColor(AValue: TGLColor);
  2192. begin
  2193. FSpecularColor.DirectColor := AValue.Color;
  2194. end;
  2195. procedure TGLCustomGLSLSemShader.Notification(AComponent: TComponent;
  2196. Operation: TOperation);
  2197. var
  2198. Index: Integer;
  2199. begin
  2200. inherited;
  2201. if Operation = opRemove then
  2202. if AComponent = FMaterialLibrary then
  2203. if FMaterialLibrary <> nil then
  2204. begin
  2205. if FMainTexture <> nil then
  2206. begin
  2207. Index := TGLMaterialLibrary(FMaterialLibrary)
  2208. .Materials.GetTextureIndex(FMainTexture);
  2209. if Index <> -1 then
  2210. SetMainTexTexture(nil);
  2211. end;
  2212. FMaterialLibrary := nil;
  2213. end;
  2214. end;
  2215. (****************************************
  2216. TGLCustomGLSLToonShader
  2217. ****************************************)
  2218. constructor TGLCustomGLSLToonShader.Create(AOwner: TComponent);
  2219. begin
  2220. inherited;
  2221. with VertexProgram.Code do
  2222. begin
  2223. Clear;
  2224. Add('varying vec3 vNormal; ');
  2225. Add('varying vec3 LightVec; ');
  2226. Add('varying vec3 ViewVec; ');
  2227. Add(' ');
  2228. Add('void main() ');
  2229. Add('{ ');
  2230. Add(' vec4 lightPos = gl_LightSource[0].position;');
  2231. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  2232. Add(' vec3 normal = gl_NormalMatrix * gl_Normal; ');
  2233. Add(' vNormal = normalize(normal); ');
  2234. Add(' LightVec = vec3(lightPos - vert); ');
  2235. Add(' ViewVec = -vec3(vert); ');
  2236. //Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  2237. Add(' gl_Position = ftransform(); ');
  2238. Add('} ');
  2239. end;
  2240. with FragmentProgram.Code do
  2241. begin
  2242. Clear;
  2243. Add('uniform vec4 HighlightColor; ');
  2244. Add('uniform vec4 MidColor; ');
  2245. Add('uniform vec4 LightenShadowColor; ');
  2246. Add('uniform vec4 DarkenShadowColor; ');
  2247. Add('uniform vec4 OutlineColor; ');
  2248. Add('uniform float HighlightSize; '); // 0.95
  2249. Add('uniform float MidSize; '); // 0.5
  2250. Add('uniform float ShadowSize; '); // 0.25
  2251. Add('uniform float OutlineWidth; ');
  2252. Add('varying vec3 vNormal; ');
  2253. Add('varying vec3 LightVec; ');
  2254. Add('varying vec3 ViewVec; ');
  2255. Add('void main() ');
  2256. Add('{ ');
  2257. Add(' vec3 n = normalize(vNormal); ');
  2258. Add(' vec3 l = normalize(LightVec); ');
  2259. Add(' vec3 v = normalize(ViewVec); ');
  2260. Add(' float lambert = dot(l,n); ');
  2261. Add(' vec4 colour = MidColor; ');
  2262. Add(' if (lambert>HighlightSize) colour = HighlightColor; ');
  2263. Add(' else if (lambert>MidSize) colour = MidColor; ');
  2264. Add(' else if (lambert>ShadowSize) colour = LightenShadowColor; ');
  2265. Add(' else if (lambert<ShadowSize) colour = DarkenShadowColor; ');
  2266. Add(' if (dot(n,v)<OutlineWidth) colour = OutlineColor; ');
  2267. Add(' gl_FragColor = colour; ');
  2268. Add('} ');
  2269. end;
  2270. // Initial stuff.
  2271. FHighLightColor := TGLColor.Create(self);
  2272. FHighLightColor.SetColor(0.9,0.9,0.9,1.0);
  2273. FMidColor := TGLColor.Create(self);
  2274. FMidColor.SetColor(0.75,0.75,0.75,1.0);
  2275. FLightenShadowColor := TGLColor.Create(self);
  2276. FLightenShadowColor.SetColor(0.5,0.5,0.5,1.0);
  2277. FDarkenShadowColor := TGLColor.Create(self);
  2278. FDarkenShadowColor.SetColor(0.3,0.3,0.3,1.0);
  2279. FOutlineColor := TGLColor.Create(self);
  2280. FOutlineColor.SetColor(0,0,0,1.0);
  2281. FHighlightSize := 0.95;
  2282. FMidSize := 0.50;
  2283. FShadowSize := 0.25;
  2284. FOutlineWidth := 0.25;
  2285. end;
  2286. destructor TGLCustomGLSLToonShader.Destroy;
  2287. begin
  2288. FHighLightColor.Free;
  2289. FMidColor.Free;
  2290. FLightenShadowColor.Free;
  2291. FDarkenShadowColor.Free;
  2292. FOutlineColor.Free;
  2293. inherited;
  2294. end;
  2295. procedure TGLCustomGLSLToonShader.DoApply(var rci: TGLRenderContextInfo;Sender: TObject);
  2296. begin
  2297. GetGLSLProg.UseProgramObject;
  2298. param['HighlightColor'].AsVector4f := FHighlightColor.Color;
  2299. param['MidColor'].AsVector4f := FMidColor.Color;
  2300. param['LightenShadowColor'].AsVector4f := FLightenShadowColor.Color;
  2301. param['DarkenShadowColor'].AsVector4f := FDarkenShadowColor.Color;
  2302. param['OutlineColor'].AsVector4f := FOutlineColor.Color;
  2303. param['HighlightSize'].AsVector1f := FHighlightSize;
  2304. param['MidSize'].AsVector1f := FMidSize;
  2305. param['ShadowSize'].AsVector1f := FShadowSize;
  2306. param['OutlineWidth'].AsVector1f := FOutlineWidth;
  2307. end;
  2308. function TGLCustomGLSLToonShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  2309. begin
  2310. GetGLSLProg.EndUseProgramObject;
  2311. Result := False;
  2312. end;
  2313. procedure TGLCustomGLSLToonShader.SetHighlightColor(AValue: TGLColor);
  2314. begin
  2315. FHighlightColor.DirectColor := AValue.Color;
  2316. end;
  2317. procedure TGLCustomGLSLToonShader.SetMidColor(AValue: TGLColor);
  2318. begin
  2319. FMidColor.DirectColor := AValue.Color;
  2320. end;
  2321. procedure TGLCustomGLSLToonShader.SetLightenShadowColor(AValue: TGLColor);
  2322. begin
  2323. FLightenShadowColor.DirectColor := AValue.Color;
  2324. end;
  2325. procedure TGLCustomGLSLToonShader.SetDarkenShadowColor(AValue: TGLColor);
  2326. begin
  2327. FDarkenShadowColor.DirectColor := AValue.Color;
  2328. end;
  2329. procedure TGLCustomGLSLToonShader.SetOutlineColor(AValue: TGLColor);
  2330. begin
  2331. FOutlineColor.DirectColor := AValue.Color;
  2332. end;
  2333. (*************************************************
  2334. TGLCustomGLSLVertexDisplacementShader
  2335. *************************************************)
  2336. constructor TGLCustomGLSLVertexDisplacementShader.Create(AOwner: TComponent);
  2337. begin
  2338. inherited;
  2339. with VertexProgram.Code do
  2340. begin
  2341. clear;
  2342. Add('uniform float time; ');
  2343. Add('uniform float NoiseFactor; ');
  2344. Add('uniform float TurbulenceFactor; ');
  2345. Add('uniform float NoiseScale; ');
  2346. Add('uniform float NoisePeriod; ');
  2347. Add('uniform float DisplacementScale; ');
  2348. Add('uniform float TimeFactor; ');
  2349. Add('varying vec3 viewVec; ');
  2350. Add('varying vec3 normal; ');
  2351. Add('varying vec3 lightVec; ');
  2352. Add('varying vec2 vTexCoord; ');
  2353. Add('varying float noise; ');
  2354. //
  2355. // GLSL textureless classic 3D noise "cnoise",
  2356. // with an RSL-style periodic variant "pnoise".
  2357. // Author: Stefan Gustavson ([email protected])
  2358. // Version: 2011-10-11
  2359. //
  2360. // Many thanks to Ian McEwan of Ashima Arts for the
  2361. // ideas for permutation and gradient selection.
  2362. //
  2363. // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
  2364. // Distributed under the MIT license. See LICENSE file.
  2365. // https://github.com/ashima/webgl-noise
  2366. //
  2367. Add('vec3 mod289(vec3 x) ');
  2368. Add('{ ');
  2369. Add(' return x - floor(x * (1.0 / 289.0)) * 289.0; ');
  2370. Add('} ');
  2371. Add('vec4 mod289(vec4 x) ');
  2372. Add('{ ');
  2373. Add(' return x - floor(x * (1.0 / 289.0)) * 289.0; ');
  2374. Add('} ');
  2375. Add('vec4 permute(vec4 x) ');
  2376. Add('{ ');
  2377. Add(' return mod289(((x*34.0)+1.0)*x); ');
  2378. Add('} ');
  2379. Add('vec4 taylorInvSqrt(vec4 r) ');
  2380. Add('{ ');
  2381. Add(' return 1.79284291400159 - 0.85373472095314 * r; ');
  2382. Add('} ');
  2383. Add('vec3 fade(vec3 t) { ');
  2384. Add(' return t*t*t*(t*(t*6.0-15.0)+10.0); ');
  2385. Add('} ');
  2386. // Classic Perlin noise, periodic variant
  2387. Add('float pnoise(vec3 P, vec3 rep) ');
  2388. Add('{ ');
  2389. Add(' vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period ');
  2390. Add(' vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period ');
  2391. Add(' Pi0 = mod289(Pi0); ');
  2392. Add(' Pi1 = mod289(Pi1); ');
  2393. Add(' vec3 Pf0 = fract(P); // Fractional part for interpolation ');
  2394. Add(' vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0 ');
  2395. Add(' vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x); ');
  2396. Add(' vec4 iy = vec4(Pi0.yy, Pi1.yy); ');
  2397. Add(' vec4 iz0 = Pi0.zzzz; ');
  2398. Add(' vec4 iz1 = Pi1.zzzz; ');
  2399. Add(' vec4 ixy = permute(permute(ix) + iy); ');
  2400. Add(' vec4 ixy0 = permute(ixy + iz0); ');
  2401. Add(' vec4 ixy1 = permute(ixy + iz1); ');
  2402. Add(' vec4 gx0 = ixy0 * (1.0 / 7.0); ');
  2403. Add(' vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5; ');
  2404. Add(' gx0 = fract(gx0); ');
  2405. Add(' vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0); ');
  2406. Add(' vec4 sz0 = step(gz0, vec4(0.0)); ');
  2407. Add(' gx0 -= sz0 * (step(0.0, gx0) - 0.5); ');
  2408. Add(' gy0 -= sz0 * (step(0.0, gy0) - 0.5); ');
  2409. Add(' vec4 gx1 = ixy1 * (1.0 / 7.0); ');
  2410. Add(' vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5; ');
  2411. Add(' gx1 = fract(gx1); ');
  2412. Add(' vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1); ');
  2413. Add(' vec4 sz1 = step(gz1, vec4(0.0)); ');
  2414. Add(' gx1 -= sz1 * (step(0.0, gx1) - 0.5); ');
  2415. Add(' gy1 -= sz1 * (step(0.0, gy1) - 0.5); ');
  2416. Add(' vec3 g000 = vec3(gx0.x,gy0.x,gz0.x); ');
  2417. Add(' vec3 g100 = vec3(gx0.y,gy0.y,gz0.y); ');
  2418. Add(' vec3 g010 = vec3(gx0.z,gy0.z,gz0.z); ');
  2419. Add(' vec3 g110 = vec3(gx0.w,gy0.w,gz0.w); ');
  2420. Add(' vec3 g001 = vec3(gx1.x,gy1.x,gz1.x); ');
  2421. Add(' vec3 g101 = vec3(gx1.y,gy1.y,gz1.y); ');
  2422. Add(' vec3 g011 = vec3(gx1.z,gy1.z,gz1.z); ');
  2423. Add(' vec3 g111 = vec3(gx1.w,gy1.w,gz1.w); ');
  2424. Add(' vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110))); ');
  2425. Add(' g000 *= norm0.x; ');
  2426. Add(' g010 *= norm0.y; ');
  2427. Add(' g100 *= norm0.z; ');
  2428. Add(' g110 *= norm0.w; ');
  2429. Add(' vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111))); ');
  2430. Add(' g001 *= norm1.x; ');
  2431. Add(' g011 *= norm1.y; ');
  2432. Add(' g101 *= norm1.z; ');
  2433. Add(' g111 *= norm1.w; ');
  2434. Add(' float n000 = dot(g000, Pf0); ');
  2435. Add(' float n100 = dot(g100, vec3(Pf1.x, Pf0.yz)); ');
  2436. Add(' float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z)); ');
  2437. Add(' float n110 = dot(g110, vec3(Pf1.xy, Pf0.z)); ');
  2438. Add(' float n001 = dot(g001, vec3(Pf0.xy, Pf1.z)); ');
  2439. Add(' float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z)); ');
  2440. Add(' float n011 = dot(g011, vec3(Pf0.x, Pf1.yz)); ');
  2441. Add(' float n111 = dot(g111, Pf1); ');
  2442. Add(' vec3 fade_xyz = fade(Pf0); ');
  2443. Add(' vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z); ');
  2444. Add(' vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y); ');
  2445. Add(' float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x); ');
  2446. Add(' return 2.2 * n_xyz; ');
  2447. Add('} ');
  2448. Add('float turbulence( vec3 p ) { ');
  2449. Add(' float w = 100.0; ');
  2450. Add(' float t = -.5; ');
  2451. Add(' for (float f = 1.0 ; f <= 10.0 ; f++ ){ ');
  2452. Add(' float power = pow( 2.0, f ); ');
  2453. Add(' t += abs( pnoise( vec3( power * p ), vec3( 10.0, 10.0, 10.0 ) ) / power ); ');
  2454. Add(' } ');
  2455. Add(' return t; ');
  2456. Add('} ');
  2457. Add('void main() { '); //96
  2458. Add(' mat4 mWorld = gl_ModelViewMatrix; ');
  2459. Add(' vec3 Normal = gl_NormalMatrix * gl_Normal; //gl_Normal; ');
  2460. Add(' vec4 Position = gl_Vertex; ');
  2461. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  2462. Add(' vec4 lightPos = gl_LightSource[0].position;');
  2463. Add(' vTexCoord = gl_MultiTexCoord0; ');
  2464. Add(' vec3 vNormal = normalize( Normal * mat3(mWorld )); ');
  2465. Add(' time = TimeFactor*time; ');
  2466. // add time to the noise parameters so it's animated
  2467. Add(' noise = NoiseFactor* -0.10* turbulence( TurbulenceFactor * vNormal+time ); ');
  2468. // get a 3d noise using the position, low frequency
  2469. Add(' float b = (NoisePeriod*time)*pnoise( vec3((NoiseScale *time)* (Position.xyz + vec3(time ))), vec3(100) ); ');
  2470. // compose both noises
  2471. Add(' float displacement =( noise + b); ');
  2472. Add(' vec4 newPosition =vec4((Position.xyz + DisplacementScale*(vec3(vNormal * displacement))),1.0); ');
  2473. Add(' normal = vNormal; ');
  2474. Add(' lightVec = vec3(lightPos - vert); ');
  2475. Add(' viewVec = -vec3(vert); ');
  2476. Add(' gl_Position = gl_ModelViewProjectionMatrix * newPosition; ');
  2477. Add('} ');
  2478. end;
  2479. with FragmentProgram.Code do
  2480. begin
  2481. clear;
  2482. Add('uniform vec4 AmbientColor; ');
  2483. Add('uniform vec4 SpecularColor; ');
  2484. Add('uniform float DiffuseIntensity; ');
  2485. Add('uniform float AmbientIntensity; ');
  2486. Add('uniform float SpecularIntensity; ');
  2487. Add('uniform sampler2D MainTexture; ');
  2488. Add('varying vec3 viewVec; ');
  2489. Add('varying vec3 normal; ');
  2490. Add('varying vec3 lightVec; ');
  2491. Add('varying float noise; ');
  2492. Add('float random( vec3 scale, float seed ){ ');
  2493. Add(' return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed ) ; ');
  2494. Add('} ');
  2495. Add('void main() { ');
  2496. // get a random offset
  2497. Add(' float r = 0.01 * random( vec3( 12.9898, 78.233, 151.7182 ), 0.0 ); ');
  2498. // lookup vertically in the texture, using noise and offset
  2499. // to get the right RGB colour
  2500. Add(' vec2 tPos = vec2( 0, 1.0 - 1.3 * noise + r ); ');
  2501. Add(' vec4 DiffuseColor; ');
  2502. Add(' DiffuseColor = texture2D( MainTexture, tPos ); ');
  2503. // Simple Lighting
  2504. Add(' vec3 L = normalize(lightVec); ');
  2505. Add(' vec3 V = normalize(viewVec); ');
  2506. Add(' vec3 halfAngle = normalize(L + V); ');
  2507. Add(' float NdotL = dot(L, normal); ');
  2508. Add(' float NdotH = clamp(dot(halfAngle, normal), 0.0, 1.0); ');
  2509. Add(' // "Half-Lambert" technique for more pleasing diffuse term ');
  2510. Add(' float diffuse = DiffuseColor*(0.5 * NdotL + 0.5); ');
  2511. Add(' float specular = pow(NdotH, 64.0); ');
  2512. Add(' vec4 FinalColour = AmbientColor*AmbientIntensity + ');
  2513. Add(' DiffuseColor*diffuse*DiffuseIntensity + ');
  2514. Add(' SpecularColor*specular*SpecularIntensity; ');
  2515. Add(' gl_FragColor = FinalColour; ; ');
  2516. // Add(' gl_FragColor = vec4(DiffuseColor,1.0); ');
  2517. Add('} ');
  2518. end;
  2519. FAmbientColor := TGLColor.Create(Self);
  2520. //FDiffuseColor := TGLColor.Create(Self);
  2521. FSpecularColor := TGLColor.Create(Self);
  2522. //setup initial parameters
  2523. FAmbientColor.SetColor(0.15, 0.15, 0.15, 1.0);
  2524. //FDiffuseColor.SetColor(1, 1, 1, 1);
  2525. FSpecularColor.SetColor(1.0, 1.0, 1.0, 1.0);
  2526. FAmbientFactor := 0.8;
  2527. FDiffuseFactor :=0.9;
  2528. FSpecularFactor :=0.8;
  2529. FElapsedTime := 1.0;
  2530. FNoise := 10.0;
  2531. FDisplacementScale := 1.0;
  2532. FNoiseScale := 0.05;
  2533. FTurbulenceFactor := 0.5;
  2534. FNoisePeriod := 5.0;
  2535. FTimeFactor := 0.05;
  2536. end;
  2537. destructor TGLCustomGLSLVertexDisplacementShader.Destroy;
  2538. begin
  2539. FAmbientColor.Destroy;
  2540. // FDiffuseColor.Destroy;
  2541. FSpecularColor.Destroy;
  2542. inherited;
  2543. end;
  2544. procedure TGLCustomGLSLVertexDisplacementShader.DoApply(var rci: TGLRenderContextInfo; Sender: TObject);
  2545. begin
  2546. GetGLSLProg.UseProgramObject;
  2547. // Param['DiffuseColor'].AsVector4f := FDiffuseColor.Color;
  2548. param['AmbientColor'].AsVector4f := FAmbientColor.Color;
  2549. param['SpecularColor'].AsVector4f := FSpecularColor.Color;
  2550. param['AmbientIntensity'].AsVector1f := FAmbientFactor;
  2551. param['DiffuseIntensity'].AsVector1f := FDiffuseFactor;
  2552. param['SpecularIntensity'].AsVector1f := FSpecularFactor;
  2553. Param['time'].AsVector1f := FElapsedTime;
  2554. Param['NoiseFactor'].AsVector1f := FNoise;
  2555. Param['NoiseScale'].AsVector1f := FNoiseScale;
  2556. Param['TurbulenceFactor'].AsVector1f := FTurbulenceFactor;
  2557. Param['NoisePeriod'].AsVector1f := FNoisePeriod;
  2558. Param['DisplacementScale'].AsVector1f := FDisplacementScale;
  2559. Param['TimeFactor'].AsVector1f := FTimeFactor;
  2560. Param['MainTexture'].AsTexture2D[0] := FMainTexture;
  2561. end;
  2562. function TGLCustomGLSLVertexDisplacementShader.DoUnApply(var rci: TGLRenderContextInfo): Boolean;
  2563. begin
  2564. gl.ActiveTexture(GL_TEXTURE0_ARB);
  2565. GetGLSLProg.EndUseProgramObject;
  2566. Result := False;
  2567. end;
  2568. function TGLCustomGLSLVertexDisplacementShader.GetMaterialLibrary: TGLAbstractMaterialLibrary;
  2569. begin
  2570. Result := FMaterialLibrary;
  2571. end;
  2572. procedure TGLCustomGLSLVertexDisplacementShader.SetMaterialLibrary(const Value: TGLAbstractMaterialLibrary);
  2573. begin
  2574. if FMaterialLibrary <> nil then FMaterialLibrary.RemoveFreeNotification(Self);
  2575. FMaterialLibrary := Value;
  2576. if (FMaterialLibrary <> nil)
  2577. and (FMaterialLibrary is TGLAbstractMaterialLibrary) then
  2578. FMaterialLibrary.FreeNotification(Self);
  2579. end;
  2580. procedure TGLCustomGLSLVertexDisplacementShader.SetMainTexTexture(const Value: TGLTexture);
  2581. begin
  2582. if FMainTexture = Value then Exit;
  2583. FMainTexture := Value;
  2584. NotifyChange(Self)
  2585. end;
  2586. function TGLCustomGLSLVertexDisplacementShader.GetMainTexName: TGLLibMaterialName;
  2587. begin
  2588. Result := TGLMaterialLibrary(FMaterialLibrary).GetNameOfTexture(FMainTexture);
  2589. if Result = '' then Result := FMainTexName;
  2590. end;
  2591. procedure TGLCustomGLSLVertexDisplacementShader.SetMainTexName(const Value: TGLLibMaterialName);
  2592. begin
  2593. // Assert(not(assigned(FMaterialLibrary)),'You must set Material Library Before');
  2594. if FMainTexName = Value then Exit;
  2595. FMainTexName := Value;
  2596. FMainTexture := TGLMaterialLibrary(FMaterialLibrary).TextureByName(FMainTexName);
  2597. NotifyChange(Self);
  2598. end;
  2599. //procedure TGLCustomGLSLVertexDisplacementShader.SetDiffuseColor(AValue: TGLColor);
  2600. //begin
  2601. // FDiffuseColor.DirectColor := AValue.Color;
  2602. //end;
  2603. procedure TGLCustomGLSLVertexDisplacementShader.SetAmbientColor(AValue: TGLColor);
  2604. begin
  2605. FAmbientColor.DirectColor := AValue.Color;
  2606. end;
  2607. procedure TGLCustomGLSLVertexDisplacementShader.SetSpecularColor(AValue: TGLColor);
  2608. begin
  2609. FSpecularColor.DirectColor := AValue.Color;
  2610. end;
  2611. procedure TGLCustomGLSLVertexDisplacementShader.Notification
  2612. (AComponent: TComponent; Operation: TOperation);
  2613. var
  2614. Index: Integer;
  2615. begin
  2616. inherited;
  2617. if Operation = opRemove then
  2618. if AComponent = FMaterialLibrary then
  2619. if FMaterialLibrary <> nil then
  2620. begin
  2621. if FMainTexture <> nil then
  2622. begin
  2623. Index := TGLMaterialLibrary(FMaterialLibrary)
  2624. .Materials.GetTextureIndex(FMainTexture);
  2625. if Index <> -1 then
  2626. SetMainTexTexture(nil);
  2627. end;
  2628. FMaterialLibrary := nil;
  2629. end;
  2630. end;
  2631. end.