fBunnyBumpC.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fBunnyBumpC.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.AsyncTimer"
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLSL.BumpShaders"
  10. #pragma link "GLS.Cadencer"
  11. #pragma link "GLS.Coordinates"
  12. #pragma link "GLS.Material"
  13. #pragma link "GLS.Objects"
  14. #pragma link "GLS.Scene"
  15. #pragma link "GLS.VectorFileObjects"
  16. #pragma link "GLS.SceneViewer"
  17. #pragma resource "*.dfm"
  18. TForm1 *Form1;
  19. //---------------------------------------------------------------------------
  20. __fastcall TForm1::TForm1(TComponent* Owner)
  21. : TForm(Owner)
  22. {
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TForm1::FormCreate(TObject *Sender)
  26. {
  27. SetGLSceneMediaDir();
  28. // Load the bunny mesh and scale for viewing
  29. Bunny->LoadFromFile("bunny.glsm");
  30. Bunny->Scale->Scale((float)(2/Bunny->BoundingSphereRadius()));
  31. // Load the normal map
  32. GLMaterialLibrary1->Materials->Items[0]->Material->Texture->Image->LoadFromFile("bunnynormals.jpg");
  33. // Link the lights to their toggles
  34. CheckBox1->Tag = Integer(WhiteLight);
  35. CheckBox2->Tag = Integer(RedLight);
  36. CheckBox3->Tag = Integer(BlueLight);
  37. Shape1->Tag = Integer(WhiteLight);
  38. Shape2->Tag = Integer(RedLight);
  39. Shape3->Tag = Integer(BlueLight);
  40. ComboBox1->ItemIndex = 0;
  41. ComboBox1Change(NULL);
  42. StartHeight = Height;
  43. }
  44. //---------------------------------------------------------------------------
  45. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  46. const double newTime)
  47. {
  48. // Orbit the camera
  49. if ((dx != 0) || (dy != 0))
  50. {
  51. Camera->MoveAroundTarget(dy, dx);
  52. dx = 0;
  53. dy = 0;
  54. }
  55. // Rotate the light sources
  56. if (CheckBox4->Checked)
  57. DCLights->Turn(deltaTime*20);
  58. GLSceneViewer1->Invalidate();
  59. }
  60. //---------------------------------------------------------------------------
  61. void __fastcall TForm1::CheckBox1Click(TObject *Sender)
  62. {
  63. WhiteLight->Shining = CheckBox1->Checked;
  64. CheckBox1->Tag = CheckBox1->Checked;
  65. }
  66. //---------------------------------------------------------------------------
  67. void __fastcall TForm1::CheckBox2Click(TObject *Sender)
  68. {
  69. RedLight->Shining = CheckBox1->Checked;
  70. CheckBox2->Tag = CheckBox2->Checked;
  71. }
  72. //---------------------------------------------------------------------------
  73. void __fastcall TForm1::CheckBox3Click(TObject *Sender)
  74. {
  75. BlueLight->Shining = CheckBox1->Checked;
  76. CheckBox3->Tag = CheckBox3->Checked;
  77. }
  78. //---------------------------------------------------------------------------
  79. void __fastcall TForm1::Shape1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  80. int X, int Y)
  81. {
  82. // WhiteLight Color Dialog
  83. ColorDialog1->Color = Shape1->Brush->Color;
  84. if (ColorDialog1->Execute())
  85. {
  86. Shape1->Brush->Color = ColorDialog1->Color;
  87. WhiteLight->Diffuse->AsWinColor = ColorDialog1->Color;
  88. }
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TForm1::Shape2MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  92. int X, int Y)
  93. {
  94. // RedLight Color Dialog
  95. ColorDialog1->Color = Shape2->Brush->Color;
  96. if (ColorDialog1->Execute())
  97. {
  98. Shape2->Brush->Color = ColorDialog1->Color;
  99. RedLight->Diffuse->AsWinColor = ColorDialog1->Color;
  100. }
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TForm1::Shape3MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  104. int X, int Y)
  105. {
  106. // BlueLight Color Dialog
  107. ColorDialog1->Color = Shape3->Brush->Color;
  108. if (ColorDialog1->Execute())
  109. {
  110. Shape3->Brush->Color = ColorDialog1->Color;
  111. BlueLight->Diffuse->AsWinColor = ColorDialog1->Color;
  112. }
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall TForm1::ComboBox1Change(TObject *Sender)
  116. {
  117. if (ComboBox1->Text == "Per-Vertex")
  118. Bunny->Material->LibMaterialName = " ";
  119. else
  120. if (ComboBox1->Text == "Dot3 Texture Combiner")
  121. {
  122. Bunny->Material->LibMaterialName = "Bump";
  123. GLBumpShader1->BumpMethod = bmDot3TexCombiner;
  124. }
  125. else
  126. if (ComboBox1->Text == "Basic Fragment Program")
  127. {
  128. Bunny->Material->LibMaterialName = "Bump";
  129. GLBumpShader1->BumpMethod = bmBasicARBFP;
  130. }
  131. }
  132. //---------------------------------------------------------------------------
  133. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  134. TShiftState Shift, int X, int Y)
  135. {
  136. mx = X;
  137. my = Y;
  138. dx = 0;
  139. dy = 0;
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  143. int X, int Y)
  144. {
  145. if (Shift.Contains(ssLeft))
  146. {
  147. dx += (mx-X);
  148. dy += (my-Y);
  149. }
  150. else
  151. {
  152. dx = 0;
  153. dy = 0;
  154. }
  155. mx = X;
  156. my = Y;
  157. }
  158. //---------------------------------------------------------------------------
  159. void __fastcall TForm1::AsyncTimer1Timer(TObject *Sender)
  160. {
  161. LabelFPS->Caption = GLSceneViewer1->FramesPerSecondText();
  162. GLSceneViewer1->ResetPerformanceMonitor();
  163. }
  164. //---------------------------------------------------------------------------
  165. void __fastcall TForm1::FormResize(TObject *Sender)
  166. {
  167. Camera->SceneScale = (float)Height/StartHeight;
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TForm1::GLSceneViewer1BeforeRender(TObject *Sender)
  171. {
  172. if (IsInitialized)
  173. exit;
  174. if (GL_ARB_multitexture && GL_ARB_vertex_program && GL_ARB_texture_env_dot3)
  175. ComboBox1->Items->Add("Dot3 Texture Combiner");
  176. if (GL_ARB_multitexture && GL_ARB_vertex_program && GL_ARB_fragment_program)
  177. {
  178. ComboBox1->Items->Add("Basic Fragment Program");
  179. if (GLSceneViewer1->Buffer->LimitOf[limNbTextureUnits] < 3)
  180. GLBumpShader1->SpecularMode = smOff;
  181. }
  182. IsInitialized = true;
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TForm1::ComboBox2Change(TObject *Sender)
  186. {
  187. switch (ComboBox2->ItemIndex)
  188. {
  189. case 0 : GLBumpShader1->SpecularMode = smOff; break;
  190. case 1 : GLBumpShader1->SpecularMode = smBlinn; break;
  191. case 2 : GLBumpShader1->SpecularMode = smPhong; break;
  192. default: ;
  193. }
  194. }
  195. //---------------------------------------------------------------------------