fSynthTerrainC.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fSynthTerrainC.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.BaseClasses"
  8. #pragma link "GLS.Cadencer"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "Stage.VectorGeometry"
  11. #pragma link "GLS.HeightData"
  12. #pragma link "GLS.Material"
  13. #pragma link "GLS.Objects"
  14. #pragma link "GLS.Scene"
  15. #pragma link "GLS.TerrainRenderer"
  16. #pragma link "GLS.SceneViewer"
  17. #pragma link "GLS.HeightData"
  18. #pragma link "GLS.HeightData"
  19. #pragma resource "*.dfm"
  20. TForm1* Form1;
  21. //---------------------------------------------------------------------------
  22. __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {}
  23. //---------------------------------------------------------------------------
  24. void __fastcall TForm1::FormCreate(TObject* Sender)
  25. {
  26. int i;
  27. TBitmap* bmp;
  28. // 8 MB height data cache
  29. // Note this is the data size in terms of elevation samples, it does not
  30. // take into account all the data required/allocated by the renderer
  31. GLCustomHDS->MaxPoolSize = 8 * 1024 * 1024;
  32. // Move camera starting point to an interesting hand-picked location
  33. DummyCube1->Position->X = 50;
  34. DummyCube1->Position->Z = 150;
  35. // Initial camera height offset (controled with pageUp/pageDown)
  36. FCamHeight = 20;
  37. // We build several basic 1D textures which are just color ramps
  38. // all use automatic texture mapping corodinates, in ObjectLinear method
  39. // (ie. texture coordinates for a vertex depend on that vertex coordinates)
  40. bmp = new TBitmap;
  41. bmp->PixelFormat = pf24bit;
  42. bmp->Width = 256;
  43. bmp->Height = 1;
  44. // Black-White ramp, autotexture maps to Z coordinate
  45. // This one changes with altitude, this is a quick way to obtain
  46. // altitude-dependant coloring
  47. for (i = 0; i < 255; i++)
  48. bmp->Canvas->Pixels[i][0] = RGB(i, i, i);
  49. GLMaterialLibrary1->AddTextureMaterial("BW", bmp)
  50. ->Material->Texture->MappingMode = tmmObjectLinear;
  51. GLMaterialLibrary1->AddTextureMaterial("BW", bmp)
  52. ->Material->Texture->MappingSCoordinates->AsVector =
  53. VectorMake(0, 0, 0.0001, 0);
  54. // Red, Blue map linearly to X and Y axis respectively
  55. for (i = 0; i < 255; i++)
  56. bmp->Canvas->Pixels[i][0] = RGB(i, 0, 0);
  57. GLMaterialLibrary1->AddTextureMaterial("Red", bmp)
  58. ->Material->Texture->MappingMode = tmmObjectLinear;
  59. GLMaterialLibrary1->AddTextureMaterial("Red", bmp)
  60. ->Material->Texture->MappingSCoordinates->AsVector =
  61. VectorMake(0.1, 0, 0, 0);
  62. for (i = 0; i < 255; i++)
  63. bmp->Canvas->Pixels[i][0] = RGB(0, 0, i);
  64. GLMaterialLibrary1->AddTextureMaterial("Blue", bmp)
  65. ->Material->Texture->MappingMode = tmmObjectLinear;
  66. GLMaterialLibrary1->AddTextureMaterial("Blue", bmp)
  67. ->Material->Texture->MappingSCoordinates->AsVector =
  68. VectorMake(0, 0.1, 0, 0);
  69. bmp->Free();
  70. TerrainRenderer1->MaterialLibrary = GLMaterialLibrary1;
  71. }
  72. //---------------------------------------------------------------------------
  73. //
  74. // The beef : this event does all the interesting elevation data stuff
  75. //
  76. void __fastcall TForm1::GLCustomHDSStartPreparingData(TGLHeightData* HeightData)
  77. {
  78. int x, y;
  79. Stage::Vectorgeometry::PByteArray rasterLine;
  80. TGLHeightDataType oldType;
  81. Byte b;
  82. float d, dy;
  83. HeightData->DataState = hdsPreparing;
  84. // retrieve data
  85. oldType = HeightData->DataType;
  86. HeightData->Allocate(hdtByte);
  87. // Cheap texture changed (32 is our tileSize = 2^5)
  88. // This basicly picks a texture for each tile depending on the tile's position
  89. switch ((((HeightData->XLeft ^ HeightData->YTop) << 5) && 3)) {
  90. case 0, 3:
  91. HeightData->MaterialName = "BW";
  92. break;
  93. case 1:
  94. HeightData->MaterialName = "Blue";
  95. break;
  96. case 2:
  97. HeightData->MaterialName = "Red";
  98. break;
  99. default:;
  100. }
  101. // 'Cheap' elevation data : this is just a formula z=f(x, y)
  102. for (y = HeightData->YTop; y < HeightData->YTop + HeightData->Size - 1; y++)
  103. {
  104. rasterLine = *HeightData->ByteRaster[y - HeightData->YTop];
  105. dy = y * y;
  106. for (x = HeightData->XLeft;
  107. x < HeightData->XLeft + HeightData->Size - 1; x++) {
  108. d = sqrt(x * x + dy);
  109. b = RoundInt(128 + 128 * Sin(d * 0.2) / (d * 0.1 + 1));
  110. *rasterLine[x - HeightData->XLeft] = b; /// * ???
  111. }
  112. }
  113. if (oldType != hdtByte)
  114. HeightData->DataType = oldType;
  115. }
  116. //---------------------------------------------------------------------------
  117. // Movement, mouse handling etc.
  118. void __fastcall TForm1::GLSceneViewer1MouseDown(
  119. TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  120. {
  121. mx = X;
  122. my = Y;
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall TForm1::GLSceneViewer1MouseMove(
  126. TObject* Sender, TShiftState Shift, int X, int Y)
  127. {
  128. if (Shift.Contains(ssLeft)) {
  129. GLCamera1->MoveAroundTarget(my - Y, mx - X);
  130. mx = X;
  131. my = Y;
  132. }
  133. }
  134. //---------------------------------------------------------------------------
  135. void __fastcall TForm1::Timer1Timer(TObject* Sender)
  136. {
  137. Caption =
  138. "Synthetic Terrain " +
  139. Format("%.1f FPS - %d", ARRAYOFCONST((GLSceneViewer1->FramesPerSecond(),
  140. TerrainRenderer1->LastTriangleCount)));
  141. GLSceneViewer1->ResetPerformanceMonitor();
  142. }
  143. //---------------------------------------------------------------------------
  144. void __fastcall TForm1::FormKeyPress(TObject* Sender, System::WideChar &Key)
  145. {
  146. switch (Key) {
  147. case '+':
  148. if (GLCamera1->DepthOfView < 4000) {
  149. GLCamera1->DepthOfView = GLCamera1->DepthOfView * 1.2;
  150. GLSceneViewer1->Buffer->FogEnvironment->FogEnd =
  151. GLSceneViewer1->Buffer->FogEnvironment->FogEnd * 1.2;
  152. GLSceneViewer1->Buffer->FogEnvironment->FogStart =
  153. GLSceneViewer1->Buffer->FogEnvironment->FogStart * 1.2;
  154. }
  155. break;
  156. case '-':
  157. if (GLCamera1->DepthOfView > 300) {
  158. GLCamera1->DepthOfView = GLCamera1->DepthOfView / 1.2;
  159. GLSceneViewer1->Buffer->FogEnvironment->FogEnd =
  160. GLSceneViewer1->Buffer->FogEnvironment->FogEnd / 1.2;
  161. GLSceneViewer1->Buffer->FogEnvironment->FogStart =
  162. GLSceneViewer1->Buffer->FogEnvironment->FogStart / 1.2;
  163. }
  164. break;
  165. case '*':
  166. if (TerrainRenderer1->CLODPrecision > 5)
  167. TerrainRenderer1->CLODPrecision =
  168. RoundInt(TerrainRenderer1->CLODPrecision * 0.8);
  169. break;
  170. case '/':
  171. if (TerrainRenderer1->CLODPrecision < 500)
  172. TerrainRenderer1->CLODPrecision =
  173. RoundInt(TerrainRenderer1->CLODPrecision * 1.2);
  174. break;
  175. case '8':
  176. if (TerrainRenderer1->QualityDistance > 40)
  177. TerrainRenderer1->QualityDistance =
  178. RoundInt(TerrainRenderer1->QualityDistance * 0.8);
  179. break;
  180. case '9':
  181. if (TerrainRenderer1->QualityDistance < 1000)
  182. TerrainRenderer1->QualityDistance =
  183. RoundInt(TerrainRenderer1->QualityDistance * 1.2);
  184. break;
  185. default:;
  186. }
  187. Key = 0x0;
  188. }
  189. //---------------------------------------------------------------------------
  190. void __fastcall TForm1::GLCadencer1Progress(
  191. TObject* Sender, const double deltaTime, const double newTime)
  192. {
  193. float speed;
  194. // handle keypresses
  195. if (IsKeyDown(VK_SHIFT))
  196. speed = 5 * deltaTime;
  197. else
  198. speed = deltaTime;
  199. if (IsKeyDown(VK_RIGHT))
  200. DummyCube1->Translate(
  201. GLCamera1->Position->Z * speed, 0, -GLCamera1->Position->X * speed);
  202. if (IsKeyDown(VK_LEFT))
  203. DummyCube1->Translate(
  204. -GLCamera1->Position->Z * speed, 0, GLCamera1->Position->X * speed);
  205. if (IsKeyDown(VK_UP))
  206. DummyCube1->Translate(-GLCamera1->Position->X * speed, 0,
  207. -GLCamera1->Position->Z * speed);
  208. if (IsKeyDown(VK_DOWN))
  209. DummyCube1->Translate(
  210. GLCamera1->Position->X * speed, 0, GLCamera1->Position->Z * speed);
  211. if (IsKeyDown(VK_PRIOR))
  212. FCamHeight = FCamHeight + 10 * speed;
  213. if (IsKeyDown(VK_NEXT))
  214. FCamHeight = FCamHeight - 10 * speed;
  215. if (IsKeyDown(VK_ESCAPE))
  216. Close();
  217. // don't drop through terrain!
  218. DummyCube1->Position->Y =
  219. TerrainRenderer1->InterpolatedHeight(DummyCube1->Position->AsVector) +
  220. FCamHeight;
  221. }
  222. //---------------------------------------------------------------------------