fTobitmapD.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. unit fTobitmapD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. Vcl.Graphics,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.ExtCtrls,
  11. Vcl.StdCtrls,
  12. Vcl.Imaging.Jpeg,
  13. GLS.Scene,
  14. GLS.VectorTypes,
  15. GLS.Cadencer,
  16. GLS.Objects,
  17. GLS.SceneViewer,
  18. GLS.HUDObjects,
  19. GLS.SpaceText,
  20. GLS.Coordinates,
  21. GLS.Graphics,
  22. GLS.Utils,
  23. GLS.BaseClasses;
  24. type
  25. TForm1 = class(TForm)
  26. GLSceneViewer1: TGLSceneViewer;
  27. GLScene1: TGLScene;
  28. Panel1: TPanel;
  29. GLCamera1: TGLCamera;
  30. GLLightSource1: TGLLightSource;
  31. Plane1: TGLPlane;
  32. Sphere1: TGLSphere;
  33. GLCadencer1: TGLCadencer;
  34. DummyCube1: TGLDummyCube;
  35. HUDSprite1: TGLHUDSprite;
  36. BUSnapShot: TButton;
  37. BURenderToBitmap: TButton;
  38. BUBitmapx2: TButton;
  39. BUBitmap600: TButton;
  40. BUBitmap300: TButton;
  41. SpaceText1: TGLSpaceText;
  42. BUViewerSnapShot: TButton;
  43. procedure Sphere1Progress(Sender: TObject; const deltaTime,
  44. newTime: Double);
  45. procedure FormCreate(Sender: TObject);
  46. procedure FormResize(Sender: TObject);
  47. procedure BUSnapShotClick(Sender: TObject);
  48. procedure BURenderToBitmapClick(Sender: TObject);
  49. procedure BUBitmapx2Click(Sender: TObject);
  50. procedure BUBitmap600Click(Sender: TObject);
  51. procedure BUBitmap300Click(Sender: TObject);
  52. procedure BUViewerSnapShotClick(Sender: TObject);
  53. private
  54. procedure RenderToBitmap(scale : Single);
  55. public
  56. end;
  57. var
  58. Form1: TForm1;
  59. implementation
  60. {$R *.dfm}
  61. uses
  62. fToBitmapImgD;
  63. (*
  64. A utility function, this takes the bitmap and uses Form2 to display it with
  65. a regular TImage component.
  66. *)
  67. procedure ViewBitmap(aBitmap : TBitmap; caption : String);
  68. var
  69. f : TForm2;
  70. begin
  71. Application.CreateForm(TForm2, f);
  72. if (aBitmap.Width<Screen.Width) and (aBitmap.Height<Screen.Height) then begin
  73. f.ClientWidth:=aBitmap.Width;
  74. f.ClientHeight:=aBitmap.Height;
  75. end else begin
  76. f.ClientWidth:=Round(Screen.Width*0.75);
  77. f.ClientHeight:=Round(Screen.Height*0.75);
  78. end;
  79. f.Image1.Picture.Bitmap:=aBitmap;
  80. f.Caption:=caption;
  81. f.Image1.Width:=aBitmap.Width;
  82. f.Image1.Height:=aBitmap.Height;
  83. f.Show;
  84. end;
  85. //
  86. // Utility stuff: load textures, animate the sphere and support resizing.
  87. //
  88. procedure TForm1.FormCreate(Sender: TObject);
  89. begin
  90. var Path: TFileName := GetCurrentAssetPath();
  91. SetCurrentDir(Path + '\texture');
  92. HUDSprite1.Material.Texture.Image.LoadFromFile('ashwood.jpg');
  93. Plane1.Material.Texture.Image.LoadFromFile('marbletiles.jpg');
  94. Sphere1.Material.Texture.Image.LoadFromFile('marbletiles.jpg');
  95. end;
  96. procedure TForm1.BUViewerSnapShotClick(Sender: TObject);
  97. var
  98. pt : Int64;
  99. bmp : TBitmap;
  100. delta : Double;
  101. begin
  102. pt:=StartPrecisionTimer;
  103. // Create a snapshot directly from the viewer content
  104. bmp:=GLSceneViewer1.CreateSnapShotBitmap;
  105. delta:=StopPrecisionTimer(pt);
  106. // Display the bitmap for the user to see and gaze in everlasting awe...
  107. ViewBitmap(bmp, Format('SnapShot %dx%d - %.3f ms',
  108. [bmp.Width, bmp.Height, delta*1000]));
  109. // Release the bitmap
  110. bmp.Free;
  111. end;
  112. procedure TForm1.BUSnapShotClick(Sender: TObject);
  113. var
  114. bmp32 : TGLBitmap32;
  115. bmp : TBitmap;
  116. pt : Int64;
  117. delta : Double;
  118. begin
  119. pt:=StartPrecisionTimer;
  120. // CreateSnapShot returns a TGLBitmap32, which is a low-level data buffer.
  121. // However TGLBitmap32 can spawn a regular TBitmap, which we use here
  122. bmp32:=GLSceneViewer1.Buffer.CreateSnapShot;
  123. bmp:=bmp32.Create32BitsBitmap;
  124. delta:=StopPrecisionTimer(pt);
  125. // Display the bitmap for the user to see and gaze in everlasting awe...
  126. ViewBitmap(bmp, Format('SnapShot %dx%d - %.3f ms',
  127. [bmp.Width, bmp.Height, delta*1000]));
  128. // Don't forget to free your TGLBitmap32 and TBitmap!
  129. bmp.Free;
  130. bmp32.Free;
  131. end;
  132. procedure TForm1.RenderToBitmap(scale : Single);
  133. var
  134. bmp : TBitmap;
  135. pt : Int64;
  136. delta : Double;
  137. begin
  138. pt:=StartPrecisionTimer;
  139. // Rendering to a bitmap requires an existing bitmap,
  140. // so we create and size a new one
  141. bmp:=TBitmap.Create;
  142. // Don't forget to specify a PixelFormat, or current screen pixel format
  143. // will be used, which may not suit your purposes!
  144. bmp.PixelFormat:=pf24bit;
  145. bmp.Width:=Round(GLSceneViewer1.Width*scale);
  146. bmp.Height:=Round(GLSceneViewer1.Height*scale);
  147. // Here we just request a render
  148. // The second parameter specifies DPI (Dots Per Inch), which is
  149. // linked to the bitmap's scaling
  150. // "96" is the "magic" DPI scale of the screen under windows
  151. GLSceneViewer1.Buffer.RenderToBitmap(bmp, Round(96*scale));
  152. delta:=StopPrecisionTimer(pt);
  153. ViewBitmap(bmp, Format('RenderToBitmap %dx%d - %.1f ms',
  154. [bmp.Width, bmp.Height, delta*1000]));
  155. bmp.Free;
  156. end;
  157. procedure TForm1.BURenderToBitmapClick(Sender: TObject);
  158. begin
  159. // Render at viewer resolution (scale = 1, DPI = 96)
  160. RenderToBitmap(1);
  161. end;
  162. procedure TForm1.BUBitmapx2Click(Sender: TObject);
  163. begin
  164. // Render at twice viewer resolution (scale = 2, DPI = 192 = 96x2)
  165. RenderToBitmap(2);
  166. end;
  167. procedure TForm1.BUBitmap300Click(Sender: TObject);
  168. begin
  169. // Screen is "magic" 96 dpi, this gives us our scale
  170. RenderToBitmap(300/96);
  171. end;
  172. procedure TForm1.BUBitmap600Click(Sender: TObject);
  173. begin
  174. // Screen is "magic" 96 dpi, this gives us our scale
  175. RenderToBitmap(600/96);
  176. end;
  177. procedure TForm1.Sphere1Progress(Sender: TObject; const deltaTime,
  178. newTime: Double);
  179. var
  180. h : Single;
  181. begin
  182. h:=2.5+2*Sin(newTime*4);
  183. Sphere1.Position.Y:=h;
  184. if h<1 then
  185. Sphere1.Scale.Y:=h
  186. else Sphere1.Scale.Y:=1;
  187. end;
  188. procedure TForm1.FormResize(Sender: TObject);
  189. begin
  190. with HUDSprite1 do begin
  191. Width:=GLSceneViewer1.Width;
  192. Position.X:=Width*0.5;
  193. Height:=GLSceneViewer1.Height;
  194. Position.Y:=Height*0.5;
  195. end;
  196. end;
  197. end.