Unit1.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLBaseClasses"
  9. #pragma link "GLCadencer"
  10. #pragma link "GLCoordinates"
  11. #pragma link "GLCrossPlatform"
  12. #pragma link "GLHUDObjects"
  13. #pragma link "GLObjects"
  14. #pragma link "GLScene"
  15. #pragma link "GLSpaceText"
  16. #pragma link "GLWin32Viewer"
  17. #pragma resource "*.dfm"
  18. TForm1 *Form1;
  19. TForm2 *f;
  20. //---------------------------------------------------------------------------
  21. __fastcall TForm1::TForm1(TComponent* Owner)
  22. : TForm(Owner)
  23. {
  24. }
  25. //---------------------------------------------------------------------------
  26. /*
  27. A utility function, this takes the bitmap and uses Form2 to display it with
  28. a regular TImage component.
  29. */
  30. void TForm1::ViewBitmap(TBitmap *aBitmap, String caption)
  31. {
  32. Application->CreateForm(__classid(TForm2), &f);
  33. if ((aBitmap->Width<Screen->Width)&&(aBitmap->Height<Screen->Height))
  34. {
  35. f->ClientWidth = aBitmap->Width;
  36. f->ClientHeight = aBitmap->Height;
  37. }
  38. else
  39. {
  40. f->ClientWidth = Round(Screen->Width*0.75);
  41. f->ClientHeight = Round(Screen->Height*0.75);
  42. }
  43. f->Image1->Picture->Bitmap = aBitmap;
  44. f->Caption = caption;
  45. f->Image1->Width = aBitmap->Width;
  46. f->Image1->Height = aBitmap->Height;
  47. f->Show();
  48. }
  49. //---------------------------------------------------------------------------
  50. void TForm1::RenderToBitmap(Single scale)
  51. {
  52. TBitmap *bmp;
  53. __int64 pt;
  54. double delta;
  55. pt = StartPrecisionTimer();
  56. // Rendering to a bitmap requires an existing bitmap,
  57. // so we create and size a new one
  58. bmp = new TBitmap();
  59. // Don't forget to specify a PixelFormat, or current screen pixel format
  60. // will be used, which may not suit your purposes!
  61. bmp->PixelFormat = pf24bit;
  62. bmp->Width = Round(GLSceneViewer1->Width*scale);
  63. bmp->Height = Round(GLSceneViewer1->Height*scale);
  64. // Here we just request a render
  65. // The second parameter specifies DPI (Dots Per Inch), which is
  66. // linked to the bitmap's scaling
  67. // "96" is the "magic" DPI scale of the screen under windows
  68. GLSceneViewer1->Buffer->RenderToBitmap(bmp, Round(96*scale));
  69. delta = StopPrecisionTimer(pt);
  70. ViewBitmap(bmp, Format("RenderToBitmap %dx%d - %.3f ms",
  71. ARRAYOFCONST((bmp->Width, bmp->Height, delta*1000))));
  72. bmp->Free();
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TForm1::FormCreate(TObject *Sender)
  76. {
  77. SetGLSceneMediaDir();
  78. HUDSprite1->Material->Texture->Image->LoadFromFile("ashwood.jpg");
  79. Plane1->Material->Texture->Image->LoadFromFile("marbletiles.jpg");
  80. Sphere1->Material->Texture->Image->LoadFromFile("marbletiles.jpg");
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TForm1::BUViewerSnapShotClick(TObject *Sender)
  84. {
  85. int pt; // Int64 pt;
  86. TBitmap *bmp;
  87. double delta;
  88. pt = StartPrecisionTimer();
  89. // Create a snapshot directly from the viewer content
  90. bmp = GLSceneViewer1->CreateSnapShotBitmap();
  91. delta = StopPrecisionTimer(pt);
  92. // Display the bitmap for the user to see and gaze in everlasting awe...
  93. ViewBitmap(bmp, Format("SnapShot %dx%d - %.3f ms",
  94. ARRAYOFCONST((bmp->Width, bmp->Height, delta*1000))));
  95. // Release the bitmap
  96. bmp->FreeImage();
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm1::BUSnapShotClick(TObject *Sender)
  100. {
  101. TGLBitmap32 *bmp32;
  102. TBitmap *bmp;
  103. int pt; //Int64;
  104. double delta;
  105. pt = StartPrecisionTimer();
  106. // CreateSnapShot returns a TGLBitmap32, which is a low-level data buffer.
  107. // However TGLBitmap32 can spawn a regular TBitmap, which we use here
  108. bmp32 = GLSceneViewer1->Buffer->CreateSnapShot();
  109. bmp = bmp32->Create32BitsBitmap();
  110. delta = StopPrecisionTimer(pt);
  111. // Display the bitmap for the user to see and gaze in everlasting awe...
  112. ViewBitmap(bmp, Format("SnapShot %dx%d - %.3f ms",
  113. ARRAYOFCONST((bmp->Width, bmp->Height, delta*1000))));
  114. // Don't forget to free your TGLBitmap32 and TBitmap!
  115. bmp->FreeImage();
  116. bmp32->Free();
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TForm1::BURenderToBitmapClick(TObject *Sender)
  120. {
  121. // Render at viewer resolution (scale = 1, DPI = 96)
  122. RenderToBitmap(1);
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall TForm1::BUBitmapx2Click(TObject *Sender)
  126. {
  127. // Render at twice viewer resolution (scale = 2, DPI = 192 = 96x2)
  128. RenderToBitmap(2);
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TForm1::BUBitmap300Click(TObject *Sender)
  132. {
  133. // Screen is "magic" 96 dpi, this gives us our scale
  134. RenderToBitmap(float(300/96));
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TForm1::BUBitmap600Click(TObject *Sender)
  138. {
  139. // Screen is "magic" 96 dpi, this gives us our scale
  140. RenderToBitmap(600/96);
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TForm1::Sphere1Progress(TObject *Sender, const double deltaTime, const double newTime)
  144. {
  145. Single h;
  146. h = 2.5+2*Sin(newTime*4);
  147. Sphere1->Position->Y = h;
  148. if (h<1)
  149. Sphere1->Scale->Y = h;
  150. else
  151. Sphere1->Scale->Y = 1;
  152. }
  153. //---------------------------------------------------------------------------
  154. void __fastcall TForm1::FormResize(TObject *Sender)
  155. {
  156. HUDSprite1->Width = GLSceneViewer1->Width;
  157. HUDSprite1->Position->X = HUDSprite1->Width*0.5;
  158. HUDSprite1->Height = GLSceneViewer1->Height;
  159. HUDSprite1->Position->Y = HUDSprite1->Height*0.5;
  160. }
  161. //---------------------------------------------------------------------------