fCelShadingC.cpp 3.7 KB

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