fTreeC.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <stdlib.h>
  4. #pragma hdrstop
  5. #include "fTreeC.h"
  6. #include "jpeg.hpp"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "GLS.Material"
  10. #pragma link "GLS.FileTGA"
  11. #pragma resource "*.dfm"
  12. TForm1 *Form1;
  13. //---------------------------------------------------------------------------
  14. void TForm1::AlignControlsToTree()
  15. {
  16. TrackBar1->Position = GLTree1->Depth;
  17. TrackBar2->Position = RoundInt(GLTree1->BranchTwist);
  18. TrackBar3->Position = RoundInt(GLTree1->BranchAngle * 100);
  19. TrackBar4->Position = RoundInt(GLTree1->BranchAngleBias * 100);
  20. TrackBar5->Position = RoundInt(GLTree1->BranchSize * 10);
  21. TrackBar6->Position = RoundInt(GLTree1->BranchRadius * 25);
  22. TrackBar7->Position = RoundInt(GLTree1->BranchNoise * 100);
  23. TrackBar8->Position = RoundInt(GLTree1->LeafSize * 100);
  24. TrackBar9->Position = RoundInt(GLTree1->LeafThreshold * 100);
  25. TrackBar10->Position = GLTree1->BranchFacets;
  26. Edit1->Text = IntToStr(GLTree1->Seed);
  27. CheckBox1->Checked = GLTree1->CentralLeader;
  28. TrackBar11->Position = RoundInt(GLTree1->CentralLeaderBias * 100);
  29. }
  30. void TForm1::NewTree()
  31. {
  32. delete GLTree1;
  33. GLTree1 = (TGLTree *) GLScene1->Objects->AddNewChild(__classid(TGLTree));
  34. GLTree1->MaterialLibrary = GLMaterialLibrary1;
  35. GLTree1->LeafMaterialName = "LeafFront";
  36. GLTree1->LeafBackMaterialName = "LeafBack";
  37. GLTree1->BranchMaterialName = "Branch";
  38. GLTree1->Depth = 8;
  39. GLTree1->LeafSize = 0.2;
  40. GLTree1->BranchRadius = 0.08;
  41. GLTree1->BranchNoise = 0.5;
  42. Randomize();
  43. GLTree1->Seed = RoundInt((2 * Random() - 1) * (MaxInt - 1));
  44. AlignControlsToTree();
  45. }
  46. __fastcall TForm1::TForm1(TComponent * Owner):TForm(Owner)
  47. {
  48. SetGLSceneMediaDir();
  49. // Set up default textures
  50. TGLLibMaterial *lm =
  51. GLMaterialLibrary1->AddTextureMaterial("LeafFront", "maple_multi.tga", true);
  52. lm->Material->BlendingMode = bmAlphaTest50;
  53. lm->Material->Texture->TextureMode = tmModulate;
  54. lm->Material->Texture->TextureFormat = tfRGBA;
  55. lm = GLMaterialLibrary1->AddTextureMaterial("LeafBack", "maple_multi.tga", true);
  56. lm->Material->BlendingMode = bmAlphaTest50;
  57. lm->Material->Texture->TextureMode = tmModulate;
  58. lm->Material->Texture->TextureFormat = tfRGBA;
  59. lm = GLMaterialLibrary1->AddTextureMaterial("Branch", "zbark_016.jpg", true);
  60. lm->Material->Texture->TextureMode = tmModulate;
  61. // Set a up a tree
  62. NewTree();
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject * Sender,
  66. TMouseButton Button,
  67. TShiftState Shift, int X, int Y)
  68. {
  69. mx = X;
  70. my = Y;
  71. }
  72. //---------------------------------------------------------------------------
  73. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject * Sender,
  74. TShiftState Shift, int X, int Y)
  75. {
  76. if(Shift.Contains(ssLeft))
  77. GLCamera1->MoveAroundTarget(my - Y, mx - X);
  78. else if(Shift.Contains(ssRight))
  79. GLCamera1->AdjustDistanceToTarget(1 + (my - Y) * 0.01);
  80. mx = X;
  81. my = Y;
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TForm1::TrackBar1Change(TObject * Sender)
  85. {
  86. GLTree1->Depth = (float) TrackBar1->Position;
  87. }
  88. //---------------------------------------------------------------------------
  89. void __fastcall TForm1::TrackBar2Change(TObject * Sender)
  90. {
  91. GLTree1->BranchTwist = (float)TrackBar2->Position;
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TForm1::TrackBar3Change(TObject * Sender)
  95. {
  96. GLTree1->BranchAngle = (float)TrackBar3->Position / 100;
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm1::TrackBar4Change(TObject * Sender)
  100. {
  101. GLTree1->BranchAngleBias = (float)TrackBar4->Position / 100;
  102. }
  103. //---------------------------------------------------------------------------
  104. void __fastcall TForm1::TrackBar5Change(TObject * Sender)
  105. {
  106. GLTree1->BranchSize = (float)TrackBar5->Position / 10;
  107. }
  108. //---------------------------------------------------------------------------
  109. void __fastcall TForm1::TrackBar6Change(TObject * Sender)
  110. {
  111. GLTree1->BranchRadius = (float)TrackBar6->Position / 25;
  112. }
  113. //---------------------------------------------------------------------------
  114. void __fastcall TForm1::TrackBar7Change(TObject * Sender)
  115. {
  116. GLTree1->BranchNoise = (float)TrackBar7->Position / 100;
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TForm1::TrackBar8Change(TObject * Sender)
  120. {
  121. GLTree1->LeafSize = (float)TrackBar8->Position / 100;
  122. }
  123. //---------------------------------------------------------------------------
  124. void __fastcall TForm1::TrackBar9Change(TObject * Sender)
  125. {
  126. GLTree1->LeafThreshold = (float)TrackBar9->Position / 100;
  127. }
  128. //---------------------------------------------------------------------------
  129. void __fastcall TForm1::TrackBar10Change(TObject * Sender)
  130. {
  131. GLTree1->BranchFacets = (float)TrackBar10->Position;
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TForm1::Button1Click(TObject * Sender)
  135. {
  136. try
  137. {
  138. GLTree1->Seed = StrToInt(Edit1->Text);
  139. }
  140. catch(Exception * E)
  141. {
  142. MessageDlg("Invalid input", mtWarning, TMsgDlgButtons() << mbOK, 0);
  143. Edit1->Text = IntToStr(GLTree1->Seed);
  144. }
  145. }
  146. //---------------------------------------------------------------------------
  147. void __fastcall TForm1::CheckBox1Click(TObject * Sender)
  148. {
  149. GLTree1->CentralLeader = CheckBox1->Checked;
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TForm1::TrackBar11Change(TObject * Sender)
  153. {
  154. GLTree1->CentralLeaderBias = (float)TrackBar11->Position / 100;
  155. }
  156. //---------------------------------------------------------------------------
  157. void __fastcall TForm1::NewTree1Click(TObject * Sender)
  158. {
  159. NewTree();
  160. }
  161. //---------------------------------------------------------------------------
  162. void __fastcall TForm1::LoadTree1Click(TObject * Sender)
  163. {
  164. if(!OpenDialog1->Execute())
  165. return;
  166. GLTree1->LoadFromFile(OpenDialog1->FileName);
  167. AlignControlsToTree();
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TForm1::SaveTree1Click(TObject * Sender)
  171. {
  172. if(!SaveDialog1->Execute())
  173. return;
  174. GLTree1->SaveToFile(SaveDialog1->FileName);
  175. }
  176. //---------------------------------------------------------------------------
  177. void __fastcall TForm1::ExportMesh1Click(TObject * Sender)
  178. {
  179. if(!SaveDialog2->Execute())
  180. return;
  181. GLTree1->BuildMesh(GLFreeForm1);
  182. GLFreeForm1->SaveToFile(SaveDialog2->FileName);
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TForm1::ExportMaterialLibrary1Click(TObject * Sender)
  186. {
  187. if(!SaveDialog3->Execute())
  188. return;
  189. GLMaterialLibrary1->SaveToFile(SaveDialog3->FileName);
  190. }
  191. //---------------------------------------------------------------------------
  192. void __fastcall TForm1::Exit1Click(TObject * Sender)
  193. {
  194. Form1->Close();
  195. }
  196. //---------------------------------------------------------------------------
  197. void __fastcall TForm1::LeafFrontTexture1Click(TObject * Sender)
  198. {
  199. if(!OpenPictureDialog1->Execute())
  200. return;
  201. TGLLibMaterial *lm =
  202. GLMaterialLibrary1->Materials->GetLibMaterialByName("LeafFront");
  203. lm->Material->Texture->Image->LoadFromFile(OpenPictureDialog1->FileName);
  204. GLTree1->StructureChanged();
  205. }
  206. //---------------------------------------------------------------------------
  207. void __fastcall TForm1::LeafBackTexture1Click(TObject * Sender)
  208. {
  209. if(!OpenPictureDialog1->Execute())
  210. return;
  211. TGLLibMaterial *lm =
  212. GLMaterialLibrary1->Materials->GetLibMaterialByName("LeafBack");
  213. lm->Material->Texture->Image->LoadFromFile(OpenPictureDialog1->FileName);
  214. GLTree1->StructureChanged();
  215. }
  216. //---------------------------------------------------------------------------
  217. void __fastcall TForm1::BranchTexture1Click(TObject * Sender)
  218. {
  219. if(!OpenPictureDialog1->Execute())
  220. return;
  221. TGLLibMaterial *lm =
  222. GLMaterialLibrary1->Materials->GetLibMaterialByName("Branch");
  223. lm->Material->Texture->Image->LoadFromFile(OpenPictureDialog1->FileName);
  224. GLTree1->StructureChanged();
  225. }
  226. //---------------------------------------------------------------------------