fTobitmap.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. unit fTobitmap;
  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. TobitmapImgFm;
  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. SetGLSceneMediaDir();
  91. HUDSprite1.Material.Texture.Image.LoadFromFile('ashwood.jpg');
  92. Plane1.Material.Texture.Image.LoadFromFile('marbletiles.jpg');
  93. Sphere1.Material.Texture.Image.LoadFromFile('marbletiles.jpg');
  94. end;
  95. procedure TForm1.BUViewerSnapShotClick(Sender: TObject);
  96. var
  97. pt : Int64;
  98. bmp : TBitmap;
  99. delta : Double;
  100. begin
  101. pt:=StartPrecisionTimer;
  102. // Create a snapshot directly from the viewer content
  103. bmp:=GLSceneViewer1.CreateSnapShotBitmap;
  104. delta:=StopPrecisionTimer(pt);
  105. // Display the bitmap for the user to see and gaze in everlasting awe...
  106. ViewBitmap(bmp, Format('SnapShot %dx%d - %.3f ms',
  107. [bmp.Width, bmp.Height, delta*1000]));
  108. // Release the bitmap
  109. bmp.Free;
  110. end;
  111. procedure TForm1.BUSnapShotClick(Sender: TObject);
  112. var
  113. bmp32 : TGLBitmap32;
  114. bmp : TBitmap;
  115. pt : Int64;
  116. delta : Double;
  117. begin
  118. pt:=StartPrecisionTimer;
  119. // CreateSnapShot returns a TGLBitmap32, which is a low-level data buffer.
  120. // However TGLBitmap32 can spawn a regular TBitmap, which we use here
  121. bmp32:=GLSceneViewer1.Buffer.CreateSnapShot;
  122. bmp:=bmp32.Create32BitsBitmap;
  123. delta:=StopPrecisionTimer(pt);
  124. // Display the bitmap for the user to see and gaze in everlasting awe...
  125. ViewBitmap(bmp, Format('SnapShot %dx%d - %.3f ms',
  126. [bmp.Width, bmp.Height, delta*1000]));
  127. // Don't forget to free your TGLBitmap32 and TBitmap!
  128. bmp.Free;
  129. bmp32.Free;
  130. end;
  131. procedure TForm1.RenderToBitmap(scale : Single);
  132. var
  133. bmp : TBitmap;
  134. pt : Int64;
  135. delta : Double;
  136. begin
  137. pt:=StartPrecisionTimer;
  138. // Rendering to a bitmap requires an existing bitmap,
  139. // so we create and size a new one
  140. bmp:=TBitmap.Create;
  141. // Don't forget to specify a PixelFormat, or current screen pixel format
  142. // will be used, which may not suit your purposes!
  143. bmp.PixelFormat:=pf24bit;
  144. bmp.Width:=Round(GLSceneViewer1.Width*scale);
  145. bmp.Height:=Round(GLSceneViewer1.Height*scale);
  146. // Here we just request a render
  147. // The second parameter specifies DPI (Dots Per Inch), which is
  148. // linked to the bitmap's scaling
  149. // "96" is the "magic" DPI scale of the screen under windows
  150. GLSceneViewer1.Buffer.RenderToBitmap(bmp, Round(96*scale));
  151. delta:=StopPrecisionTimer(pt);
  152. ViewBitmap(bmp, Format('RenderToBitmap %dx%d - %.1f ms',
  153. [bmp.Width, bmp.Height, delta*1000]));
  154. bmp.Free;
  155. end;
  156. procedure TForm1.BURenderToBitmapClick(Sender: TObject);
  157. begin
  158. // Render at viewer resolution (scale = 1, DPI = 96)
  159. RenderToBitmap(1);
  160. end;
  161. procedure TForm1.BUBitmapx2Click(Sender: TObject);
  162. begin
  163. // Render at twice viewer resolution (scale = 2, DPI = 192 = 96x2)
  164. RenderToBitmap(2);
  165. end;
  166. procedure TForm1.BUBitmap300Click(Sender: TObject);
  167. begin
  168. // Screen is "magic" 96 dpi, this gives us our scale
  169. RenderToBitmap(300/96);
  170. end;
  171. procedure TForm1.BUBitmap600Click(Sender: TObject);
  172. begin
  173. // Screen is "magic" 96 dpi, this gives us our scale
  174. RenderToBitmap(600/96);
  175. end;
  176. procedure TForm1.Sphere1Progress(Sender: TObject; const deltaTime,
  177. newTime: Double);
  178. var
  179. h : Single;
  180. begin
  181. h:=2.5+2*Sin(newTime*4);
  182. Sphere1.Position.Y:=h;
  183. if h<1 then
  184. Sphere1.Scale.Y:=h
  185. else Sphere1.Scale.Y:=1;
  186. end;
  187. procedure TForm1.FormResize(Sender: TObject);
  188. begin
  189. with HUDSprite1 do begin
  190. Width:=GLSceneViewer1.Width;
  191. Position.X:=Width*0.5;
  192. Height:=GLSceneViewer1.Height;
  193. Position.Y:=Height*0.5;
  194. end;
  195. end;
  196. end.