fToBitmapC.cpp 5.8 KB

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