Unit1.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLMaterial"
  8. #pragma resource "*.dfm"
  9. TForm1 *Form1;
  10. //---------------------------------------------------------------------------
  11. __fastcall TForm1::TForm1(TComponent * Owner):TForm(Owner)
  12. {
  13. SetGLSceneMediaDir();
  14. // 8 MB height data cache
  15. // Note this is the data size in terms of elevation samples, it does not
  16. // take into account all the data required/allocated by the renderer
  17. GLBitmapHDS1->MaxPoolSize = 8 * 1024 * 1024;
  18. // specify height map data
  19. GLBitmapHDS1->Picture->LoadFromFile("terrain.bmp");
  20. // load the texture maps
  21. GLMaterialLibrary1->LibMaterialByName("details")->Material->Texture->Image->
  22. LoadFromFile("detailmap.jpg");
  23. SPSun->Material->Texture->Image->LoadFromFile("flare1.bmp");
  24. // apply texture map scale (our heightmap size is 256)
  25. TerrainRenderer1->TilesPerTexture = 1; //256/TerrainRenderer1.TileSize;
  26. TerrainRenderer1->MaterialLibrary = GLMaterialLibrary1;
  27. // Could've been done at design time, but then it hurts the eyes ;)
  28. GLSceneViewer1->Buffer->BackgroundColor = clWhite;
  29. // Initial camera height offset (controled with pageUp/pageDown)
  30. FCamHeight = 10;
  31. // initialize intensity texture
  32. TBIntensityChange(this);
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TForm1::FormShow(TObject * Sender)
  36. {
  37. TBSubSamplingChange(this);
  38. }
  39. //---------------------------------------------------------------------------
  40. void __fastcall TForm1::GLBumpmapHDS1NewTilePrepared(TGLBumpmapHDS * Sender,
  41. TGLHeightData * heightData,
  42. TGLLibMaterial *
  43. normalMapMaterial)
  44. {
  45. Glvectorgeometry::TVector n;
  46. heightData->MaterialName = normalMapMaterial->Name;
  47. normalMapMaterial->Texture2Name = "contrast";
  48. normalMapMaterial->Shader = GLTexCombineShader1;
  49. normalMapMaterial->Material->MaterialOptions =
  50. normalMapMaterial->Material->MaterialOptions << moNoLighting;
  51. n = VectorNormalize(SPSun->AbsolutePosition);
  52. ScaleVector(n, 0.5);
  53. n.Y = -n.Y;
  54. n.Z = -n.Z;
  55. AddVector(n, 0.5);
  56. normalMapMaterial->Material->FrontProperties->Diffuse->Color = n;
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TForm1::GLCadencer1Progress(TObject * Sender,
  60. const double deltaTime,
  61. const double newTime)
  62. {
  63. float speed;
  64. // handle keypresses
  65. if(IsKeyDown(VK_SHIFT))
  66. speed = 5 * deltaTime;
  67. else
  68. speed = deltaTime;
  69. TGLCoordinates *p = GLCamera1->Position;
  70. if(IsKeyDown(VK_UP))
  71. DummyCube1->Translate(-p->X * speed, 0, -p->Z * speed);
  72. if(IsKeyDown(VK_DOWN))
  73. DummyCube1->Translate(p->X * speed, 0, p->Z * speed);
  74. if(IsKeyDown(VK_LEFT))
  75. DummyCube1->Translate(-p->Z * speed, 0, p->X * speed);
  76. if(IsKeyDown(VK_RIGHT))
  77. DummyCube1->Translate(p->Z * speed, 0, -p->X * speed);
  78. if(IsKeyDown(VK_PRIOR))
  79. FCamHeight = FCamHeight + 10 * speed;
  80. if(IsKeyDown(VK_NEXT))
  81. FCamHeight = FCamHeight - 10 * speed;
  82. if(IsKeyDown(VK_ESCAPE))
  83. Close();
  84. // don't drop through terrain!
  85. DummyCube1->Position->Y =
  86. TerrainRenderer1->InterpolatedHeight(DummyCube1->Position->AsVector) +
  87. FCamHeight;
  88. }
  89. //---------------------------------------------------------------------------
  90. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject * Sender,
  91. TMouseButton Button,
  92. TShiftState Shift, int X, int Y)
  93. {
  94. GLSceneViewer1->SetFocus();
  95. mx = X;
  96. my = Y;
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject * Sender,
  100. TShiftState Shift, int X, int Y)
  101. {
  102. if(Shift.Contains(ssLeft))
  103. {
  104. GLCamera1->MoveAroundTarget((my - Y) * 0.5, (mx - X) * 0.5);
  105. my = Y;
  106. mx = X;
  107. }
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TForm1::Timer1Timer(TObject * Sender)
  111. {
  112. Caption = GLSceneViewer1->FramesPerSecondText(1);
  113. GLSceneViewer1->ResetPerformanceMonitor();
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall TForm1::FormKeyPress(TObject * Sender, char &Key)
  117. {
  118. TGLMaterial *fp;
  119. TGLFogEnvironment *fe;
  120. switch (Key)
  121. {
  122. case 'w':
  123. case 'W':
  124. fp = GLMaterialLibrary1->Materials->Items[0]->Material;
  125. if(fp->PolygonMode == pmLines)
  126. fp->PolygonMode = pmFill;
  127. else
  128. fp->PolygonMode = pmLines;
  129. break;
  130. case '+':
  131. if(GLCamera1->DepthOfView < 2000.0)
  132. {
  133. GLCamera1->DepthOfView = GLCamera1->DepthOfView * 1.2;
  134. fe = GLSceneViewer1->Buffer->FogEnvironment;
  135. fe->FogEnd = fe->FogEnd * 1.2;
  136. fe->FogStart = fe->FogStart * 1.2;
  137. }
  138. break;
  139. case '-':
  140. if(GLCamera1->DepthOfView > 300)
  141. {
  142. GLCamera1->DepthOfView = GLCamera1->DepthOfView / 1.2;
  143. fe = GLSceneViewer1->Buffer->FogEnvironment;
  144. fe->FogEnd = fe->FogEnd / 1.2;
  145. fe->FogStart = fe->FogStart / 1.2;
  146. }
  147. break;
  148. case '*':
  149. if(TerrainRenderer1->CLODPrecision > 10)
  150. TerrainRenderer1->CLODPrecision =
  151. Round(TerrainRenderer1->CLODPrecision * 0.8);
  152. break;
  153. case '/':
  154. if(TerrainRenderer1->CLODPrecision < 1000)
  155. TerrainRenderer1->CLODPrecision =
  156. Round(TerrainRenderer1->CLODPrecision * 1.2);
  157. break;
  158. case 'l':
  159. GLLensFlare->Visible = (!GLLensFlare->Visible) && SPSun->Visible;
  160. break;
  161. }
  162. Key = '\0';
  163. }
  164. //---------------------------------------------------------------------------
  165. void __fastcall TForm1::GLSceneViewer1BeforeRender(TObject * Sender)
  166. {
  167. GLLensFlare->PreRender((TGLSceneBuffer *) Sender);
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TForm1::TBSubSamplingChange(TObject * Sender)
  171. {
  172. GLBumpmapHDS1->SubSampling = (1 << TBSubSampling->Position);
  173. String s;
  174. s.printf(L"(%d) -> BumpMaps are %dx",
  175. GLBumpmapHDS1->SubSampling,
  176. TerrainRenderer1->TileSize / GLBumpmapHDS1->SubSampling);
  177. LASubFactor->Caption = s;
  178. // don't leave the focus to the trackbar, otherwise it'll keep some keystrokes
  179. // for itself, like the arrow keys
  180. SetFocus();
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TForm1::TBIntensityChange(TObject * Sender)
  184. {
  185. int i;
  186. Graphics::TBitmap * bmp;
  187. TGLMaterial *m = GLMaterialLibrary1->LibMaterialByName("contrast")->Material;
  188. bmp = new Graphics::TBitmap;
  189. try
  190. {
  191. bmp->PixelFormat = pf24bit;
  192. bmp->Width = 1;
  193. bmp->Height = 1;
  194. i = 255;
  195. bmp->Canvas->Pixels[0][0] = (Graphics::TColor) RGB(i, i, i);
  196. m->Texture->Image->Assign(bmp);
  197. }
  198. __finally
  199. {
  200. delete bmp;
  201. }
  202. i = (TBIntensity->Position * 255) / 100;
  203. m->Texture->EnvColor->AsWinColor = (Graphics::TColor) RGB(i, i, i);
  204. LABumpIntensity->Caption = IntToStr(TBIntensity->Position) + " %";
  205. }
  206. //---------------------------------------------------------------------------