fShadedTerrainC.cpp 7.2 KB

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