fCgCellShader.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. unit fCgCellShader;
  2. interface
  3. uses
  4. System.SysUtils,
  5. System.Classes,
  6. Vcl.Graphics,
  7. Vcl.Imaging.Jpeg,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. Imports.cgGL,
  12. GLS.Scene,
  13. GLS.VectorTypes,
  14. GLS.VectorGeometry,
  15. GLS.Objects,
  16. GLS.Cadencer,
  17. GLS.Texture,
  18. GLS.SceneViewer,
  19. GLS.VectorFileObjects,
  20. GLS.AsyncTimer,
  21. GLS.Material,
  22. GLS.Coordinates,
  23. GLS.BaseClasses,
  24. GLS.Utils,
  25. GLS.FileMD2,
  26. Cg.Shader;
  27. type
  28. TFormCellShading = class(TForm)
  29. GLScene1: TGLScene;
  30. GLSceneViewer1: TGLSceneViewer;
  31. CgCellShader: TCgShader;
  32. GLMaterialLibrary1: TGLMaterialLibrary;
  33. GLCadencer1: TGLCadencer;
  34. GLCamera1: TGLCamera;
  35. GLDummyCube1: TGLDummyCube;
  36. GLLightSource1: TGLLightSource;
  37. GLActor1: TGLActor;
  38. AsyncTimer1: TGLAsyncTimer;
  39. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  40. Shift: TShiftState; X, Y: Integer);
  41. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  42. X, Y: Integer);
  43. procedure CgCellShaderApplyVP(CgProgram: TCgProgram; Sender: TObject);
  44. procedure FormCreate(Sender: TObject);
  45. procedure CgCellShaderInitialize(CgShader: TCustomCgShader);
  46. procedure CgCellShaderApplyFP(CgProgram: TCgProgram; Sender: TObject);
  47. procedure CgCellShaderUnApplyFP(CgProgram: TCgProgram);
  48. procedure AsyncTimer1Timer(Sender: TObject);
  49. public
  50. mx, my: Integer;
  51. end;
  52. var
  53. FormCellShading: TFormCellShading;
  54. implementation
  55. {$R *.dfm}
  56. procedure TFormCellShading.FormCreate(Sender: TObject);
  57. var
  58. r: Single;
  59. begin
  60. SetGLSceneMediaDir();
  61. // Load the vertex and fragment Cg programs from Shaders dir
  62. CgCellShader.VertexProgram.LoadFromFile('Shaders\cellshading_vp.cg');
  63. CgCellShader.FragmentProgram.LoadFromFile('Shaders\cellshading_fp.cg');
  64. // Load and scale the actor
  65. GLActor1.LoadFromFile('waste.md2');
  66. r := GLActor1.BoundingSphereRadius;
  67. GLActor1.Scale.SetVector(2.5 / r, 2.5 / r, 2.5 / r);
  68. GLActor1.AnimationMode := aamLoop;
  69. // Load the texture
  70. GLMaterialLibrary1.Materials[0].Material.Texture.Image.LoadFromFile
  71. ('wastecell.jpg');
  72. end;
  73. procedure TFormCellShading.CgCellShaderApplyVP(CgProgram: TCgProgram; Sender: TObject);
  74. begin
  75. // Apply the per frame uniform parameters
  76. with CgProgram do
  77. begin
  78. ParamByName('LightDir').SetAsVector(GLLightSource1.AbsoluteDirection);
  79. ParamByName('ModelViewProj').SetAsStateMatrix
  80. (CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
  81. ParamByName('ModelViewIT').SetAsStateMatrix(CG_GL_MODELVIEW_MATRIX,
  82. CG_GL_MATRIX_INVERSE_TRANSPOSE);
  83. end;
  84. end;
  85. procedure TFormCellShading.CgCellShaderInitialize(CgShader: TCustomCgShader);
  86. begin
  87. // Set up the texture sampler parameter
  88. CgCellShader.FragmentProgram.ParamByName('Map0')
  89. .SetAsTexture2D(GLMaterialLibrary1.Materials[0].Material.Texture.Handle);
  90. end;
  91. procedure TFormCellShading.CgCellShaderApplyFP(CgProgram: TCgProgram; Sender: TObject);
  92. begin
  93. // Enable the texture map sampler for use in the fragment
  94. // program
  95. CgProgram.ParamByName('Map0').EnableTexture();
  96. end;
  97. procedure TFormCellShading.CgCellShaderUnApplyFP(CgProgram: TCgProgram);
  98. begin
  99. // Disable the texture map sampler
  100. CgProgram.ParamByName('Map0').DisableTexture();
  101. end;
  102. procedure TFormCellShading.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  103. Shift: TShiftState; X, Y: Integer);
  104. begin
  105. mx := X;
  106. my := Y;
  107. end;
  108. procedure TFormCellShading.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  109. X, Y: Integer);
  110. begin
  111. if ssLeft in Shift then
  112. GLCamera1.MoveAroundTarget(my - Y, mx - X);
  113. mx := X;
  114. my := Y;
  115. end;
  116. procedure TFormCellShading.AsyncTimer1Timer(Sender: TObject);
  117. begin
  118. FormCellShading.Caption := Format('Cg Cell Shading - %.2f FPS',
  119. [GLSceneViewer1.FramesPerSecond]);
  120. GLSceneViewer1.ResetPerformanceMonitor;
  121. end;
  122. end.