Unit1.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.AsyncTimer"
  9. #pragma link "GLS.BaseClasses"
  10. #pragma link "GLS.Cadencer"
  11. #pragma link "GLSL.cgShader"
  12. #pragma link "GLS.Coordinates"
  13. #pragma link "GLS.Material"
  14. #pragma link "GLS.Objects"
  15. #pragma link "GLS.Scene"
  16. #pragma link "GLS.VectorFileObjects"
  17. #pragma link "GLS.SceneViewer"
  18. #pragma link "GLS.FileMD2"
  19. #pragma link "GLSL.cgShader"
  20. #pragma resource "*.dfm"
  21. TForm1 *Form1;
  22. //---------------------------------------------------------------------------
  23. __fastcall TForm1::TForm1(TComponent* Owner)
  24. : TForm(Owner)
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TForm1::FormCreate(TObject *Sender)
  29. {
  30. float r;
  31. SetGLSceneMediaDir();
  32. // Load the vertex and fragment Cg programs from project dir
  33. CgCellShader->VertexProgram->LoadFromFile("Shaders\\cellshading_vp.cg");
  34. CgCellShader->FragmentProgram->LoadFromFile("Shaders\\cellshading_fp.cg");
  35. // Load and scale the actor from media dir
  36. GLActor1->LoadFromFile("waste.md2");
  37. r = GLActor1->BoundingSphereRadius();
  38. GLActor1->Scale->SetVector(2.5/r,2.5/r,2.5/r);
  39. GLActor1->AnimationMode = aamLoop;
  40. // Load the texture
  41. GLMaterialLibrary1->Materials->Items[0]->Material->Texture->Image->LoadFromFile("wastecell.jpg");
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall TForm1::CgCellShaderApplyVP(TCgProgram *CgProgram, TObject *Sender)
  45. {
  46. // Apply the per frame uniform parameters
  47. CgProgram->ParamByName("LightDir")->SetAsVector(GLLightSource1->AbsoluteDirection);
  48. CgProgram->ParamByName("ModelViewProj")->SetAsStateMatrix(CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
  49. CgProgram->ParamByName("ModelViewIT")->SetAsStateMatrix(CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE_TRANSPOSE);
  50. }
  51. //---------------------------------------------------------------------------
  52. void __fastcall TForm1::CgCellShaderInitialize(TCustomCgShader *CgShader)
  53. {
  54. // Set up the texture sampler parameter
  55. CgCellShader->FragmentProgram->ParamByName("Map0")->SetAsTexture2D(
  56. GLMaterialLibrary1->Materials->Items[0]->Material->Texture->Handle);
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TForm1::CgCellShaderApplyFP(TCgProgram *CgProgram, TObject *Sender)
  60. {
  61. // Enable the texture map sampler for use in the fragment
  62. // program
  63. CgProgram->ParamByName("Map0")->EnableTexture();
  64. }
  65. //---------------------------------------------------------------------------
  66. void __fastcall TForm1::CgCellShaderUnApplyFP(TCgProgram *CgProgram)
  67. {
  68. // Disable the texture map sampler
  69. CgProgram->ParamByName("Map0")->DisableTexture();
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  73. TShiftState Shift, int X, int Y)
  74. {
  75. mx = X;
  76. my = Y;
  77. }
  78. //---------------------------------------------------------------------------
  79. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  80. int X, int Y)
  81. {
  82. if (Shift.Contains(ssLeft))
  83. GLCamera1->MoveAroundTarget(my-Y,mx-X);
  84. mx = X;
  85. my = Y;
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall TForm1::AsyncTimer1Timer(TObject *Sender)
  89. {
  90. Form1->Caption = Format("Cg Cell Shading - %.2f FPS",
  91. ARRAYOFCONST ((GLSceneViewer1->FramesPerSecond())));
  92. GLSceneViewer1->ResetPerformanceMonitor();
  93. }
  94. //---------------------------------------------------------------------------