fSynthTerrainC.cpp 7.7 KB

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