fTreeC.cpp 8.5 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. TFileName Path = GetCurrentAssetPath();
  49. SetCurrentDir(Path + "\\texture");
  50. // Set up default textures
  51. TGLLibMaterial* lm = GLMaterialLibrary1->AddTextureMaterial(
  52. "LeafFront", "maple_multi.tga", true);
  53. lm->Material->BlendingMode = bmAlphaTest50;
  54. lm->Material->Texture->TextureMode = tmModulate;
  55. lm->Material->Texture->TextureFormat = tfRGBA;
  56. lm = GLMaterialLibrary1->AddTextureMaterial(
  57. "LeafBack", "maple_multi.tga", true);
  58. lm->Material->BlendingMode = bmAlphaTest50;
  59. lm->Material->Texture->TextureMode = tmModulate;
  60. lm->Material->Texture->TextureFormat = tfRGBA;
  61. lm =
  62. GLMaterialLibrary1->AddTextureMaterial("Branch", "zbark_016.jpg", true);
  63. lm->Material->Texture->TextureMode = tmModulate;
  64. // Set a up a tree
  65. NewTree();
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TForm1::GLSceneViewer1MouseDown(
  69. TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  70. {
  71. mx = X;
  72. my = Y;
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TForm1::GLSceneViewer1MouseMove(
  76. TObject* Sender, TShiftState Shift, int X, int Y)
  77. {
  78. if (Shift.Contains(ssLeft))
  79. GLCamera1->MoveAroundTarget(my - Y, mx - X);
  80. else if (Shift.Contains(ssRight))
  81. GLCamera1->AdjustDistanceToTarget(1 + (my - Y) * 0.01);
  82. mx = X;
  83. my = Y;
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TForm1::TrackBar1Change(TObject* Sender)
  87. {
  88. GLTree1->Depth = (float)TrackBar1->Position;
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TForm1::TrackBar2Change(TObject* Sender)
  92. {
  93. GLTree1->BranchTwist = (float)TrackBar2->Position;
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TForm1::TrackBar3Change(TObject* Sender)
  97. {
  98. GLTree1->BranchAngle = (float)TrackBar3->Position / 100;
  99. }
  100. //---------------------------------------------------------------------------
  101. void __fastcall TForm1::TrackBar4Change(TObject* Sender)
  102. {
  103. GLTree1->BranchAngleBias = (float)TrackBar4->Position / 100;
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TForm1::TrackBar5Change(TObject* Sender)
  107. {
  108. GLTree1->BranchSize = (float)TrackBar5->Position / 10;
  109. }
  110. //---------------------------------------------------------------------------
  111. void __fastcall TForm1::TrackBar6Change(TObject* Sender)
  112. {
  113. GLTree1->BranchRadius = (float)TrackBar6->Position / 25;
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall TForm1::TrackBar7Change(TObject* Sender)
  117. {
  118. GLTree1->BranchNoise = (float)TrackBar7->Position / 100;
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TForm1::TrackBar8Change(TObject* Sender)
  122. {
  123. GLTree1->LeafSize = (float)TrackBar8->Position / 100;
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TForm1::TrackBar9Change(TObject* Sender)
  127. {
  128. GLTree1->LeafThreshold = (float)TrackBar9->Position / 100;
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TForm1::TrackBar10Change(TObject* Sender)
  132. {
  133. GLTree1->BranchFacets = (float)TrackBar10->Position;
  134. }
  135. //---------------------------------------------------------------------------
  136. void __fastcall TForm1::Button1Click(TObject* Sender)
  137. {
  138. try {
  139. GLTree1->Seed = StrToInt(Edit1->Text);
  140. } catch (Exception* E) {
  141. MessageDlg("Invalid input", mtWarning, TMsgDlgButtons() << mbOK, 0);
  142. Edit1->Text = IntToStr(GLTree1->Seed);
  143. }
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TForm1::CheckBox1Click(TObject* Sender)
  147. {
  148. GLTree1->CentralLeader = CheckBox1->Checked;
  149. }
  150. //---------------------------------------------------------------------------
  151. void __fastcall TForm1::TrackBar11Change(TObject* Sender)
  152. {
  153. GLTree1->CentralLeaderBias = (float)TrackBar11->Position / 100;
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TForm1::NewTree1Click(TObject* Sender)
  157. {
  158. NewTree();
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TForm1::LoadTree1Click(TObject* Sender)
  162. {
  163. if (!OpenDialog1->Execute())
  164. return;
  165. GLTree1->LoadFromFile(OpenDialog1->FileName);
  166. AlignControlsToTree();
  167. }
  168. //---------------------------------------------------------------------------
  169. void __fastcall TForm1::SaveTree1Click(TObject* Sender)
  170. {
  171. if (!SaveDialog1->Execute())
  172. return;
  173. GLTree1->SaveToFile(SaveDialog1->FileName);
  174. }
  175. //---------------------------------------------------------------------------
  176. void __fastcall TForm1::ExportMesh1Click(TObject* Sender)
  177. {
  178. if (!SaveDialog2->Execute())
  179. return;
  180. GLTree1->BuildMesh(GLFreeForm1);
  181. GLFreeForm1->SaveToFile(SaveDialog2->FileName);
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall TForm1::ExportMaterialLibrary1Click(TObject* Sender)
  185. {
  186. if (!SaveDialog3->Execute())
  187. return;
  188. GLMaterialLibrary1->SaveToFile(SaveDialog3->FileName);
  189. }
  190. //---------------------------------------------------------------------------
  191. void __fastcall TForm1::Exit1Click(TObject* Sender)
  192. {
  193. Form1->Close();
  194. }
  195. //---------------------------------------------------------------------------
  196. void __fastcall TForm1::LeafFrontTexture1Click(TObject* Sender)
  197. {
  198. if (!OpenPictureDialog1->Execute())
  199. return;
  200. TGLLibMaterial* lm =
  201. GLMaterialLibrary1->Materials->GetLibMaterialByName("LeafFront");
  202. lm->Material->Texture->Image->LoadFromFile(OpenPictureDialog1->FileName);
  203. GLTree1->StructureChanged();
  204. }
  205. //---------------------------------------------------------------------------
  206. void __fastcall TForm1::LeafBackTexture1Click(TObject* Sender)
  207. {
  208. if (!OpenPictureDialog1->Execute())
  209. return;
  210. TGLLibMaterial* lm =
  211. GLMaterialLibrary1->Materials->GetLibMaterialByName("LeafBack");
  212. lm->Material->Texture->Image->LoadFromFile(OpenPictureDialog1->FileName);
  213. GLTree1->StructureChanged();
  214. }
  215. //---------------------------------------------------------------------------
  216. void __fastcall TForm1::BranchTexture1Click(TObject* Sender)
  217. {
  218. if (!OpenPictureDialog1->Execute())
  219. return;
  220. TGLLibMaterial* lm =
  221. GLMaterialLibrary1->Materials->GetLibMaterialByName("Branch");
  222. lm->Material->Texture->Image->LoadFromFile(OpenPictureDialog1->FileName);
  223. GLTree1->StructureChanged();
  224. }
  225. //---------------------------------------------------------------------------