fShadedTerrainC.cpp 7.1 KB

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