Unit1.cpp 7.9 KB

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