GLSL.ShaderIvory.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLSL.ShaderIvory;
  5. (*
  6. Ivory shader simulate Ivory material.
  7. At this time only one light source is supported
  8. *)
  9. interface
  10. {$I GLScene.inc}
  11. uses
  12. System.Classes,
  13. OpenGLTokens,
  14. GLScene,
  15. GLBaseClasses,
  16. GLState,
  17. GLContext,
  18. GLRenderContextInfo,
  19. GLVectorGeometry,
  20. GLCoordinates,
  21. GLTextureFormat,
  22. GLColor,
  23. GLTexture,
  24. GLMaterial,
  25. GLSL.Shader,
  26. GLS.ShaderCustom;
  27. (* Custom class for GLSLIvoryShader.
  28. A shader that simulate Ivory Material *)
  29. type
  30. TGLCustomGLSLIvoryShader = class(TGLCustomGLSLShader)
  31. protected
  32. procedure DoApply(var rci : TGLRenderContextInfo; Sender : TObject); override;
  33. function DoUnApply(var rci: TGLRenderContextInfo): Boolean; override;
  34. public
  35. constructor Create(AOwner : TComponent); override;
  36. destructor Destroy; override;
  37. end;
  38. type
  39. TGLSLIvoryShader = class(TGLCustomGLSLIvoryShader)
  40. end;
  41. //---------------------------------
  42. implementation
  43. //---------------------------------
  44. //---------------------------------
  45. // TGLCustomGLSLIvoryShader
  46. //---------------------------------
  47. constructor TGLCustomGLSLIvoryShader.Create(AOwner: TComponent);
  48. begin
  49. inherited;
  50. with VertexProgram.Code do
  51. begin
  52. Clear;
  53. Add('varying vec3 normal; ');
  54. Add('varying vec3 lightVec; ');
  55. Add('varying vec3 viewVec; ');
  56. Add(' ');
  57. Add('void main() ');
  58. Add('{ ');
  59. Add(' gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; ');
  60. Add(' vec4 lightPos = gl_LightSource[0].position;');
  61. Add(' vec4 vert = gl_ModelViewMatrix * gl_Vertex; ');
  62. Add(' normal = gl_NormalMatrix * gl_Normal; ');
  63. Add(' lightVec = vec3(lightPos - vert); ');
  64. Add(' viewVec = -vec3(vert); ');
  65. Add('} ');
  66. end;
  67. with FragmentProgram.Code do
  68. begin
  69. Clear;
  70. Add('varying vec3 normal; ');
  71. Add('varying vec3 lightVec; ');
  72. Add('varying vec3 viewVec; ');
  73. Add(' ');
  74. Add('void main() ');
  75. Add('{ ');
  76. Add('vec3 norm = normalize(normal); ');
  77. Add('vec3 L = normalize(lightVec); ');
  78. Add('vec3 V = normalize(viewVec); ');
  79. Add('vec3 halfAngle = normalize(L + V); ');
  80. Add('float NdotL = dot(L, norm); ');
  81. Add('float NdotH = clamp(dot(halfAngle, norm), 0.0, 1.0); ');
  82. Add('// "Half-Lambert" technique for more pleasing diffuse term ');
  83. Add('float diffuse = 0.5 * NdotL + 0.5; ');
  84. Add('float specular = pow(NdotH, 64.0); ');
  85. Add('float result = diffuse + specular; ');
  86. Add('gl_FragColor = vec4(result); ');
  87. Add('} ');
  88. end;
  89. // Initial stuff.
  90. end;
  91. destructor TGLCustomGLSLIvoryShader.Destroy;
  92. begin
  93. inherited;
  94. end;
  95. procedure TGLCustomGLSLIvoryShader.DoApply(var rci: TGLRenderContextInfo;
  96. Sender: TObject);
  97. begin
  98. GetGLSLProg.UseProgramObject;
  99. end;
  100. function TGLCustomGLSLIvoryShader.DoUnApply(
  101. var rci: TGLRenderContextInfo): Boolean;
  102. begin
  103. Result := False;
  104. GetGLSLProg.EndUseProgramObject;
  105. end;
  106. end.