Unit1.cpp 6.5 KB

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