fTerrainC.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <GLS.Keyboard.hpp>
  4. #include <stdlib.h>
  5. #pragma hdrstop
  6. #include "fTerrainC.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "GLS.LensFlare"
  10. #pragma link "GLS.VectorGeometry"
  11. #pragma link "Sounds.BASS"
  12. #pragma link "GLS.SoundManager"
  13. #pragma link "GLS.SceneViewer"
  14. #pragma link "GLS.SkyDome"
  15. #pragma link "GLS.BitmapFont"
  16. #pragma link "GLS.HUDObjects"
  17. #pragma link "GLS.Texture"
  18. #pragma link "GLS.Cadencer"
  19. #pragma link "GLS.HeightData"
  20. #pragma link "GLS.Objects"
  21. #pragma link "GLS.TerrainRenderer"
  22. #pragma link "GLS.Scene"
  23. #pragma link "GLS.Keyboard"
  24. #pragma link "GLS.BaseClasses"
  25. #pragma link "GLS.Coordinates"
  26. #pragma link "GLS.Material"
  27. #pragma link "GLS.FileMP3"
  28. #pragma link "GLS.SoundManager"
  29. #pragma resource "*.dfm"
  30. TForm1* Form1;
  31. float random(void)
  32. {
  33. return (float)(rand() & 0x1FFF) / (float)0x1FFF;
  34. }
  35. //---------------------------------------------------------------------------
  36. __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
  37. {
  38. TFileName Path = GetCurrentAssetPath();
  39. SetCurrentDir(Path + "\\texture");
  40. // 8 MB height data cache
  41. // Note this is the data size in terms of elevation samples, it does not
  42. // take into account all the data required/allocated by the renderer
  43. GLBitmapHDS1->MaxPoolSize = 8 * 1024 * 1024;
  44. // specify height map data
  45. GLBitmapHDS1->Picture->LoadFromFile("terrain.bmp");
  46. // load the texture maps
  47. GLMaterialLibrary1->Materials->Items[0]
  48. ->Material->Texture->Image->LoadFromFile("snow512.jpg");
  49. GLMaterialLibrary1->Materials->Items[1]
  50. ->Material->Texture->Image->LoadFromFile("detailmap.jpg");
  51. SPMoon->Material->Texture->Image->LoadFromFile("moon.bmp");
  52. SPSun->Material->Texture->Image->LoadFromFile("flare1.bmp");
  53. // apply texture map scale (our heightmap size is 256)
  54. TerrainRenderer1->TilesPerTexture = 256.0 / TerrainRenderer1->TileSize;
  55. // Load Bitmap Font
  56. SetCurrentDir(Path + "\\font");
  57. BitmapFont1->Glyphs->LoadFromFile("darkgold_font.bmp");
  58. // Load and setup sound samples
  59. // Load and setup sound samples
  60. SetCurrentDir(Path + "\\audio");
  61. GLSoundLibrary->Samples->Add()->LoadFromFile("ChillyWind.mp3");
  62. GLSoundLibrary->Samples->Add()->LoadFromFile("howl.mp3");
  63. // Could've been done at design time, but then it hurts the eyes ;)
  64. GLSceneViewer1->Buffer->BackgroundColor = clWhite;
  65. // Move camera starting point to an interesting hand-picked location
  66. DummyCube1->Position->X = 570;
  67. DummyCube1->Position->Z = -385;
  68. DummyCube1->Turn(90);
  69. // Initial camera height offset (controled with pageUp/pageDown)
  70. FCamHeight = 10;
  71. randomize();
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TForm1::GLCadencer1Progress(
  75. TObject* Sender, const double deltaTime, const double newTime)
  76. {
  77. float speed;
  78. // handle keypresses
  79. if (IsKeyDown(VK_SHIFT))
  80. speed = 5 * deltaTime;
  81. else
  82. speed = deltaTime;
  83. TGLCoordinates* c = GLCamera1->Position;
  84. if (IsKeyDown(VK_UP))
  85. DummyCube1->Translate(c->Z * speed, 0, -c->X * speed);
  86. if (IsKeyDown(VK_DOWN))
  87. DummyCube1->Translate(-c->Z * speed, 0, c->X * speed);
  88. if (IsKeyDown(VK_LEFT))
  89. DummyCube1->Translate(-c->X * speed, 0, -c->Z * speed);
  90. if (IsKeyDown(VK_RIGHT))
  91. DummyCube1->Translate(c->X * speed, 0, c->Z * speed);
  92. if (IsKeyDown(VK_PRIOR))
  93. FCamHeight = FCamHeight + 10 * speed;
  94. if (IsKeyDown(VK_NEXT))
  95. FCamHeight = FCamHeight - 10 * speed;
  96. if (IsKeyDown(VK_ESCAPE))
  97. Close();
  98. // don't drop through terrain!
  99. DummyCube1->Position->Y =
  100. TerrainRenderer1->InterpolatedHeight(DummyCube1->Position->AsVector) +
  101. FCamHeight;
  102. }
  103. //---------------------------------------------------------------------------
  104. void __fastcall TForm1::GLSceneViewer1MouseDown(
  105. TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  106. {
  107. my = Y;
  108. mx = X;
  109. }
  110. //---------------------------------------------------------------------------
  111. void __fastcall TForm1::GLSceneViewer1MouseMove(
  112. TObject* Sender, TShiftState Shift, int X, int Y)
  113. {
  114. if (Shift.Contains(ssLeft)) {
  115. GLCamera1->MoveAroundTarget((my - Y) * 0.5, (mx - X) * 0.5);
  116. mx = X;
  117. my = Y;
  118. }
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TForm1::Timer1Timer(TObject* Sender)
  122. {
  123. String s;
  124. // s.printf("%.1f FPS - %d", GLSceneViewer1->FramesPerSecond(),
  125. // TerrainRenderer1->LastTriangleCount());
  126. HUDText1->Text = s;
  127. GLSceneViewer1->ResetPerformanceMonitor();
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TForm1::FormKeyPress(TObject* Sender, char &Key)
  131. {
  132. TGLMaterial* fp;
  133. TGLFogEnvironment* fe;
  134. TGIFColor Color;
  135. switch (Key) {
  136. case 'w':
  137. case 'W':
  138. fp = GLMaterialLibrary1->Materials->Items[0]->Material;
  139. if (fp->PolygonMode == pmLines)
  140. fp->PolygonMode = pmFill;
  141. else
  142. fp->PolygonMode = pmLines;
  143. break;
  144. case '+':
  145. if (GLCamera1->DepthOfView < 2000) {
  146. GLCamera1->DepthOfView = GLCamera1->DepthOfView * 1.2;
  147. fe = GLSceneViewer1->Buffer->FogEnvironment;
  148. fe->FogEnd = fe->FogEnd * 1.2;
  149. fe->FogStart = fe->FogStart * 1.2;
  150. }
  151. break;
  152. case '-':
  153. if (GLCamera1->DepthOfView > 300) {
  154. GLCamera1->DepthOfView = GLCamera1->DepthOfView / 1.2;
  155. fe = GLSceneViewer1->Buffer->FogEnvironment;
  156. fe->FogEnd = fe->FogEnd / 1.2;
  157. fe->FogStart = fe->FogStart / 1.2;
  158. }
  159. break;
  160. case '*':
  161. if (TerrainRenderer1->CLODPrecision > 20)
  162. TerrainRenderer1->CLODPrecision =
  163. RoundInt(TerrainRenderer1->CLODPrecision * 0.8);
  164. break;
  165. case '/':
  166. if (TerrainRenderer1->CLODPrecision < 1000)
  167. TerrainRenderer1->CLODPrecision =
  168. RoundInt(TerrainRenderer1->CLODPrecision * 1.2);
  169. break;
  170. case '8':
  171. if (TerrainRenderer1->QualityDistance > 40)
  172. TerrainRenderer1->QualityDistance =
  173. RoundInt(TerrainRenderer1->QualityDistance * 0.8);
  174. break;
  175. case '9':
  176. if (TerrainRenderer1->QualityDistance < 1000)
  177. TerrainRenderer1->QualityDistance =
  178. RoundInt(TerrainRenderer1->QualityDistance * 1.2);
  179. break;
  180. case 'n':
  181. case 'N':
  182. if (SkyDome1->Stars->Count == 0) {
  183. // turn on 'night' mode
  184. Color.Red = 0;
  185. Color.Green = 0;
  186. Color.Blue = 8;
  187. SkyDome1->Bands->Items[0]->StopColor->AsWinColor =
  188. TGIFColorMap::RGB2Color(Color);
  189. Color.Red = 0;
  190. Color.Green = 0;
  191. Color.Blue = 0;
  192. SkyDome1->Bands->Items[0]->StartColor->AsWinColor =
  193. TGIFColorMap::RGB2Color(Color);
  194. Color.Red = 0;
  195. Color.Green = 0;
  196. Color.Blue = 16;
  197. SkyDome1->Bands->Items[1]->StopColor->AsWinColor =
  198. TGIFColorMap::RGB2Color(Color);
  199. Color.Red = 0;
  200. Color.Green = 0;
  201. Color.Blue = 8;
  202. SkyDome1->Bands->Items[1]->StartColor->AsWinColor =
  203. TGIFColorMap::RGB2Color(Color);
  204. SkyDome1->Stars->AddRandomStars(
  205. 700, clWhite, True); // many white stars
  206. Color.Red = 255;
  207. Color.Green = 100;
  208. Color.Blue = 100;
  209. SkyDome1->Stars->AddRandomStars(100,
  210. TGIFColorMap::RGB2Color(Color), True); // some redish ones
  211. Color.Red = 100;
  212. Color.Green = 100;
  213. Color.Blue = 255;
  214. SkyDome1->Stars->AddRandomStars(100,
  215. TGIFColorMap::RGB2Color(Color), True); // some blueish ones
  216. Color.Red = 255;
  217. Color.Green = 255;
  218. Color.Blue = 100;
  219. SkyDome1->Stars->AddRandomStars(100,
  220. TGIFColorMap::RGB2Color(Color),
  221. True); // some yellowish ones
  222. GLSceneViewer1->Buffer->BackgroundColor = clBlack;
  223. fe = GLSceneViewer1->Buffer->FogEnvironment;
  224. fe->FogColor->AsWinColor = clBlack;
  225. fe->FogStart =
  226. -fe->FogStart; // Fog is used to make things darker
  227. SPMoon->Visible = True;
  228. SPSun->Visible = False;
  229. GLLensFlare->Visible = False;
  230. }
  231. break;
  232. case 'd':
  233. case 'D':
  234. if (SkyDome1->Stars->Count > 0) {
  235. // turn on 'day' mode
  236. SkyDome1->Bands->Items[1]->StopColor->Color = clrNavy;
  237. SkyDome1->Bands->Items[1]->StartColor->Color = clrBlue;
  238. SkyDome1->Bands->Items[0]->StopColor->Color = clrBlue;
  239. SkyDome1->Bands->Items[0]->StartColor->Color = clrWhite;
  240. SkyDome1->Stars->Clear();
  241. GLSceneViewer1->Buffer->BackgroundColor = clWhite;
  242. fe = GLSceneViewer1->Buffer->FogEnvironment;
  243. fe->FogColor->AsWinColor = clWhite;
  244. fe->FogStart = -fe->FogStart;
  245. GLSceneViewer1->Buffer->FogEnvironment->FogStart = 0;
  246. SPMoon->Visible = False;
  247. SPSun->Visible = True;
  248. }
  249. break;
  250. case 't':
  251. if (SkyDome1->Options.Contains(sdoTwinkle))
  252. SkyDome1->Options = SkyDome1->Options << sdoTwinkle;
  253. else
  254. SkyDome1->Options = SkyDome1->Options >> sdoTwinkle;
  255. break;
  256. case 'l':
  257. GLLensFlare->Visible = (!GLLensFlare->Visible) && SPSun->Visible;
  258. }
  259. Key = '\0';
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TForm1::TISoundTimer(TObject* Sender)
  263. {
  264. TGLVector wolfPos;
  265. float c, s;
  266. TGLBSoundEmitter* be;
  267. if (!GLSMBASS1->Active)
  268. return;
  269. if (SkyDome1->Stars->Count == 0) {
  270. // wind blows around camera
  271. be = GetOrCreateSoundEmitter(GLCamera1);
  272. be->Source->SoundLibrary = GLSoundLibrary;
  273. be->Source->SoundName = GLSoundLibrary->Samples->Items[0]->Name;
  274. be->Source->Volume = random() * 0.5 + 0.5;
  275. be->Playing = True;
  276. } else {
  277. // wolf howl at some distance, at ground level
  278. wolfPos = GLCamera1->AbsolutePosition;
  279. SinCosine(
  280. random() * Gls::Vectorgeometry::c2PI, 100 + random(1000), s, c);
  281. wolfPos.X = wolfPos.X + c;
  282. wolfPos.Z = wolfPos.Z + s;
  283. wolfPos.Y = TerrainRenderer1->InterpolatedHeight(wolfPos);
  284. DCSound->Position->AsVector = wolfPos;
  285. be = GetOrCreateSoundEmitter(DCSound);
  286. be->Source->SoundLibrary = GLSoundLibrary;
  287. be->Source->SoundName = GLSoundLibrary->Samples->Items[1]->Name;
  288. be->Source->MinDistance = 100;
  289. be->Source->MaxDistance = 4000;
  290. be->Playing = True;
  291. }
  292. TISound->Enabled = False;
  293. TISound->Interval = 10000 + random(10000);
  294. TISound->Enabled = True;
  295. }
  296. //---------------------------------------------------------------------------